open-nomad/command/agent
Luiz Aoqui e012d9411e
Task lifecycle restart (#14127)
* 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
2022-08-24 17:43:07 -04:00
..
consul allocrunner: refactor task coordinator (#14009) 2022-08-22 18:38:49 -04:00
event
host
monitor
pprof
test-resources Add Nomad RetryConfig to agent template config (#13907) 2022-08-03 16:56:30 -04:00
testdata
acl_endpoint.go
acl_endpoint_test.go
agent.go client: fix data races in config handling (#14139) 2022-08-18 16:32:04 -07:00
agent_endpoint.go client: fix data races in config handling (#14139) 2022-08-18 16:32:04 -07:00
agent_endpoint_test.go cleanup: replace TypeToPtr helper methods with pointer.Of (#14151) 2022-08-17 18:26:34 +02:00
agent_oss.go
agent_test.go vault: detect namespace change in config reload (#14298) 2022-08-24 17:03:29 -04:00
alloc_endpoint.go Task lifecycle restart (#14127) 2022-08-24 17:43:07 -04:00
alloc_endpoint_test.go cleanup: replace TypeToPtr helper methods with pointer.Of (#14151) 2022-08-17 18:26:34 +02:00
bindata_assetfs.go Post 1.3.3 release (#14064) 2022-08-09 17:27:29 -04:00
command.go client: fix data races in config handling (#14139) 2022-08-18 16:32:04 -07:00
command_test.go cleanup: replace TypeToPtr helper methods with pointer.Of (#14151) 2022-08-17 18:26:34 +02:00
config.go client: fix data races in config handling (#14139) 2022-08-18 16:32:04 -07:00
config_oss.go
config_parse.go Add Nomad RetryConfig to agent template config (#13907) 2022-08-03 16:56:30 -04:00
config_parse_test.go cleanup: replace TypeToPtr helper methods with pointer.Of (#14151) 2022-08-17 18:26:34 +02:00
config_test.go cleanup: replace TypeToPtr helper methods with pointer.Of (#14151) 2022-08-17 18:26:34 +02:00
csi_endpoint.go
csi_endpoint_test.go
deployment_endpoint.go
deployment_endpoint_test.go
eval_endpoint.go
eval_endpoint_test.go
event_endpoint.go
event_endpoint_test.go
fs_endpoint.go build: run gofmt on all go source files 2022-08-16 11:14:11 -05:00
fs_endpoint_test.go
helpers.go
helpers_test.go
http.go
http_oss.go
http_stdlog.go
http_stdlog_test.go
http_test.go client: fix data races in config handling (#14139) 2022-08-18 16:32:04 -07:00
job_endpoint.go template: custom change_mode scripts (#13972) 2022-08-24 17:43:01 +02:00
job_endpoint_test.go template: custom change_mode scripts (#13972) 2022-08-24 17:43:01 +02:00
keyring.go
keyring_endpoint.go
keyring_endpoint_test.go
keyring_test.go
log_file.go
log_file_bsd.go
log_file_linux.go
log_file_test.go
log_file_windows.go
log_levels.go
log_levels_test.go
metrics_endpoint.go
metrics_endpoint_test.go
namespace_endpoint.go
namespace_endpoint_test.go
node_endpoint.go
node_endpoint_test.go
operator_endpoint.go
operator_endpoint_oss.go
operator_endpoint_test.go
plugins.go
region_endpoint.go
region_endpoint_test.go
retry_join.go
retry_join_test.go
scaling_endpoint.go
scaling_endpoint_test.go
search_endpoint.go
search_endpoint_test.go move secure variable conflict resolution to state store (#13922) 2022-08-15 11:19:53 -04:00
secure_variable_endpoint.go move secure variable conflict resolution to state store (#13922) 2022-08-15 11:19:53 -04:00
secure_variable_endpoint_test.go move secure variable conflict resolution to state store (#13922) 2022-08-15 11:19:53 -04:00
service_registration_endpoint.go
service_registration_endpoint_test.go
stats_endpoint.go
stats_endpoint_test.go
status_endpoint.go
status_endpoint_test.go
stub_asset.go
syslog.go
syslog_test.go
system_endpoint.go
system_endpoint_test.go
testagent.go
testagent_oss.go
testingutils_test.go cleanup: replace TypeToPtr helper methods with pointer.Of (#14151) 2022-08-17 18:26:34 +02:00