Conditionally refresh cache TTL on restore (A-1454)#4038
Draft
claude[bot] wants to merge 1 commit into
Draft
Conversation
bdb5d84 to
dea1333
Compare
The S3 cache restore path issued a synchronous full-object self-CopyObject on every restore to reset LastModified so the bucket lifecycle rule wouldn't expire the object. That's a ~27s blocking server-side rewrite for a 2GB cache and hard-errors above S3's 5GB CopyObject limit. Now the self-copy only runs when the blob is near expiry: thread expires_at from the retrieve response into Blob.Download and skip the copy when there's more than restoreTTLRefreshThreshold of life left. Any copy failure (including >5GB) is logged as a warning and no longer fails the restore. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HT7Sg6wJVRSf9nfstxGWG1
dea1333 to
8d881e6
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Requested by Ming Guo · Slack thread
Description
The S3 cache restore path refreshes a cache object's lifecycle expiration by copying the object to itself (source == dest,
MetadataDirective: REPLACE), which resets itsLastModifiedso the bucket lifecycle rule doesn't expire it.Before: that self-copy ran on every restore. It's a synchronous full-object server-side rewrite — about 27s of blocking work for a 2GB cache — and it hard-errors on objects above S3's 5GB CopyObject limit, failing the whole restore (and the pipeline).
After: the self-copy runs only when the cached object has 20% or less of its lifecycle window left. A hot cache that was recently refreshed (most of its window remaining) skips the copy entirely, so most restores no longer block on it. And if the copy does run and fails — including the >5GB case — the restore continues normally; the failure is logged as a warning instead of aborting.
How: the lifecycle window is derived per object as
expires_at - LastModified, whereexpires_atcomes from the retrieve response (threaded throughBlob.Downloadinto the S3 store) andLastModifiedis read with one extraHeadObjectper restore — a cheap metadata call, unlike the full-object copy it gates. A small pure function,shouldRefreshExpiration(expiresAt, lastModified, now, refreshFraction), decides whether to refresh: it returns true when the remaining lifetime (expires_at - now) is at or belowrefreshFractionof the window, and also whenexpires_atorLastModifiedis unknown (zero) or the window is non-positive — preserving the old always-refresh behaviour when the fraction can't be computed. When it returns false the copy is skipped; when it returns true the copy runs but soft-fails on error.restoreTTLRefreshFractionis0.20, so the refresh fires only in the last 20% of each object's lifecycle window. Because the window is computed from the object's ownLastModifiedandexpires_at, the threshold self-tunes to the bucket's actual lifecycle with no hard-coded duration.Context
Linear ticket: A-1454. The per-restore CopyObject was identified as the largest single cost in the cache-restore investigation.
Changes
internal/cache/store/blob.go: addexpiresAt time.Timeto theBlob.Downloadinterface.internal/cache/store/s3.go: add therestoreTTLRefreshFractionconst and the pureshouldRefreshExpirationfunction; inDownload, read the object'sLastModifiedviaHeadObject, skip the self-copy when more than 20% of the lifecycle window remains, and soft-fail (log a warning, continue) when the copy errors.internal/cache/store/file.go,internal/cache/store/nsc.go: accept and ignore the newexpiresAtparameter.internal/cache/restore.go: passretrieveResp.ExpiresAtinto theDownloadcall.internal/cache/store/s3_test.go: table-driven unit tests forshouldRefreshExpiration.file_test.go/nsc_test.gocall sites for the new signature.Testing
go test ./internal/cache/... ./api/...pass;go build ./...andgo vet ./internal/cache/...clean).Disclosures / Credits
Implemented with Claude Code.
Generated by Claude Code