Repository navigation
fix(deletes): protect branch-pinned deployments during retention - #15075
Conversation
Retention only kept deployments listed on manual rules' deploymentId. Branch-pinned domains follow the newest ready VCS build on that branch, but activateBranchRule ran after the deployment was marked ready, so maintenance could retire the build before the rule row was repointed. Resolve the protected deployment from the branch build query used when creating branch rules, and repoint branch rules in the same transaction that marks a function build ready. Co-authored-by: Chirag Aggarwal <[email protected]>
|
Review complete. 🟡 2 medium 💬 Inline comments (2)
🧹 Nitpicks (1) — 🟢 1 low
The change keeps the latest ready deployment for each branch-rule alive during retention sweeps and repoints those rules transactionally. One producer artifact (the high-recall diff sweep) never published, so its coverage is absent from this review.
Reviewed commit: da23f61 |
🟢 Tier S · Ready to merge
Deployment retention now excludes active deployments, deployments referenced by manual rules, and the newest ready VCS build for branch-pinned rules. The protection query is refreshed for retention passes and before the count cutoff, while successful VCS builds update branch rules only after the ready-state transaction commits. The added unit regression verifies that refreshed protection keeps a deployment out of the count-cap calculation. Latest changes: The latest commit adds a RetentionTest regression that supplies the pre-count refresh callback and verifies a protected deployment does not consume a retention slot.
📂 Walkthrough · 4
✅ Fixed since the last review · 1
Reviewed the commits since |
Worker retention protection stays covered by VCSGitea E2E; AGENTS.md reserves unit tests for libraries, not Platform workers. Co-authored-by: Chirag Aggarwal <[email protected]>
There was a problem hiding this comment.
This PR protects the newest ready branch build from retention cleanup and moves branch-rule activation inside a database transaction.
Key findings
- 🟡 Retention kept-list races build worker rule repoint — Deletes.php:650
- 🟡 Branch-rule repoint inside transaction can outlive rollback — Jobs.php:812
| $kept[] = $rule->getAttribute('deploymentId'); | ||
| } | ||
| ); | ||
| $kept = $this->deploymentIdsProtectedFromRetention($dbForPlatform, $dbForProject, $project, $resource); |
There was a problem hiding this comment.
🟡 bug · medium
Retention kept-list races build worker rule repoint
deleteOldDeployments computes the protected-id snapshot once and retires deployments in later passes (src/Appwrite/Platform/Workers/Deletes.php:650, 662-664). A build worker can finalize between the snapshot and the delete and repoint a branch rule at the new deployment (src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php:917-921); that deploymentId was not in the kept list, so retention can delete the deployment the rule now serves. The new branch-newest lookup narrows but does not close the window, since a deployment still building at snapshot time matches neither status: ready nor any rule's deploymentId, and when two builds of the same branch finish out of order activateBranchRule repoints the rule to the just-finished build even though the kept lookup already captured a different newest-ready id. A domain pinned to a branch can briefly serve a deleted build after retention runs concurrently with a successful push.
📋 Prompt for AI Agents
In src/Appwrite/Platform/Workers/Deletes.php, deleteOldDeployments() around lines 650-664: the $kept id list from deploymentIdsProtectedFromRetention() is snapshotted once, but the build worker can repoint a manual/branch rule to a newer deployment between the snapshot and retireDeployments(), letting retention delete a deployment a rule now serves. Close the window by recomputing deploymentIdsProtectedFromRetention() inside the loop over $retention->passes(...) (or immediately before enqueueing each delete in retireDeployments(), re-fetching the resource's manual rules with the same filters and skipping deployments a rule's current deploymentId points at) so a rule repointed by the build worker during retention is still honored.
| } elseif ($applied > 0 && $success && $collection !== 'sites' && $deployment->getAttribute('activate') !== true && ! $resource->isEmpty()) { | ||
| $this->activateBranchRule($dbForPlatform, $project, $resource, $deployment, $bus); |
There was a problem hiding this comment.
🟡 bug · medium
Branch-rule repoint inside transaction can outlive rollback
activateBranchRule moved inside the $dbForProject->withTransaction(...) closure (src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php:812-813), but it writes rules through $dbForPlatform->updateDocument and dispatches RuleUpdated bus events that the transaction does not cover. If the transaction fails or rolls back after the closure body ran (deadlock, serialization failure on the status update), the deployment's status: ready transition is rolled back while the branch rules were already repointed to it — the router can serve a deployment that never became ready on a branch-pinned domain. Pre-patch, activateBranchRule ran strictly after the transaction returned, so a rollback could never leave repointed rules behind.
📋 Prompt for AI Agents
In src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php, finalize() around lines 803-819: activateBranchRule was moved inside the $dbForProject->withTransaction closure, but it mutates platform-DB rules and dispatches RuleUpdated bus events that are outside the transaction's rollback scope. Restore the previous ordering: inside the transaction keep only the status update and activate() (which was already transactional pre-patch), and after the transaction returns with $applied > 0, run the branch-rule condition ($success && $collection !== 'sites' && $deployment->getAttribute('activate') !== true && !$resource->isEmpty()) before the activateBranchPreviewRule block, so a transaction rollback never leaves branch rules repointed to a deployment whose ready status was rolled back.
Recompute rule and branch-pinned keep list before every retention pass so a build worker repoint during maintenance is honored. Move activateBranchRule back outside the project transaction so platform rule writes cannot outlive a rollback of the ready status. Co-authored-by: Chirag Aggarwal <[email protected]>
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
Pass retention the same resource queries as before, including notEqual filters for protected builds, while still recomputing that keep list before each pass when enqueuing deletes. Co-authored-by: Chirag Aggarwal <[email protected]>
Retention passes now accept a callback that rebuilds resource queries immediately before the count-cap lookup, after age passes may have run concurrently with new branch builds. Co-authored-by: Chirag Aggarwal <[email protected]>
Assert that refreshResourceBeforeCount shifts the count-cap cutoff when a protected deployment must not consume a retention slot. Co-authored-by: Chirag Aggarwal <[email protected]>
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Evidence
Flaky E2E:
Tests\E2E\Services\VCSGitea\VCSGiteaConsoleClientTest::testDeploymentRetentionKeepsTheBuildBehindABranchDomain(PostgreSQL dedicated VCSGitea lane).the build behind the custom domain is kept—$pinnedIdmissing from deployments after maintenance retention (~line 268)Before fix (historical): 4/4 failing runs on unique commits; ~0% pass rate on those attempts (1/1 pass on manual rerun only).
After fix: local 20× E2E loop was not feasible in this cloud agent VM (no Docker daemon). Validation is via CI on this PR (VCSGitea lane) and the existing E2E test that reproduces the scenario.
Root cause
Retention kept deployments only from a one-time snapshot of manual rules’
deploymentId. Branch-pinned domains serve the newest ready VCS build on that branch, but the build worker repoints rules on the platform DB after the deployment isready, so maintenance could retire the pinned build while the rule row still pointed at the active deployment (or while a concurrent build finished mid-retention).Fix
Deletes:deploymentIdsProtectedFromRetention()keeps deployments referenced by manual rules and resolves branch-pinned rules via the same newest-ready VCS query used when creating branch rules. The keep list is recomputed before each retention pass (with a fresh resource read) so repoints during maintenance are honored.Jobs:activateBranchRule()stays after the project DB transaction commits (avoids platform rule writes surviving a rollback ofready). The Deletes-side branch lookup closes the original E2E race.Tests
VCSGiteaConsoleClientTest::testDeploymentRetentionKeepsTheBuildBehindABranchDomain(existing E2E).Related: appwrite-labs/cloud#6673