mirror of
https://github.com/wassname/ray.git
synced 2026-07-14 11:17:54 +08:00
[autoscaler] Add unit tests for stopped node caching, fix flaky tests (#5793)
This commit is contained in:
@@ -2,7 +2,6 @@ from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
from flaky import flaky
|
||||
import shutil
|
||||
import tempfile
|
||||
import threading
|
||||
@@ -15,7 +14,8 @@ import ray
|
||||
import ray.services as services
|
||||
from ray.autoscaler.autoscaler import StandardAutoscaler, LoadMetrics, \
|
||||
fillout_defaults, validate_config
|
||||
from ray.autoscaler.tags import TAG_RAY_NODE_TYPE, TAG_RAY_NODE_STATUS
|
||||
from ray.autoscaler.tags import TAG_RAY_NODE_TYPE, TAG_RAY_NODE_STATUS, \
|
||||
STATUS_UP_TO_DATE, STATUS_UPDATE_FAILED
|
||||
from ray.autoscaler.node_provider import NODE_PROVIDERS, NodeProvider
|
||||
from ray.tests.utils import RayTestTimeoutException
|
||||
import pytest
|
||||
@@ -47,22 +47,53 @@ class MockProcessRunner(object):
|
||||
raise Exception("Failing command on purpose")
|
||||
self.calls.append(cmd)
|
||||
|
||||
def assert_has_call(self, ip, pattern):
|
||||
out = ""
|
||||
for cmd in self.calls:
|
||||
msg = " ".join(cmd)
|
||||
if ip in msg:
|
||||
out += msg
|
||||
out += "\n"
|
||||
if pattern in out:
|
||||
return True
|
||||
else:
|
||||
raise Exception("Did not find [{}] in [{}] for {}".format(
|
||||
pattern, out, ip))
|
||||
|
||||
def assert_not_has_call(self, ip, pattern):
|
||||
out = ""
|
||||
for cmd in self.calls:
|
||||
msg = " ".join(cmd)
|
||||
if ip in msg:
|
||||
out += msg
|
||||
out += "\n"
|
||||
if pattern in out:
|
||||
raise Exception("Found [{}] in [{}] for {}".format(
|
||||
pattern, out, ip))
|
||||
else:
|
||||
return True
|
||||
|
||||
def clear_history(self):
|
||||
self.calls = []
|
||||
|
||||
|
||||
class MockProvider(NodeProvider):
|
||||
def __init__(self):
|
||||
def __init__(self, cache_stopped=False):
|
||||
self.mock_nodes = {}
|
||||
self.next_id = 0
|
||||
self.throw = False
|
||||
self.fail_creates = False
|
||||
self.ready_to_create = threading.Event()
|
||||
self.ready_to_create.set()
|
||||
self.cache_stopped = cache_stopped
|
||||
|
||||
def non_terminated_nodes(self, tag_filters):
|
||||
if self.throw:
|
||||
raise Exception("oops")
|
||||
return [
|
||||
n.node_id for n in self.mock_nodes.values()
|
||||
if n.matches(tag_filters) and n.state != "terminated"
|
||||
if n.matches(tag_filters)
|
||||
and n.state not in ["stopped", "terminated"]
|
||||
]
|
||||
|
||||
def non_terminated_node_ips(self, tag_filters):
|
||||
@@ -70,14 +101,15 @@ class MockProvider(NodeProvider):
|
||||
raise Exception("oops")
|
||||
return [
|
||||
n.internal_ip for n in self.mock_nodes.values()
|
||||
if n.matches(tag_filters) and n.state != "terminated"
|
||||
if n.matches(tag_filters)
|
||||
and n.state not in ["stopped", "terminated"]
|
||||
]
|
||||
|
||||
def is_running(self, node_id):
|
||||
return self.mock_nodes[node_id].state == "running"
|
||||
|
||||
def is_terminated(self, node_id):
|
||||
return self.mock_nodes[node_id].state == "terminated"
|
||||
return self.mock_nodes[node_id].state in ["stopped", "terminated"]
|
||||
|
||||
def node_tags(self, node_id):
|
||||
return self.mock_nodes[node_id].tags
|
||||
@@ -92,15 +124,29 @@ class MockProvider(NodeProvider):
|
||||
self.ready_to_create.wait()
|
||||
if self.fail_creates:
|
||||
return
|
||||
if self.cache_stopped:
|
||||
for node in self.mock_nodes.values():
|
||||
if node.state == "stopped" and count > 0:
|
||||
count -= 1
|
||||
node.state = "pending"
|
||||
node.tags.update(tags)
|
||||
for _ in range(count):
|
||||
self.mock_nodes[self.next_id] = MockNode(self.next_id, tags)
|
||||
self.mock_nodes[self.next_id] = MockNode(self.next_id, tags.copy())
|
||||
self.next_id += 1
|
||||
|
||||
def set_node_tags(self, node_id, tags):
|
||||
self.mock_nodes[node_id].tags.update(tags)
|
||||
|
||||
def terminate_node(self, node_id):
|
||||
self.mock_nodes[node_id].state = "terminated"
|
||||
if self.cache_stopped:
|
||||
self.mock_nodes[node_id].state = "stopped"
|
||||
else:
|
||||
self.mock_nodes[node_id].state = "terminated"
|
||||
|
||||
def finish_starting_nodes(self):
|
||||
for node in self.mock_nodes.values():
|
||||
if node.state == "pending":
|
||||
node.state = "running"
|
||||
|
||||
|
||||
SMALL_CLUSTER = {
|
||||
@@ -131,10 +177,10 @@ SMALL_CLUSTER = {
|
||||
"TestProp": 2,
|
||||
},
|
||||
"file_mounts": {},
|
||||
"initialization_commands": ["cmd0"],
|
||||
"setup_commands": ["cmd1"],
|
||||
"head_setup_commands": ["cmd2"],
|
||||
"worker_setup_commands": ["cmd3"],
|
||||
"initialization_commands": ["init_cmd"],
|
||||
"setup_commands": ["setup_cmd"],
|
||||
"head_setup_commands": ["head_setup_cmd"],
|
||||
"worker_setup_commands": ["worker_setup_cmd"],
|
||||
"head_start_ray_commands": ["start_ray_head"],
|
||||
"worker_start_ray_commands": ["start_ray_worker"],
|
||||
}
|
||||
@@ -344,11 +390,13 @@ class AutoscalingTest(unittest.TestCase):
|
||||
autoscaler.update()
|
||||
self.waitForNodes(0)
|
||||
autoscaler.request_resources({"CPU": cores_per_node * 10})
|
||||
for _ in range(3): # Maximum launch batch is 5
|
||||
for _ in range(5): # Maximum launch batch is 5
|
||||
time.sleep(0.01)
|
||||
autoscaler.update()
|
||||
self.waitForNodes(10)
|
||||
autoscaler.request_resources({"CPU": cores_per_node * 30})
|
||||
for _ in range(4): # Maximum launch batch is 5
|
||||
time.sleep(0.01)
|
||||
autoscaler.update()
|
||||
self.waitForNodes(30)
|
||||
|
||||
@@ -700,10 +748,10 @@ class AutoscalingTest(unittest.TestCase):
|
||||
autoscaler.update()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(2)
|
||||
for node in self.provider.mock_nodes.values():
|
||||
node.state = "running"
|
||||
self.provider.finish_starting_nodes()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(2, tag_filters={TAG_RAY_NODE_STATUS: "up-to-date"})
|
||||
self.waitForNodes(
|
||||
2, tag_filters={TAG_RAY_NODE_STATUS: STATUS_UP_TO_DATE})
|
||||
|
||||
def testReportsConfigFailures(self):
|
||||
config = copy.deepcopy(SMALL_CLUSTER)
|
||||
@@ -712,7 +760,7 @@ class AutoscalingTest(unittest.TestCase):
|
||||
config["provider"]["type"] = "mock"
|
||||
config_path = self.write_config(config)
|
||||
self.provider = MockProvider()
|
||||
runner = MockProcessRunner(fail_cmds=["cmd1"])
|
||||
runner = MockProcessRunner(fail_cmds=["setup_cmd"])
|
||||
autoscaler = StandardAutoscaler(
|
||||
config_path,
|
||||
LoadMetrics(),
|
||||
@@ -722,11 +770,10 @@ class AutoscalingTest(unittest.TestCase):
|
||||
autoscaler.update()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(2)
|
||||
for node in self.provider.mock_nodes.values():
|
||||
node.state = "running"
|
||||
self.provider.finish_starting_nodes()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(
|
||||
2, tag_filters={TAG_RAY_NODE_STATUS: "update-failed"})
|
||||
2, tag_filters={TAG_RAY_NODE_STATUS: STATUS_UPDATE_FAILED})
|
||||
|
||||
def testConfiguresOutdatedNodes(self):
|
||||
config_path = self.write_config(SMALL_CLUSTER)
|
||||
@@ -741,10 +788,10 @@ class AutoscalingTest(unittest.TestCase):
|
||||
autoscaler.update()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(2)
|
||||
for node in self.provider.mock_nodes.values():
|
||||
node.state = "running"
|
||||
self.provider.finish_starting_nodes()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(2, tag_filters={TAG_RAY_NODE_STATUS: "up-to-date"})
|
||||
self.waitForNodes(
|
||||
2, tag_filters={TAG_RAY_NODE_STATUS: STATUS_UP_TO_DATE})
|
||||
runner.calls = []
|
||||
new_config = SMALL_CLUSTER.copy()
|
||||
new_config["worker_setup_commands"] = ["cmdX", "cmdY"]
|
||||
@@ -843,7 +890,6 @@ class AutoscalingTest(unittest.TestCase):
|
||||
autoscaler.update()
|
||||
assert len(self.provider.non_terminated_nodes({})) == 0
|
||||
|
||||
@flaky(max_runs=4)
|
||||
def testRecoverUnhealthyWorkers(self):
|
||||
config_path = self.write_config(SMALL_CLUSTER)
|
||||
self.provider = MockProvider()
|
||||
@@ -857,14 +903,19 @@ class AutoscalingTest(unittest.TestCase):
|
||||
update_interval_s=0)
|
||||
autoscaler.update()
|
||||
self.waitForNodes(2)
|
||||
for node in self.provider.mock_nodes.values():
|
||||
node.state = "running"
|
||||
self.provider.finish_starting_nodes()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(2, tag_filters={TAG_RAY_NODE_STATUS: "up-to-date"})
|
||||
self.waitForNodes(
|
||||
2, tag_filters={TAG_RAY_NODE_STATUS: STATUS_UP_TO_DATE})
|
||||
|
||||
# Mark a node as unhealthy
|
||||
lm.last_heartbeat_time_by_ip["172.0.0.0"] = 0
|
||||
for _ in range(5):
|
||||
if autoscaler.updaters:
|
||||
time.sleep(0.05)
|
||||
autoscaler.update()
|
||||
assert not autoscaler.updaters
|
||||
num_calls = len(runner.calls)
|
||||
lm.last_heartbeat_time_by_ip["172.0.0.0"] = 0
|
||||
autoscaler.update()
|
||||
self.waitFor(lambda: len(runner.calls) > num_calls, num_retries=150)
|
||||
|
||||
@@ -901,6 +952,136 @@ class AutoscalingTest(unittest.TestCase):
|
||||
StandardAutoscaler(
|
||||
invalid_provider, LoadMetrics(), update_interval_s=0)
|
||||
|
||||
def testSetupCommandsWithNoNodeCaching(self):
|
||||
config = SMALL_CLUSTER.copy()
|
||||
config["min_workers"] = 1
|
||||
config["max_workers"] = 1
|
||||
config_path = self.write_config(config)
|
||||
self.provider = MockProvider(cache_stopped=False)
|
||||
runner = MockProcessRunner()
|
||||
lm = LoadMetrics()
|
||||
autoscaler = StandardAutoscaler(
|
||||
config_path,
|
||||
lm,
|
||||
max_failures=0,
|
||||
process_runner=runner,
|
||||
update_interval_s=0)
|
||||
autoscaler.update()
|
||||
self.waitForNodes(1)
|
||||
self.provider.finish_starting_nodes()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(
|
||||
1, tag_filters={TAG_RAY_NODE_STATUS: STATUS_UP_TO_DATE})
|
||||
runner.assert_has_call("172.0.0.0", "init_cmd")
|
||||
runner.assert_has_call("172.0.0.0", "setup_cmd")
|
||||
runner.assert_has_call("172.0.0.0", "worker_setup_cmd")
|
||||
runner.assert_has_call("172.0.0.0", "start_ray_worker")
|
||||
|
||||
# Check the node was not reused
|
||||
self.provider.terminate_node(0)
|
||||
autoscaler.update()
|
||||
self.waitForNodes(1)
|
||||
runner.clear_history()
|
||||
self.provider.finish_starting_nodes()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(
|
||||
1, tag_filters={TAG_RAY_NODE_STATUS: STATUS_UP_TO_DATE})
|
||||
runner.assert_has_call("172.0.0.1", "init_cmd")
|
||||
runner.assert_has_call("172.0.0.1", "setup_cmd")
|
||||
runner.assert_has_call("172.0.0.1", "worker_setup_cmd")
|
||||
runner.assert_has_call("172.0.0.1", "start_ray_worker")
|
||||
|
||||
def testSetupCommandsWithStoppedNodeCaching(self):
|
||||
config = SMALL_CLUSTER.copy()
|
||||
config["min_workers"] = 1
|
||||
config["max_workers"] = 1
|
||||
config_path = self.write_config(config)
|
||||
self.provider = MockProvider(cache_stopped=True)
|
||||
runner = MockProcessRunner()
|
||||
lm = LoadMetrics()
|
||||
autoscaler = StandardAutoscaler(
|
||||
config_path,
|
||||
lm,
|
||||
max_failures=0,
|
||||
process_runner=runner,
|
||||
update_interval_s=0)
|
||||
autoscaler.update()
|
||||
self.waitForNodes(1)
|
||||
self.provider.finish_starting_nodes()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(
|
||||
1, tag_filters={TAG_RAY_NODE_STATUS: STATUS_UP_TO_DATE})
|
||||
runner.assert_has_call("172.0.0.0", "init_cmd")
|
||||
runner.assert_has_call("172.0.0.0", "setup_cmd")
|
||||
runner.assert_has_call("172.0.0.0", "worker_setup_cmd")
|
||||
runner.assert_has_call("172.0.0.0", "start_ray_worker")
|
||||
|
||||
# Check the node was indeed reused
|
||||
self.provider.terminate_node(0)
|
||||
autoscaler.update()
|
||||
self.waitForNodes(1)
|
||||
runner.clear_history()
|
||||
self.provider.finish_starting_nodes()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(
|
||||
1, tag_filters={TAG_RAY_NODE_STATUS: STATUS_UP_TO_DATE})
|
||||
runner.assert_not_has_call("172.0.0.0", "init_cmd")
|
||||
runner.assert_not_has_call("172.0.0.0", "setup_cmd")
|
||||
runner.assert_not_has_call("172.0.0.0", "worker_setup_cmd")
|
||||
runner.assert_has_call("172.0.0.0", "start_ray_worker")
|
||||
|
||||
runner.clear_history()
|
||||
autoscaler.update()
|
||||
runner.assert_not_has_call("172.0.0.0", "setup_cmd")
|
||||
|
||||
# We did not start any other nodes
|
||||
runner.assert_not_has_call("172.0.0.1", " ")
|
||||
|
||||
def testMultiNodeReuse(self):
|
||||
config = SMALL_CLUSTER.copy()
|
||||
config["min_workers"] = 3
|
||||
config["max_workers"] = 3
|
||||
config_path = self.write_config(config)
|
||||
self.provider = MockProvider(cache_stopped=True)
|
||||
runner = MockProcessRunner()
|
||||
lm = LoadMetrics()
|
||||
autoscaler = StandardAutoscaler(
|
||||
config_path,
|
||||
lm,
|
||||
max_failures=0,
|
||||
process_runner=runner,
|
||||
update_interval_s=0)
|
||||
autoscaler.update()
|
||||
self.waitForNodes(3)
|
||||
self.provider.finish_starting_nodes()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(
|
||||
3, tag_filters={TAG_RAY_NODE_STATUS: STATUS_UP_TO_DATE})
|
||||
|
||||
self.provider.terminate_node(0)
|
||||
self.provider.terminate_node(1)
|
||||
self.provider.terminate_node(2)
|
||||
runner.clear_history()
|
||||
|
||||
# Scale up to 10 nodes, check we reuse the first 3 and add 7 more.
|
||||
config["min_workers"] = 10
|
||||
config["max_workers"] = 10
|
||||
self.write_config(config)
|
||||
autoscaler.update()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(10)
|
||||
self.provider.finish_starting_nodes()
|
||||
autoscaler.update()
|
||||
self.waitForNodes(
|
||||
10, tag_filters={TAG_RAY_NODE_STATUS: STATUS_UP_TO_DATE})
|
||||
autoscaler.update()
|
||||
for i in [0, 1, 2]:
|
||||
runner.assert_not_has_call("172.0.0.{}".format(i), "setup_cmd")
|
||||
runner.assert_has_call("172.0.0.{}".format(i), "start_ray_worker")
|
||||
for i in [3, 4, 5, 6, 7, 8, 9]:
|
||||
runner.assert_has_call("172.0.0.{}".format(i), "setup_cmd")
|
||||
runner.assert_has_call("172.0.0.{}".format(i), "start_ray_worker")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
|
||||
Reference in New Issue
Block a user