forked from data-prep-kit/data-prep-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_launcher.py
More file actions
124 lines (114 loc) · 4.69 KB
/
Copy pathtransform_launcher.py
File metadata and controls
124 lines (114 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# (C) Copyright IBM Corp. 2024.
# Licensed under the Apache License, Version 2.0 (the “License”);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################
import argparse
import sys
import time
import ray
from data_processing.data_access import DataAccessFactory, DataAccessFactoryBase
from data_processing.runtime.ray import RayTransformExecutionConfiguration, orchestrate
from data_processing.runtime.ray.runtime_configuration import (
RayTransformRuntimeConfiguration,
)
from data_processing.runtime.transform_launcher import AbstractTransformLauncher
from data_processing.utils import get_logger, str2bool
logger = get_logger(__name__)
class RayTransformLauncher(AbstractTransformLauncher):
"""
Driver class starting Filter execution
"""
def __init__(
self,
runtime_config: RayTransformRuntimeConfiguration,
data_access_factory: DataAccessFactoryBase = DataAccessFactory(),
):
"""
Creates driver
:param runtime_config: transform runtime factory
:param data_access_factory: the factory to create DataAccess instances.
"""
super().__init__(runtime_config, data_access_factory)
self.execution_config = RayTransformExecutionConfiguration(name=self.name)
def __get_parameters(self) -> bool:
"""
This method creates arg parser, fill it with the parameters
and does parameters validation
:return: True id validation passe or False, if not
"""
parser = argparse.ArgumentParser(
description=f"Driver for {self.name} processing",
# RawText is used to allow better formatting of ast-based arguments
# See uses of ParamsUtils.dict_to_str()
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"--run_locally", type=lambda x: bool(str2bool(x)), default=False, help="running ray local flag"
)
# add additional arguments
self.runtime_config.add_input_params(parser=parser)
self.data_access_factory.add_input_params(parser=parser)
self.execution_config.add_input_params(parser=parser)
args = parser.parse_args()
self.run_locally = args.run_locally
if self.run_locally:
logger.info("Running locally")
else:
logger.info("connecting to existing cluster")
return (
self.runtime_config.apply_input_params(args=args)
and self.data_access_factory.apply_input_params(args=args)
and self.execution_config.apply_input_params(args=args)
)
def _submit_for_execution(self) -> int:
"""
Submit for Ray execution
:return:
"""
res = 1
start = time.time()
try:
if self.run_locally:
# Will create a local Ray cluster
logger.debug("running locally creating Ray cluster")
ray.init()
else:
# connect to the existing cluster
logger.info("Connecting to the existing Ray cluster")
ray.init(f"ray://localhost:10001", ignore_reinit_error=True)
logger.debug("Starting orchestrator")
res = ray.get(
orchestrate.remote(
preprocessing_params=self.execution_config,
data_access_factory=self.data_access_factory,
runtime_config=self.runtime_config,
)
)
logger.debug("Completed orchestrator")
time.sleep(10)
except Exception as e:
logger.info(f"Exception running ray remote orchestration\n{e}")
finally:
logger.info(f"Completed execution in {(time.time() - start)/60.} min, execution result {res}")
ray.shutdown()
return res
def launch(self) -> int:
"""
Execute method orchestrates driver invocation
:return: launch result
"""
if self.__get_parameters():
res = self._submit_for_execution()
else:
res = 1
if not self.run_locally and res > 0:
# if we are running in kfp exit to signal kfp that we failed
sys.exit(1)
return res