Repository navigation
fleet queue: preserve the holder’s deadline on early heartbeats #13589
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
Open
dezren39
wants to merge
1
commit into
main
Choose a base branch
from
codex/fleet-13553-heartbeat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| UPDATE tasks | ||
| SET lease_until = CASE WHEN julianday(lease_until) >= julianday(?1) | ||
| THEN lease_until ELSE ?1 END, | ||
| progress = ?2, updated_at = ?3 | ||
| WHERE id = ?4 AND status = 'claimed' AND worker_id = ?5 | ||
| AND claimed_at IS ?6 AND lane_id IS ?7 AND attempts = ?8 |
12 changes: 12 additions & 0 deletions
12
pkgs/fleet/crates/fleet-queue/src/turso_queue/heartbeat_lease_mirror.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| CREATE TRIGGER IF NOT EXISTS heartbeat_lease_mirror | ||
| AFTER UPDATE OF lease_until ON tasks | ||
| WHEN OLD.status = 'claimed' AND NEW.status = 'claimed' | ||
| AND NEW.worker_id IS NOT NULL AND NEW.worker_id IS OLD.worker_id | ||
| AND NEW.claimed_at IS OLD.claimed_at AND NEW.lane_id IS OLD.lane_id | ||
| AND NEW.lease_until IS NOT NULL | ||
| BEGIN | ||
| INSERT INTO leases (task_id, worker_id, lease_until, heartbeat_at) | ||
| VALUES (NEW.id, NEW.worker_id, NEW.lease_until, NEW.updated_at) | ||
| ON CONFLICT(task_id) DO UPDATE SET worker_id = excluded.worker_id, | ||
| lease_until = excluded.lease_until, heartbeat_at = excluded.heartbeat_at; | ||
| END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Run the broker's heartbeat SQL against an isolated SQLite database.""" | ||
|
|
||
| from pathlib import Path | ||
| import sqlite3 | ||
| import unittest | ||
|
|
||
| SQL = Path(__file__).resolve().parents[1] / "crates/fleet-queue/src/turso_queue" | ||
| HEARTBEAT = (SQL / "heartbeat.sql").read_text() | ||
| MIRROR = (SQL / "heartbeat_lease_mirror.sql").read_text() | ||
|
|
||
|
|
||
| class HeartbeatLeaseTest(unittest.TestCase): | ||
| def setUp(self): | ||
| self.db = sqlite3.connect(":memory:") | ||
| self.addCleanup(self.db.close) | ||
| self.db.executescript(""" | ||
| CREATE TABLE tasks (id INTEGER PRIMARY KEY, worker_id TEXT, | ||
| lane_id TEXT, claimed_at TEXT, attempts INTEGER, status TEXT, | ||
| progress TEXT, updated_at TEXT, lease_until TEXT); | ||
| CREATE TABLE leases (task_id INTEGER PRIMARY KEY, worker_id TEXT, | ||
| lease_until TEXT, heartbeat_at TEXT); | ||
| INSERT INTO tasks VALUES (1,'worker','lane','start',2,'claimed', | ||
| '{}','2026-01-01T00:00:00Z','2026-01-01T04:00:00Z'); | ||
| """) | ||
| self.db.execute(MIRROR) | ||
|
|
||
| def beat(self, proposed, now="2026-01-01T00:05:00Z"): | ||
| return self.db.execute(HEARTBEAT, ( | ||
| proposed, '{"step":"review"}', now, 1, "worker", "start", "lane", 2 | ||
| )).rowcount | ||
|
|
||
| def deadline(self): | ||
| return self.db.execute("SELECT lease_until FROM tasks").fetchone()[0] | ||
|
|
||
| def test_later_deadline_and_heartbeat_clock_are_mirrored(self): | ||
| initial = self.deadline() | ||
| later = "2026-01-01T04:10:00Z" | ||
| for now, proposed, expected in [ | ||
| ("2026-01-01T00:05:00Z", "2026-01-01T00:20:00Z", initial), | ||
| ("2026-01-01T00:05:00Z", "2026-01-01T00:20:00Z", initial), | ||
| ("2026-01-01T03:55:00Z", later, later), | ||
| ("2026-01-01T03:56:00Z", "2026-01-01T04:00:00Z", later), | ||
| ]: | ||
| with self.subTest(proposed=proposed, now=now): | ||
| self.assertEqual(self.beat(proposed, now), 1) | ||
| self.assertEqual(self.deadline(), expected) | ||
| self.assertEqual(self.db.execute( | ||
| "SELECT worker_id, lease_until, heartbeat_at FROM leases" | ||
| ).fetchone(), ("worker", expected, now)) | ||
|
|
||
| def test_changed_claim_cannot_be_renewed(self): | ||
| for column, value in [ | ||
| ("status", "parked"), ("worker_id", "other"), ("lane_id", None), | ||
| ("claimed_at", "next claim"), ("attempts", 3), | ||
| ]: | ||
| with self.subTest(column=column): | ||
| self.db.execute("SAVEPOINT replacement") | ||
| self.db.execute(f"UPDATE tasks SET {column} = ?", (value,)) | ||
| self.assertEqual(self.beat("2026-01-01T09:00:00Z"), 0) | ||
| self.assertEqual(self.deadline(), "2026-01-01T04:00:00Z") | ||
| self.assertIsNone(self.db.execute("SELECT * FROM leases").fetchone()) | ||
| self.db.execute("ROLLBACK TO replacement") | ||
| self.db.execute("RELEASE replacement") | ||
|
|
||
| def test_lease_mirror_failure_rolls_back_task_update(self): | ||
| self.db.execute("""CREATE TRIGGER fail_lease BEFORE INSERT ON leases | ||
| BEGIN SELECT RAISE(ABORT, 'injected mirror failure'); END""") | ||
| with self.assertRaisesRegex(sqlite3.IntegrityError, "injected mirror failure"): | ||
| self.beat("2026-01-01T09:00:00Z") | ||
| self.assertEqual(self.deadline(), "2026-01-01T04:00:00Z") | ||
| self.assertEqual(self.db.execute("SELECT progress FROM tasks").fetchone(), ("{}",)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The new heartbeat tests exercise fixed examples only, although deadline monotonicity, task/lease agreement, and rejection of changed claim identities are meaningful state-machine invariants. The repository's testing doctrine requires property/model-based coverage for these invariants; example tests alone do not satisfy that requirement. Add generated renewal sequences and claim mutations checked against a reference model, retaining these examples as regression cases.
AGENTS.md reference: AGENTS.md:L36-L39
Useful? React with 👍 / 👎.