Repository navigation
[Feature] Restart cancelled block allocation workers in SlurmClusterExecutor #1129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -277,6 +277,9 @@ class TestClusterExecutor(BaseExecutor): | |
| to be defined on the executor, rather than during the submission of the individual | ||
| function. | ||
| init_function (None): optional function to preset arguments for functions which are submitted later | ||
| restart_limit (int): The maximum number of restarting worker processes. Only available with | ||
| block_allocation=True. Mirrors the restart_limit of the SlurmClusterExecutor, so restarting | ||
| workers which were cancelled by the queuing system can be tested locally. Defaults to 0. | ||
| disable_dependencies (boolean): Disable resolving future objects during the submission. | ||
| refresh_rate (float): Set the refresh rate in seconds, how frequently the input queue is checked. | ||
| plot_dependency_graph (bool): Plot the dependencies of multiple future objects without executing them. For | ||
|
|
@@ -317,6 +320,7 @@ def __init__( | |
| hostname_localhost: bool | None = None, | ||
| block_allocation: bool = False, | ||
| init_function: Callable | None = None, | ||
| restart_limit: int = 0, | ||
| disable_dependencies: bool = False, | ||
| refresh_rate: float = 0.01, | ||
| plot_dependency_graph: bool = False, | ||
|
|
@@ -368,6 +372,9 @@ def __init__( | |
| resources have to be defined on the executor, rather than during the submission | ||
| of the individual function. | ||
| init_function (None): optional function to preset arguments for functions which are submitted later | ||
| restart_limit (int): The maximum number of restarting worker processes. Only available with | ||
| block_allocation=True. Mirrors the restart_limit of the SlurmClusterExecutor, so restarting | ||
| workers which were cancelled by the queuing system can be tested locally. Defaults to 0. | ||
| disable_dependencies (boolean): Disable resolving future objects during the submission. | ||
| refresh_rate (float): Set the refresh rate in seconds, how frequently the input queue is checked. | ||
| plot_dependency_graph (bool): Plot the dependencies of multiple future objects without executing them. For | ||
|
|
@@ -392,7 +399,27 @@ def __init__( | |
| resource_dict.update( | ||
| {k: v for k, v in default_resource_dict.items() if k not in resource_dict} | ||
| ) | ||
| if not plot_dependency_graph: | ||
| check_restart_limit( | ||
| restart_limit=restart_limit, block_allocation=block_allocation | ||
| ) | ||
| if not plot_dependency_graph and block_allocation: | ||
| # Mirror the block allocation mode of the SlurmClusterExecutor: long-lived workers which are restarted up | ||
| # to restart_limit times, started as local processes rather than as jobs of the queuing system. | ||
| super().__init__( | ||
| executor=create_single_node_executor( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Block-allocation mode skips Future argument resolution. With the default Suggested fix
|
||
| max_workers=max_workers, | ||
| cache_directory=cache_directory, | ||
| max_cores=max_cores, | ||
| executor_kwargs=resource_dict, | ||
| hostname_localhost=hostname_localhost, | ||
| block_allocation=block_allocation, | ||
| init_function=init_function, | ||
| log_obj_size=log_obj_size, | ||
| validator=validate_resource_dict_with_optional_keys, | ||
| restart_limit=restart_limit, | ||
| ) | ||
| ) | ||
| elif not plot_dependency_graph: | ||
| from executorlib.task_scheduler.file.spawner_subprocess import ( | ||
| subprocess_execute, | ||
| ) | ||
|
|
@@ -434,6 +461,7 @@ def __init__( | |
| block_allocation=block_allocation, | ||
| init_function=init_function, | ||
| log_obj_size=log_obj_size, | ||
| restart_limit=restart_limit, | ||
| ), | ||
| max_cores=max_cores, | ||
| refresh_rate=refresh_rate, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -235,6 +235,7 @@ def create_pysqa_block_allocation_scheduler( | |
| pysqa_config_directory: str | None = None, | ||
| backend: str | None = None, | ||
| validator: Callable = validate_resource_dict, | ||
| restart_limit: int = 0, | ||
| ): | ||
| check_init_function_with_cache( | ||
| init_function=init_function, cache_directory=cache_directory | ||
|
|
@@ -265,4 +266,5 @@ def create_pysqa_block_allocation_scheduler( | |
| executor_kwargs=executor_kwargs, | ||
| spawner=PysqaSpawner, | ||
| validator=validator, | ||
| restart_limit=restart_limit, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A failed worker restart can leave queued Futures pending. If the replacement job's status query returns This path skips Handle a failed bootup the same way as an exhausted restart budget. Decrement the alive-worker count, then recycle or fail the queued tasks through the same recovery path as
|
||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import os | ||
| import shutil | ||
| import signal | ||
| import tempfile | ||
| import unittest | ||
| from time import sleep | ||
|
|
||
| from executorlib.api import TestClusterExecutor | ||
| from executorlib.standalone.interactive.communication import ExecutorlibSocketError | ||
| from executorlib.standalone.serialize import cloudpickle_register | ||
|
|
||
|
|
||
| def get_pid(): | ||
| return os.getpid() | ||
|
|
||
|
|
||
| def add_function(parameter_1, parameter_2): | ||
| return parameter_1 + parameter_2 | ||
|
|
||
|
|
||
| def init_function(): | ||
| return {"parameter_2": 3} | ||
|
|
||
|
|
||
| def kill_worker_on_first_call(marker_file): | ||
| """ | ||
| Simulate a worker which is cancelled by the queuing system (e.g. preempted on an overflow queue) while it executes | ||
| a task: the first call creates the marker file and kills its own worker process, every following call returns the | ||
| process id of the worker. | ||
| """ | ||
| import os | ||
| import signal | ||
|
|
||
| if not os.path.exists(marker_file): | ||
| open(marker_file, "w").close() | ||
| os.kill(os.getpid(), signal.SIGKILL) | ||
| return os.getpid() | ||
|
|
||
|
|
||
| def wait_for_process_to_exit(pid, timeout=10.0): | ||
| """The worker is a child of the test process, so poll until it is gone (or reaped).""" | ||
| for _ in range(int(timeout / 0.1)): | ||
| try: | ||
| os.kill(pid, 0) | ||
| except ProcessLookupError: | ||
| return | ||
| try: | ||
| finished_pid, _ = os.waitpid(pid, os.WNOHANG) | ||
| except ChildProcessError: | ||
| return | ||
| if finished_pid == pid: | ||
| return | ||
| sleep(0.1) | ||
|
|
||
|
|
||
| class TestTestClusterExecutorBlockAllocation(unittest.TestCase): | ||
| def setUp(self): | ||
| self.working_directory = tempfile.mkdtemp() | ||
| self.marker_file = os.path.join(self.working_directory, "killed") | ||
|
|
||
| def tearDown(self): | ||
| shutil.rmtree(self.working_directory, ignore_errors=True) | ||
|
|
||
| def test_block_allocation(self): | ||
| with TestClusterExecutor(max_workers=2, block_allocation=True) as exe: | ||
| cloudpickle_register(ind=1) | ||
| future_lst = [exe.submit(add_function, i, parameter_2=1) for i in range(4)] | ||
| self.assertEqual([f.result() for f in future_lst], [1, 2, 3, 4]) | ||
|
|
||
| def test_block_allocation_init_function(self): | ||
| # The init_function is transferred when the worker boots up, so it has to be registered beforehand. | ||
| cloudpickle_register(ind=1) | ||
| with TestClusterExecutor( | ||
| max_workers=1, block_allocation=True, init_function=init_function | ||
| ) as exe: | ||
| self.assertEqual(exe.submit(add_function, 1).result(), 4) | ||
|
|
||
| def test_restart_worker_cancelled_during_task(self): | ||
| with TestClusterExecutor( | ||
| max_workers=1, block_allocation=True, restart_limit=1 | ||
| ) as exe: | ||
| cloudpickle_register(ind=1) | ||
| first_pid = exe.submit(get_pid).result() | ||
| restarted_pid = exe.submit( | ||
| kill_worker_on_first_call, self.marker_file | ||
| ).result() | ||
| self.assertTrue(os.path.exists(self.marker_file)) | ||
| self.assertNotEqual(first_pid, restarted_pid) | ||
| self.assertEqual(exe.submit(get_pid).result(), restarted_pid) | ||
|
|
||
| def test_restart_worker_cancelled_while_idle(self): | ||
| with TestClusterExecutor( | ||
| max_workers=1, block_allocation=True, restart_limit=1 | ||
| ) as exe: | ||
| cloudpickle_register(ind=1) | ||
| first_pid = exe.submit(get_pid).result() | ||
| os.kill(first_pid, signal.SIGKILL) | ||
| wait_for_process_to_exit(pid=first_pid) | ||
| restarted_pid = exe.submit(get_pid).result(timeout=60) | ||
| self.assertNotEqual(first_pid, restarted_pid) | ||
|
|
||
| def test_restart_limit_exhausted(self): | ||
| with self.assertRaises(ExecutorlibSocketError): | ||
| with TestClusterExecutor( | ||
| max_workers=1, block_allocation=True, restart_limit=0 | ||
| ) as exe: | ||
| cloudpickle_register(ind=1) | ||
| exe.submit(kill_worker_on_first_call, self.marker_file).result() | ||
|
|
||
| def test_restart_limit_requires_block_allocation(self): | ||
| with self.assertRaises(ValueError): | ||
| TestClusterExecutor(block_allocation=False, restart_limit=1) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,5 +153,13 @@ def test_slurm_cluster_block_allocation(self): | |
| with self.assertRaises(ValueError): | ||
| SlurmClusterExecutor(block_allocation=True) | ||
|
|
||
| def test_slurm_cluster_block_allocation_restart_limit(self): | ||
| with self.assertRaises(ValueError): | ||
| SlurmClusterExecutor(block_allocation=True, restart_limit=2) | ||
|
Comment on lines
+157
to
+158
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The restart-limit test does not check restart-limit acceptance.
Patch
|
||
|
|
||
| def test_slurm_cluster_restart_limit_requires_block_allocation(self): | ||
| with self.assertRaises(ValueError): | ||
| SlurmClusterExecutor(block_allocation=False, restart_limit=1) | ||
|
|
||
| def test_slurm_cluster_file(self): | ||
| self.assertTrue(SlurmClusterExecutor(block_allocation=False)) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve existing positional constructor arguments.
An existing call such as
TestClusterExecutor(1, None, None, None, None, False, None, True)previously setdisable_dependencies=True. It now setsrestart_limit=Trueand raisesValueError.The insertion in
SlurmClusterExecutorhas the same effect. Appendrestart_limitafter the existing parameters in both constructors. This preserves existing calls and supports the new keyword argument.