Repository navigation
fix(jobs): append a deployment's pending log callbacks in one write - #15881
Conversation
Every log callback took the per-deployment lock on its own and rewrote the whole buildLogs attribute, so a build streaming output queued callbacks on the lock faster than it drained. Those left waiting past LOCK_TIMEOUT threw Contention and went back to the queue, landing their lines out of order. Log callbacks now leave their lines in a per-process buffer and queue on a per-deployment gate, so one coroutine per process contends for the lock and appends every pending callback's lines in a single write. The others pass the gate and return without touching the lock once their lines are in. Dedupe markers for every event in the batch are saved in the same transaction, so a redelivery of an applied callback is still skipped. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01F3M7ttqmi3HYfSr5f9kLt8 Co-authored-by: Torsten Dittmann <[email protected]>
Code review
Log callbacks for a deployment are buffered and serialized per process so pending chunks can be appended in one database write and broadcast in one realtime event. Event dedupe markers are recorded with the database transaction, and markers written by a batch are purged if that transaction fails so redelivery can apply the logs. Latest changes: The newest commit routes the buffered-entry check through a private Walkthrough (1)
Reviewed the commits since Tip Comment |
Security rulesWARNING (1 new) — review signal only; does not fail the job.
|
The markers live in Redis, outside the database transaction. When a later marker save or the commit failed, the append rolled back but the markers already written stayed, so those callbacks were skipped on redelivery and their lines lost. Purge the markers the batch wrote before rethrowing. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01F3M7ttqmi3HYfSr5f9kLt8 Co-authored-by: Torsten Dittmann <[email protected]>
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
PHPStan read the isset() right after the assignment as always true: it cannot see other coroutines appending and removing the entry while this one waits at the gate. The check now asks pending(), which names that. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01F3M7ttqmi3HYfSr5f9kLt8 Co-authored-by: Torsten Dittmann <[email protected]>
A state callback (artifact/exit/complete) that cannot acquire the per-deployment lock within LOCK_TIMEOUT (10s) threw Contention, and on the Redis broker a rejection is final: the message was dead-lettered to the failed list and never applied, while the jobs-service does not resend (the ingest endpoint already answered 204). A lost exit or complete callback could leave a deployment in building, and the failed list grew by hundreds of entries per deploy day (issue appwrite#15745). Contention is raised before the callback body runs, so a contended message has applied nothing and is safe to requeue as-is: catch it, publish the same envelope back onto v1-jobs through publisherForJobs, and let the queue provide the spacing. Requeues are capped at MAX_REQUEUES (3, i.e. four deliveries); beyond that the message is dead-lettered on purpose via PermanentFailure, so a persistently stuck lock cannot loop a message forever. A failed requeue enqueue surfaces as a RuntimeException like the existing marker-save failures, instead of losing the callback silently. The envelope gains an attempts field (default 0) that only the worker raises; the ingest endpoint and payloads already in queues are unchanged. Log callbacks do not reach this path: appwrite#15881 gates them behind a per-process batch so they no longer contend with each other. Two e2e regression tests cover the contention path: one holds the deployment lock while a real build streams its callbacks and asserts the build completes after the release, the other holds the lock past the cap and asserts a state callback dead-letters only after carrying the cap's requeue count.
A callback (log batch leader, artifact, exit, complete) that cannot acquire the per-deployment lock within LOCK_TIMEOUT (10s) threw Contention, and on the Redis broker a rejection is final: the message was dead-lettered to the failed list and never applied, while the jobs-service does not resend (the ingest endpoint already answered 204). A lost exit or complete callback could leave a deployment in building, and a lost log batch lost its lines - the failed list grew by hundreds of entries per deploy day (issue appwrite#15745). Contention is raised before the callback body runs, so a contended message has applied nothing and is safe to requeue as-is: catch it (for the state callbacks in the action body and for the gated log batch leader in log()), publish the same envelope back onto v1-jobs through publisherForJobs, and let the queue provide the spacing. A requeued log callback re-adds its lines on the next delivery, and the batch dedupe marker keeps them from appending twice. Requeues are capped at MAX_REQUEUES (3, i.e. four deliveries); beyond that the message is dead-lettered on purpose via PermanentFailure, so a persistently stuck lock cannot loop a message forever. A failed requeue enqueue surfaces as a RuntimeException like the existing marker-save failures, instead of losing the callback silently. The envelope gains an attempts field (default 0) that only the worker raises; the ingest endpoint and payloads already in queues are unchanged. Within one worker process log callbacks keep batching at appwrite#15881's gate and no longer contend with each other at all. Two e2e regression tests cover the contention path: one holds the deployment lock while a real build streams its callbacks and asserts the build completes after the release, the other holds the lock past the cap and asserts a callback dead-letters only after carrying the cap's requeue count.

What does this PR do?
Fixes the main source of
Utopia\Lock\Exception\Contention: Failed to acquire distributed lock: jobs-deployment:…in the jobs worker (Cloud Sentry CLOUD-3QVY: ~72k events, about 90–150 per deployment of a chatty site build, spread over the build's 1–2 minutes).Each log callback took the per-deployment lock on its own. Under it, it read the deployment, rewrote the whole
buildLogsattribute (up to 1 MB), read it back and published it over realtime. That cost grows with the log, so a build streaming output queues callbacks on the lock faster than they drain. Every callback still waiting afterLOCK_TIMEOUT(10s) throwsContentionand goes back to the queue. Its lines then land out of order, or are lost once the retries run out. All 8 coroutines of a worker process can also end up waiting on one deployment's lock, which stalls every other deployment's callbacks on that process.Log callbacks are now merged within each process:
Channel(1)). Only one coroutine per process contends for the distributed lock at a time.onLog()write and sends one realtime event. As before, dedupe markers for all events in the batch are saved in the same transaction. Events that are already marked, or repeated within the batch, are skipped.onLog()now takes the joined chunk and the list of event ids instead of oneJobLogand one id. Both paths send their realtime event through a smallbroadcast()method. Nothing else changes: not the payload, the status transitions or any non-log callback.Test Plan
pint --testand PHPStan (phpstan.neon) on the changed file: both pass locally.Jobs::log()/onLog()ran under Swoole 6 (PHP 8.4) against stub dependencies. It modelled 3 worker processes × 8 coroutines on one deployment, with a shared lock using the same backoff asUtopia\Lock\Distributedand a fixed DB write latency:The buffer and gate maps were empty after each run, so nothing leaks. The contention left under the heaviest load comes from the shared lock itself: its polling is unfair between processes.
Related PRs and Issues
Checklist
🤖 Generated with Claude Code
https://claude.ai/code/session_01F3M7ttqmi3HYfSr5f9kLt8