-
Notifications
You must be signed in to change notification settings - Fork 359
[EN-1048] fix(api): heal sandbox missing from eviction index #3199
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
Open
jakubno
wants to merge
7
commits into
main
Choose a base branch
from
fix/heal-sandbox-missing-from-eviction-index
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.
+873
−26
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
727622d
fix(api): heal sandbox missing from eviction index
jakubno 3256394
chore: add metrics
jakubno 609845f
Merge branch 'main' into fix/heal-sandbox-missing-from-eviction-index
jakubno fb1bf45
chore: add todo to clean up migration artifacts
jakubno 08a5e44
fix(api): bound healer per-command work with SSCAN batches
jakubno 1572212
refactor(api): simplify parseExpirationMember with Split
jakubno 48134ad
Update main.go
jakubno 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
415 changes: 415 additions & 0 deletions
415
packages/api/internal/sandbox/storage/redis/expiration_index_test.go
Large diffs are not rendered by default.
Oops, something went wrong.
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,195 @@ | ||
| package redis | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/redis/go-redis/v9" | ||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/api/internal/sandbox/sandboxtypes" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/logger" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/utils" | ||
| ) | ||
|
|
||
| const ( | ||
| healInterval = 5 * time.Minute | ||
|
|
||
| // healGracePeriod skips recently started sandboxes: | ||
| // this prevents the healer from clearing in-flight Add/Remove | ||
| healGracePeriod = time.Minute | ||
|
|
||
| // healScanBatchSize bounds per-command work (SSCAN page, MGET keys, | ||
| // ZMSCORE members, ZADD members) so teams with many sandboxes can't | ||
| // produce huge single commands/replies that stall Redis or explode service memory | ||
| healScanBatchSize = 256 | ||
| ) | ||
|
|
||
| // startHealer restores "sandbox key exists => expiration index member exists". | ||
| // A sandbox missing from the global expiration ZSET is never seen by the evictor | ||
| // and would otherwise live forever. | ||
| // | ||
| // Runs on every API pod with jitter; ZADD NX makes concurrent passes idempotent and harmless. | ||
| func (s *Storage) startHealer(ctx context.Context) { | ||
| timer := time.NewTimer(jitterBackoff(healInterval)) | ||
| defer timer.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case <-timer.C: | ||
| healed, err := s.healExpirationIndex(ctx) | ||
| if err != nil { | ||
| logger.L().Warn(ctx, "Expiration index heal pass failed", zap.Error(err)) | ||
| } | ||
| if healed > 0 { | ||
| logger.L().Warn(ctx, "Healed sandboxes missing from expiration index", zap.Int("count", healed)) | ||
| } | ||
|
|
||
| timer.Reset(jitterBackoff(healInterval)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // healExpirationIndex scans all stored sandboxes and re-adds expiration index | ||
| // members missing for live sandbox keys. Returns the number of healed members. | ||
| func (s *Storage) healExpirationIndex(ctx context.Context) (int, error) { | ||
| teams, err := s.redisClient.ZRange(ctx, globalTeamsSet, 0, -1).Result() | ||
| if err != nil { | ||
| return 0, fmt.Errorf("failed to list teams from global index: %w", err) | ||
| } | ||
|
|
||
| healed := 0 | ||
| for _, teamID := range teams { | ||
| n, err := s.healTeamExpirationIndex(ctx, teamID) | ||
| if err != nil { | ||
| // Isolate per-team failures; other teams still get healed. | ||
| logger.L().Warn(ctx, "Failed to heal team expiration index", zap.Error(err), zap.String("team_id", teamID)) | ||
|
|
||
| continue | ||
| } | ||
|
|
||
| healed += n | ||
| } | ||
|
|
||
| return healed, nil | ||
| } | ||
|
|
||
| func (s *Storage) healTeamExpirationIndex(ctx context.Context, teamID string) (int, error) { | ||
| healed := 0 | ||
| var cursor uint64 | ||
|
|
||
| for { | ||
| sandboxIDs, next, err := s.redisClient.SScan(ctx, GetSandboxStorageTeamIndexKey(teamID), cursor, "", healScanBatchSize).Result() | ||
| if err != nil { | ||
| return healed, fmt.Errorf("failed to scan team index: %w", err) | ||
| } | ||
|
|
||
| // SSCAN COUNT is a hint, not a cap: split oversized pages so | ||
| // downstream commands stay bounded. | ||
| for start := 0; start < len(sandboxIDs); start += healScanBatchSize { | ||
| end := min(start+healScanBatchSize, len(sandboxIDs)) | ||
|
|
||
| n, err := s.healSandboxBatch(ctx, teamID, sandboxIDs[start:end]) | ||
| if err != nil { | ||
| return healed, err | ||
| } | ||
|
|
||
| healed += n | ||
| } | ||
|
|
||
| cursor = next | ||
| if cursor == 0 { | ||
| return healed, nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // healSandboxBatch re-adds missing expiration index members for one bounded | ||
| // batch of sandbox IDs. Returns the number of healed members. | ||
| func (s *Storage) healSandboxBatch(ctx context.Context, teamID string, sandboxIDs []string) (int, error) { | ||
| if len(sandboxIDs) == 0 { | ||
| return 0, nil | ||
| } | ||
|
|
||
| // Per-team MGET: all keys share the team hash tag (cluster slot safe). | ||
| keys := utils.Map(sandboxIDs, func(id string) string { return getSandboxKey(teamID, id) }) | ||
| vals, err := s.redisClient.MGet(ctx, keys...).Result() | ||
| if err != nil { | ||
| return 0, fmt.Errorf("MGET failed: %w", err) | ||
| } | ||
|
|
||
| now := time.Now() | ||
| type candidate struct { | ||
| member string | ||
| legacyMember string | ||
| score float64 | ||
| } | ||
| var candidates []candidate | ||
| for _, raw := range vals { | ||
| str, ok := raw.(string) | ||
| if !ok { | ||
| continue // stale team index entry; TeamItems tolerates these too | ||
| } | ||
|
|
||
| var sbx sandboxtypes.Sandbox | ||
| if err := json.Unmarshal([]byte(str), &sbx); err != nil { | ||
| continue | ||
| } | ||
|
|
||
| if now.Sub(sbx.StartTime) < healGracePeriod { | ||
| continue | ||
| } | ||
|
|
||
| candidates = append(candidates, candidate{ | ||
| member: sandboxExpirationMember(sbx), | ||
| legacyMember: legacyExpirationMember(teamID, sbx.SandboxID), | ||
| score: float64(sbx.EndTime.UnixMilli()), | ||
| }) | ||
| } | ||
| if len(candidates) == 0 { | ||
| return 0, nil | ||
| } | ||
|
|
||
| // A sandbox is indexed if either its execution-scoped member or the | ||
| // legacy member (written by old pods during rolling deploys) exists. | ||
| // ZMSCORE returns 0 for absent members; legitimate scores are unix | ||
| // milliseconds and can never be 0. | ||
| members := make([]string, 0, len(candidates)*2) | ||
| for _, c := range candidates { | ||
| members = append(members, c.member, c.legacyMember) | ||
| } | ||
|
|
||
| scores, err := s.redisClient.ZMScore(ctx, globalExpirationSet, members...).Result() | ||
| if err != nil { | ||
| return 0, fmt.Errorf("ZMSCORE failed: %w", err) | ||
| } | ||
|
|
||
| var missing []redis.Z | ||
| for i, c := range candidates { | ||
| if scores[2*i] != 0 || scores[2*i+1] != 0 { | ||
| continue | ||
| } | ||
|
|
||
| missing = append(missing, redis.Z{Score: c.score, Member: c.member}) | ||
| } | ||
| if len(missing) == 0 { | ||
| return 0, nil | ||
| } | ||
|
|
||
| // NX: only fill holes. A concurrent Add/Update owns the member's score. | ||
| // A TOCTOU with a concurrent Remove can only plant an orphan member, | ||
| // which ExpiredItems sweeps once its score passes — garbage, never a | ||
| // false eviction (eviction re-checks the stored JSON and re-validates | ||
| // expiry under the lock in StartRemoving). | ||
| if err := s.redisClient.ZAddNX(ctx, globalExpirationSet, missing...).Err(); err != nil { | ||
| return 0, fmt.Errorf("ZADD NX failed: %w", err) | ||
| } | ||
|
|
||
| s.metrics.indexHealed.Add(ctx, int64(len(missing))) | ||
|
|
||
| return len(missing), nil | ||
| } | ||
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
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.