-
Notifications
You must be signed in to change notification settings - Fork 328
Add Rapid bucket template cache #2876
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
Draft
ValentaTomas
wants to merge
13
commits into
main
Choose a base branch
from
valenta/rapid-bucket-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
be410c7
feat(storage): add rapid bucket template cache
ValentaTomas 79f733c
fix(storage): harden rapid cache hits
ValentaTomas de1bc4d
fix(storage): keep rapid cache best effort
ValentaTomas 7418f81
fix(storage): add rapid cache cleanup job
ValentaTomas f379ea3
fix(storage): ignore rapid cleaner close error
ValentaTomas ce60164
fix(storage): format rapid cleaner loop
ValentaTomas 7d161c4
fix(storage): track rapid cache chunks in redis
ValentaTomas 585d7e8
chore: auto-commit generated changes
github-actions[bot] 3e5ade6
fix(storage): satisfy rapid cleaner lint
ValentaTomas eec262b
fix(storage): guard rapid cleanup with redis recency
ValentaTomas 5749400
fix(storage): gofumpt rapid cache index
ValentaTomas 1043f73
fix(storage): tighten rapid cleanup errors
ValentaTomas b61e373
fix(storage): keep rapid cleanup fallback running
ValentaTomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| job "rapid-cache-cleanup" { | ||
| type = "batch" | ||
| node_pool = "${node_pool}" | ||
|
|
||
| periodic { | ||
| cron = "0 * * * *" | ||
| prohibit_overlap = true | ||
| time_zone = "America/Los_Angeles" | ||
| } | ||
|
|
||
| group "rapid-cache-cleanup" { | ||
| restart { | ||
| attempts = 0 | ||
| mode = "fail" | ||
| } | ||
|
|
||
| task "rapid-cache-cleanup" { | ||
| driver = "raw_exec" | ||
|
|
||
| resources { | ||
| memory = 512 | ||
| } | ||
|
|
||
| env { | ||
| RAPID_BUCKET_CACHE_BUCKET_NAME = "${bucket_name}" | ||
| REDIS_URL = "${redis_url}" | ||
| REDIS_CLUSTER_URL = "${redis_cluster_url}" | ||
| REDIS_TLS_CA_BASE64 = "${redis_tls_ca_base64}" | ||
| } | ||
|
|
||
| config { | ||
| command = "local/clean-rapid-cache" | ||
| args = [ | ||
| "--dry-run=${dry_run}", | ||
| "--max-age=${max_age}", | ||
| "--max-deletions=${max_deletions}", | ||
| "${bucket_name}", | ||
| ] | ||
| } | ||
|
|
||
| artifact { | ||
| source = "${artifact_source}" | ||
| destination = "local/clean-rapid-cache" | ||
| mode = "file" | ||
| } | ||
| } | ||
| } | ||
| } |
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "time" | ||
|
|
||
| gcs "cloud.google.com/go/storage" | ||
| "cloud.google.com/go/storage/experimental" | ||
| "google.golang.org/api/iterator" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/shared/pkg/factories" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/storage" | ||
| ) | ||
|
|
||
| const defaultPrefix = "rapid-cache/" | ||
|
|
||
| func main() { | ||
| var ( | ||
| prefix string | ||
| maxAge time.Duration | ||
| maxDeletions int | ||
| dryRun bool | ||
| ) | ||
|
|
||
| flags := flag.NewFlagSet("clean-rapid-cache", flag.ExitOnError) | ||
| flags.StringVar(&prefix, "prefix", defaultPrefix, "cache object prefix") | ||
| flags.DurationVar(&maxAge, "max-age", 7*24*time.Hour, "delete objects older than this") | ||
| flags.IntVar(&maxDeletions, "max-deletions", 10000, "maximum objects to delete") | ||
| flags.BoolVar(&dryRun, "dry-run", true, "dry run") | ||
| if err := flags.Parse(os.Args[1:]); err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| bucket := os.Getenv("RAPID_BUCKET_CACHE_BUCKET_NAME") | ||
| if flags.NArg() > 0 { | ||
| bucket = flags.Arg(0) | ||
| } | ||
| if bucket == "" { | ||
| log.Fatal("missing bucket") | ||
| } | ||
| if prefix == "" { | ||
| log.Fatal("missing prefix") | ||
| } | ||
| if maxAge <= 0 { | ||
| log.Fatal("max-age must be positive") | ||
| } | ||
| if maxDeletions <= 0 { | ||
| log.Fatal("max-deletions must be positive") | ||
| } | ||
|
|
||
| ctx := context.Background() | ||
| index, closeIndex := newRapidIndex(ctx, bucket) | ||
| err := clean(ctx, bucket, prefix, time.Now().Add(-maxAge), maxDeletions, dryRun, index) | ||
| closeIndex() | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| func clean(ctx context.Context, bucket string, prefix string, cutoff time.Time, maxDeletions int, dryRun bool, index storage.RapidCacheIndex) error { | ||
| client, err := gcs.NewGRPCClient(ctx, experimental.WithZonalBucketAPIs()) | ||
| if err != nil { | ||
| return fmt.Errorf("create storage client: %w", err) | ||
| } | ||
| defer func() { | ||
| _ = client.Close() | ||
| }() | ||
|
|
||
| deleted := cleanFromIndex(ctx, client, bucket, cutoff, maxDeletions, dryRun, index) | ||
|
|
||
| return cleanFromBucket(ctx, client, bucket, prefix, cutoff, maxDeletions-deleted, dryRun, index) | ||
| } | ||
|
|
||
| func cleanFromIndex(ctx context.Context, client *gcs.Client, bucket string, cutoff time.Time, maxDeletions int, dryRun bool, index storage.RapidCacheIndex) int { | ||
| candidates, err := index.Candidates(ctx, cutoff, int64(maxDeletions)) | ||
| if err != nil || len(candidates) == 0 { | ||
| return 0 | ||
| } | ||
|
|
||
| deleted := 0 | ||
| for _, path := range candidates { | ||
| lastAccess, ok, err := index.LastAccess(ctx, path) | ||
| if err != nil || (ok && lastAccess >= cutoff.Unix()) { | ||
| continue | ||
| } | ||
| obj := client.Bucket(bucket).Object(path) | ||
| attrs, err := obj.Attrs(ctx) | ||
| if errors.Is(err, gcs.ErrObjectNotExist) { | ||
| if !dryRun { | ||
| _ = index.Evict(ctx, path, 0) | ||
| } | ||
|
|
||
| continue | ||
| } | ||
| if err != nil { | ||
| continue | ||
| } | ||
| if dryRun { | ||
| deleted++ | ||
|
|
||
| continue | ||
| } | ||
| if err := obj.Delete(ctx); err != nil { | ||
| continue | ||
| } | ||
| _ = index.Evict(ctx, path, attrs.Size) | ||
| deleted++ | ||
| } | ||
|
|
||
| return deleted | ||
| } | ||
|
|
||
| func cleanFromBucket(ctx context.Context, client *gcs.Client, bucket string, prefix string, cutoff time.Time, maxDeletions int, dryRun bool, index storage.RapidCacheIndex) error { | ||
| if maxDeletions <= 0 { | ||
| return nil | ||
| } | ||
|
|
||
| var scanned, matched, deleted int | ||
| objects := client.Bucket(bucket).Objects(ctx, &gcs.Query{Prefix: prefix}) | ||
| for { | ||
| attrs, err := objects.Next() | ||
| if errors.Is(err, iterator.Done) { | ||
| break | ||
| } | ||
| if err != nil { | ||
| return fmt.Errorf("list cache objects: %w", err) | ||
| } | ||
|
|
||
| scanned++ | ||
| if !attrs.Updated.Before(cutoff) { | ||
| continue | ||
| } | ||
| lastAccess, ok, err := index.LastAccess(ctx, attrs.Name) | ||
| if err == nil && ok && lastAccess >= cutoff.Unix() { | ||
| continue | ||
|
ValentaTomas marked this conversation as resolved.
|
||
| } | ||
| matched++ | ||
| if deleted >= maxDeletions { | ||
| break | ||
| } | ||
| if dryRun { | ||
| deleted++ | ||
|
|
||
| continue | ||
| } | ||
| if err := client.Bucket(bucket).Object(attrs.Name).Delete(ctx); err != nil { | ||
| return fmt.Errorf("delete cache object: %w", err) | ||
| } | ||
| _ = index.Evict(ctx, attrs.Name, attrs.Size) | ||
| deleted++ | ||
| } | ||
|
ValentaTomas marked this conversation as resolved.
|
||
|
|
||
| log.Printf("summary dry_run=%t scanned=%d matched=%d deleted=%d", dryRun, scanned, matched, deleted) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func newRapidIndex(ctx context.Context, bucket string) (storage.RapidCacheIndex, func()) { | ||
| redisClient, err := factories.NewRedisClient(ctx, factories.RedisConfig{ | ||
| RedisURL: os.Getenv("REDIS_URL"), | ||
| RedisClusterURL: os.Getenv("REDIS_CLUSTER_URL"), | ||
| RedisTLSCABase64: os.Getenv("REDIS_TLS_CA_BASE64"), | ||
| }) | ||
| if err != nil { | ||
| return storage.NoopRapidCacheIndex(), func() {} | ||
| } | ||
|
|
||
| return storage.NewRedisRapidCacheIndex(redisClient, bucket), func() { | ||
| _ = factories.CloseCleanly(redisClient) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.