MediumSchedulingPython 3
Worker Lease Registry
Coordinate worker ownership with TTL leases, heartbeat extension, stale-worker rejection, and fencing tokens.
40m2 sample tests4 hidden tests
Implement LeaseRegistry, an in-memory lease table for long-running worker jobs.
Requirements
acquire(run_id, worker_id, now)returns a lease dictionary orNone.- Only one active lease may exist per run.
- An expired lease can be acquired by another worker.
- Every new lease gets a monotonically increasing
fencing_token. heartbeat(lease_id, fencing_token, now)extends the current lease and returnsTrueorFalse.validate(run_id, lease_id, fencing_token, now)returns whether a worker may still mutate the run.release(lease_id, fencing_token)releases only the current lease.
Example
python
1registry = LeaseRegistry(ttl_seconds=10)
2lease = registry.acquire("run-1", "worker-a", now=0)
3assert lease["fencing_token"] == 1
4assert registry.acquire("run-1", "worker-b", now=5) is None
5assert registry.acquire("run-1", "worker-b", now=11)["fencing_token"] == 2Constraints
- Keep it single-process and in memory.
- Use the provided
nowvalue. Don't call wall-clock time. - Treat expiry as expired when
expires_at <= now.
Editor