e012d9411e
* allocrunner: handle lifecycle when all tasks die When all tasks die the Coordinator must transition to its terminal state, coordinatorStatePoststop, to unblock poststop tasks. Since this could happen at any time (for example, a prestart task dies), all states must be able to transition to this terminal state. * allocrunner: implement different alloc restarts Add a new alloc restart mode where all tasks are restarted, even if they have already exited. Also unifies the alloc restart logic to use the implementation that restarts tasks concurrently and ignores ErrTaskNotRunning errors since those are expected when restarting the allocation. * allocrunner: allow tasks to run again Prevent the task runner Run() method from exiting to allow a dead task to run again. When the task runner is signaled to restart, the function will jump back to the MAIN loop and run it again. The task runner determines if a task needs to run again based on two new task events that were added to differentiate between a request to restart a specific task, the tasks that are currently running, or all tasks that have already run. * api/cli: add support for all tasks alloc restart Implement the new -all-tasks alloc restart CLI flag and its API counterpar, AllTasks. The client endpoint calls the appropriate restart method from the allocrunner depending on the restart parameters used. * test: fix tasklifecycle Coordinator test * allocrunner: kill taskrunners if all tasks are dead When all non-poststop tasks are dead we need to kill the taskrunners so we don't leak their goroutines, which are blocked in the alloc restart loop. This also ensures the allocrunner exits on its own. * taskrunner: fix tests that waited on WaitCh Now that "dead" tasks may run again, the taskrunner Run() method will not return when the task finishes running, so tests must wait for the task state to be "dead" instead of using the WaitCh, since it won't be closed until the taskrunner is killed. * tests: add tests for all tasks alloc restart * changelog: add entry for #14127 * taskrunner: fix restore logic. The first implementation of the task runner restore process relied on server data (`tr.Alloc().TerminalStatus()`) which may not be available to the client at the time of restore. It also had the incorrect code path. When restoring a dead task the driver handle always needs to be clear cleanly using `clearDriverHandle` otherwise, after exiting the MAIN loop, the task may be killed by `tr.handleKill`. The fix is to store the state of the Run() loop in the task runner local client state: if the task runner ever exits this loop cleanly (not with a shutdown) it will never be able to run again. So if the Run() loops starts with this local state flag set, it must exit early. This local state flag is also being checked on task restart requests. If the task is "dead" and its Run() loop is not active it will never be able to run again. * address code review requests * apply more code review changes * taskrunner: add different Restart modes Using the task event to differentiate between the allocrunner restart methods proved to be confusing for developers to understand how it all worked. So instead of relying on the event type, this commit separated the logic of restarting an taskRunner into two methods: - `Restart` will retain the current behaviour and only will only restart the task if it's currently running. - `ForceRestart` is the new method where a `dead` task is allowed to restart if its `Run()` method is still active. Callers will need to restart the allocRunner taskCoordinator to make sure it will allow the task to run again. * minor fixes
171 lines
4.4 KiB
Go
171 lines
4.4 KiB
Go
package taskrunner
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
)
|
|
|
|
// Restart restarts a task that is already running. Returns an error if the
|
|
// task is not running. Blocks until existing task exits or passed-in context
|
|
// is canceled.
|
|
func (tr *TaskRunner) Restart(ctx context.Context, event *structs.TaskEvent, failure bool) error {
|
|
tr.logger.Trace("Restart requested", "failure", failure, "event", event.GoString())
|
|
|
|
taskState := tr.TaskState()
|
|
if taskState == nil {
|
|
return ErrTaskNotRunning
|
|
}
|
|
|
|
switch taskState.State {
|
|
case structs.TaskStatePending, structs.TaskStateDead:
|
|
return ErrTaskNotRunning
|
|
}
|
|
|
|
return tr.restartImpl(ctx, event, failure)
|
|
}
|
|
|
|
// ForceRestart restarts a task that is already running or reruns it if dead.
|
|
// Returns an error if the task is not able to rerun. Blocks until existing
|
|
// task exits or passed-in context is canceled.
|
|
//
|
|
// Callers must restart the AllocRuner taskCoordinator beforehand to make sure
|
|
// the task will be able to run again.
|
|
func (tr *TaskRunner) ForceRestart(ctx context.Context, event *structs.TaskEvent, failure bool) error {
|
|
tr.logger.Trace("Force restart requested", "failure", failure, "event", event.GoString())
|
|
|
|
taskState := tr.TaskState()
|
|
if taskState == nil {
|
|
return ErrTaskNotRunning
|
|
}
|
|
|
|
tr.stateLock.Lock()
|
|
localState := tr.localState.Copy()
|
|
tr.stateLock.Unlock()
|
|
|
|
if localState == nil {
|
|
return ErrTaskNotRunning
|
|
}
|
|
|
|
switch taskState.State {
|
|
case structs.TaskStatePending:
|
|
return ErrTaskNotRunning
|
|
|
|
case structs.TaskStateDead:
|
|
// Tasks that are in the "dead" state are only allowed to restart if
|
|
// their Run() method is still active.
|
|
if localState.RunComplete {
|
|
return ErrTaskNotRunning
|
|
}
|
|
}
|
|
|
|
return tr.restartImpl(ctx, event, failure)
|
|
}
|
|
|
|
// restartImpl implements to task restart process.
|
|
//
|
|
// It should never be called directly as it doesn't verify if the task state
|
|
// allows for a restart.
|
|
func (tr *TaskRunner) restartImpl(ctx context.Context, event *structs.TaskEvent, failure bool) error {
|
|
|
|
// Check if the task is able to restart based on its state and the type of
|
|
// restart event that was triggered.
|
|
taskState := tr.TaskState()
|
|
if taskState == nil {
|
|
return ErrTaskNotRunning
|
|
}
|
|
|
|
// Emit the event since it may take a long time to kill
|
|
tr.EmitEvent(event)
|
|
|
|
// Tell the restart tracker that a restart triggered the exit
|
|
tr.restartTracker.SetRestartTriggered(failure)
|
|
|
|
// Signal a restart to unblock tasks that are in the "dead" state, but
|
|
// don't block since the channel is buffered. Only one signal is enough to
|
|
// notify the tr.Run() loop.
|
|
// The channel must be signaled after SetRestartTriggered is called so the
|
|
// tr.Run() loop runs again.
|
|
if taskState.State == structs.TaskStateDead {
|
|
select {
|
|
case tr.restartCh <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// Grab the handle to see if the task is still running and needs to be
|
|
// killed.
|
|
handle := tr.getDriverHandle()
|
|
if handle == nil {
|
|
return nil
|
|
}
|
|
|
|
// Run the pre-kill hooks prior to restarting the task
|
|
tr.preKill()
|
|
|
|
// Grab a handle to the wait channel that will timeout with context cancelation
|
|
// _before_ killing the task.
|
|
waitCh, err := handle.WaitCh(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Kill the task using an exponential backoff in-case of failures.
|
|
if _, err := tr.killTask(handle, waitCh); err != nil {
|
|
// We couldn't successfully destroy the resource created.
|
|
tr.logger.Error("failed to kill task. Resources may have been leaked", "error", err)
|
|
}
|
|
|
|
select {
|
|
case <-waitCh:
|
|
case <-ctx.Done():
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (tr *TaskRunner) Signal(event *structs.TaskEvent, s string) error {
|
|
tr.logger.Trace("Signal requested", "signal", s)
|
|
|
|
// Grab the handle
|
|
handle := tr.getDriverHandle()
|
|
|
|
// Check it is running
|
|
if handle == nil {
|
|
return ErrTaskNotRunning
|
|
}
|
|
|
|
// Emit the event
|
|
tr.EmitEvent(event)
|
|
|
|
// Send the signal
|
|
return handle.Signal(s)
|
|
}
|
|
|
|
// Kill a task. Blocks until task exits or context is canceled. State is set to
|
|
// dead.
|
|
func (tr *TaskRunner) Kill(ctx context.Context, event *structs.TaskEvent) error {
|
|
tr.logger.Trace("Kill requested")
|
|
|
|
// Cancel the task runner to break out of restart delay or the main run
|
|
// loop.
|
|
tr.killCtxCancel()
|
|
|
|
// Emit kill event
|
|
if event != nil {
|
|
tr.logger.Trace("Kill event", "event_type", event.Type, "event_reason", event.KillReason)
|
|
tr.EmitEvent(event)
|
|
}
|
|
|
|
select {
|
|
case <-tr.WaitCh():
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
|
|
return tr.getKillErr()
|
|
}
|
|
|
|
func (tr *TaskRunner) IsRunning() bool {
|
|
return tr.getDriverHandle() != nil
|
|
}
|