-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Task runner recovers from external plugin exiting #5152
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ffadab1
remove nil logger
dadgar 06a52b5
rkt fingerprint logs once
dadgar bdf4e1f
Don't log event error on driver shutdown
dadgar d45d6c6
add docker logger to separate main
dadgar 437f03d
recover
dadgar 3257eb6
Fix hooks
dadgar b300306
comments
dadgar 4e7da3e
regenerate protos
dadgar 144866a
Mock driver has recovery, stats
dadgar 19e67a0
Test recovery
dadgar 5424d3b
vet
dadgar 6bb99c9
Review comments
dadgar 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package taskrunner | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
log "github.com/hashicorp/go-hclog" | ||
cstructs "github.com/hashicorp/nomad/client/structs" | ||
bstructs "github.com/hashicorp/nomad/plugins/base/structs" | ||
) | ||
|
||
const ( | ||
// retrieveBackoffBaseline is the baseline time for exponential backoff while | ||
// retrieving a handle. | ||
retrieveBackoffBaseline = 250 * time.Millisecond | ||
|
||
// retrieveBackoffLimit is the limit of the exponential backoff for | ||
// retrieving a handle. | ||
retrieveBackoffLimit = 5 * time.Second | ||
|
||
// retrieveFailureLimit is how many times we will attempt to retrieve a | ||
// new handle before giving up. | ||
retrieveFailureLimit = 5 | ||
) | ||
|
||
// retrieveHandleFn is used to retrieve the latest driver handle | ||
type retrieveHandleFn func() *DriverHandle | ||
|
||
// LazyHandle is used to front calls to a DriverHandle where it is expected the | ||
// existing handle may no longer be valid because the backing plugin has | ||
// shutdown. LazyHandle detects the plugin shutting down and retrieves a new | ||
// handle so that the consumer does not need to worry whether the handle is to | ||
// the latest driver instance. | ||
type LazyHandle struct { | ||
// retrieveHandle is used to retrieve the latest handle | ||
retrieveHandle retrieveHandleFn | ||
|
||
// h is the current handle and may be nil | ||
h *DriverHandle | ||
|
||
// shutdownCtx is used to cancel retries if the agent is shutting down | ||
shutdownCtx context.Context | ||
|
||
logger log.Logger | ||
sync.Mutex | ||
} | ||
|
||
// NewLazyHandle takes the function to receive the latest handle and a logger | ||
// and returns a LazyHandle | ||
func NewLazyHandle(shutdownCtx context.Context, fn retrieveHandleFn, logger log.Logger) *LazyHandle { | ||
return &LazyHandle{ | ||
retrieveHandle: fn, | ||
h: fn(), | ||
shutdownCtx: shutdownCtx, | ||
logger: logger.Named("lazy_handle"), | ||
} | ||
} | ||
|
||
// getHandle returns the current handle or retrieves a new one | ||
func (l *LazyHandle) getHandle() (*DriverHandle, error) { | ||
l.Lock() | ||
defer l.Unlock() | ||
|
||
if l.h != nil { | ||
return l.h, nil | ||
} | ||
|
||
return l.refreshHandleLocked() | ||
} | ||
|
||
// refreshHandle retrieves a new handle | ||
func (l *LazyHandle) refreshHandle() (*DriverHandle, error) { | ||
l.Lock() | ||
defer l.Unlock() | ||
return l.refreshHandleLocked() | ||
} | ||
|
||
// refreshHandleLocked retrieves a new handle and should be called with the lock | ||
// held. It will retry to give the client time to restart the driver and restore | ||
// the handle. | ||
func (l *LazyHandle) refreshHandleLocked() (*DriverHandle, error) { | ||
for i := 0; i < retrieveFailureLimit; i++ { | ||
l.h = l.retrieveHandle() | ||
if l.h != nil { | ||
return l.h, nil | ||
} | ||
|
||
// Calculate the new backoff | ||
backoff := (1 << (2 * uint64(i))) * retrieveBackoffBaseline | ||
if backoff > retrieveBackoffLimit { | ||
backoff = retrieveBackoffLimit | ||
} | ||
|
||
l.logger.Debug("failed to retrieve handle", "backoff", backoff) | ||
|
||
select { | ||
case <-l.shutdownCtx.Done(): | ||
return nil, l.shutdownCtx.Err() | ||
case <-time.After(backoff): | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("no driver handle") | ||
schmichael marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (l *LazyHandle) Exec(timeout time.Duration, cmd string, args []string) ([]byte, int, error) { | ||
h, err := l.getHandle() | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
// Only retry once | ||
first := true | ||
|
||
TRY: | ||
out, c, err := h.Exec(timeout, cmd, args) | ||
if err == bstructs.ErrPluginShutdown && first { | ||
first = false | ||
|
||
h, err = l.refreshHandle() | ||
if err == nil { | ||
goto TRY | ||
} | ||
} | ||
|
||
return out, c, err | ||
} | ||
|
||
func (l *LazyHandle) Stats() (*cstructs.TaskResourceUsage, error) { | ||
h, err := l.getHandle() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Only retry once | ||
first := true | ||
|
||
TRY: | ||
out, err := h.Stats() | ||
if err == bstructs.ErrPluginShutdown && first { | ||
first = false | ||
|
||
h, err = l.refreshHandle() | ||
if err == nil { | ||
goto TRY | ||
} | ||
} | ||
|
||
return out, err | ||
} |
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
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.
Is a named sublogger helpful here? I feel like we may have gone overboard with subloggers and now have some really long named loggers that don't really help end users at all (eg
client.alloc_runner.task_runner
)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.
I don't expect the log to be emitted but the named logger helps for debugability since we will be able to easily narrow down the log lines