-
Notifications
You must be signed in to change notification settings - Fork 461
feat(orchestrator): graceful template-build drain on shutdown #3079
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| //go:build linux | ||
|
|
||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/shared/pkg/logger" | ||
| ) | ||
|
|
||
| func TestWaitReturnsErrorWhileBuildsInFlight(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| s := &ServerStore{ | ||
| logger: logger.NewNopLogger(), | ||
| wg: &sync.WaitGroup{}, | ||
| } | ||
| s.wg.Add(1) | ||
| defer s.wg.Done() | ||
|
|
||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| cancel() | ||
|
|
||
| require.ErrorIs(t, s.Wait(ctx), context.Canceled) | ||
| } | ||
|
|
||
| func TestWaitStopsAtContextDeadlineDuringGracePeriod(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| s := &ServerStore{ | ||
| logger: logger.NewNopLogger(), | ||
| wg: &sync.WaitGroup{}, | ||
| } | ||
|
|
||
| // No builds in flight, so the wait passes straight to the consumer grace | ||
| // period, which must be bounded by ctx rather than the full sleep. | ||
| ctx, cancel := context.WithTimeout(t.Context(), 50*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| require.ErrorIs(t, s.Wait(ctx), context.DeadlineExceeded) | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -27,12 +27,18 @@ import ( | |
| templatemanager "github.com/e2b-dev/infra/packages/shared/pkg/grpc/template-manager" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/logger" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/storage" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/utils" | ||
| ) | ||
|
|
||
| type closeable interface { | ||
| Close() error | ||
| } | ||
|
|
||
| // consumerStatusCheckGracePeriod is how long the graceful drain waits, after | ||
| // in-flight builds finish, for consumers to read the final build status before | ||
| // the server shuts down. | ||
| const consumerStatusCheckGracePeriod = 15 * time.Second | ||
|
Contributor
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. can we pass it via args or envs?
Contributor
Author
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. None of the existing code hoists this to CLI env vars today. We can change it if it ever needs to be tweaked. |
||
|
|
||
| type ServerStore struct { | ||
| templatemanager.UnimplementedTemplateServiceServer | ||
|
|
||
|
|
@@ -149,21 +155,26 @@ func (s *ServerStore) Close(ctx context.Context) error { | |
| } | ||
| } | ||
|
|
||
| // Wait gracefully drains in-flight template builds during shutdown. It waits | ||
| // for running builds to finish, bounded by ctx, then gives consumers a grace | ||
| // period to read the final build status. It returns ctx.Err() if ctx is | ||
| // cancelled before the drain completes. | ||
| func (s *ServerStore) Wait(ctx context.Context) error { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return errors.New("force exit, not waiting for builds to finish") | ||
| default: | ||
| s.logger.Info(ctx, "Waiting for all build jobs to finish", zap.Int64("active_builds", s.activeBuilds.Load())) | ||
| s.wg.Wait() | ||
| s.logger.Info(ctx, "Waiting for all build jobs to finish", zap.Int64("active_builds", s.activeBuilds.Load())) | ||
| if err := utils.WaitGroupWait(ctx, s.wg); err != nil { | ||
| return fmt.Errorf("waiting for template builds: %w", err) | ||
| } | ||
|
|
||
| if !env.IsLocal() { | ||
| s.logger.Info(ctx, "Waiting for consumers to check build status") | ||
| time.Sleep(15 * time.Second) | ||
| if !env.IsLocal() { | ||
| s.logger.Info(ctx, "Waiting for consumers to check build status") | ||
| select { | ||
| case <-time.After(consumerStatusCheckGracePeriod): | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| } | ||
| } | ||
|
|
||
| s.logger.Info(ctx, "Template build queue cleaned") | ||
| s.logger.Info(ctx, "Template build queue cleaned") | ||
|
|
||
| return nil | ||
| } | ||
| return nil | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
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,36 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| ) | ||
|
|
||
| func WaitGroupWait(ctx context.Context, wg *sync.WaitGroup) error { | ||
| done := make(chan struct{}) | ||
| go func() { | ||
| wg.Wait() | ||
| close(done) | ||
| }() | ||
|
|
||
| return waitDoneOrContext(ctx, done) | ||
| } | ||
|
|
||
| // waitDoneOrContext blocks until done is closed or ctx is cancelled. | ||
| // | ||
| // When both are ready, select chooses pseudo-randomly, so a re-check biases the | ||
| // result toward done: a closed done channel means the wait group has actually | ||
| // finished, which is a definitive success and must win over the context error. | ||
| func waitDoneOrContext(ctx context.Context, done <-chan struct{}) error { | ||
| select { | ||
| case <-done: | ||
| return nil | ||
|
wj-e2b marked this conversation as resolved.
|
||
| case <-ctx.Done(): | ||
| select { | ||
| case <-done: | ||
| return nil | ||
| default: | ||
| return fmt.Errorf("waiting for wait group: %w", ctx.Err()) | ||
| } | ||
| } | ||
| } | ||
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,89 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestWaitGroupWaitAlreadyDone(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var wg sync.WaitGroup | ||
|
|
||
| require.NoError(t, WaitGroupWait(t.Context(), &wg)) | ||
| } | ||
|
|
||
| func TestWaitGroupWaitCompletesLater(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(1) | ||
|
|
||
| release := make(chan struct{}) | ||
| done := make(chan error, 1) | ||
| go func() { | ||
| <-release | ||
| wg.Done() | ||
| }() | ||
| go func() { | ||
| done <- WaitGroupWait(t.Context(), &wg) | ||
| }() | ||
|
|
||
| select { | ||
| case err := <-done: | ||
| require.Failf(t, "WaitGroupWait returned before wait group completed", "err: %v", err) | ||
| case <-time.After(10 * time.Millisecond): | ||
| } | ||
|
|
||
| close(release) | ||
|
|
||
| select { | ||
| case err := <-done: | ||
| require.NoError(t, err) | ||
| case <-time.After(time.Second): | ||
| t.Fatal("WaitGroupWait did not return after wait group completed") | ||
| } | ||
| } | ||
|
|
||
| func TestWaitGroupWaitReturnsContextErrorWhileWaiting(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(1) | ||
| defer wg.Done() | ||
|
|
||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| cancel() | ||
|
|
||
| require.ErrorIs(t, WaitGroupWait(ctx, &wg), context.Canceled) | ||
| } | ||
|
|
||
| func TestWaitDoneOrContextPrefersDoneWhenBothReady(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| cancel() | ||
|
|
||
| done := make(chan struct{}) | ||
| close(done) | ||
|
|
||
| // Both done and ctx are ready; select would pick a branch pseudo-randomly. | ||
| // A closed done channel means the wait group finished, which must win over | ||
| // the context error, so loop enough times to surface a regression. | ||
| for range 1000 { | ||
| require.NoError(t, waitDoneOrContext(ctx, done)) | ||
| } | ||
| } | ||
|
|
||
| func TestWaitDoneOrContextReturnsContextErrorWhenNotDone(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| cancel() | ||
|
|
||
| require.ErrorIs(t, waitDoneOrContext(ctx, make(chan struct{})), context.Canceled) | ||
| } |
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.