2015-07-28 22:12:08 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2019-03-12 21:25:14 +00:00
|
|
|
"context"
|
2015-07-28 22:12:08 +00:00
|
|
|
"fmt"
|
2015-08-05 23:23:37 +00:00
|
|
|
"strings"
|
2015-08-23 17:52:31 +00:00
|
|
|
"sync"
|
2015-07-28 22:12:08 +00:00
|
|
|
"time"
|
|
|
|
|
2019-01-15 19:46:12 +00:00
|
|
|
metrics "github.com/armon/go-metrics"
|
2018-09-15 23:23:13 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2017-02-08 04:31:23 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2019-03-12 21:25:14 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2015-07-28 22:12:08 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-07-28 23:15:32 +00:00
|
|
|
"github.com/hashicorp/nomad/scheduler"
|
2015-07-28 22:12:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-08-24 00:39:49 +00:00
|
|
|
// backoffBaselineFast is the baseline time for exponential backoff
|
|
|
|
backoffBaselineFast = 20 * time.Millisecond
|
2015-07-28 22:12:08 +00:00
|
|
|
|
2015-08-24 00:39:49 +00:00
|
|
|
// backoffBaselineSlow is the baseline time for exponential backoff
|
|
|
|
// but that is much slower than backoffBaselineFast
|
|
|
|
backoffBaselineSlow = 500 * time.Millisecond
|
|
|
|
|
|
|
|
// backoffLimitSlow is the limit of the exponential backoff for
|
|
|
|
// the slower backoff
|
|
|
|
backoffLimitSlow = 10 * time.Second
|
2015-07-28 22:12:08 +00:00
|
|
|
|
2016-10-27 18:41:43 +00:00
|
|
|
// backoffSchedulerVersionMismatch is the backoff between retries when the
|
|
|
|
// scheduler version mismatches that of the leader.
|
|
|
|
backoffSchedulerVersionMismatch = 30 * time.Second
|
|
|
|
|
2015-07-28 22:12:08 +00:00
|
|
|
// dequeueTimeout is used to timeout an evaluation dequeue so that
|
|
|
|
// we can check if there is a shutdown event
|
|
|
|
dequeueTimeout = 500 * time.Millisecond
|
|
|
|
|
|
|
|
// raftSyncLimit is the limit of time we will wait for Raft replication
|
|
|
|
// to catch up to the evaluation. This is used to fast Nack and
|
|
|
|
// allow another scheduler to pick it up.
|
|
|
|
raftSyncLimit = 5 * time.Second
|
2015-09-07 03:18:47 +00:00
|
|
|
|
|
|
|
// dequeueErrGrace is the grace period where we don't log about
|
|
|
|
// dequeue errors after start. This is to improve the user experience
|
|
|
|
// in dev mode where the leader isn't elected for a few seconds.
|
|
|
|
dequeueErrGrace = 10 * time.Second
|
2015-07-28 22:12:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Worker is a single threaded scheduling worker. There may be multiple
|
|
|
|
// running per server (leader or follower). They are responsible for dequeuing
|
|
|
|
// pending evaluations, invoking schedulers, plan submission and the
|
|
|
|
// lifecycle around making task allocations. They bridge the business logic
|
|
|
|
// of the scheduler with the plumbing required to make it all work.
|
|
|
|
type Worker struct {
|
|
|
|
srv *Server
|
2018-09-15 23:23:13 +00:00
|
|
|
logger log.Logger
|
2015-09-07 03:18:47 +00:00
|
|
|
start time.Time
|
2015-07-28 22:12:08 +00:00
|
|
|
|
2015-08-23 17:52:31 +00:00
|
|
|
paused bool
|
|
|
|
pauseLock sync.Mutex
|
|
|
|
pauseCond *sync.Cond
|
|
|
|
|
2015-07-28 22:12:08 +00:00
|
|
|
failures uint
|
2015-08-12 22:44:36 +00:00
|
|
|
|
2016-05-23 22:24:31 +00:00
|
|
|
evalToken string
|
|
|
|
|
|
|
|
// snapshotIndex is the index of the snapshot in which the scheduler was
|
2018-03-11 18:00:32 +00:00
|
|
|
// first invoked. It is used to mark the SnapshotIndex of evaluations
|
2016-05-23 22:24:31 +00:00
|
|
|
// Created, Updated or Reblocked.
|
2016-05-21 01:07:10 +00:00
|
|
|
snapshotIndex uint64
|
2015-07-28 22:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWorker starts a new worker associated with the given server
|
|
|
|
func NewWorker(srv *Server) (*Worker, error) {
|
|
|
|
w := &Worker{
|
|
|
|
srv: srv,
|
2018-09-15 23:23:13 +00:00
|
|
|
logger: srv.logger.ResetNamed("worker"),
|
2015-09-07 03:18:47 +00:00
|
|
|
start: time.Now(),
|
2015-07-28 22:12:08 +00:00
|
|
|
}
|
2015-08-23 17:52:31 +00:00
|
|
|
w.pauseCond = sync.NewCond(&w.pauseLock)
|
2015-07-28 22:12:08 +00:00
|
|
|
go w.run()
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2015-08-23 17:52:31 +00:00
|
|
|
// SetPause is used to pause or unpause a worker
|
|
|
|
func (w *Worker) SetPause(p bool) {
|
|
|
|
w.pauseLock.Lock()
|
|
|
|
w.paused = p
|
|
|
|
w.pauseLock.Unlock()
|
|
|
|
if !p {
|
|
|
|
w.pauseCond.Broadcast()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkPaused is used to park the worker when paused
|
|
|
|
func (w *Worker) checkPaused() {
|
|
|
|
w.pauseLock.Lock()
|
|
|
|
for w.paused {
|
|
|
|
w.pauseCond.Wait()
|
|
|
|
}
|
|
|
|
w.pauseLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-07-28 22:12:08 +00:00
|
|
|
// run is the long-lived goroutine which is used to run the worker
|
|
|
|
func (w *Worker) run() {
|
|
|
|
for {
|
|
|
|
// Dequeue a pending evaluation
|
2017-09-13 20:47:01 +00:00
|
|
|
eval, token, waitIndex, shutdown := w.dequeueEvaluation(dequeueTimeout)
|
2015-07-28 22:12:08 +00:00
|
|
|
if shutdown {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a shutdown
|
|
|
|
if w.srv.IsShutdown() {
|
2018-09-25 17:47:45 +00:00
|
|
|
w.logger.Error("nacking eval because the server is shutting down", "eval", log.Fmt("%#v", eval))
|
2015-08-12 22:25:31 +00:00
|
|
|
w.sendAck(eval.ID, token, false)
|
2015-07-28 22:12:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-11 19:31:40 +00:00
|
|
|
// Wait for the raft log to catchup to the evaluation
|
2019-03-12 21:25:14 +00:00
|
|
|
snap, err := w.snapshotAfter(waitIndex, raftSyncLimit)
|
|
|
|
if err != nil {
|
2018-09-25 17:47:45 +00:00
|
|
|
w.logger.Error("error waiting for Raft index", "error", err, "index", waitIndex)
|
2015-08-12 22:25:31 +00:00
|
|
|
w.sendAck(eval.ID, token, false)
|
2015-07-28 22:12:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke the scheduler to determine placements
|
2019-03-12 21:25:14 +00:00
|
|
|
if err := w.invokeScheduler(snap, eval, token); err != nil {
|
2018-09-25 17:47:45 +00:00
|
|
|
w.logger.Error("error invoking scheduler", "error", err)
|
2015-08-12 22:25:31 +00:00
|
|
|
w.sendAck(eval.ID, token, false)
|
2015-07-28 22:12:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complete the evaluation
|
2015-08-12 22:25:31 +00:00
|
|
|
w.sendAck(eval.ID, token, true)
|
2015-07-28 22:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dequeueEvaluation is used to fetch the next ready evaluation.
|
|
|
|
// This blocks until an evaluation is available or a timeout is reached.
|
2017-09-13 20:47:01 +00:00
|
|
|
func (w *Worker) dequeueEvaluation(timeout time.Duration) (
|
|
|
|
eval *structs.Evaluation, token string, waitIndex uint64, shutdown bool) {
|
2015-07-28 22:12:08 +00:00
|
|
|
// Setup the request
|
|
|
|
req := structs.EvalDequeueRequest{
|
2016-10-26 21:52:48 +00:00
|
|
|
Schedulers: w.srv.config.EnabledSchedulers,
|
|
|
|
Timeout: timeout,
|
|
|
|
SchedulerVersion: scheduler.SchedulerVersion,
|
2015-07-28 22:12:08 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: w.srv.config.Region,
|
|
|
|
},
|
|
|
|
}
|
2015-08-12 22:25:31 +00:00
|
|
|
var resp structs.EvalDequeueResponse
|
2015-07-28 22:12:08 +00:00
|
|
|
|
|
|
|
REQ:
|
2015-08-23 17:52:31 +00:00
|
|
|
// Check if we are paused
|
|
|
|
w.checkPaused()
|
|
|
|
|
2015-07-28 22:12:08 +00:00
|
|
|
// Make a blocking RPC
|
2015-07-28 23:36:15 +00:00
|
|
|
start := time.Now()
|
2015-07-28 22:12:08 +00:00
|
|
|
err := w.srv.RPC("Eval.Dequeue", &req, &resp)
|
2015-07-28 23:36:15 +00:00
|
|
|
metrics.MeasureSince([]string{"nomad", "worker", "dequeue_eval"}, start)
|
2015-07-28 22:12:08 +00:00
|
|
|
if err != nil {
|
2015-09-07 03:18:47 +00:00
|
|
|
if time.Since(w.start) > dequeueErrGrace && !w.srv.IsShutdown() {
|
2018-09-15 23:23:13 +00:00
|
|
|
w.logger.Error("failed to dequeue evaluation", "error", err)
|
2015-09-07 03:18:47 +00:00
|
|
|
}
|
2016-10-27 18:41:43 +00:00
|
|
|
|
|
|
|
// Adjust the backoff based on the error. If it is a scheduler version
|
|
|
|
// mismatch we increase the baseline.
|
|
|
|
base, limit := backoffBaselineFast, backoffLimitSlow
|
|
|
|
if strings.Contains(err.Error(), "calling scheduler version") {
|
|
|
|
base = backoffSchedulerVersionMismatch
|
|
|
|
limit = backoffSchedulerVersionMismatch
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.backoffErr(base, limit) {
|
2017-09-13 20:47:01 +00:00
|
|
|
return nil, "", 0, true
|
2015-07-28 22:12:08 +00:00
|
|
|
}
|
|
|
|
goto REQ
|
|
|
|
}
|
|
|
|
w.backoffReset()
|
|
|
|
|
|
|
|
// Check if we got a response
|
|
|
|
if resp.Eval != nil {
|
2018-09-15 23:23:13 +00:00
|
|
|
w.logger.Debug("dequeued evaluation", "eval_id", resp.Eval.ID)
|
2017-09-13 20:47:01 +00:00
|
|
|
return resp.Eval, resp.Token, resp.GetWaitIndex(), false
|
2015-07-28 22:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for potential shutdown
|
|
|
|
if w.srv.IsShutdown() {
|
2017-09-13 20:47:01 +00:00
|
|
|
return nil, "", 0, true
|
2015-07-28 22:12:08 +00:00
|
|
|
}
|
|
|
|
goto REQ
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendAck makes a best effort to ack or nack the evaluation.
|
|
|
|
// Any errors are logged but swallowed.
|
2015-08-12 22:25:31 +00:00
|
|
|
func (w *Worker) sendAck(evalID, token string, ack bool) {
|
2015-07-28 23:36:15 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "worker", "send_ack"}, time.Now())
|
2015-07-28 22:12:08 +00:00
|
|
|
// Setup the request
|
2015-08-12 22:25:31 +00:00
|
|
|
req := structs.EvalAckRequest{
|
2015-07-28 22:12:08 +00:00
|
|
|
EvalID: evalID,
|
2015-08-12 22:25:31 +00:00
|
|
|
Token: token,
|
2015-07-28 22:12:08 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: w.srv.config.Region,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.GenericResponse
|
|
|
|
|
|
|
|
// Determine if this is an Ack or Nack
|
|
|
|
verb := "ack"
|
|
|
|
endpoint := "Eval.Ack"
|
|
|
|
if !ack {
|
|
|
|
verb = "nack"
|
|
|
|
endpoint = "Eval.Nack"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the RPC call
|
|
|
|
err := w.srv.RPC(endpoint, &req, &resp)
|
|
|
|
if err != nil {
|
2018-09-15 23:23:13 +00:00
|
|
|
w.logger.Error(fmt.Sprintf("failed to %s evaluation", verb), "eval_id", evalID, "error", err)
|
2015-07-28 23:36:15 +00:00
|
|
|
} else {
|
2018-09-15 23:23:13 +00:00
|
|
|
w.logger.Debug(fmt.Sprintf("%s evaluation", verb), "eval_id", evalID)
|
2015-07-28 22:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 21:25:14 +00:00
|
|
|
// snapshotAfter times calls to StateStore.SnapshotAfter which may block.
|
|
|
|
func (w *Worker) snapshotAfter(waitIndex uint64, timeout time.Duration) (*state.StateSnapshot, error) {
|
2015-07-28 22:12:08 +00:00
|
|
|
start := time.Now()
|
2019-03-12 21:25:14 +00:00
|
|
|
ctx, cancel := context.WithTimeout(w.srv.shutdownCtx, timeout)
|
|
|
|
snap, err := w.srv.fsm.State().SnapshotAfter(ctx, waitIndex)
|
|
|
|
cancel()
|
|
|
|
metrics.MeasureSince([]string{"nomad", "worker", "wait_for_index"}, start)
|
2019-05-17 21:37:42 +00:00
|
|
|
|
|
|
|
// Wrap error to ensure callers don't disregard timeouts.
|
|
|
|
if err == context.DeadlineExceeded {
|
|
|
|
err = fmt.Errorf("timed out after %s waiting for index=%d", timeout, waitIndex)
|
|
|
|
}
|
|
|
|
|
2019-03-12 21:25:14 +00:00
|
|
|
return snap, err
|
2015-07-28 22:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// invokeScheduler is used to invoke the business logic of the scheduler
|
2019-03-12 21:25:14 +00:00
|
|
|
func (w *Worker) invokeScheduler(snap *state.StateSnapshot, eval *structs.Evaluation, token string) error {
|
2015-08-07 00:45:27 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "worker", "invoke_scheduler", eval.Type}, time.Now())
|
2015-08-12 22:44:36 +00:00
|
|
|
// Store the evaluation token
|
|
|
|
w.evalToken = token
|
|
|
|
|
2016-05-21 01:07:10 +00:00
|
|
|
// Store the snapshot's index
|
2019-03-12 21:25:14 +00:00
|
|
|
var err error
|
2016-05-21 01:07:10 +00:00
|
|
|
w.snapshotIndex, err = snap.LatestIndex()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to determine snapshot's index: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-07 00:04:35 +00:00
|
|
|
// Create the scheduler, or use the special system scheduler
|
|
|
|
var sched scheduler.Scheduler
|
2015-08-15 19:38:58 +00:00
|
|
|
if eval.Type == structs.JobTypeCore {
|
|
|
|
sched = NewCoreScheduler(w.srv, snap)
|
2015-08-07 00:04:35 +00:00
|
|
|
} else {
|
2019-03-08 12:48:12 +00:00
|
|
|
sched, err = scheduler.NewScheduler(eval.Type, w.logger, snap, w)
|
2015-08-07 00:04:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to instantiate scheduler: %v", err)
|
|
|
|
}
|
2015-07-28 23:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process the evaluation
|
|
|
|
err = sched.Process(eval)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to process evaluation: %v", err)
|
|
|
|
}
|
2015-07-28 22:12:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-28 23:15:32 +00:00
|
|
|
// SubmitPlan is used to submit a plan for consideration. This allows
|
|
|
|
// the worker to act as the planner for the scheduler.
|
2015-07-28 23:36:15 +00:00
|
|
|
func (w *Worker) SubmitPlan(plan *structs.Plan) (*structs.PlanResult, scheduler.State, error) {
|
2015-07-28 23:52:13 +00:00
|
|
|
// Check for a shutdown before plan submission
|
|
|
|
if w.srv.IsShutdown() {
|
|
|
|
return nil, nil, fmt.Errorf("shutdown while planning")
|
|
|
|
}
|
2015-07-28 23:36:15 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "worker", "submit_plan"}, time.Now())
|
2015-07-28 23:52:13 +00:00
|
|
|
|
2015-08-12 22:44:36 +00:00
|
|
|
// Add the evaluation token to the plan
|
|
|
|
plan.EvalToken = w.evalToken
|
|
|
|
|
2019-03-04 09:49:32 +00:00
|
|
|
// Normalize stopped and preempted allocs before RPC
|
2019-03-08 12:48:12 +00:00
|
|
|
normalizePlan := ServersMeetMinimumVersion(w.srv.Members(), MinVersionPlanNormalization, true)
|
|
|
|
if normalizePlan {
|
|
|
|
plan.NormalizeAllocations()
|
|
|
|
}
|
2019-03-04 09:49:32 +00:00
|
|
|
|
2015-07-28 23:36:15 +00:00
|
|
|
// Setup the request
|
|
|
|
req := structs.PlanRequest{
|
|
|
|
Plan: plan,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: w.srv.config.Region,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.PlanResponse
|
|
|
|
|
2015-08-05 23:23:37 +00:00
|
|
|
SUBMIT:
|
2015-07-28 23:36:15 +00:00
|
|
|
// Make the RPC call
|
|
|
|
if err := w.srv.RPC("Plan.Submit", &req, &resp); err != nil {
|
2018-09-15 23:23:13 +00:00
|
|
|
w.logger.Error("failed to submit plan for evaluation", "eval_id", plan.EvalID, "error", err)
|
2015-08-24 00:39:49 +00:00
|
|
|
if w.shouldResubmit(err) && !w.backoffErr(backoffBaselineSlow, backoffLimitSlow) {
|
2015-08-05 23:23:37 +00:00
|
|
|
goto SUBMIT
|
|
|
|
}
|
2015-07-28 23:36:15 +00:00
|
|
|
return nil, nil, err
|
|
|
|
} else {
|
2018-09-21 23:55:35 +00:00
|
|
|
w.logger.Debug("submitted plan for evaluation", "eval_id", plan.EvalID)
|
2015-08-05 23:23:37 +00:00
|
|
|
w.backoffReset()
|
2015-07-28 23:36:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look for a result
|
|
|
|
result := resp.Result
|
|
|
|
if result == nil {
|
|
|
|
return nil, nil, fmt.Errorf("missing result")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a state update is required. This could be required if we
|
|
|
|
// planning based on stale data, which is causing issues. For example, a
|
|
|
|
// node failure since the time we've started planning or conflicting task
|
|
|
|
// allocations.
|
|
|
|
var state scheduler.State
|
|
|
|
if result.RefreshIndex != 0 {
|
2016-10-11 19:31:40 +00:00
|
|
|
// Wait for the raft log to catchup to the evaluation
|
2018-09-15 23:23:13 +00:00
|
|
|
w.logger.Debug("refreshing state", "refresh_index", result.RefreshIndex, "eval_id", plan.EvalID)
|
2015-07-28 23:36:15 +00:00
|
|
|
|
2019-03-12 21:25:14 +00:00
|
|
|
var err error
|
|
|
|
state, err = w.snapshotAfter(result.RefreshIndex, raftSyncLimit)
|
2015-07-28 23:36:15 +00:00
|
|
|
if err != nil {
|
2019-03-12 21:25:14 +00:00
|
|
|
return nil, nil, err
|
2015-07-28 23:36:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the result and potential state update
|
|
|
|
return result, state, nil
|
2015-07-28 23:15:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-15 21:25:00 +00:00
|
|
|
// UpdateEval is used to submit an updated evaluation. This allows
|
|
|
|
// the worker to act as the planner for the scheduler.
|
|
|
|
func (w *Worker) UpdateEval(eval *structs.Evaluation) error {
|
|
|
|
// Check for a shutdown before plan submission
|
|
|
|
if w.srv.IsShutdown() {
|
|
|
|
return fmt.Errorf("shutdown while planning")
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "worker", "update_eval"}, time.Now())
|
|
|
|
|
2016-05-21 01:07:10 +00:00
|
|
|
// Store the snapshot index in the eval
|
|
|
|
eval.SnapshotIndex = w.snapshotIndex
|
|
|
|
|
2015-08-15 21:25:00 +00:00
|
|
|
// Setup the request
|
|
|
|
req := structs.EvalUpdateRequest{
|
|
|
|
Evals: []*structs.Evaluation{eval},
|
|
|
|
EvalToken: w.evalToken,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: w.srv.config.Region,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.GenericResponse
|
|
|
|
|
|
|
|
SUBMIT:
|
|
|
|
// Make the RPC call
|
|
|
|
if err := w.srv.RPC("Eval.Update", &req, &resp); err != nil {
|
2018-09-15 23:23:13 +00:00
|
|
|
w.logger.Error("failed to update evaluation", "eval", log.Fmt("%#v", eval), "error", err)
|
2015-08-24 00:39:49 +00:00
|
|
|
if w.shouldResubmit(err) && !w.backoffErr(backoffBaselineSlow, backoffLimitSlow) {
|
2015-08-15 21:25:00 +00:00
|
|
|
goto SUBMIT
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
} else {
|
2018-09-15 23:23:13 +00:00
|
|
|
w.logger.Debug("updated evaluation", "eval", log.Fmt("%#v", eval))
|
2015-08-15 21:25:00 +00:00
|
|
|
w.backoffReset()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-07 21:23:48 +00:00
|
|
|
// CreateEval is used to create a new evaluation. This allows
|
|
|
|
// the worker to act as the planner for the scheduler.
|
|
|
|
func (w *Worker) CreateEval(eval *structs.Evaluation) error {
|
|
|
|
// Check for a shutdown before plan submission
|
|
|
|
if w.srv.IsShutdown() {
|
|
|
|
return fmt.Errorf("shutdown while planning")
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "worker", "create_eval"}, time.Now())
|
|
|
|
|
2016-05-21 01:07:10 +00:00
|
|
|
// Store the snapshot index in the eval
|
|
|
|
eval.SnapshotIndex = w.snapshotIndex
|
|
|
|
|
2015-09-07 21:23:48 +00:00
|
|
|
// Setup the request
|
|
|
|
req := structs.EvalUpdateRequest{
|
|
|
|
Evals: []*structs.Evaluation{eval},
|
|
|
|
EvalToken: w.evalToken,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: w.srv.config.Region,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.GenericResponse
|
|
|
|
|
|
|
|
SUBMIT:
|
|
|
|
// Make the RPC call
|
|
|
|
if err := w.srv.RPC("Eval.Create", &req, &resp); err != nil {
|
2018-09-15 23:23:13 +00:00
|
|
|
w.logger.Error("failed to create evaluation", "eval", log.Fmt("%#v", eval), "error", err)
|
2015-09-07 21:23:48 +00:00
|
|
|
if w.shouldResubmit(err) && !w.backoffErr(backoffBaselineSlow, backoffLimitSlow) {
|
|
|
|
goto SUBMIT
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
} else {
|
2018-09-15 23:23:13 +00:00
|
|
|
w.logger.Debug("created evaluation", "eval", log.Fmt("%#v", eval))
|
2015-09-07 21:23:48 +00:00
|
|
|
w.backoffReset()
|
2016-05-20 23:03:53 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReblockEval is used to reinsert a blocked evaluation into the blocked eval
|
|
|
|
// tracker. This allows the worker to act as the planner for the scheduler.
|
|
|
|
func (w *Worker) ReblockEval(eval *structs.Evaluation) error {
|
|
|
|
// Check for a shutdown before plan submission
|
|
|
|
if w.srv.IsShutdown() {
|
|
|
|
return fmt.Errorf("shutdown while planning")
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "worker", "reblock_eval"}, time.Now())
|
|
|
|
|
2016-07-20 21:09:03 +00:00
|
|
|
// Update the evaluation if the queued jobs is not same as what is
|
|
|
|
// recorded in the job summary
|
2017-02-08 04:31:23 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2017-09-07 23:56:15 +00:00
|
|
|
summary, err := w.srv.fsm.state.JobSummaryByID(ws, eval.Namespace, eval.JobID)
|
2016-07-20 21:09:03 +00:00
|
|
|
if err != nil {
|
2017-08-07 21:13:05 +00:00
|
|
|
return fmt.Errorf("couldn't retrieve job summary: %v", err)
|
2016-07-20 21:09:03 +00:00
|
|
|
}
|
|
|
|
if summary != nil {
|
|
|
|
var hasChanged bool
|
|
|
|
for tg, summary := range summary.Summary {
|
|
|
|
if queued, ok := eval.QueuedAllocations[tg]; ok {
|
2016-07-22 06:13:07 +00:00
|
|
|
if queued != summary.Queued {
|
|
|
|
hasChanged = true
|
|
|
|
break
|
|
|
|
}
|
2016-07-20 21:09:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if hasChanged {
|
|
|
|
if err := w.UpdateEval(eval); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-21 01:07:10 +00:00
|
|
|
// Store the snapshot index in the eval
|
|
|
|
eval.SnapshotIndex = w.snapshotIndex
|
2016-05-20 23:03:53 +00:00
|
|
|
|
|
|
|
// Setup the request
|
|
|
|
req := structs.EvalUpdateRequest{
|
|
|
|
Evals: []*structs.Evaluation{eval},
|
|
|
|
EvalToken: w.evalToken,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: w.srv.config.Region,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.GenericResponse
|
|
|
|
|
|
|
|
SUBMIT:
|
|
|
|
// Make the RPC call
|
|
|
|
if err := w.srv.RPC("Eval.Reblock", &req, &resp); err != nil {
|
2018-09-15 23:23:13 +00:00
|
|
|
w.logger.Error("failed to reblock evaluation", "eval", log.Fmt("%#v", eval), "error", err)
|
2016-05-20 23:03:53 +00:00
|
|
|
if w.shouldResubmit(err) && !w.backoffErr(backoffBaselineSlow, backoffLimitSlow) {
|
|
|
|
goto SUBMIT
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
} else {
|
2018-09-15 23:23:13 +00:00
|
|
|
w.logger.Debug("reblocked evaluation", "eval", log.Fmt("%#v", eval))
|
2016-05-20 23:03:53 +00:00
|
|
|
w.backoffReset()
|
2015-09-07 21:23:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-05 23:23:37 +00:00
|
|
|
// shouldResubmit checks if a given error should be swallowed and the plan
|
|
|
|
// resubmitted after a backoff. Usually these are transient errors that
|
|
|
|
// the cluster should heal from quickly.
|
|
|
|
func (w *Worker) shouldResubmit(err error) bool {
|
|
|
|
s := err.Error()
|
|
|
|
switch {
|
|
|
|
case strings.Contains(s, "No cluster leader"):
|
|
|
|
return true
|
|
|
|
case strings.Contains(s, "plan queue is disabled"):
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 22:12:08 +00:00
|
|
|
// backoffErr is used to do an exponential back off on error. This is
|
|
|
|
// maintained statefully for the worker. Returns if attempts should be
|
2018-03-11 17:34:16 +00:00
|
|
|
// abandoned due to shutdown.
|
2015-08-24 00:39:49 +00:00
|
|
|
func (w *Worker) backoffErr(base, limit time.Duration) bool {
|
|
|
|
backoff := (1 << (2 * w.failures)) * base
|
|
|
|
if backoff > limit {
|
|
|
|
backoff = limit
|
2015-07-28 22:12:08 +00:00
|
|
|
} else {
|
|
|
|
w.failures++
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(backoff):
|
|
|
|
return false
|
|
|
|
case <-w.srv.shutdownCh:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// backoffReset is used to reset the failure count for
|
|
|
|
// exponential backoff
|
|
|
|
func (w *Worker) backoffReset() {
|
|
|
|
w.failures = 0
|
|
|
|
}
|