Repository navigation
fix(sites): bind branch preview rules and build state to their project - #14786
Conversation
Branch preview rules live in the shared platform rules collection, keyed by md5 of the domain. On a duplicate, activation now repoints the rule only when it belongs to the same project, site and branch, and logs and skips otherwise. New preview domains hash the project and site IDs with a separator into 16 hex characters; a branch that already has a rule under the old 7-character hash keeps that domain so existing preview URLs keep updating. The jobs worker's build-state cache markers and the per-deployment lock are now namespaced by the project's internal ID, in the worker and in both cancel endpoints, so deployments with the same ID in different projects cannot share state.
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
The change scopes deployment locks and dedupe/join cache keys per project (keying on the project sequence), aligns the cancel path with the new lock keys, and extracts a shared
Reviewed commit: 3842227 |
🟢 Tier S · Ready to mergeBranch preview domains now use a wider project/resource hash while retaining existing legacy URLs for rules owned by the same site and branch. Build callback cache markers and deployment locks are scoped by project, with the cancellation endpoints using the same lock key as the worker.
📂 Walkthrough · 10
Reviewed |
There was a problem hiding this comment.
This PR re-keys deployment lock and join-marker cache keys with the project sequence and refactors the branch preview-domain lookup into Compute/Base, with unit tests.
Key findings
- 🟡 Renamed cache markers strand builds across rolling deploys — Jobs.php:601
| if ($cache->load(self::key('exit', $project, $deploymentId), self::DEDUPE_TTL) === false || $cache->load(self::key('complete', $project, $deploymentId), self::DEDUPE_TTL) === false) { | ||
| return $deployment; | ||
| } | ||
|
|
||
| if ($deviceForBuilds->getType() !== DeviceType::Local && $cache->load('jobs-output-' . $deploymentId, self::DEDUPE_TTL) === false) { | ||
| if ($deviceForBuilds->getType() !== DeviceType::Local && $cache->load(self::key('output', $project, $deploymentId), self::DEDUPE_TTL) === false) { | ||
| return $deployment; | ||
| } | ||
|
|
||
| $manifest = $isSite ? $cache->load('jobs-manifest-' . $deploymentId, self::DEDUPE_TTL) : null; | ||
| $manifest = $isSite ? $cache->load(self::key('manifest', $project, $deploymentId), self::DEDUPE_TTL) : null; | ||
| if ($isSite && $manifest === false) { | ||
| return $deployment; | ||
| } |
There was a problem hiding this comment.
🟡 bug · medium
Renamed cache markers strand builds across rolling deploys
This PR re-keys the join markers (jobs-exit-, jobs-complete-, jobs-output-, jobs-manifest-) from jobs-<marker>-<id> to jobs-<marker>-<sequence>-<id> via self::key() (src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php:601-612). During a rolling deploy, an old-version pod writes terminal markers under the legacy unscoped names while a new-version pod's ready() join reads only the project-scoped names, so ready() never sees both markers and the deployment stays building. Because the dedupe key (jobs-event-) was renamed too, the old pod marks the CloudEvent id under a key the new pod does not honor, so at-least-once redelivery does not reliably rescue the join. The deployment remains stuck until a user cancels or redeploys.
📋 Prompt for AI Agents
In src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php, make self::key() callers (the marker saves at lines 364, 386, 485, 512 and the ready() loads at lines 601-609) tolerant of mixed versions for one release: when a versioned key (jobs-{marker}-{sequence}-{id}) is missing on load, fall back to the legacy key jobs-{marker}-{id}, and on save write both keys. Apply the same dual-write/dual-read to the jobs-event- dedupe check at line 166. Remove the fallbacks in the next release. This prevents in-flight builds from being stranded in building during a rolling deploy.
There was a problem hiding this comment.
Leaving out the dual-read/dual-write on purpose. It would only help builds whose callbacks arrive during a rollout, with some handled by old pods and some by new ones. Those builds can stay in building until they are redeployed or canceled. The PR body already says so. Self-hosted upgrades recreate all containers at once, so a build there is interrupted either way. A fallback for one release would be temporary legacy code in the join path, and I would rather not ship it for a window that short. If maintainers prefer the fallback for Cloud rollouts, I can add it.
There was a problem hiding this comment.
That's a reasonable engineering trade-off, and the reasoning is clear. The risk is real but bounded: only in-flight builds whose callbacks are split across old and new pods during a rolling deploy window would be affected, and those can be recovered by redeploying. If the team is comfortable accepting that narrow breakage window for Cloud rollouts, the finding can stay as a known limitation rather than a code change. I'll leave this open since no code fix has been applied — feel free to close it manually if maintainers accept the risk.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
|
@hansi-codes review |
Ported the preview rule ownership check and the wider, separated branch domain hash (existing previews keep their legacy domain). The jobs worker cache key and lock changes target a worker 1.9.x does not have. The ownership tests live in the existing BranchDomainTest because the rule activation helper does not exist on 1.9.x. (cherry picked from commit 9fc6d27)
Ported the preview rule ownership check and the wider, separated branch domain hash (existing previews keep their legacy domain). The jobs worker cache key and lock changes target a worker 1.9.x does not have. The ownership tests live in the existing BranchDomainTest because the rule activation helper does not exist on 1.9.x. (cherry picked from commit 9fc6d27)
Ported the preview rule ownership check and the wider, separated branch domain hash (existing previews keep their legacy domain). The jobs worker cache key and lock changes target a worker 1.9.x does not have. The ownership tests live in the existing BranchDomainTest because the rule activation helper does not exist on 1.9.x. (cherry picked from commit 9fc6d27)
Ported the preview rule ownership check and the wider, separated branch domain hash (existing previews keep their legacy domain). The jobs worker cache key and lock changes target a worker 1.9.x does not have. The ownership tests live in the existing BranchDomainTest because the rule activation helper does not exist on 1.9.x. (cherry picked from commit 9fc6d27)
…rship fix(sites): bind branch preview rules and build state to their project (cherry picked from commit 9fc6d27)
What
Two hardening changes around site/function builds.
Branch preview rules
Compute\Base::activateBranchPreviewRule()updated the existing rule bymd5(domain)on a duplicate without checking who owns it. It now loads the rule and repoints it only whenprojectInternalId,deploymentResourceType = site,deploymentResourceInternalIdanddeploymentVcsProviderBranchall match; otherwise it logs and skips the preview rule (manual branch-pinned rules still follow the deployment as before).BranchDomainnow hashesprojectId . ':' . resourceIdinto 16 hex characters (was 7 characters of the unseparated concatenation). The domain label stays well under 63 characters.Backwards compatibility: changing the hash alone would give every existing branch a new preview URL on its next build and freeze the old URL at its last deployment. To avoid that, the new
Base::branchDomain()first computes the legacy domain ('legacy' => trueon the filter) and keeps it when a rule for that domain already belongs to the same project, site and branch. Branches with an existing preview keep their URL; new branches get the wider hash. All three places that mint the branch preview rule (redeployVcsSite, the VCS push trait, and the jobs worker activation) use the same resolution.Jobs worker build state
event,manifest,output,exit,complete) are nowjobs-<marker>-<projectInternalId>-<id>, and the per-deployment lock isDeployments::lock($projectInternalId, $deploymentId), used by the worker and both Functions/Sites deployment cancel endpoints so writers and readers stay consistent.Tests
tests/unit/Platform/Modules/Compute/BranchPreviewRuleTest.php: duplicate rule of the same branch is repointed; duplicate rule of another project, site, branch or a function is left alone; a branch with a legacy-format rule keeps its domain; a new branch gets the 16-character hash.tests/unit/Filter/BranchDomainTest.php: separator makessite1/23andsite12/3distinct; legacy output is pinned to the previous value.tests/unit/Deployment/DeploymentsTest.php: the lock key differs per project.An e2e test for the duplicate-rule check is not practical: it needs two projects whose site and project IDs produce the same branch domain plus a real VCS-triggered build, which the CI lanes cannot set up. The ownership logic is covered at the
activateBranchPreviewRule()level instead, and the existing Sites/VCS e2e lanes cover the unchanged happy path. I did not run the docker e2e stack locally; relying on CI lanes.Related: appwrite-labs/cloud#6673