2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-10-01 13:59:55 +00:00
|
|
|
//go:build !windows
|
2019-03-01 23:02:53 +00:00
|
|
|
|
|
|
|
package allocrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-03-15 08:38:30 +00:00
|
|
|
regMock "github.com/hashicorp/nomad/client/serviceregistration/mock"
|
2019-03-01 23:02:53 +00:00
|
|
|
"github.com/hashicorp/nomad/client/state"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2023-05-12 17:29:44 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2019-03-01 23:02:53 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestAllocRunner_Restore_RunningTerminal asserts that restoring a terminal
|
|
|
|
// alloc with a running task properly kills the running the task. This is meant
|
|
|
|
// to simulate a Nomad agent crash after receiving an updated alloc with
|
|
|
|
// DesiredStatus=Stop, persisting the update, but crashing before terminating
|
|
|
|
// the task.
|
|
|
|
func TestAllocRunner_Restore_RunningTerminal(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-03-01 23:02:53 +00:00
|
|
|
|
|
|
|
// 1. Run task
|
|
|
|
// 2. Shutdown alloc runner
|
|
|
|
// 3. Set alloc.desiredstatus=false
|
|
|
|
// 4. Start new alloc runner
|
|
|
|
// 5. Assert task and logmon are cleaned up
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
2019-11-18 18:04:01 +00:00
|
|
|
alloc.Job.TaskGroups[0].Services = []*structs.Service{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
PortLabel: "8888",
|
2022-03-21 09:29:57 +00:00
|
|
|
Provider: structs.ServiceProviderConsul,
|
2019-11-18 18:04:01 +00:00
|
|
|
},
|
|
|
|
}
|
2019-03-01 23:02:53 +00:00
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
task.Driver = "mock_driver"
|
|
|
|
task.Config = map[string]interface{}{
|
|
|
|
"run_for": "1h",
|
|
|
|
}
|
|
|
|
|
|
|
|
conf, cleanup := testAllocRunnerConfig(t, alloc.Copy())
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Maintain state for subsequent run
|
|
|
|
conf.StateDB = state.NewMemDB(conf.Logger)
|
|
|
|
|
|
|
|
// Start and wait for task to be running
|
|
|
|
ar, err := NewAllocRunner(conf)
|
2023-05-12 17:29:44 +00:00
|
|
|
must.NoError(t, err)
|
2019-03-01 23:02:53 +00:00
|
|
|
go ar.Run()
|
|
|
|
defer destroy(ar)
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
s := ar.AllocState()
|
|
|
|
return s.ClientStatus == structs.AllocClientStatusRunning, fmt.Errorf("expected running, got %s", s.ClientStatus)
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Shutdown the AR and manually change the state to mimic a crash where
|
|
|
|
// a stopped alloc update is received, but Nomad crashes before
|
|
|
|
// stopping the alloc.
|
|
|
|
ar.Shutdown()
|
|
|
|
select {
|
|
|
|
case <-ar.ShutdownCh():
|
|
|
|
case <-time.After(30 * time.Second):
|
|
|
|
require.Fail(t, "AR took too long to exit")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert logmon is still running. This is a super ugly hack that pulls
|
|
|
|
// logmon's PID out of its reattach config, but it does properly ensure
|
|
|
|
// logmon gets cleaned up.
|
|
|
|
ls, _, err := conf.StateDB.GetTaskRunnerState(alloc.ID, task.Name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, ls)
|
|
|
|
|
|
|
|
logmonReattach := struct {
|
|
|
|
Pid int
|
|
|
|
}{}
|
|
|
|
err = json.Unmarshal([]byte(ls.Hooks["logmon"].Data["reattach_config"]), &logmonReattach)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
logmonProc, _ := os.FindProcess(logmonReattach.Pid)
|
|
|
|
require.NoError(t, logmonProc.Signal(syscall.Signal(0)))
|
|
|
|
|
|
|
|
// Fake alloc terminal during Restore()
|
|
|
|
alloc.DesiredStatus = structs.AllocDesiredStatusStop
|
|
|
|
alloc.ModifyIndex++
|
|
|
|
alloc.AllocModifyIndex++
|
|
|
|
|
|
|
|
// Start a new alloc runner and assert it gets stopped
|
|
|
|
conf2, cleanup2 := testAllocRunnerConfig(t, alloc)
|
|
|
|
defer cleanup2()
|
|
|
|
|
|
|
|
// Use original statedb to maintain hook state
|
|
|
|
conf2.StateDB = conf.StateDB
|
|
|
|
|
|
|
|
// Restore, start, and wait for task to be killed
|
2023-05-12 17:29:44 +00:00
|
|
|
ar2Iface, err := NewAllocRunner(conf2)
|
|
|
|
must.NoError(t, err)
|
|
|
|
ar2 := ar2Iface.(*allocRunner)
|
2019-03-01 23:02:53 +00:00
|
|
|
|
|
|
|
require.NoError(t, ar2.Restore())
|
|
|
|
|
|
|
|
go ar2.Run()
|
|
|
|
defer destroy(ar2)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ar2.WaitCh():
|
|
|
|
case <-time.After(30 * time.Second):
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert logmon was cleaned up
|
|
|
|
require.Error(t, logmonProc.Signal(syscall.Signal(0)))
|
2019-03-05 23:12:02 +00:00
|
|
|
|
|
|
|
// Assert consul was cleaned up:
|
2021-07-06 14:37:53 +00:00
|
|
|
// 1 removal during prekill
|
2022-02-11 08:29:38 +00:00
|
|
|
// - removal during exited is de-duped due to prekill
|
|
|
|
// - removal during stop is de-duped due to prekill
|
2021-07-06 14:37:53 +00:00
|
|
|
// 1 removal group during stop
|
2022-03-15 08:38:30 +00:00
|
|
|
consulOps := conf2.Consul.(*regMock.ServiceRegistrationHandler).GetOps()
|
2022-02-11 08:29:38 +00:00
|
|
|
require.Len(t, consulOps, 2)
|
2019-11-18 18:04:01 +00:00
|
|
|
for _, op := range consulOps {
|
2019-03-05 23:12:02 +00:00
|
|
|
require.Equal(t, "remove", op.Op)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert terminated task event was emitted
|
|
|
|
events := ar2.AllocState().TaskStates[task.Name].Events
|
|
|
|
require.Len(t, events, 4)
|
|
|
|
require.Equal(t, events[0].Type, structs.TaskReceived)
|
|
|
|
require.Equal(t, events[1].Type, structs.TaskSetup)
|
|
|
|
require.Equal(t, events[2].Type, structs.TaskStarted)
|
|
|
|
require.Equal(t, events[3].Type, structs.TaskTerminated)
|
2019-03-01 23:02:53 +00:00
|
|
|
}
|
2019-06-26 15:39:10 +00:00
|
|
|
|
2019-07-02 06:53:50 +00:00
|
|
|
// TestAllocRunner_Restore_CompletedBatch asserts that restoring a completed
|
2019-06-26 15:39:10 +00:00
|
|
|
// batch alloc doesn't run it again
|
|
|
|
func TestAllocRunner_Restore_CompletedBatch(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-06-26 15:39:10 +00:00
|
|
|
|
|
|
|
// 1. Run task and wait for it to complete
|
|
|
|
// 2. Start new alloc runner
|
|
|
|
// 3. Assert task didn't run again
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.Job.Type = structs.JobTypeBatch
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
task.Driver = "mock_driver"
|
|
|
|
task.Config = map[string]interface{}{
|
|
|
|
"run_for": "2ms",
|
|
|
|
}
|
|
|
|
|
|
|
|
conf, cleanup := testAllocRunnerConfig(t, alloc.Copy())
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Maintain state for subsequent run
|
|
|
|
conf.StateDB = state.NewMemDB(conf.Logger)
|
|
|
|
|
|
|
|
// Start and wait for task to be running
|
2023-05-12 17:29:44 +00:00
|
|
|
arIface, err := NewAllocRunner(conf)
|
|
|
|
must.NoError(t, err)
|
|
|
|
ar := arIface.(*allocRunner)
|
2019-06-26 15:39:10 +00:00
|
|
|
go ar.Run()
|
|
|
|
defer destroy(ar)
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
s := ar.AllocState()
|
|
|
|
if s.ClientStatus != structs.AllocClientStatusComplete {
|
|
|
|
return false, fmt.Errorf("expected complete, got %s", s.ClientStatus)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// once job finishes, it shouldn't run again
|
|
|
|
require.False(t, ar.shouldRun())
|
|
|
|
initialRunEvents := ar.AllocState().TaskStates[task.Name].Events
|
|
|
|
require.Len(t, initialRunEvents, 4)
|
|
|
|
|
|
|
|
ls, ts, err := conf.StateDB.GetTaskRunnerState(alloc.ID, task.Name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, ls)
|
|
|
|
require.Equal(t, structs.TaskStateDead, ts.State)
|
|
|
|
|
|
|
|
// Start a new alloc runner and assert it gets stopped
|
|
|
|
conf2, cleanup2 := testAllocRunnerConfig(t, alloc)
|
|
|
|
defer cleanup2()
|
|
|
|
|
|
|
|
// Use original statedb to maintain hook state
|
|
|
|
conf2.StateDB = conf.StateDB
|
|
|
|
|
|
|
|
// Restore, start, and wait for task to be killed
|
2023-05-12 17:29:44 +00:00
|
|
|
ar2Iface, err := NewAllocRunner(conf2)
|
|
|
|
must.NoError(t, err)
|
|
|
|
ar2 := ar2Iface.(*allocRunner)
|
|
|
|
must.NoError(t, ar2.Restore())
|
2019-06-26 15:39:10 +00:00
|
|
|
|
|
|
|
go ar2.Run()
|
|
|
|
defer destroy(ar2)
|
|
|
|
|
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 21:43:07 +00:00
|
|
|
// AR waitCh must be open as the task waits for a possible alloc restart.
|
2019-06-26 15:39:10 +00:00
|
|
|
select {
|
|
|
|
case <-ar2.WaitCh():
|
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 21:43:07 +00:00
|
|
|
require.Fail(t, "alloc.waitCh was closed")
|
|
|
|
default:
|
2019-06-26 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
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 21:43:07 +00:00
|
|
|
// TR waitCh must be open too!
|
2019-06-26 15:39:10 +00:00
|
|
|
select {
|
|
|
|
case <-ar2.tasks[task.Name].WaitCh():
|
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 21:43:07 +00:00
|
|
|
require.Fail(t, "tr.waitCh was closed")
|
|
|
|
default:
|
2019-06-26 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that events are unmodified, which they would if task re-run
|
|
|
|
events := ar2.AllocState().TaskStates[task.Name].Events
|
|
|
|
require.Equal(t, initialRunEvents, events)
|
|
|
|
}
|
2019-06-29 08:56:40 +00:00
|
|
|
|
|
|
|
// TestAllocRunner_PreStartFailuresLeadToFailed asserts that if an alloc
|
|
|
|
// prestart hooks failed, then the alloc and subsequent tasks transition
|
|
|
|
// to failed state
|
|
|
|
func TestAllocRunner_PreStartFailuresLeadToFailed(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-06-29 08:56:40 +00:00
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.Job.Type = structs.JobTypeBatch
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
task.Driver = "mock_driver"
|
|
|
|
task.Config = map[string]interface{}{
|
|
|
|
"run_for": "2ms",
|
|
|
|
}
|
2020-03-25 01:52:39 +00:00
|
|
|
rp := &structs.RestartPolicy{Attempts: 0}
|
|
|
|
alloc.Job.TaskGroups[0].RestartPolicy = rp
|
|
|
|
task.RestartPolicy = rp
|
2019-06-29 08:56:40 +00:00
|
|
|
|
|
|
|
conf, cleanup := testAllocRunnerConfig(t, alloc.Copy())
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Maintain state for subsequent run
|
|
|
|
conf.StateDB = state.NewMemDB(conf.Logger)
|
|
|
|
|
|
|
|
// Start and wait for task to be running
|
2023-05-12 17:29:44 +00:00
|
|
|
arIface, err := NewAllocRunner(conf)
|
|
|
|
must.NoError(t, err)
|
|
|
|
ar := arIface.(*allocRunner)
|
2019-06-29 08:56:40 +00:00
|
|
|
ar.runnerHooks = append(ar.runnerHooks, &allocFailingPrestartHook{})
|
|
|
|
|
|
|
|
go ar.Run()
|
|
|
|
defer destroy(ar)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ar.WaitCh():
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
require.Fail(t, "alloc.waitCh wasn't closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
s := ar.AllocState()
|
|
|
|
if s.ClientStatus != structs.AllocClientStatusFailed {
|
|
|
|
return false, fmt.Errorf("expected complete, got %s", s.ClientStatus)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// once job finishes, it shouldn't run again
|
|
|
|
require.False(t, ar.shouldRun())
|
|
|
|
initialRunEvents := ar.AllocState().TaskStates[task.Name].Events
|
|
|
|
require.Len(t, initialRunEvents, 2)
|
|
|
|
|
|
|
|
ls, ts, err := conf.StateDB.GetTaskRunnerState(alloc.ID, task.Name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, ls)
|
|
|
|
require.NotNil(t, ts)
|
|
|
|
require.Equal(t, structs.TaskStateDead, ts.State)
|
|
|
|
require.True(t, ts.Failed)
|
|
|
|
|
|
|
|
// TR waitCh must be closed too!
|
|
|
|
select {
|
|
|
|
case <-ar.tasks[task.Name].WaitCh():
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
require.Fail(t, "tr.waitCh wasn't closed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type allocFailingPrestartHook struct{}
|
|
|
|
|
|
|
|
func (*allocFailingPrestartHook) Name() string { return "failing_prestart" }
|
|
|
|
|
|
|
|
func (*allocFailingPrestartHook) Prerun() error {
|
|
|
|
return fmt.Errorf("failing prestart hooks")
|
|
|
|
}
|