2018-10-04 23:22:01 +00:00
|
|
|
package allocrunner
|
2018-06-22 00:35:07 +00:00
|
|
|
|
|
|
|
import (
|
2018-07-17 20:57:57 +00:00
|
|
|
"context"
|
2018-06-22 00:35:07 +00:00
|
|
|
"fmt"
|
2018-06-29 00:01:05 +00:00
|
|
|
"path/filepath"
|
2018-06-22 00:35:07 +00:00
|
|
|
"sync"
|
2018-07-19 00:06:44 +00:00
|
|
|
"time"
|
2018-06-22 00:35:07 +00:00
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2018-10-04 23:22:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/state"
|
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/taskrunner"
|
2018-08-23 19:03:17 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocwatcher"
|
2018-07-13 16:45:29 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-07-20 00:40:25 +00:00
|
|
|
"github.com/hashicorp/nomad/client/consul"
|
2018-07-19 00:06:44 +00:00
|
|
|
cinterfaces "github.com/hashicorp/nomad/client/interfaces"
|
2018-08-08 00:46:37 +00:00
|
|
|
cstate "github.com/hashicorp/nomad/client/state"
|
2018-06-29 00:01:05 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2018-07-13 16:45:29 +00:00
|
|
|
"github.com/hashicorp/nomad/client/vaultclient"
|
2018-07-19 00:06:44 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2018-06-22 00:35:07 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// allocRunner is used to run all the tasks in a given allocation
|
|
|
|
type allocRunner struct {
|
2018-07-19 00:06:44 +00:00
|
|
|
// id is the ID of the allocation. Can be accessed without a lock
|
|
|
|
id string
|
|
|
|
|
2018-06-22 00:35:07 +00:00
|
|
|
// Logger is the logger for the alloc runner.
|
|
|
|
logger log.Logger
|
|
|
|
|
2018-07-13 16:45:29 +00:00
|
|
|
clientConfig *config.Config
|
|
|
|
|
2018-07-19 00:06:44 +00:00
|
|
|
// stateUpdater is used to emit updated task state
|
|
|
|
stateUpdater cinterfaces.AllocStateHandler
|
|
|
|
|
2018-07-20 00:40:25 +00:00
|
|
|
// consulClient is the client used by the consul service hook for
|
|
|
|
// registering services and checks
|
|
|
|
consulClient consul.ConsulServiceAPI
|
|
|
|
|
2018-07-13 16:45:29 +00:00
|
|
|
// vaultClient is the used to manage Vault tokens
|
|
|
|
vaultClient vaultclient.VaultClient
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2018-08-23 19:03:17 +00:00
|
|
|
// waitCh is closed when the Run() loop has exited
|
2018-06-22 00:35:07 +00:00
|
|
|
waitCh chan struct{}
|
|
|
|
|
2018-08-23 19:03:17 +00:00
|
|
|
// destroyed is true when the Run() loop has exited, postrun hooks have
|
|
|
|
// run, and alloc runner has been destroyed
|
|
|
|
destroyed bool
|
|
|
|
destroyedLock sync.Mutex
|
|
|
|
|
2018-06-22 00:35:07 +00:00
|
|
|
// Alloc captures the allocation being run.
|
|
|
|
alloc *structs.Allocation
|
2018-06-29 00:01:05 +00:00
|
|
|
allocLock sync.RWMutex
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2018-09-28 00:30:10 +00:00
|
|
|
// state is the alloc runner's state
|
|
|
|
state *state.State
|
2018-07-19 00:06:44 +00:00
|
|
|
stateLock sync.RWMutex
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2018-08-08 00:46:37 +00:00
|
|
|
stateDB cstate.StateDB
|
2018-07-11 04:21:12 +00:00
|
|
|
|
2018-06-22 00:35:07 +00:00
|
|
|
// allocDir is used to build the allocations directory structure.
|
|
|
|
allocDir *allocdir.AllocDir
|
|
|
|
|
|
|
|
// runnerHooks are alloc runner lifecycle hooks that should be run on state
|
|
|
|
// transistions.
|
|
|
|
runnerHooks []interfaces.RunnerHook
|
|
|
|
|
|
|
|
// tasks are the set of task runners
|
2018-07-19 00:06:44 +00:00
|
|
|
tasks map[string]*taskrunner.TaskRunner
|
|
|
|
tasksLock sync.RWMutex
|
2018-06-28 00:27:03 +00:00
|
|
|
|
2018-08-23 19:03:17 +00:00
|
|
|
// allocBroadcaster sends client allocation updates to all listeners
|
|
|
|
allocBroadcaster *cstructs.AllocBroadcaster
|
|
|
|
|
|
|
|
// prevAllocWatcher allows waiting for a previous allocation to exit
|
|
|
|
// and if necessary migrate its alloc dir.
|
|
|
|
prevAllocWatcher allocwatcher.PrevAllocWatcher
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAllocRunner returns a new allocation runner.
|
2018-07-13 00:56:52 +00:00
|
|
|
func NewAllocRunner(config *Config) (*allocRunner, error) {
|
|
|
|
alloc := config.Alloc
|
|
|
|
tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
|
|
|
|
if tg == nil {
|
|
|
|
return nil, fmt.Errorf("failed to lookup task group %q", alloc.TaskGroup)
|
|
|
|
}
|
|
|
|
|
2018-06-22 00:35:07 +00:00
|
|
|
ar := &allocRunner{
|
2018-08-23 19:03:17 +00:00
|
|
|
id: alloc.ID,
|
|
|
|
alloc: alloc,
|
|
|
|
clientConfig: config.ClientConfig,
|
|
|
|
consulClient: config.Consul,
|
|
|
|
vaultClient: config.Vault,
|
|
|
|
tasks: make(map[string]*taskrunner.TaskRunner, len(tg.Tasks)),
|
|
|
|
waitCh: make(chan struct{}),
|
|
|
|
state: &state.State{},
|
|
|
|
stateDB: config.StateDB,
|
|
|
|
stateUpdater: config.StateUpdater,
|
2018-08-30 21:33:50 +00:00
|
|
|
allocBroadcaster: cstructs.NewAllocBroadcaster(),
|
2018-08-23 19:03:17 +00:00
|
|
|
prevAllocWatcher: config.PrevAllocWatcher,
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the logger based on the allocation ID
|
2018-08-30 21:33:50 +00:00
|
|
|
ar.logger = config.Logger.Named("alloc_runner").With("alloc_id", alloc.ID)
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
// Create alloc dir
|
|
|
|
ar.allocDir = allocdir.NewAllocDir(ar.logger, filepath.Join(config.ClientConfig.AllocDir, alloc.ID))
|
|
|
|
|
2018-06-22 00:35:07 +00:00
|
|
|
// Initialize the runners hooks.
|
|
|
|
ar.initRunnerHooks()
|
|
|
|
|
2018-07-13 00:56:52 +00:00
|
|
|
// Create the TaskRunners
|
|
|
|
if err := ar.initTaskRunners(tg.Tasks); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ar, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// initTaskRunners creates task runners but does *not* run them.
|
|
|
|
func (ar *allocRunner) initTaskRunners(tasks []*structs.Task) error {
|
|
|
|
for _, task := range tasks {
|
|
|
|
config := &taskrunner.Config{
|
|
|
|
Alloc: ar.alloc,
|
|
|
|
ClientConfig: ar.clientConfig,
|
|
|
|
Task: task,
|
|
|
|
TaskDir: ar.allocDir.NewTaskDir(task.Name),
|
|
|
|
Logger: ar.logger,
|
|
|
|
StateDB: ar.stateDB,
|
2018-07-18 20:45:55 +00:00
|
|
|
StateUpdater: ar,
|
2018-07-20 00:40:25 +00:00
|
|
|
Consul: ar.consulClient,
|
2018-07-13 00:56:52 +00:00
|
|
|
VaultClient: ar.vaultClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create, but do not Run, the task runner
|
|
|
|
tr, err := taskrunner.NewTaskRunner(config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed creating runner for task %q: %v", task.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ar.tasks[task.Name] = tr
|
|
|
|
}
|
|
|
|
return nil
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ar *allocRunner) WaitCh() <-chan struct{} {
|
|
|
|
return ar.waitCh
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX How does alloc Restart work
|
2018-08-23 19:03:17 +00:00
|
|
|
// Run is the main goroutine that executes all the tasks.
|
2018-06-22 00:35:07 +00:00
|
|
|
func (ar *allocRunner) Run() {
|
|
|
|
// Close the wait channel
|
|
|
|
defer close(ar.waitCh)
|
|
|
|
|
2018-06-28 00:27:03 +00:00
|
|
|
var taskWaitCh <-chan struct{}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
// Run the prestart hooks
|
|
|
|
// XXX Equivalent to TR.Prestart hook
|
2018-06-22 00:35:07 +00:00
|
|
|
if err := ar.prerun(); err != nil {
|
|
|
|
ar.logger.Error("prerun failed", "error", err)
|
|
|
|
goto POST
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the runners
|
2018-07-13 00:56:52 +00:00
|
|
|
taskWaitCh = ar.runImpl()
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2018-07-17 20:57:57 +00:00
|
|
|
MAIN:
|
2018-06-28 00:27:03 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-taskWaitCh:
|
|
|
|
// TaskRunners have all exited
|
2018-07-17 20:57:57 +00:00
|
|
|
break MAIN
|
2018-06-28 00:27:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 00:35:07 +00:00
|
|
|
POST:
|
|
|
|
// Run the postrun hooks
|
|
|
|
// XXX Equivalent to TR.Poststop hook
|
|
|
|
if err := ar.postrun(); err != nil {
|
|
|
|
ar.logger.Error("postrun failed", "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// runImpl is used to run the runners.
|
2018-07-13 00:56:52 +00:00
|
|
|
func (ar *allocRunner) runImpl() <-chan struct{} {
|
|
|
|
for _, task := range ar.tasks {
|
|
|
|
go task.Run()
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 00:27:03 +00:00
|
|
|
// Return a combined WaitCh that is closed when all task runners have
|
|
|
|
// exited.
|
|
|
|
waitCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(waitCh)
|
|
|
|
for _, task := range ar.tasks {
|
2018-06-22 00:35:07 +00:00
|
|
|
<-task.WaitCh()
|
2018-06-28 00:27:03 +00:00
|
|
|
}
|
|
|
|
}()
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2018-07-13 00:56:52 +00:00
|
|
|
return waitCh
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
2018-06-28 00:27:03 +00:00
|
|
|
|
2018-09-20 00:32:50 +00:00
|
|
|
// Alloc returns the current allocation being run by this runner as sent by the
|
|
|
|
// server. This view of the allocation does not have updated task states.
|
2018-06-29 00:01:05 +00:00
|
|
|
func (ar *allocRunner) Alloc() *structs.Allocation {
|
|
|
|
ar.allocLock.RLock()
|
|
|
|
defer ar.allocLock.RUnlock()
|
|
|
|
return ar.alloc
|
|
|
|
}
|
|
|
|
|
2018-08-01 18:03:52 +00:00
|
|
|
func (ar *allocRunner) setAlloc(updated *structs.Allocation) {
|
|
|
|
ar.allocLock.Lock()
|
|
|
|
ar.alloc = updated
|
|
|
|
ar.allocLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:03:17 +00:00
|
|
|
// GetAllocDir returns the alloc dir which is safe for concurrent use.
|
|
|
|
func (ar *allocRunner) GetAllocDir() *allocdir.AllocDir {
|
|
|
|
return ar.allocDir
|
|
|
|
}
|
|
|
|
|
2018-07-13 00:56:52 +00:00
|
|
|
// Restore state from database. Must be called after NewAllocRunner but before
|
|
|
|
// Run.
|
|
|
|
func (ar *allocRunner) Restore() error {
|
2018-08-08 00:46:37 +00:00
|
|
|
// Restore task runners
|
|
|
|
for _, tr := range ar.tasks {
|
|
|
|
if err := tr.Restore(); err != nil {
|
|
|
|
return err
|
2018-07-13 00:56:52 +00:00
|
|
|
}
|
2018-08-08 00:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-06-29 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 17:48:01 +00:00
|
|
|
// TaskStateUpdated is called by TaskRunner when a task's state has been
|
|
|
|
// updated. This hook is used to compute changes to the alloc's ClientStatus
|
|
|
|
// and to update the server with the new state.
|
|
|
|
func (ar *allocRunner) TaskStateUpdated(taskName string, state *structs.TaskState) {
|
2018-07-19 00:06:44 +00:00
|
|
|
// If a task is dead, we potentially want to kill other tasks in the group
|
|
|
|
if state.State == structs.TaskStateDead {
|
|
|
|
// Find all tasks that are not the one that is dead and check if the one
|
|
|
|
// that is dead is a leader
|
|
|
|
var otherTaskRunners []*taskrunner.TaskRunner
|
|
|
|
var otherTaskNames []string
|
|
|
|
leader := false
|
|
|
|
for name, tr := range ar.tasks {
|
|
|
|
if name != taskName {
|
|
|
|
otherTaskRunners = append(otherTaskRunners, tr)
|
|
|
|
otherTaskNames = append(otherTaskNames, name)
|
|
|
|
} else if tr.Task().Leader {
|
|
|
|
leader = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the task failed, we should kill all the other tasks in the task group.
|
|
|
|
if state.Failed {
|
2018-07-19 17:49:46 +00:00
|
|
|
if len(otherTaskRunners) > 0 {
|
|
|
|
ar.logger.Debug("task failure, destroying all tasks", "failed_task", taskName, "destroying", otherTaskNames)
|
|
|
|
}
|
2018-07-19 00:06:44 +00:00
|
|
|
for _, tr := range otherTaskRunners {
|
|
|
|
tr.Kill(context.Background(), structs.NewTaskEvent(structs.TaskSiblingFailed).SetFailedSibling(taskName))
|
|
|
|
}
|
2018-07-19 17:49:46 +00:00
|
|
|
} else if leader {
|
2018-07-19 00:06:44 +00:00
|
|
|
if len(otherTaskRunners) > 0 {
|
2018-07-19 17:49:46 +00:00
|
|
|
ar.logger.Debug("leader task dead, destroying all tasks", "leader_task", taskName, "destroying", otherTaskNames)
|
2018-07-19 00:06:44 +00:00
|
|
|
}
|
|
|
|
// If the task was a leader task we should kill all the other tasks.
|
|
|
|
for _, tr := range otherTaskRunners {
|
|
|
|
tr.Kill(context.Background(), structs.NewTaskEvent(structs.TaskLeaderDead))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather the state of the other tasks
|
2018-09-20 00:32:50 +00:00
|
|
|
ar.tasksLock.RLock()
|
2018-07-19 00:06:44 +00:00
|
|
|
states := make(map[string]*structs.TaskState, len(ar.tasks))
|
|
|
|
for name, tr := range ar.tasks {
|
|
|
|
if name == taskName {
|
|
|
|
states[name] = state
|
|
|
|
} else {
|
|
|
|
states[name] = tr.TaskState()
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 00:32:50 +00:00
|
|
|
ar.tasksLock.RUnlock()
|
2018-07-19 00:06:44 +00:00
|
|
|
|
|
|
|
// Get the client allocation
|
|
|
|
calloc := ar.clientAlloc(states)
|
|
|
|
|
|
|
|
// Update the server
|
2018-08-17 17:34:44 +00:00
|
|
|
ar.stateUpdater.AllocStateUpdated(calloc)
|
2018-08-23 19:03:17 +00:00
|
|
|
|
|
|
|
// Broadcast client alloc to listeners
|
|
|
|
ar.allocBroadcaster.Send(calloc)
|
2018-07-18 20:45:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 00:06:44 +00:00
|
|
|
// clientAlloc takes in the task states and returns an Allocation populated
|
|
|
|
// with Client specific fields
|
|
|
|
func (ar *allocRunner) clientAlloc(taskStates map[string]*structs.TaskState) *structs.Allocation {
|
|
|
|
ar.stateLock.RLock()
|
|
|
|
defer ar.stateLock.RUnlock()
|
|
|
|
|
2018-09-27 00:08:43 +00:00
|
|
|
// store task states for AllocState to expose
|
|
|
|
ar.state.TaskStates = taskStates
|
|
|
|
|
2018-07-19 00:06:44 +00:00
|
|
|
a := &structs.Allocation{
|
|
|
|
ID: ar.id,
|
|
|
|
TaskStates: taskStates,
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:08:43 +00:00
|
|
|
if d := ar.state.DeploymentStatus; d != nil {
|
2018-07-19 00:06:44 +00:00
|
|
|
a.DeploymentStatus = d.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the ClientStatus
|
2018-09-27 00:08:43 +00:00
|
|
|
if ar.state.ClientStatus != "" {
|
2018-07-19 00:06:44 +00:00
|
|
|
// The client status is being forced
|
2018-09-27 00:08:43 +00:00
|
|
|
a.ClientStatus, a.ClientDescription = ar.state.ClientStatus, ar.state.ClientDescription
|
2018-07-19 00:06:44 +00:00
|
|
|
} else {
|
|
|
|
a.ClientStatus, a.ClientDescription = getClientStatus(taskStates)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the allocation is terminal, make sure all required fields are properly
|
|
|
|
// set.
|
|
|
|
if a.ClientTerminalStatus() {
|
|
|
|
alloc := ar.Alloc()
|
|
|
|
|
|
|
|
// If we are part of a deployment and the task has failed, mark the
|
|
|
|
// alloc as unhealthy. This guards against the watcher not be started.
|
|
|
|
if a.ClientStatus == structs.AllocClientStatusFailed &&
|
|
|
|
alloc.DeploymentID != "" && !a.DeploymentStatus.IsUnhealthy() {
|
|
|
|
a.DeploymentStatus = &structs.AllocDeploymentStatus{
|
|
|
|
Healthy: helper.BoolToPtr(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we have marked the finished at for every task. This is used
|
|
|
|
// to calculate the reschedule time for failed allocations.
|
|
|
|
now := time.Now()
|
|
|
|
for _, task := range alloc.Job.LookupTaskGroup(alloc.TaskGroup).Tasks {
|
|
|
|
ts, ok := a.TaskStates[task.Name]
|
|
|
|
if !ok {
|
|
|
|
ts = &structs.TaskState{}
|
|
|
|
a.TaskStates[task.Name] = ts
|
|
|
|
}
|
|
|
|
if ts.FinishedAt.IsZero() {
|
|
|
|
ts.FinishedAt = now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// getClientStatus takes in the task states for a given allocation and computes
|
|
|
|
// the client status and description
|
|
|
|
func getClientStatus(taskStates map[string]*structs.TaskState) (status, description string) {
|
|
|
|
var pending, running, dead, failed bool
|
|
|
|
for _, state := range taskStates {
|
|
|
|
switch state.State {
|
|
|
|
case structs.TaskStateRunning:
|
|
|
|
running = true
|
|
|
|
case structs.TaskStatePending:
|
|
|
|
pending = true
|
|
|
|
case structs.TaskStateDead:
|
|
|
|
if state.Failed {
|
|
|
|
failed = true
|
|
|
|
} else {
|
|
|
|
dead = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the alloc status
|
|
|
|
if failed {
|
|
|
|
return structs.AllocClientStatusFailed, "Failed tasks"
|
|
|
|
} else if running {
|
|
|
|
return structs.AllocClientStatusRunning, "Tasks are running"
|
|
|
|
} else if pending {
|
|
|
|
return structs.AllocClientStatusPending, "No tasks have started"
|
|
|
|
} else if dead {
|
|
|
|
return structs.AllocClientStatusComplete, "All tasks have completed"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:08:43 +00:00
|
|
|
// AllocState returns a copy of allocation state including a snapshot of task
|
|
|
|
// states.
|
|
|
|
func (ar *allocRunner) AllocState() *state.State {
|
2018-09-28 00:30:10 +00:00
|
|
|
ar.stateLock.RLock()
|
|
|
|
state := ar.state.Copy()
|
|
|
|
ar.stateLock.RUnlock()
|
2018-09-27 00:08:43 +00:00
|
|
|
|
|
|
|
// If TaskStateUpdated has not been called yet, ar.state.TaskStates
|
|
|
|
// won't be set as it is not the canonical source of TaskStates.
|
2018-09-28 00:30:10 +00:00
|
|
|
if len(state.TaskStates) == 0 {
|
2018-09-27 00:08:43 +00:00
|
|
|
ar.tasksLock.RLock()
|
|
|
|
ar.state.TaskStates = make(map[string]*structs.TaskState, len(ar.tasks))
|
|
|
|
for k, tr := range ar.tasks {
|
2018-09-28 00:30:10 +00:00
|
|
|
state.TaskStates[k] = tr.TaskState()
|
2018-09-27 00:08:43 +00:00
|
|
|
}
|
|
|
|
ar.tasksLock.RUnlock()
|
|
|
|
}
|
|
|
|
|
2018-09-28 00:30:10 +00:00
|
|
|
return state
|
2018-09-27 00:08:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 00:27:03 +00:00
|
|
|
// Update the running allocation with a new version received from the server.
|
|
|
|
//
|
2018-08-01 18:03:52 +00:00
|
|
|
// This method sends the updated alloc to Run for serially processing updates.
|
|
|
|
// If there is already a pending update it will be discarded and replaced by
|
|
|
|
// the latest update.
|
2018-06-28 00:27:03 +00:00
|
|
|
func (ar *allocRunner) Update(update *structs.Allocation) {
|
2018-08-17 17:34:44 +00:00
|
|
|
// Update ar.alloc
|
|
|
|
ar.setAlloc(update)
|
|
|
|
|
2018-08-30 21:33:50 +00:00
|
|
|
// Run hooks
|
|
|
|
if err := ar.update(update); err != nil {
|
|
|
|
ar.logger.Error("error running update hooks", "error", err)
|
|
|
|
}
|
2018-08-17 17:34:44 +00:00
|
|
|
|
|
|
|
// Update task runners
|
|
|
|
for _, tr := range ar.tasks {
|
|
|
|
tr.Update(update)
|
2018-08-01 18:03:52 +00:00
|
|
|
}
|
2018-06-28 00:27:03 +00:00
|
|
|
}
|
2018-06-29 00:01:05 +00:00
|
|
|
|
2018-08-23 19:03:17 +00:00
|
|
|
func (ar *allocRunner) Listener() *cstructs.AllocListener {
|
|
|
|
return ar.allocBroadcaster.Listen()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the alloc runner by synchronously stopping it if it is still running
|
|
|
|
// and cleaning up all of its resources.
|
2018-06-29 00:01:05 +00:00
|
|
|
//
|
2018-08-23 19:03:17 +00:00
|
|
|
// This method is safe for calling concurrently with Run() and will cause it to
|
|
|
|
// exit (thus closing WaitCh).
|
2018-06-29 00:01:05 +00:00
|
|
|
func (ar *allocRunner) Destroy() {
|
2018-08-23 19:03:17 +00:00
|
|
|
// Stop tasks
|
2018-09-15 00:08:26 +00:00
|
|
|
ar.tasksLock.RLock()
|
2018-08-23 19:03:17 +00:00
|
|
|
for name, tr := range ar.tasks {
|
|
|
|
err := tr.Kill(context.TODO(), structs.NewTaskEvent(structs.TaskKilled))
|
|
|
|
if err != nil {
|
|
|
|
if err == taskrunner.ErrTaskNotRunning {
|
|
|
|
ar.logger.Trace("task not running", "task_name", name)
|
|
|
|
} else {
|
|
|
|
ar.logger.Warn("failed to kill task", "error", err, "task_name", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-15 00:08:26 +00:00
|
|
|
ar.tasksLock.RUnlock()
|
2018-07-17 20:57:57 +00:00
|
|
|
|
2018-08-23 19:03:17 +00:00
|
|
|
// Wait for tasks to exit and postrun hooks to finish
|
|
|
|
<-ar.waitCh
|
|
|
|
|
|
|
|
// Run destroy hooks
|
|
|
|
if err := ar.destroy(); err != nil {
|
|
|
|
ar.logger.Warn("error running destroy hooks", "error", err)
|
2018-07-17 20:57:57 +00:00
|
|
|
}
|
2018-08-23 19:03:17 +00:00
|
|
|
|
|
|
|
// Cleanup state db
|
|
|
|
if err := ar.stateDB.DeleteAllocationBucket(ar.id); err != nil {
|
|
|
|
ar.logger.Warn("failed to delete allocation state", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark alloc as destroyed
|
|
|
|
ar.destroyedLock.Lock()
|
|
|
|
ar.destroyed = true
|
|
|
|
ar.destroyedLock.Unlock()
|
2018-06-29 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsDestroyed returns true if the alloc runner has been destroyed (stopped and
|
|
|
|
// garbage collected).
|
|
|
|
//
|
|
|
|
// This method is safe for calling concurrently with Run(). Callers must
|
|
|
|
// receive on WaitCh() to block until alloc runner has stopped and been
|
|
|
|
// destroyed.
|
|
|
|
func (ar *allocRunner) IsDestroyed() bool {
|
2018-08-23 19:03:17 +00:00
|
|
|
ar.destroyedLock.Lock()
|
|
|
|
defer ar.destroyedLock.Unlock()
|
|
|
|
return ar.destroyed
|
2018-06-29 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsWaiting returns true if the alloc runner is waiting for its previous
|
|
|
|
// allocation to terminate.
|
|
|
|
//
|
|
|
|
// This method is safe for calling concurrently with Run().
|
|
|
|
func (ar *allocRunner) IsWaiting() bool {
|
2018-08-23 19:03:17 +00:00
|
|
|
return ar.prevAllocWatcher.IsWaiting()
|
2018-06-29 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsMigrating returns true if the alloc runner is migrating data from its
|
|
|
|
// previous allocation.
|
|
|
|
//
|
|
|
|
// This method is safe for calling concurrently with Run().
|
|
|
|
func (ar *allocRunner) IsMigrating() bool {
|
2018-08-23 19:03:17 +00:00
|
|
|
return ar.prevAllocWatcher.IsMigrating()
|
2018-06-29 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 22:45:46 +00:00
|
|
|
func (ar *allocRunner) StatsReporter() interfaces.AllocStatsReporter {
|
2018-09-15 00:08:26 +00:00
|
|
|
return ar
|
2018-06-29 00:01:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 00:08:26 +00:00
|
|
|
// LatestAllocStats returns the latest stats for an allocation. If taskFilter
|
|
|
|
// is set, only stats for that task -- if it exists -- are returned.
|
|
|
|
func (ar *allocRunner) LatestAllocStats(taskFilter string) (*cstructs.AllocResourceUsage, error) {
|
|
|
|
ar.tasksLock.RLock()
|
|
|
|
defer ar.tasksLock.RUnlock()
|
|
|
|
|
|
|
|
astat := &cstructs.AllocResourceUsage{
|
|
|
|
Tasks: make(map[string]*cstructs.TaskResourceUsage, len(ar.tasks)),
|
|
|
|
ResourceUsage: &cstructs.ResourceUsage{
|
|
|
|
MemoryStats: &cstructs.MemoryStats{},
|
|
|
|
CpuStats: &cstructs.CpuStats{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tr := range ar.tasks {
|
|
|
|
if taskFilter != "" && taskFilter != name {
|
|
|
|
// Getting stats for a particular task and its not this one!
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if usage := tr.LatestResourceUsage(); usage != nil {
|
|
|
|
astat.Tasks[name] = usage
|
|
|
|
astat.ResourceUsage.Add(usage.ResourceUsage)
|
|
|
|
if usage.Timestamp > astat.Timestamp {
|
|
|
|
astat.Timestamp = usage.Timestamp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-29 00:01:05 +00:00
|
|
|
|
2018-09-15 00:08:26 +00:00
|
|
|
return astat, nil
|
2018-06-29 00:01:05 +00:00
|
|
|
}
|