-
Notifications
You must be signed in to change notification settings - Fork 301
feat(sandbox): pre-pause guest reclaim via envd #2551
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9c42f62
feat(sandbox): pre-pause guest reclaim via envd
ValentaTomas c041b7d
fix(sandbox): forward access token on reclaim envd call
ValentaTomas 2a02232
feat(sandbox): per-step timeouts for pre-pause reclaim chain
ValentaTomas 8698667
fix(sandbox): correct reclaim chain — drop master flag, fix timeout s…
ValentaTomas 13ca893
refactor(sandbox): extract StartEnvdProcess helper; reuse from resume…
ValentaTomas db30f55
fix(sandbox): add missing envd_process.go helper
ValentaTomas e4a31de
fix(sandbox): silence reclaim step output and surface failures
ValentaTomas 40a1e24
refactor(sandbox): inline envd reclaim call; drop --foreground
ValentaTomas 294c7e4
Merge branch 'main' into feat/sandbox-pause-reclaim
ValentaTomas a7e6b75
chore(featureflags): isolate reclaim flags in own var block
ValentaTomas ec3db39
refactor(sandbox): extract StartEnvdBash; reuse from reclaim + resume…
ValentaTomas 801a37f
refactor(reclaim): use DurationFlag and run via /bin/sh
ValentaTomas d03b564
fix(reclaim): skip sub-ms step durations to avoid no-timeout
ValentaTomas 2760098
fix(sandbox): parameterize StartEnvdShell binary; keep bash for resum…
ValentaTomas c3f843b
refactor(reclaim): collapse 4 flags into one targeted JSON flag
ValentaTomas eecb55b
chore(reclaim): drop NewOfflineClient, trim docstrings
ValentaTomas e23ee08
chore(reclaim): use int ms directly, drop string parsing
ValentaTomas 76f3b04
chore(reclaim): rename LD flag to guest-pause-reclaim
ValentaTomas 1a742bf
perf(sandbox): reorder reclaim chain to fstrim → sync → drop_caches →…
ValentaTomas 1b451c2
fix(resume-build): restore 10-minute upper bound on runCommandInSandbox
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package sandbox | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "connectrpc.com/connect" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/shared/pkg/consts" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/grpc" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/grpc/envd/process" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/grpc/envd/process/processconnect" | ||
| ) | ||
|
|
||
| // StartEnvdShell opens a streaming Process.Start call against the sandbox's | ||
| // envd. timeout > 0 sets Connect-Timeout-Ms so envd kills the process at | ||
| // the deadline. Caller owns the returned stream. | ||
| func (s *Sandbox) StartEnvdShell( | ||
| ctx context.Context, | ||
| shell string, | ||
| shellArgs []string, | ||
| user string, | ||
| timeout time.Duration, | ||
| ) (*connect.ServerStreamForClient[process.StartResponse], error) { | ||
| addr := fmt.Sprintf("http://%s:%d", s.Slot.HostIPString(), consts.DefaultEnvdServerPort) | ||
| pc := processconnect.NewProcessClient(&http.Client{Transport: sandboxHttpClient.Transport}, addr) | ||
|
|
||
| req := connect.NewRequest(&process.StartRequest{ | ||
| Process: &process.ProcessConfig{Cmd: shell, Args: shellArgs}, | ||
| }) | ||
| if timeout > 0 { | ||
| req.Header().Set("Connect-Timeout-Ms", strconv.FormatInt(timeout.Milliseconds(), 10)) | ||
| } | ||
| if s.Config.Envd.AccessToken != nil { | ||
| req.Header().Set("X-Access-Token", *s.Config.Envd.AccessToken) | ||
| } | ||
| grpc.SetUserHeader(req.Header(), user) | ||
|
|
||
| return pc.Start(ctx, req) | ||
| } |
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,95 @@ | ||
| package sandbox | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/shared/pkg/featureflags" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/logger" | ||
| ) | ||
|
|
||
| // Slack covers shell start + envd round-trip overhead. | ||
| const reclaimOuterSlack = 500 * time.Millisecond | ||
|
|
||
| // Order: fstrim → sync → drop_caches → compact_memory. fstrim runs first so | ||
| // that the fs metadata it pulls into the page cache and the superblock dirties | ||
| // (last-trim timestamps) get flushed by sync and evicted by drop_caches in the | ||
| // same pass; compact_memory then consolidates the minimal RSS so the snapshot | ||
| // has long contiguous zero runs that compress well. Each step is disabled at | ||
| // sub-ms cap. Returns ("", 0) when every step is disabled. | ||
| func (s *Sandbox) buildReclaimScript(ctx context.Context) (string, time.Duration) { | ||
| cfg := featureflags.GetReclaimConfig(ctx, s.featureFlags, | ||
| featureflags.SandboxContext(s.Runtime.SandboxID), | ||
| featureflags.TeamContext(s.Runtime.TeamID), | ||
| featureflags.TemplateContext(s.Runtime.TemplateID), | ||
| ) | ||
|
|
||
| steps := []struct { | ||
| cap time.Duration | ||
| cmd string | ||
| }{ | ||
| {cfg.Fstrim, "fstrim -av"}, | ||
| {cfg.Sync, "sync"}, | ||
| {cfg.DropCaches, "echo 3 > /proc/sys/vm/drop_caches"}, | ||
| {cfg.CompactMemory, "echo 1 > /proc/sys/vm/compact_memory"}, | ||
| } | ||
|
|
||
| var ( | ||
| parts []string | ||
| sum time.Duration | ||
| ) | ||
| for _, st := range steps { | ||
| // %.3f at <1ms renders as 0.000 → GNU timeout reads as "no timeout". | ||
| if st.cap < time.Millisecond { | ||
| continue | ||
| } | ||
| parts = append(parts, fmt.Sprintf("timeout -s KILL %.3f sh -c %q >/dev/null 2>&1 || rc=$?", st.cap.Seconds(), st.cmd)) | ||
| sum += st.cap | ||
| } | ||
| if len(parts) == 0 { | ||
| return "", 0 | ||
| } | ||
|
|
||
| return "rc=0; " + strings.Join(parts, "; ") + "; exit $rc", sum + reclaimOuterSlack | ||
| } | ||
|
|
||
| // bestEffortReclaim runs the reclaim chain via envd before pause. | ||
| func (s *Sandbox) bestEffortReclaim(ctx context.Context) { | ||
| script, timeout := s.buildReclaimScript(ctx) | ||
| if script == "" { | ||
| return | ||
| } | ||
|
|
||
| ctx, span := tracer.Start(ctx, "envd-reclaim") | ||
| defer span.End() | ||
|
|
||
| rcCtx, cancel := context.WithTimeout(ctx, timeout) | ||
| defer cancel() | ||
|
|
||
| stream, err := s.StartEnvdShell(rcCtx, "/bin/sh", []string{"-c", script}, "root", timeout) | ||
| if err != nil { | ||
| logger.L().Warn(ctx, "envd reclaim failed", logger.WithSandboxID(s.Runtime.SandboxID), zap.Error(err)) | ||
|
|
||
| return | ||
| } | ||
| defer stream.Close() | ||
|
|
||
| var exitCode int32 | ||
| for stream.Receive() { | ||
| if end := stream.Msg().GetEvent().GetEnd(); end != nil { | ||
| exitCode = end.GetExitCode() | ||
| } | ||
| } | ||
| if err := stream.Err(); err != nil { | ||
| logger.L().Warn(ctx, "envd reclaim stream error", logger.WithSandboxID(s.Runtime.SandboxID), zap.Error(err)) | ||
|
|
||
| return | ||
| } | ||
| if exitCode != 0 { | ||
| logger.L().Warn(ctx, "envd reclaim non-zero exit", logger.WithSandboxID(s.Runtime.SandboxID), zap.Int32("exit_code", exitCode)) | ||
| } | ||
| } |
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
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.