forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
414 lines (335 loc) · 10.8 KB
/
conftest.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"""
This file defines the common pytest fixtures used in current directory.
"""
import os
from contextlib import contextmanager
import pytest
import subprocess
import json
import time
import ray
from ray.cluster_utils import Cluster
from ray._private.services import REDIS_EXECUTABLE, _start_redis_instance
from ray._private.test_utils import init_error_pubsub, setup_tls, teardown_tls
import ray.util.client.server.server as ray_client_server
import ray._private.gcs_utils as gcs_utils
@pytest.fixture
def shutdown_only():
yield None
# The code after the yield will run as teardown code.
ray.shutdown()
def get_default_fixure_system_config():
system_config = {
"object_timeout_milliseconds": 200,
"num_heartbeats_timeout": 10,
"object_store_full_delay_ms": 100,
}
return system_config
def get_default_fixture_ray_kwargs():
system_config = get_default_fixure_system_config()
ray_kwargs = {
"num_cpus": 1,
"object_store_memory": 150 * 1024 * 1024,
"dashboard_port": None,
"namespace": "default_test_namespace",
"_system_config": system_config,
}
return ray_kwargs
@contextmanager
def _ray_start(**kwargs):
init_kwargs = get_default_fixture_ray_kwargs()
init_kwargs.update(kwargs)
# Start the Ray processes.
address_info = ray.init(**init_kwargs)
yield address_info
# The code after the yield will run as teardown code.
ray.shutdown()
@pytest.fixture
def ray_start_with_dashboard(request):
param = getattr(request, "param", {})
if param.get("num_cpus") is None:
param["num_cpus"] = 1
with _ray_start(include_dashboard=True, **param) as address_info:
yield address_info
# The following fixture will start ray with 0 cpu.
@pytest.fixture
def ray_start_no_cpu(request):
param = getattr(request, "param", {})
with _ray_start(num_cpus=0, **param) as res:
yield res
# The following fixture will start ray with 1 cpu.
@pytest.fixture
def ray_start_regular(request):
param = getattr(request, "param", {})
with _ray_start(**param) as res:
yield res
@pytest.fixture(scope="module")
def ray_start_regular_shared(request):
param = getattr(request, "param", {})
with _ray_start(**param) as res:
yield res
@pytest.fixture(
scope="module",
params=[{
"local_mode": True
}, {
"local_mode": False
}, {
"local_mode": False,
"use_tls": True
}])
def ray_start_shared_local_modes(request):
param = getattr(request, "param", {})
use_tls = param.pop("use_tls", False)
with manage_tls(use_tls):
with _ray_start(**param) as res:
yield res
@pytest.fixture
def ray_start_2_cpus(request):
param = getattr(request, "param", {})
with _ray_start(num_cpus=2, **param) as res:
yield res
@pytest.fixture
def ray_start_10_cpus(request):
param = getattr(request, "param", {})
with _ray_start(num_cpus=10, **param) as res:
yield res
@contextmanager
def _ray_start_cluster(**kwargs):
init_kwargs = get_default_fixture_ray_kwargs()
num_nodes = 0
do_init = False
# num_nodes & do_init are not arguments for ray.init, so delete them.
if "num_nodes" in kwargs:
num_nodes = kwargs["num_nodes"]
del kwargs["num_nodes"]
if "do_init" in kwargs:
do_init = kwargs["do_init"]
del kwargs["do_init"]
elif num_nodes > 0:
do_init = True
init_kwargs.update(kwargs)
namespace = init_kwargs.pop("namespace")
cluster = Cluster()
remote_nodes = []
for i in range(num_nodes):
if i > 0 and "_system_config" in init_kwargs:
del init_kwargs["_system_config"]
remote_nodes.append(cluster.add_node(**init_kwargs))
# We assume driver will connect to the head (first node),
# so ray init will be invoked if do_init is true
if len(remote_nodes) == 1 and do_init:
ray.init(address=cluster.address, namespace=namespace)
yield cluster
# The code after the yield will run as teardown code.
ray.shutdown()
cluster.shutdown()
# This fixture will start a cluster with empty nodes.
@pytest.fixture
def ray_start_cluster(request):
param = getattr(request, "param", {})
with _ray_start_cluster(**param) as res:
yield res
@pytest.fixture
def ray_start_cluster_init(request):
param = getattr(request, "param", {})
with _ray_start_cluster(do_init=True, **param) as res:
yield res
@pytest.fixture
def ray_start_cluster_head(request):
param = getattr(request, "param", {})
with _ray_start_cluster(do_init=True, num_nodes=1, **param) as res:
yield res
@pytest.fixture
def ray_start_cluster_2_nodes(request):
param = getattr(request, "param", {})
with _ray_start_cluster(do_init=True, num_nodes=2, **param) as res:
yield res
@pytest.fixture
def ray_start_object_store_memory(request):
# Start the Ray processes.
store_size = request.param
system_config = get_default_fixure_system_config()
init_kwargs = {
"num_cpus": 1,
"_system_config": system_config,
"object_store_memory": store_size,
}
ray.init(**init_kwargs)
yield store_size
# The code after the yield will run as teardown code.
ray.shutdown()
@pytest.fixture
def call_ray_start(request):
parameter = getattr(
request, "param", "ray start --head --num-cpus=1 --min-worker-port=0 "
"--max-worker-port=0 --port 0")
command_args = parameter.split(" ")
out = ray._private.utils.decode(
subprocess.check_output(command_args, stderr=subprocess.STDOUT))
# Get the redis address from the output.
redis_substring_prefix = "--address='"
address_location = (
out.find(redis_substring_prefix) + len(redis_substring_prefix))
address = out[address_location:]
address = address.split("'")[0]
yield address
# Disconnect from the Ray cluster.
ray.shutdown()
# Kill the Ray cluster.
subprocess.check_call(["ray", "stop"])
@pytest.fixture
def call_ray_start_with_external_redis(request):
ports = getattr(request, "param", "6379")
port_list = ports.split(",")
for port in port_list:
_start_redis_instance(REDIS_EXECUTABLE, int(port), password="123")
address_str = ",".join(map(lambda x: "localhost:" + x, port_list))
cmd = f"ray start --head --address={address_str} --redis-password=123"
subprocess.call(cmd.split(" "))
yield address_str.split(",")[0]
# Disconnect from the Ray cluster.
ray.shutdown()
# Kill the Ray cluster.
subprocess.check_call(["ray", "stop"])
@pytest.fixture
def init_and_serve():
server_handle, _ = ray_client_server.init_and_serve("localhost:50051")
yield server_handle
ray_client_server.shutdown_with_server(server_handle.grpc_server)
time.sleep(2)
@pytest.fixture
def call_ray_stop_only():
yield
subprocess.check_call(["ray", "stop"])
@pytest.fixture
def enable_pickle_debug():
os.environ["RAY_PICKLE_VERBOSE_DEBUG"] = "1"
yield
del os.environ["RAY_PICKLE_VERBOSE_DEBUG"]
@pytest.fixture
def set_enable_auto_connect(enable_auto_connect: str = "0"):
try:
os.environ["RAY_ENABLE_AUTO_CONNECT"] = enable_auto_connect
yield enable_auto_connect
finally:
del os.environ["RAY_ENABLE_AUTO_CONNECT"]
@pytest.fixture()
def two_node_cluster():
system_config = {
"object_timeout_milliseconds": 200,
"num_heartbeats_timeout": 10,
}
cluster = ray.cluster_utils.Cluster(
head_node_args={"_system_config": system_config})
for _ in range(2):
remote_node = cluster.add_node(num_cpus=1)
ray.init(address=cluster.address)
yield cluster, remote_node
# The code after the yield will run as teardown code.
ray.shutdown()
cluster.shutdown()
@pytest.fixture()
def error_pubsub():
p = init_error_pubsub()
yield p
p.close()
@pytest.fixture()
def log_pubsub():
p = ray.worker.global_worker.redis_client.pubsub(
ignore_subscribe_messages=True)
log_channel = gcs_utils.LOG_FILE_CHANNEL
p.psubscribe(log_channel)
yield p
p.close()
@contextmanager
def manage_tls(use_tls):
if use_tls:
key_filepath, cert_filepath, temp_dir = setup_tls()
yield use_tls
if use_tls:
teardown_tls(key_filepath, cert_filepath, temp_dir)
@pytest.fixture
def use_tls(request):
if request.param:
key_filepath, cert_filepath, temp_dir = setup_tls()
yield request.param
if request.param:
teardown_tls(key_filepath, cert_filepath, temp_dir)
"""
Object spilling test fixture
"""
# -- Smart open param --
bucket_name = "object-spilling-test"
# -- File system param --
spill_local_path = "/tmp/spill"
# -- Spilling configs --
file_system_object_spilling_config = {
"type": "filesystem",
"params": {
"directory_path": spill_local_path
}
}
# Since we have differet protocol for a local external storage (e.g., fs)
# and distributed external storage (e.g., S3), we need to test both cases.
# This mocks the distributed fs with cluster utils.
mock_distributed_fs_object_spilling_config = {
"type": "mock_distributed_fs",
"params": {
"directory_path": spill_local_path
}
}
smart_open_object_spilling_config = {
"type": "smart_open",
"params": {
"uri": f"s3:https://{bucket_name}/"
}
}
unstable_object_spilling_config = {
"type": "unstable_fs",
"params": {
"directory_path": spill_local_path,
}
}
slow_object_spilling_config = {
"type": "slow_fs",
"params": {
"directory_path": spill_local_path,
}
}
def create_object_spilling_config(request, tmp_path):
temp_folder = tmp_path / "spill"
temp_folder.mkdir()
if (request.param["type"] == "filesystem"
or request.param["type"] == "mock_distributed_fs"):
request.param["params"]["directory_path"] = str(temp_folder)
return json.dumps(request.param), temp_folder
@pytest.fixture(
scope="function",
params=[
file_system_object_spilling_config,
# TODO(sang): Add a mock dependency to test S3.
# smart_open_object_spilling_config,
])
def object_spilling_config(request, tmp_path):
yield create_object_spilling_config(request, tmp_path)
@pytest.fixture(
scope="function",
params=[
file_system_object_spilling_config,
mock_distributed_fs_object_spilling_config
])
def multi_node_object_spilling_config(request, tmp_path):
yield create_object_spilling_config(request, tmp_path)
@pytest.fixture(
scope="function", params=[
unstable_object_spilling_config,
])
def unstable_spilling_config(request, tmp_path):
yield create_object_spilling_config(request, tmp_path)
@pytest.fixture(
scope="function", params=[
slow_object_spilling_config,
])
def slow_spilling_config(request, tmp_path):
yield create_object_spilling_config(request, tmp_path)