2019-12-04 20:44:21 +00:00
|
|
|
package allocrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-03-02 19:12:16 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2020-06-29 19:07:48 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/taskrunner"
|
2019-12-04 20:44:21 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2020-03-21 21:52:11 +00:00
|
|
|
// TaskHookCoordinator helps coordinate when main start tasks can launch
|
2019-12-04 20:44:21 +00:00
|
|
|
// namely after all Prestart Tasks have run, and after all BlockUntilCompleted have completed
|
|
|
|
type taskHookCoordinator struct {
|
|
|
|
logger hclog.Logger
|
|
|
|
|
|
|
|
closedCh chan struct{}
|
|
|
|
|
|
|
|
mainTaskCtx context.Context
|
|
|
|
mainTaskCtxCancel func()
|
|
|
|
|
2020-08-11 16:47:44 +00:00
|
|
|
poststartTaskCtx context.Context
|
2020-07-08 18:01:35 +00:00
|
|
|
poststartTaskCtxCancel func()
|
|
|
|
|
2020-03-02 19:12:16 +00:00
|
|
|
prestartSidecar map[string]struct{}
|
|
|
|
prestartEphemeral map[string]struct{}
|
2020-08-11 16:47:44 +00:00
|
|
|
mainTasksPending map[string]struct{}
|
2019-12-04 20:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTaskHookCoordinator(logger hclog.Logger, tasks []*structs.Task) *taskHookCoordinator {
|
|
|
|
closedCh := make(chan struct{})
|
|
|
|
close(closedCh)
|
|
|
|
|
2020-07-08 18:01:35 +00:00
|
|
|
mainTaskCtx, mainCancelFn := context.WithCancel(context.Background())
|
|
|
|
poststartTaskCtx, poststartCancelFn := context.WithCancel(context.Background())
|
2019-12-04 20:44:21 +00:00
|
|
|
|
|
|
|
c := &taskHookCoordinator{
|
2020-08-11 16:47:44 +00:00
|
|
|
logger: logger,
|
|
|
|
closedCh: closedCh,
|
|
|
|
mainTaskCtx: mainTaskCtx,
|
|
|
|
mainTaskCtxCancel: mainCancelFn,
|
|
|
|
prestartSidecar: map[string]struct{}{},
|
|
|
|
prestartEphemeral: map[string]struct{}{},
|
|
|
|
mainTasksPending: map[string]struct{}{},
|
|
|
|
poststartTaskCtx: poststartTaskCtx,
|
2020-07-08 18:01:35 +00:00
|
|
|
poststartTaskCtxCancel: poststartCancelFn,
|
2019-12-04 20:44:21 +00:00
|
|
|
}
|
|
|
|
c.setTasks(tasks)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *taskHookCoordinator) setTasks(tasks []*structs.Task) {
|
|
|
|
for _, task := range tasks {
|
2020-03-21 21:52:11 +00:00
|
|
|
|
|
|
|
if task.Lifecycle == nil {
|
2020-07-10 16:03:10 +00:00
|
|
|
c.mainTasksPending[task.Name] = struct{}{}
|
2019-12-04 20:44:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-21 21:52:11 +00:00
|
|
|
switch task.Lifecycle.Hook {
|
|
|
|
case structs.TaskLifecycleHookPrestart:
|
|
|
|
if task.Lifecycle.Sidecar {
|
|
|
|
c.prestartSidecar[task.Name] = struct{}{}
|
|
|
|
} else {
|
|
|
|
c.prestartEphemeral[task.Name] = struct{}{}
|
|
|
|
}
|
2020-08-11 16:47:44 +00:00
|
|
|
case structs.TaskLifecycleHookPoststart:
|
|
|
|
// Poststart hooks don't need to be tracked.
|
2020-03-21 21:52:11 +00:00
|
|
|
default:
|
2020-08-11 16:47:44 +00:00
|
|
|
c.logger.Error("invalid lifecycle hook", "task", task.Name, "hook", task.Lifecycle.Hook)
|
2019-12-04 20:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 21:52:11 +00:00
|
|
|
if !c.hasPrestartTasks() {
|
2019-12-04 20:44:21 +00:00
|
|
|
c.mainTaskCtxCancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 21:52:11 +00:00
|
|
|
func (c *taskHookCoordinator) hasPrestartTasks() bool {
|
|
|
|
return len(c.prestartSidecar)+len(c.prestartEphemeral) > 0
|
|
|
|
}
|
|
|
|
|
2020-07-08 18:01:35 +00:00
|
|
|
func (c *taskHookCoordinator) hasPendingMainTasks() bool {
|
|
|
|
return len(c.mainTasksPending) > 0
|
|
|
|
}
|
|
|
|
|
2019-12-04 20:44:21 +00:00
|
|
|
func (c *taskHookCoordinator) startConditionForTask(task *structs.Task) <-chan struct{} {
|
2020-07-10 16:03:10 +00:00
|
|
|
if task.Lifecycle == nil {
|
|
|
|
return c.mainTaskCtx.Done()
|
2019-12-04 20:44:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 18:01:35 +00:00
|
|
|
switch task.Lifecycle.Hook {
|
|
|
|
case structs.TaskLifecycleHookPrestart:
|
|
|
|
// Prestart tasks start without checking status of other tasks
|
|
|
|
return c.closedCh
|
|
|
|
case structs.TaskLifecycleHookPoststart:
|
|
|
|
return c.poststartTaskCtx.Done()
|
|
|
|
default:
|
|
|
|
return c.mainTaskCtx.Done()
|
|
|
|
}
|
2019-12-04 20:44:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 19:12:16 +00:00
|
|
|
// This is not thread safe! This must only be called from one thread per alloc runner.
|
2019-12-04 20:44:21 +00:00
|
|
|
func (c *taskHookCoordinator) taskStateUpdated(states map[string]*structs.TaskState) {
|
2020-03-02 19:12:16 +00:00
|
|
|
for task := range c.prestartSidecar {
|
2019-12-04 20:44:21 +00:00
|
|
|
st := states[task]
|
|
|
|
if st == nil || st.StartedAt.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-02 19:12:16 +00:00
|
|
|
delete(c.prestartSidecar, task)
|
2019-12-04 20:44:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 19:12:16 +00:00
|
|
|
for task := range c.prestartEphemeral {
|
2019-12-04 20:44:21 +00:00
|
|
|
st := states[task]
|
|
|
|
if st == nil || !st.Successful() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-02 19:12:16 +00:00
|
|
|
delete(c.prestartEphemeral, task)
|
2019-12-04 20:44:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 18:01:35 +00:00
|
|
|
for task := range c.mainTasksPending {
|
|
|
|
st := states[task]
|
|
|
|
if st == nil || st.StartedAt.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(c.mainTasksPending, task)
|
|
|
|
}
|
|
|
|
|
2019-12-04 20:44:21 +00:00
|
|
|
// everything well
|
2020-03-21 21:52:11 +00:00
|
|
|
if !c.hasPrestartTasks() {
|
2019-12-04 20:44:21 +00:00
|
|
|
c.mainTaskCtxCancel()
|
|
|
|
}
|
2020-07-08 18:01:35 +00:00
|
|
|
|
|
|
|
if !c.hasPendingMainTasks() {
|
|
|
|
c.poststartTaskCtxCancel()
|
|
|
|
}
|
2019-12-04 20:44:21 +00:00
|
|
|
}
|
2020-06-29 19:07:48 +00:00
|
|
|
|
|
|
|
// hasNonSidecarTasks returns false if all the passed tasks are sidecar tasks
|
|
|
|
func hasNonSidecarTasks(tasks []*taskrunner.TaskRunner) bool {
|
|
|
|
for _, tr := range tasks {
|
|
|
|
lc := tr.Task().Lifecycle
|
|
|
|
if lc == nil || !lc.Sidecar {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// hasSidecarTasks returns true if all the passed tasks are sidecar tasks
|
|
|
|
func hasSidecarTasks(tasks map[string]*taskrunner.TaskRunner) bool {
|
|
|
|
for _, tr := range tasks {
|
|
|
|
lc := tr.Task().Lifecycle
|
|
|
|
if lc != nil && lc.Sidecar {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|