-
Notifications
You must be signed in to change notification settings - Fork 721
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
Move Shutdown lock from Handler into Engines #2179
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"errors" | ||
"fmt" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
|
@@ -116,7 +117,7 @@ type handler struct { | |
startClosingTime time.Time | ||
totalClosingTime time.Duration | ||
closingChan chan struct{} | ||
numDispatchersClosed int | ||
numDispatchersClosed atomic.Uint32 | ||
// Closed when this handler and [engine] are done shutting down | ||
closed chan struct{} | ||
|
||
|
@@ -220,22 +221,24 @@ func (h *handler) selectStartingGear(ctx context.Context) (common.Engine, error) | |
|
||
func (h *handler) Start(ctx context.Context, recoverPanic bool) { | ||
h.ctx.Lock.Lock() | ||
defer h.ctx.Lock.Unlock() | ||
|
||
gear, err := h.selectStartingGear(ctx) | ||
if err != nil { | ||
h.ctx.Lock.Unlock() | ||
|
||
h.ctx.Log.Error("chain failed to select starting gear", | ||
zap.Error(err), | ||
) | ||
h.shutdown(ctx) | ||
h.shutdown(ctx, h.clock.Time()) | ||
return | ||
} | ||
|
||
if err := gear.Start(ctx, 0); err != nil { | ||
err = gear.Start(ctx, 0) | ||
h.ctx.Lock.Unlock() | ||
if err != nil { | ||
h.ctx.Log.Error("chain failed to start", | ||
zap.Error(err), | ||
) | ||
h.shutdown(ctx) | ||
h.shutdown(ctx, h.clock.Time()) | ||
return | ||
} | ||
|
||
|
@@ -326,7 +329,7 @@ func (h *handler) Stop(ctx context.Context) { | |
state := h.ctx.State.Get() | ||
bootstrapper, ok := h.engineManager.Get(state.Type).Get(snow.Bootstrapping) | ||
if !ok { | ||
h.ctx.Log.Error("bootstrapping engine doesn't exists", | ||
h.ctx.Log.Error("bootstrapping engine doesn't exist", | ||
zap.Stringer("type", state.Type), | ||
) | ||
return | ||
|
@@ -998,35 +1001,27 @@ func (h *handler) popUnexpiredMsg( | |
} | ||
} | ||
|
||
// Invariant: if closeDispatcher is called, Stop has already been called. | ||
func (h *handler) closeDispatcher(ctx context.Context) { | ||
h.ctx.Lock.Lock() | ||
defer h.ctx.Lock.Unlock() | ||
|
||
h.numDispatchersClosed++ | ||
if h.numDispatchersClosed < numDispatchersToClose { | ||
if h.numDispatchersClosed.Add(1) < numDispatchersToClose { | ||
return | ||
} | ||
|
||
h.shutdown(ctx) | ||
h.shutdown(ctx, h.startClosingTime) | ||
} | ||
|
||
// Note: shutdown is only called after all message dispatchers have exited. | ||
func (h *handler) shutdown(ctx context.Context) { | ||
// Note: shutdown is only called after all message dispatchers have exited or if | ||
// no message dispatchers ever started. | ||
func (h *handler) shutdown(ctx context.Context, startClosingTime time.Time) { | ||
defer func() { | ||
if h.onStopped != nil { | ||
go h.onStopped() | ||
} | ||
|
||
h.totalClosingTime = h.clock.Time().Sub(h.startClosingTime) | ||
h.totalClosingTime = h.clock.Time().Sub(startClosingTime) | ||
close(h.closed) | ||
}() | ||
|
||
// shutdown may be called during Start, so we populate the start closing | ||
// time here in case Stop was never called. | ||
if h.startClosingTime.IsZero() { | ||
h.startClosingTime = h.clock.Time() | ||
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. This was previously racy with |
||
} | ||
|
||
state := h.ctx.State.Get() | ||
engine, ok := h.engineManager.Get(state.Type).Get(state.State) | ||
if !ok { | ||
|
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.
This is basically the whole goal of the PR