Repository navigation
feat: add active cdn purge on file changes to support smart cdn v2 #1772
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { wasNoncurrentVersion } from './adapter' | ||
|
|
||
| describe('wasNoncurrentVersion', () => { | ||
| it('is true for a row archived before it was removed', () => { | ||
| expect(wasNoncurrentVersion({ archived_at: '2026-01-01T00:00:00Z' })).toBe(true) | ||
| }) | ||
|
|
||
| it('is false for a row that was current (archived_at null)', () => { | ||
| expect(wasNoncurrentVersion({ archived_at: null })).toBe(false) | ||
| }) | ||
|
|
||
| it('is false when archived_at was not selected on the row', () => { | ||
| expect(wasNoncurrentVersion({})).toBe(false) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,11 +24,78 @@ export class PurgeCdnCache extends storageEvent<PurgeCdnCachePayload>({ | |
| purgeOptions.tenant, | ||
| 'bucket' in purgeOptions ? purgeOptions.bucket : undefined, | ||
| 'objectName' in purgeOptions ? purgeOptions.objectName : undefined, | ||
| 'versionId' in purgeOptions ? purgeOptions.versionId : undefined, | ||
| ] | ||
| .filter(Boolean) | ||
| .join('/'), | ||
| }) {} | ||
|
|
||
| export interface PurgeableObject { | ||
| readonly tenant: { readonly ref: string; readonly host: string } | ||
| readonly bucketId: string | ||
| readonly name: string | ||
| readonly sbReqId?: string | ||
| readonly versionId?: string | ||
| } | ||
|
|
||
| const toPurgeOptions = (object: PurgeableObject): PurgeCdnCachePayload => ({ | ||
| tenant: object.tenant, | ||
| sbReqId: object.sbReqId, | ||
| purgeOptions: object.versionId | ||
| ? { | ||
| type: 'object-version', | ||
| tenant: object.tenant.ref, | ||
| bucket: object.bucketId, | ||
| objectName: object.name, | ||
| versionId: object.versionId, | ||
| } | ||
| : { | ||
| type: 'object', | ||
| tenant: object.tenant.ref, | ||
| bucket: object.bucketId, | ||
| objectName: object.name, | ||
| }, | ||
| }) | ||
|
|
||
| /** | ||
| * Purges one object's CDN cache: for Smart CDN V2 tenants this is the only invalidation | ||
| * signal there is, since V2 removes the V1 edge KV and purges the cache directly. | ||
| * Best-effort: failures are logged, never thrown, so a purge failure can't fail the write | ||
| * that triggered it. | ||
| */ | ||
| export async function purgeObjectCache(object: PurgeableObject): Promise<void> { | ||
| try { | ||
| await PurgeCdnCache.send(toPurgeOptions(object)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⚪ Severity: LOW The purge queue is 💡 Fix SuggestionSuggestion: The |
||
| } catch (error) { | ||
| logSchema.error(logger, 'Failed to purge object cache', { | ||
| type: 'cdn', | ||
| project: object.tenant.ref, | ||
| sbReqId: object.sbReqId, | ||
| error, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| export async function purgeObjectCacheBatch( | ||
| objects: readonly PurgeableObject[], | ||
| options?: { readonly signal?: AbortSignal } | ||
| ): Promise<void> { | ||
| if (objects.length === 0) return | ||
| try { | ||
| const payloads = objects.map(toPurgeOptions) | ||
| await PurgeCdnCache.batchSend(payloads, options) | ||
| } catch (error) { | ||
| for (const object of objects) { | ||
| logSchema.error(logger, 'Failed to purge object cache', { | ||
|
Comment on lines
+66
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 (optional) Deployments with no CDN purge endpoint now get a queue job for every delete, overwrite, move and promotion. Each job does nothing when it runs. Why this was flaggedThe trigger is any deployment without Verification: Trigger: a deployment with the queue enabled and no |
||
| type: 'cdn', | ||
| project: object.tenant.ref, | ||
| sbReqId: object.sbReqId, | ||
| error, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export class PurgeCdnCacheHandler extends QueueHandler(PurgeCdnCache) { | ||
| override readonly options: SubscribeOptions = { | ||
| prefetch: pgQueueConcurrentTasksPerQueue * 2, | ||
|
|
||
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.
🔴 Smart CDN V2 tenants can keep serving stale bytes when an object changes again while an earlier purge for the same key is still in flight. The purgeCdnCache queue uses pg-boss policy
exclusive(registry.ts:238), keyed by type/tenant/bucket/objectName. That policy silently drops a new job while one with the same key is queued or active. Fix: keep each per-object purge sent after a commit from being dropped by a purge already in flight, e.g. use a non-dropping policy for this queue (or a key that is unique per write) and accept duplicate purges as cheap.Why this was flagged
Overwrite write1 commits and
notifyenqueues purge P1 with the keyobject/<tenant>/<bucket>/<name>. While P1 is active incdnCacheManager.purge(up to the 10s abort budget), a second upsert or a delete of the same object commits and sends P2. pg-bossexclusive(registry.ts:238, options.ts:14-15) skips P2 silently because P1 holds the key. If P1 reaches the edge before write2 commits, a read in that gap caches write1's bytes, and no later purge ever clears them. The new comment at purge-cdn-cache.ts:51-52 says V2 has no other invalidation signal. On the base branch, object writes did not depend on this queue: V1 invalidation went through webhooks, which do not have this policy. The idempotency key was built for v1 manual or bucket purges, where dropping a duplicate was harmless.Verification: run-migrations.ts:194 confirms that "v12's
exclusiveunique index spans created..active". So an enqueue is dropped while the earlier job is active, not only while it is queued. This change routes per-object writes into that queue. Before it, the only producer was the manual admin purge at storage.ts:325. V2 tenants then serve stale content until the edge TTL runs out.