open-nomad/client/allocrunner/task_hook_coordinator.go

111 lines
2.5 KiB
Go
Raw Normal View History

2019-12-04 20:44:21 +00:00
package allocrunner
import (
"context"
"github.com/hashicorp/go-hclog"
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()
prestartSidecar map[string]struct{}
prestartEphemeral 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)
mainTaskCtx, cancelFn := context.WithCancel(context.Background())
c := &taskHookCoordinator{
logger: logger,
closedCh: closedCh,
mainTaskCtx: mainTaskCtx,
mainTaskCtxCancel: cancelFn,
prestartSidecar: map[string]struct{}{},
prestartEphemeral: map[string]struct{}{},
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 {
2019-12-04 20:44:21 +00:00
// move nothing
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{}{}
}
default:
c.logger.Error("invalid lifecycle hook", "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
}
2019-12-04 20:44:21 +00:00
func (c *taskHookCoordinator) startConditionForTask(task *structs.Task) <-chan struct{} {
if task.Lifecycle != nil && task.Lifecycle.Hook == structs.TaskLifecycleHookPrestart {
return c.closedCh
}
return c.mainTaskCtx.Done()
}
// 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) {
if c.mainTaskCtx.Err() != nil {
// nothing to do here
return
}
for task := range c.prestartSidecar {
2019-12-04 20:44:21 +00:00
st := states[task]
if st == nil || st.StartedAt.IsZero() {
continue
}
delete(c.prestartSidecar, task)
2019-12-04 20:44:21 +00:00
}
for task := range c.prestartEphemeral {
2019-12-04 20:44:21 +00:00
st := states[task]
if st == nil || !st.Successful() {
continue
}
delete(c.prestartEphemeral, 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()
}
}