-
Notifications
You must be signed in to change notification settings - Fork 13
Fix DCP controller shutdown ordering #155
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
Closed
+271
−24
Closed
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
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,135 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See LICENSE in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package bootstrap | ||
|
|
||
| import ( | ||
| "errors" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/microsoft/dcp/internal/notifications" | ||
| "github.com/microsoft/dcp/pkg/testutil" | ||
| ) | ||
|
|
||
| func TestShutdownHostServicesWaitsForGracefulControllerExit(t *testing.T) { | ||
| ctx, cancel := testutil.GetTestContext(t, 5*time.Second) | ||
| defer cancel() | ||
|
|
||
| shutdownErrors := make(chan error, 1) | ||
| releaseHost := make(chan struct{}) | ||
| disposed := make(chan struct{}) | ||
| var hostCanceled atomic.Bool | ||
|
|
||
| notificationSource := notifications.NotifySubscribersFunc(func(n notifications.Notification) error { | ||
| require.Equal(t, notifications.NotificationKindShutdownRequested, n.Kind()) | ||
| return nil | ||
| }) | ||
|
|
||
| go func() { | ||
| <-releaseHost | ||
| shutdownErrors <- nil | ||
| }() | ||
|
|
||
| result := make(chan error, 1) | ||
| go func() { | ||
| result <- shutdownHostServicesAndDisposeApiServer( | ||
| notificationSource, | ||
| shutdownErrors, | ||
| func() { hostCanceled.Store(true) }, | ||
| func() error { | ||
| close(disposed) | ||
| return nil | ||
| }, | ||
| time.Second, | ||
| testutil.NewLogForTesting(t.Name()), | ||
| ) | ||
| }() | ||
|
|
||
| select { | ||
| case <-disposed: | ||
| t.Fatal("API server was disposed before the controller host exited") | ||
| case <-time.After(100 * time.Millisecond): | ||
| } | ||
|
|
||
| close(releaseHost) | ||
|
|
||
| select { | ||
| case shutdownErr := <-result: | ||
| require.NoError(t, shutdownErr) | ||
| case <-ctx.Done(): | ||
| t.Fatal("Timed out waiting for shutdown to complete") | ||
| } | ||
|
|
||
| require.False(t, hostCanceled.Load(), "host context should not be canceled after graceful controller exit") | ||
| } | ||
|
|
||
| func TestShutdownHostServicesCancelsHostAfterControllerShutdownTimeout(t *testing.T) { | ||
| ctx, cancel := testutil.GetTestContext(t, 5*time.Second) | ||
| defer cancel() | ||
|
|
||
| shutdownErrors := make(chan error, 1) | ||
| disposed := make(chan struct{}) | ||
| var hostCanceled atomic.Bool | ||
|
|
||
| notificationSource := notifications.NotifySubscribersFunc(func(n notifications.Notification) error { | ||
| require.Equal(t, notifications.NotificationKindShutdownRequested, n.Kind()) | ||
| return nil | ||
| }) | ||
|
|
||
| shutdownErr := shutdownHostServicesAndDisposeApiServer( | ||
| notificationSource, | ||
| shutdownErrors, | ||
| func() { | ||
| hostCanceled.Store(true) | ||
| shutdownErrors <- nil | ||
| }, | ||
| func() error { | ||
| close(disposed) | ||
| return nil | ||
| }, | ||
| 10*time.Millisecond, | ||
| testutil.NewLogForTesting(t.Name()), | ||
| ) | ||
|
|
||
| require.Error(t, shutdownErr) | ||
| require.Contains(t, shutdownErr.Error(), "controller host did not shut down") | ||
| require.True(t, hostCanceled.Load(), "host context should be canceled after controller shutdown timeout") | ||
|
|
||
| select { | ||
| case <-disposed: | ||
| case <-ctx.Done(): | ||
| t.Fatal("Timed out waiting for API server disposal") | ||
| } | ||
| } | ||
|
|
||
| func TestShutdownHostServicesDisposesApiServerWhenHostShutdownFails(t *testing.T) { | ||
| expectedErr := errors.New("host shutdown failed") | ||
| shutdownErrors := make(chan error, 1) | ||
| shutdownErrors <- expectedErr | ||
| disposed := make(chan struct{}) | ||
|
|
||
| shutdownErr := shutdownHostServicesAndDisposeApiServer( | ||
| nil, | ||
| shutdownErrors, | ||
| func() {}, | ||
| func() error { | ||
| close(disposed) | ||
| return nil | ||
| }, | ||
| time.Second, | ||
| testutil.NewLogForTesting(t.Name()), | ||
| ) | ||
|
|
||
| require.ErrorIs(t, shutdownErr, expectedErr) | ||
| select { | ||
| case <-disposed: | ||
| default: | ||
| t.Fatal("API server was not disposed") | ||
| } | ||
| } |
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,37 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See LICENSE in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| package commands | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/microsoft/dcp/internal/notifications" | ||
| "github.com/microsoft/dcp/pkg/testutil" | ||
| ) | ||
|
|
||
| func TestShutdownRequestedNotificationCancelsControllerContext(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| handleNotification( | ||
| ctx, | ||
| cancel, | ||
| ¬ifications.ShutdownRequestedNotification{}, | ||
| testutil.NewLogForTesting(t.Name()), | ||
| ) | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| case <-time.After(time.Second): | ||
| t.Fatal("Timed out waiting for controller context cancellation") | ||
| } | ||
|
|
||
| require.ErrorIs(t, ctx.Err(), context.Canceled) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 71b32e6 by guarding the shutdownHost closure with sync.Once. Even if a future path calls it more than once, it will only read shutdownErrors and dispose API server resources once.