-
Notifications
You must be signed in to change notification settings - Fork 647
/
cluster.py
309 lines (272 loc) · 11.3 KB
/
cluster.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# Licensed to Modin Development Team under one or more contributor license agreements.
# See the NOTICE file distributed with this work for additional information regarding
# copyright ownership. The Modin Development Team licenses this file to you 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 os
import errno
from typing import NamedTuple, Union
import atexit
import warnings
from modin import set_execution
from .base import ConnectionDetails
from .connection import Connection
class _RegionZone(NamedTuple):
region: str
zone: str
class Provider:
AWS = "aws"
__KNOWN = {AWS: [_RegionZone(region="us-west-1", zone="us-west-1a")]}
__DEFAULT_HEAD = {AWS: "m5.large"}
__DEFAULT_WORKER = {AWS: "m5.large"}
__DEFAULT_IMAGE = {AWS: "ami-0f56279347d2fa43e"}
def __init__(
self,
name: str,
credentials_file: str = None,
region: str = None,
zone: str = None,
image: str = None,
):
"""
Class that holds all information about particular connection to cluster provider, namely
* provider name (must be one of known ones)
* path to file with credentials (file format is provider-specific); omit to use global provider-default credentials
* region and zone where cluster is to be spawned (optional, would be deduced if omitted)
* image to use (optional, would use default for provider if omitted)
"""
if name not in self.__KNOWN:
raise ValueError(f"Unknown provider name: {name}")
if credentials_file is not None and not os.path.exists(credentials_file):
raise OSError(
errno.ENOENT, "Credentials file does not exist", credentials_file
)
if region is None:
if zone is not None:
raise ValueError("Cannot specify a zone without specifying a region")
try:
region, zone = self.__KNOWN[name][0]
except IndexError:
raise ValueError(f"No defaults for provider {name}")
elif zone is None:
for regzone in self.__KNOWN[name]:
if regzone.region == region:
zone = regzone.zone
break
else:
raise ValueError(f"No default for region {region} for provider {name}")
self.name = name
self.region = region
self.zone = zone
self.credentials_file = (
os.path.abspath(credentials_file) if credentials_file is not None else None
)
self.image = image or self.__DEFAULT_IMAGE[name]
@property
def default_head_type(self):
return self.__DEFAULT_HEAD[self.name]
@property
def default_worker_type(self):
return self.__DEFAULT_WORKER[self.name]
class BaseCluster:
"""
Cluster manager for Modin. Knows how to use certain tools to spawn and destroy clusters,
can serve as context manager to switch execution engine and storage format to remote.
"""
target_engine = None
target_storage_format = None
wrap_cmd = None
Connector = Connection
def __init__(
self,
provider: Provider,
project_name: str = None,
cluster_name: str = "modin-cluster",
worker_count: int = 4,
head_node_type: str = None,
worker_node_type: str = None,
add_conda_packages: list = None,
):
"""
Prepare the cluster manager. It needs to know a few things:
* which cloud provider to use
* what is project name (could be omitted to use default one for account used to connect)
* cluster name
* worker count
* head and worker node instance types (can be omitted to default to provider-defined)
* custom conda packages for remote environment
"""
self.provider = provider
self.project_name = project_name
self.cluster_name = cluster_name
self.worker_count = worker_count
self.head_node_type = head_node_type or provider.default_head_type
self.worker_node_type = worker_node_type or provider.default_worker_type
self.add_conda_packages = add_conda_packages
self.old_execution = None
self.connection: Connection = None
def spawn(self, wait=False):
"""
Actually spawns the cluster. When already spawned, should be a no-op.
Always call .spawn(True) before assuming a cluster is ready.
When wait==False it spawns cluster asynchronously.
"""
self._spawn(wait=wait)
atexit.register(self.destroy, wait=True)
if wait:
# cluster is ready now
if self.connection is None:
self.connection = self.Connector(
self._get_connection_details(),
self._get_main_python(),
self.wrap_cmd,
)
def destroy(self, wait=False):
"""
Destroys the cluster. When already destroyed, should be a no-op.
Always call .destroy(True) before assuming a cluster is dead.
When wait==False it destroys cluster asynchronously.
"""
if self.connection is not None:
self.connection.stop()
self._destroy(wait=wait)
if wait:
atexit.unregister(self.destroy)
def _spawn(self, wait=False):
"""
Subclass must implement the real spawning
"""
raise NotImplementedError()
def _destroy(self, wait=False):
"""
Subclass must implement the real destruction
"""
raise NotImplementedError()
def _get_connection_details(self) -> ConnectionDetails:
"""
Gets the coordinates on how to connect to cluster frontend node.
"""
raise NotImplementedError()
def _get_main_python(self) -> str:
"""
Gets the path to 'main' interpreter (the one that houses created environment for running everything)
"""
raise NotImplementedError()
def __enter__(self):
self.spawn(wait=True) # make sure cluster is ready
self.connection.activate()
self.old_execution = set_execution(
self.target_engine, self.target_storage_format
)
return self
def __exit__(self, *a, **kw):
set_execution(*self.old_execution)
self.connection.deactivate()
self.old_execution = None
# TODO: implement __del__() properly; naive implementation calling .destroy() crashes
# somewhere in the innards of Ray when a cluster is destroyed during interpreter exit.
def create(
provider: Union[Provider, str],
credentials: str = None,
region: str = None,
zone: str = None,
image: str = None,
project_name: str = None,
cluster_name: str = "modin-cluster",
workers: int = 4,
head_node: str = None,
worker_node: str = None,
add_conda_packages: list = None,
cluster_type: str = "rayscale",
) -> BaseCluster:
"""
Creates an instance of a cluster with desired characteristics in a cloud.
Upon entering a context via with statement Modin will redirect its work to the remote cluster.
Spawned cluster can be destroyed manually, or it will be destroyed when the program exits.
Parameters
----------
provider : str or instance of Provider class
Specify the name of the provider to use or a Provider object.
If Provider object is given, then credentials, region and zone are ignored.
credentials : str, optional
Path to the file which holds credentials used by given cloud provider.
If not specified, cloud provider will use its default means of finding credentials on the system.
region : str, optional
Region in the cloud where to spawn the cluster.
If omitted a default for given provider will be taken.
zone : str, optional
Availability zone (part of region) where to spawn the cluster.
If omitted a default for given provider and region will be taken.
image: str, optional
Image to use for spawning head and worker nodes.
If omitted a default for given provider will be taken.
project_name : str, optional
Project name to assign to the cluster in cloud, for easier manual tracking.
cluster_name : str, optional
Name to be given to the cluster.
To spawn multiple clusters in single region and zone use different names.
workers : int, optional
How many worker nodes to spawn in the cluster. Head node is not counted for here.
head_node : str, optional
What machine type to use for head node in the cluster.
worker_node : str, optional
What machine type to use for worker nodes in the cluster.
add_conda_packages : list, optional
Custom conda packages for remote environments. By default remote modin version is
the same as local version.
cluster_type : str, optional
How to spawn the cluster.
Currently spawning by Ray autoscaler ("rayscale" for general and "hdk" for HDK-based) is supported
Returns
-------
BaseCluster descendant
The object that knows how to destroy the cluster and how to activate it as remote context.
Note that by default spawning and destroying of the cluster happens in the background,
as it's usually a rather lengthy process.
Notes
-----
Cluster computation actually can work when proxies are required to access the cloud.
You should set normal "http_proxy"/"https_proxy" variables for HTTP/HTTPS proxies and
set "MODIN_SOCKS_PROXY" variable for SOCKS proxy before calling the function.
Using SOCKS proxy requires Ray newer than 0.8.6, which might need to be installed manually.
"""
if not isinstance(provider, Provider) and cluster_type != "local":
provider = Provider(
name=provider,
credentials_file=credentials,
region=region,
zone=zone,
image=image,
)
else:
if any(p is not None for p in (credentials, region, zone, image)):
warnings.warn(
"Ignoring credentials, region, zone and image parameters because provider is specified as Provider descriptor, not as name",
UserWarning,
)
if cluster_type == "rayscale":
from .rayscale import RayCluster as Spawner
elif cluster_type == "hdk":
from .hdk import RemoteHdk as Spawner
elif cluster_type == "local":
from .local_cluster import LocalCluster as Spawner
else:
raise ValueError(f"Unknown cluster type: {cluster_type}")
instance = Spawner(
provider,
project_name,
cluster_name,
worker_count=workers,
head_node_type=head_node,
worker_node_type=worker_node,
add_conda_packages=add_conda_packages,
)
instance.spawn(wait=False)
return instance