|
| 1 | +# Copyright The Marin Authors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""Checkpoint/restore test for Iris controller.""" |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import time |
| 8 | + |
| 9 | +from iris.client.client import IrisClient, Job |
| 10 | +from iris.cluster.config import load_config, make_local_config |
| 11 | +from iris.cluster.providers.local.cluster import LocalCluster |
| 12 | +from iris.cluster.types import Entrypoint, EnvironmentSpec, ResourceSpec, is_job_finished |
| 13 | +from iris.rpc import cluster_pb2 |
| 14 | +from iris.rpc.cluster_connect import ControllerServiceClientSync |
| 15 | + |
| 16 | +IRIS_ROOT = Path(__file__).resolve().parents[1] |
| 17 | +DEFAULT_CONFIG = IRIS_ROOT / "examples" / "test.yaml" |
| 18 | + |
| 19 | + |
| 20 | +def _quick(): |
| 21 | + return 1 |
| 22 | + |
| 23 | + |
| 24 | +class _IrisTestHelper: |
| 25 | + """Minimal helper to submit and wait for jobs (standalone, no integration fixtures).""" |
| 26 | + |
| 27 | + def __init__(self, url: str, client: IrisClient, controller_client: ControllerServiceClientSync): |
| 28 | + self.url = url |
| 29 | + self.client = client |
| 30 | + self.controller_client = controller_client |
| 31 | + |
| 32 | + def wait_for_workers(self, count: int, timeout: float = 30): |
| 33 | + deadline = time.monotonic() + timeout |
| 34 | + while time.monotonic() < deadline: |
| 35 | + resp = self.controller_client.list_workers(cluster_pb2.Controller.ListWorkersRequest()) |
| 36 | + healthy = [w for w in resp.workers if w.healthy] |
| 37 | + if len(healthy) >= count: |
| 38 | + return |
| 39 | + time.sleep(0.5) |
| 40 | + raise TimeoutError(f"Expected {count} healthy workers, timed out") |
| 41 | + |
| 42 | + def submit(self, fn, name: str) -> Job: |
| 43 | + return self.client.submit( |
| 44 | + entrypoint=Entrypoint.from_callable(fn), |
| 45 | + name=name, |
| 46 | + resources=ResourceSpec(cpu=1, memory="1g"), |
| 47 | + environment=EnvironmentSpec(), |
| 48 | + ) |
| 49 | + |
| 50 | + def wait(self, job: Job, timeout: float = 30) -> cluster_pb2.JobStatus: |
| 51 | + deadline = time.monotonic() + timeout |
| 52 | + while time.monotonic() < deadline: |
| 53 | + resp = self.controller_client.get_job_status( |
| 54 | + cluster_pb2.Controller.GetJobStatusRequest(job_id=job.job_id.to_wire()) |
| 55 | + ) |
| 56 | + if is_job_finished(resp.job.state): |
| 57 | + return resp.job |
| 58 | + time.sleep(0.5) |
| 59 | + raise TimeoutError(f"Job {job.job_id} did not finish within {timeout}s") |
| 60 | + |
| 61 | + |
| 62 | +def test_checkpoint_restore(): |
| 63 | + """Controller restart resumes from checkpoint: completed jobs visible, cluster functional.""" |
| 64 | + config = load_config(DEFAULT_CONFIG) |
| 65 | + config = make_local_config(config) |
| 66 | + |
| 67 | + cluster = LocalCluster(config) |
| 68 | + url = cluster.start() |
| 69 | + try: |
| 70 | + client = IrisClient.remote(url, workspace=IRIS_ROOT) |
| 71 | + controller_client = ControllerServiceClientSync(address=url, timeout_ms=30000) |
| 72 | + tc = _IrisTestHelper(url=url, client=client, controller_client=controller_client) |
| 73 | + tc.wait_for_workers(1, timeout=30) |
| 74 | + |
| 75 | + job = tc.submit(_quick, "pre-restart") |
| 76 | + tc.wait(job, timeout=30) |
| 77 | + saved_job_id = job.job_id.to_wire() |
| 78 | + |
| 79 | + ckpt = controller_client.begin_checkpoint(cluster_pb2.Controller.BeginCheckpointRequest()) |
| 80 | + assert ckpt.checkpoint_path, "begin_checkpoint returned empty path" |
| 81 | + assert ckpt.job_count >= 1 |
| 82 | + controller_client.close() |
| 83 | + |
| 84 | + url = cluster.restart() |
| 85 | + |
| 86 | + controller_client = ControllerServiceClientSync(address=url, timeout_ms=30000) |
| 87 | + tc = _IrisTestHelper( |
| 88 | + url=url, client=IrisClient.remote(url, workspace=IRIS_ROOT), controller_client=controller_client |
| 89 | + ) |
| 90 | + |
| 91 | + resp = controller_client.get_job_status(cluster_pb2.Controller.GetJobStatusRequest(job_id=saved_job_id)) |
| 92 | + assert ( |
| 93 | + resp.job.state == cluster_pb2.JOB_STATE_SUCCEEDED |
| 94 | + ), f"Pre-restart job has state {resp.job.state} after restore" |
| 95 | + |
| 96 | + tc.wait_for_workers(1, timeout=30) |
| 97 | + post_job = tc.submit(_quick, "post-restart") |
| 98 | + status = tc.wait(post_job, timeout=30) |
| 99 | + assert status.state == cluster_pb2.JOB_STATE_SUCCEEDED |
| 100 | + |
| 101 | + controller_client.close() |
| 102 | + finally: |
| 103 | + cluster.close() |
0 commit comments