2015-07-24 00:31:08 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
2022-02-02 16:59:53 +00:00
|
|
|
"context"
|
2015-10-23 17:22:17 +00:00
|
|
|
"errors"
|
2015-07-24 00:31:08 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2022-07-06 14:13:48 +00:00
|
|
|
"strconv"
|
2015-07-24 00:31:08 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-12-19 20:26:57 +00:00
|
|
|
metrics "github.com/armon/go-metrics"
|
2022-02-02 16:59:53 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2022-07-06 14:13:48 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/broker"
|
2017-09-29 16:58:48 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2018-08-18 00:26:43 +00:00
|
|
|
"github.com/hashicorp/nomad/lib/delayheap"
|
2015-07-24 00:31:08 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2015-08-16 17:55:55 +00:00
|
|
|
const (
|
|
|
|
// failedQueue is the queue we add Evaluations to once
|
|
|
|
// they've reached the deliveryLimit. This allows the leader to
|
|
|
|
// set the status to failed.
|
|
|
|
failedQueue = "_failed"
|
|
|
|
)
|
|
|
|
|
2015-10-23 17:22:17 +00:00
|
|
|
var (
|
|
|
|
// ErrNotOutstanding is returned if an evaluation is not outstanding
|
|
|
|
ErrNotOutstanding = errors.New("evaluation is not outstanding")
|
|
|
|
|
|
|
|
// ErrTokenMismatch is the outstanding eval has a different token
|
|
|
|
ErrTokenMismatch = errors.New("evaluation token does not match")
|
|
|
|
|
|
|
|
// ErrNackTimeoutReached is returned if an expired evaluation is reset
|
|
|
|
ErrNackTimeoutReached = errors.New("evaluation nack timeout reached")
|
|
|
|
)
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// EvalBroker is used to manage brokering of evaluations. When an evaluation is
|
|
|
|
// created, due to a change in a job specification or a node, we put it into the
|
|
|
|
// broker. The broker sorts by evaluations by priority and scheduler type. This
|
|
|
|
// allows us to dequeue the highest priority work first, while also allowing sub-schedulers
|
|
|
|
// to only dequeue work they know how to handle. The broker is designed to be entirely
|
|
|
|
// in-memory and is managed by the leader node.
|
|
|
|
//
|
|
|
|
// The broker must provide at-least-once delivery semantics. It relies on explicit
|
|
|
|
// Ack/Nack messages to handle this. If a delivery is not Ack'd in a sufficient time
|
|
|
|
// span, it will be assumed Nack'd.
|
|
|
|
type EvalBroker struct {
|
2015-08-16 17:55:55 +00:00
|
|
|
nackTimeout time.Duration
|
|
|
|
deliveryLimit int
|
2015-07-24 00:31:08 +00:00
|
|
|
|
2022-07-06 14:13:48 +00:00
|
|
|
enabled bool
|
|
|
|
enabledNotifier *broker.GenericNotifier
|
|
|
|
|
|
|
|
stats *BrokerStats
|
2015-07-24 00:31:08 +00:00
|
|
|
|
2015-08-16 17:55:55 +00:00
|
|
|
// evals tracks queued evaluations by ID to de-duplicate enqueue.
|
|
|
|
// The counter is the number of times we've attempted delivery,
|
|
|
|
// and is used to eventually fail an evaluation.
|
|
|
|
evals map[string]int
|
2015-08-06 00:06:02 +00:00
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
// jobEvals tracks queued evaluations by a job's ID and namespace to serialize them
|
|
|
|
jobEvals map[structs.NamespacedID]string
|
2015-08-06 00:55:15 +00:00
|
|
|
|
|
|
|
// blocked tracks the blocked evaluations by JobID in a priority queue
|
2022-11-16 21:10:11 +00:00
|
|
|
blocked map[structs.NamespacedID]BlockedEvaluations
|
|
|
|
|
|
|
|
// cancelable tracks previously blocked evaluations (for any job) that are
|
|
|
|
// now safe for the Eval.Ack RPC to cancel in batches
|
|
|
|
cancelable []*structs.Evaluation
|
2015-08-06 00:55:15 +00:00
|
|
|
|
|
|
|
// ready tracks the ready jobs by scheduler in a priority queue
|
|
|
|
ready map[string]PendingEvaluations
|
|
|
|
|
|
|
|
// unack is a map of evalID to an un-acknowledged evaluation
|
|
|
|
unack map[string]*unackEval
|
|
|
|
|
|
|
|
// waiting is used to notify on a per-scheduler basis of ready work
|
2015-07-24 00:31:08 +00:00
|
|
|
waiting map[string]chan struct{}
|
|
|
|
|
2016-05-27 23:53:10 +00:00
|
|
|
// requeue tracks evaluations that need to be re-enqueued once the current
|
|
|
|
// evaluation finishes by token. If the token is Nacked or rejected the
|
|
|
|
// evaluation is dropped but if Acked successfully, the evaluation is
|
|
|
|
// queued.
|
|
|
|
requeue map[string]*structs.Evaluation
|
|
|
|
|
2015-09-07 20:00:45 +00:00
|
|
|
// timeWait has evaluations that are waiting for time to elapse
|
|
|
|
timeWait map[string]*time.Timer
|
|
|
|
|
2018-03-02 00:28:02 +00:00
|
|
|
// delayedEvalCancelFunc is used to stop the long running go routine
|
|
|
|
// that processes delayed evaluations
|
|
|
|
delayedEvalCancelFunc context.CancelFunc
|
|
|
|
|
|
|
|
// delayHeap is a heap used to track incoming evaluations that are
|
|
|
|
// not eligible to enqueue until their WaitTime
|
2018-08-18 00:26:43 +00:00
|
|
|
delayHeap *delayheap.DelayHeap
|
2018-03-02 00:28:02 +00:00
|
|
|
|
|
|
|
// delayedEvalsUpdateCh is used to trigger notifications for updates
|
|
|
|
// to the delayHeap
|
|
|
|
delayedEvalsUpdateCh chan struct{}
|
|
|
|
|
|
|
|
// initialNackDelay is the delay applied before re-enqueuing a
|
2017-04-14 22:24:55 +00:00
|
|
|
// Nacked evaluation for the first time.
|
|
|
|
initialNackDelay time.Duration
|
|
|
|
|
|
|
|
// subsequentNackDelay is the delay applied before reenqueuing
|
|
|
|
// an evaluation that has been Nacked more than once. This delay is
|
|
|
|
// compounding after the first Nack.
|
2017-04-12 20:39:19 +00:00
|
|
|
subsequentNackDelay time.Duration
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
l sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// unackEval tracks an unacknowledged evaluation along with the Nack timer
|
|
|
|
type unackEval struct {
|
|
|
|
Eval *structs.Evaluation
|
2015-08-12 22:25:31 +00:00
|
|
|
Token string
|
2015-07-24 00:31:08 +00:00
|
|
|
NackTimer *time.Timer
|
|
|
|
}
|
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
// PendingEvaluations is a list of ready evaluations across multiple jobs. We
|
|
|
|
// implement the container/heap interface so that this is a priority queue.
|
2015-07-24 00:31:08 +00:00
|
|
|
type PendingEvaluations []*structs.Evaluation
|
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
// BlockedEvaluations is a list of blocked evaluations for a given job. We
|
|
|
|
// implement the container/heap interface so that this is a priority queue.
|
|
|
|
type BlockedEvaluations []*structs.Evaluation
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// NewEvalBroker creates a new evaluation broker. This is parameterized
|
|
|
|
// with the timeout used for messages that are not acknowledged before we
|
2015-08-16 17:55:55 +00:00
|
|
|
// assume a Nack and attempt to redeliver as well as the deliveryLimit
|
2017-04-14 22:26:54 +00:00
|
|
|
// which prevents a failing eval from being endlessly delivered. The
|
2018-03-11 18:01:35 +00:00
|
|
|
// initialNackDelay is the delay before making a Nacked evaluation available
|
2017-04-14 22:26:54 +00:00
|
|
|
// again for the first Nack and subsequentNackDelay is the compounding delay
|
|
|
|
// after the first Nack.
|
2017-04-14 22:24:55 +00:00
|
|
|
func NewEvalBroker(timeout, initialNackDelay, subsequentNackDelay time.Duration, deliveryLimit int) (*EvalBroker, error) {
|
2015-07-24 00:31:08 +00:00
|
|
|
if timeout < 0 {
|
|
|
|
return nil, fmt.Errorf("timeout cannot be negative")
|
|
|
|
}
|
|
|
|
b := &EvalBroker{
|
2018-03-02 00:28:02 +00:00
|
|
|
nackTimeout: timeout,
|
|
|
|
deliveryLimit: deliveryLimit,
|
|
|
|
enabled: false,
|
2022-07-06 14:13:48 +00:00
|
|
|
enabledNotifier: broker.NewGenericNotifier(),
|
2018-03-02 00:28:02 +00:00
|
|
|
stats: new(BrokerStats),
|
|
|
|
evals: make(map[string]int),
|
|
|
|
jobEvals: make(map[structs.NamespacedID]string),
|
2022-11-16 21:10:11 +00:00
|
|
|
blocked: make(map[structs.NamespacedID]BlockedEvaluations),
|
|
|
|
cancelable: make([]*structs.Evaluation, 0, structs.MaxUUIDsPerWriteRequest),
|
2018-03-02 00:28:02 +00:00
|
|
|
ready: make(map[string]PendingEvaluations),
|
|
|
|
unack: make(map[string]*unackEval),
|
|
|
|
waiting: make(map[string]chan struct{}),
|
|
|
|
requeue: make(map[string]*structs.Evaluation),
|
|
|
|
timeWait: make(map[string]*time.Timer),
|
|
|
|
initialNackDelay: initialNackDelay,
|
|
|
|
subsequentNackDelay: subsequentNackDelay,
|
2018-08-18 00:26:43 +00:00
|
|
|
delayHeap: delayheap.NewDelayHeap(),
|
2018-03-02 00:28:02 +00:00
|
|
|
delayedEvalsUpdateCh: make(chan struct{}, 1),
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
b.stats.ByScheduler = make(map[string]*SchedulerStats)
|
2022-01-06 20:57:40 +00:00
|
|
|
b.stats.DelayedEvals = make(map[string]*structs.Evaluation)
|
2018-03-02 00:28:02 +00:00
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2015-08-05 23:41:39 +00:00
|
|
|
// Enabled is used to check if the broker is enabled.
|
|
|
|
func (b *EvalBroker) Enabled() bool {
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
return b.enabled
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// SetEnabled is used to control if the broker is enabled. The broker
|
|
|
|
// should only be enabled on the active leader.
|
|
|
|
func (b *EvalBroker) SetEnabled(enabled bool) {
|
|
|
|
b.l.Lock()
|
2019-05-14 11:26:56 +00:00
|
|
|
defer b.l.Unlock()
|
|
|
|
|
2018-03-06 03:46:54 +00:00
|
|
|
prevEnabled := b.enabled
|
2015-07-24 00:31:08 +00:00
|
|
|
b.enabled = enabled
|
2018-03-06 03:46:54 +00:00
|
|
|
if !prevEnabled && enabled {
|
|
|
|
// start the go routine for delayed evals
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
b.delayedEvalCancelFunc = cancel
|
2018-12-19 20:26:57 +00:00
|
|
|
go b.runDelayedEvalsWatcher(ctx, b.delayedEvalsUpdateCh)
|
2018-03-06 03:46:54 +00:00
|
|
|
}
|
2019-05-14 11:26:56 +00:00
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
if !enabled {
|
2018-03-06 03:46:54 +00:00
|
|
|
b.flush()
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
2022-07-06 14:13:48 +00:00
|
|
|
|
|
|
|
// Notify all subscribers to state changes of the broker enabled value.
|
|
|
|
b.enabledNotifier.Notify("eval broker enabled status changed to " + strconv.FormatBool(enabled))
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 17:14:34 +00:00
|
|
|
// Enqueue is used to enqueue a new evaluation
|
|
|
|
func (b *EvalBroker) Enqueue(eval *structs.Evaluation) {
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
b.processEnqueue(eval, "")
|
|
|
|
}
|
|
|
|
|
2016-06-24 05:40:22 +00:00
|
|
|
// EnqueueAll is used to enqueue many evaluations. The map allows evaluations
|
|
|
|
// that are being re-enqueued to include their token.
|
2016-06-24 17:14:34 +00:00
|
|
|
//
|
2018-03-11 18:43:05 +00:00
|
|
|
// When requeuing an evaluation that potentially may be already
|
2016-06-24 17:14:34 +00:00
|
|
|
// enqueued. The evaluation is handled in one of the following ways:
|
|
|
|
// * Evaluation not outstanding: Process as a normal Enqueue
|
|
|
|
// * Evaluation outstanding: Do not allow the evaluation to be dequeued til:
|
2022-08-16 14:06:30 +00:00
|
|
|
// - Ack received: Unblock the evaluation allowing it to be dequeued
|
|
|
|
// - Nack received: Drop the evaluation as it was created as a result of a
|
|
|
|
// scheduler run that was Nack'd
|
2016-06-24 05:40:22 +00:00
|
|
|
func (b *EvalBroker) EnqueueAll(evals map[*structs.Evaluation]string) {
|
2016-05-18 19:13:59 +00:00
|
|
|
// The lock needs to be held until all evaluations are enqueued. This is so
|
|
|
|
// that when Dequeue operations are unblocked they will pick the highest
|
|
|
|
// priority evaluations.
|
2015-07-24 00:31:08 +00:00
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
2016-06-24 05:40:22 +00:00
|
|
|
for eval, token := range evals {
|
|
|
|
b.processEnqueue(eval, token)
|
2016-05-18 19:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-06 00:06:02 +00:00
|
|
|
|
2016-05-27 23:53:10 +00:00
|
|
|
// processEnqueue deduplicates evals and either enqueue immediately or enforce
|
|
|
|
// the evals wait time. If the token is passed, and the evaluation ID is
|
2018-04-11 17:36:28 +00:00
|
|
|
// outstanding, the evaluation is blocked until an Ack/Nack is received.
|
2016-05-27 23:53:10 +00:00
|
|
|
// processEnqueue must be called with the lock held.
|
|
|
|
func (b *EvalBroker) processEnqueue(eval *structs.Evaluation, token string) {
|
2019-05-14 11:59:10 +00:00
|
|
|
// If we're not enabled, don't enable more queuing.
|
|
|
|
if !b.enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-06 00:06:02 +00:00
|
|
|
// Check if already enqueued
|
|
|
|
if _, ok := b.evals[eval.ID]; ok {
|
2016-05-27 23:53:10 +00:00
|
|
|
if token == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the token has been passed, the evaluation is being reblocked by
|
|
|
|
// the scheduler and should be processed once the outstanding evaluation
|
|
|
|
// is Acked or Nacked.
|
2016-05-31 18:39:03 +00:00
|
|
|
if unack, ok := b.unack[eval.ID]; ok && unack.Token == token {
|
|
|
|
b.requeue[token] = eval
|
|
|
|
}
|
2016-05-18 18:35:15 +00:00
|
|
|
return
|
2015-08-06 00:06:02 +00:00
|
|
|
} else if b.enabled {
|
2015-08-16 17:55:55 +00:00
|
|
|
b.evals[eval.ID] = 0
|
2015-08-06 00:06:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 20:00:45 +00:00
|
|
|
// Check if we need to enforce a wait
|
|
|
|
if eval.Wait > 0 {
|
2017-04-12 20:39:19 +00:00
|
|
|
b.processWaitingEnqueue(eval)
|
2016-05-18 18:35:15 +00:00
|
|
|
return
|
2015-09-07 20:00:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 00:28:02 +00:00
|
|
|
if !eval.WaitUntil.IsZero() {
|
|
|
|
b.delayHeap.Push(&evalWrapper{eval}, eval.WaitUntil)
|
2018-03-06 03:46:54 +00:00
|
|
|
b.stats.TotalWaiting += 1
|
2022-01-06 20:57:40 +00:00
|
|
|
b.stats.DelayedEvals[eval.ID] = eval
|
2018-03-02 00:28:02 +00:00
|
|
|
// Signal an update.
|
|
|
|
select {
|
|
|
|
case b.delayedEvalsUpdateCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-07 20:00:45 +00:00
|
|
|
b.enqueueLocked(eval, eval.Type)
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:39:19 +00:00
|
|
|
// processWaitingEnqueue waits the given duration on the evaluation before
|
2018-03-11 18:00:07 +00:00
|
|
|
// enqueuing.
|
2017-04-12 20:39:19 +00:00
|
|
|
func (b *EvalBroker) processWaitingEnqueue(eval *structs.Evaluation) {
|
|
|
|
timer := time.AfterFunc(eval.Wait, func() {
|
|
|
|
b.enqueueWaiting(eval)
|
|
|
|
})
|
|
|
|
b.timeWait[eval.ID] = timer
|
|
|
|
b.stats.TotalWaiting += 1
|
|
|
|
}
|
|
|
|
|
2015-09-07 20:00:45 +00:00
|
|
|
// enqueueWaiting is used to enqueue a waiting evaluation
|
|
|
|
func (b *EvalBroker) enqueueWaiting(eval *structs.Evaluation) {
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
2019-05-14 11:59:10 +00:00
|
|
|
|
2015-09-07 20:00:45 +00:00
|
|
|
delete(b.timeWait, eval.ID)
|
|
|
|
b.stats.TotalWaiting -= 1
|
2019-05-14 11:59:10 +00:00
|
|
|
|
2015-09-07 20:00:45 +00:00
|
|
|
b.enqueueLocked(eval, eval.Type)
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// enqueueLocked is used to enqueue with the lock held
|
2015-09-07 20:00:45 +00:00
|
|
|
func (b *EvalBroker) enqueueLocked(eval *structs.Evaluation, queue string) {
|
2015-07-24 00:31:08 +00:00
|
|
|
// Do nothing if not enabled
|
|
|
|
if !b.enabled {
|
2015-09-07 20:00:45 +00:00
|
|
|
return
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 00:55:15 +00:00
|
|
|
// Check if there is an evaluation for this JobID pending
|
2018-03-13 18:23:11 +00:00
|
|
|
namespacedID := structs.NamespacedID{
|
2017-09-07 23:56:15 +00:00
|
|
|
ID: eval.JobID,
|
|
|
|
Namespace: eval.Namespace,
|
|
|
|
}
|
2018-03-13 18:23:11 +00:00
|
|
|
pendingEval := b.jobEvals[namespacedID]
|
2015-08-06 00:55:15 +00:00
|
|
|
if pendingEval == "" {
|
2018-03-13 18:23:11 +00:00
|
|
|
b.jobEvals[namespacedID] = eval.ID
|
2015-08-06 00:55:15 +00:00
|
|
|
} else if pendingEval != eval.ID {
|
2018-03-13 18:23:11 +00:00
|
|
|
blocked := b.blocked[namespacedID]
|
2015-08-06 00:55:15 +00:00
|
|
|
heap.Push(&blocked, eval)
|
2018-03-13 18:23:11 +00:00
|
|
|
b.blocked[namespacedID] = blocked
|
2015-08-06 00:55:15 +00:00
|
|
|
b.stats.TotalBlocked += 1
|
2015-09-07 20:00:45 +00:00
|
|
|
return
|
2015-08-06 00:55:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// Find the pending by scheduler class
|
2015-08-16 17:55:55 +00:00
|
|
|
pending, ok := b.ready[queue]
|
2015-07-24 00:31:08 +00:00
|
|
|
if !ok {
|
2015-07-24 04:37:28 +00:00
|
|
|
pending = make([]*structs.Evaluation, 0, 16)
|
2015-08-16 17:55:55 +00:00
|
|
|
if _, ok := b.waiting[queue]; !ok {
|
|
|
|
b.waiting[queue] = make(chan struct{}, 1)
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push onto the heap
|
2015-07-24 04:37:28 +00:00
|
|
|
heap.Push(&pending, eval)
|
2015-08-16 17:55:55 +00:00
|
|
|
b.ready[queue] = pending
|
2015-07-24 00:31:08 +00:00
|
|
|
|
|
|
|
// Update the stats
|
|
|
|
b.stats.TotalReady += 1
|
2015-08-16 17:55:55 +00:00
|
|
|
bySched, ok := b.stats.ByScheduler[queue]
|
2015-07-24 00:31:08 +00:00
|
|
|
if !ok {
|
|
|
|
bySched = &SchedulerStats{}
|
2015-08-16 17:55:55 +00:00
|
|
|
b.stats.ByScheduler[queue] = bySched
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
bySched.Ready += 1
|
|
|
|
|
|
|
|
// Unblock any blocked dequeues
|
|
|
|
select {
|
2015-08-16 17:55:55 +00:00
|
|
|
case b.waiting[queue] <- struct{}{}:
|
2015-07-24 00:31:08 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 19:20:27 +00:00
|
|
|
// Dequeue is used to perform a blocking dequeue. The next available evalution
|
|
|
|
// is returned as well as a unique token identifier for this dequeue. The token
|
|
|
|
// changes on leadership election to ensure a Dequeue prior to a leadership
|
|
|
|
// election cannot conflict with a Dequeue of the same evaluation after a
|
|
|
|
// leadership election.
|
2015-08-12 22:25:31 +00:00
|
|
|
func (b *EvalBroker) Dequeue(schedulers []string, timeout time.Duration) (*structs.Evaluation, string, error) {
|
2015-07-24 00:31:08 +00:00
|
|
|
var timeoutTimer *time.Timer
|
2015-11-23 19:59:49 +00:00
|
|
|
var timeoutCh <-chan time.Time
|
2015-07-24 00:31:08 +00:00
|
|
|
SCAN:
|
|
|
|
// Scan for work
|
2015-08-12 22:25:31 +00:00
|
|
|
eval, token, err := b.scanForSchedulers(schedulers)
|
2015-07-24 00:31:08 +00:00
|
|
|
if err != nil {
|
|
|
|
if timeoutTimer != nil {
|
|
|
|
timeoutTimer.Stop()
|
|
|
|
}
|
2015-08-12 22:25:31 +00:00
|
|
|
return nil, "", err
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have something
|
|
|
|
if eval != nil {
|
|
|
|
if timeoutTimer != nil {
|
|
|
|
timeoutTimer.Stop()
|
|
|
|
}
|
2015-08-12 22:25:31 +00:00
|
|
|
return eval, token, nil
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the timeout channel the first time around
|
2015-11-23 19:59:49 +00:00
|
|
|
if timeoutTimer == nil && timeout != 0 {
|
2015-07-24 00:31:08 +00:00
|
|
|
timeoutTimer = time.NewTimer(timeout)
|
2015-11-23 19:59:49 +00:00
|
|
|
timeoutCh = timeoutTimer.C
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Block until we get work
|
2015-11-23 19:59:49 +00:00
|
|
|
scan := b.waitForSchedulers(schedulers, timeoutCh)
|
2015-07-24 00:31:08 +00:00
|
|
|
if scan {
|
|
|
|
goto SCAN
|
|
|
|
}
|
2015-08-12 22:25:31 +00:00
|
|
|
return nil, "", nil
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// scanForSchedulers scans for work on any of the schedulers. The highest priority work
|
|
|
|
// is dequeued first. This may return nothing if there is no work waiting.
|
2015-08-12 22:25:31 +00:00
|
|
|
func (b *EvalBroker) scanForSchedulers(schedulers []string) (*structs.Evaluation, string, error) {
|
2015-07-24 00:31:08 +00:00
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
|
|
|
// Do nothing if not enabled
|
|
|
|
if !b.enabled {
|
2015-08-12 22:25:31 +00:00
|
|
|
return nil, "", fmt.Errorf("eval broker disabled")
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan for eligible work
|
|
|
|
var eligibleSched []string
|
|
|
|
var eligiblePriority int
|
|
|
|
for _, sched := range schedulers {
|
|
|
|
// Get the pending queue
|
|
|
|
pending, ok := b.ready[sched]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Peek at the next item
|
|
|
|
ready := pending.Peek()
|
|
|
|
if ready == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to eligible if equal or greater priority
|
|
|
|
if len(eligibleSched) == 0 || ready.Priority > eligiblePriority {
|
|
|
|
eligibleSched = []string{sched}
|
|
|
|
eligiblePriority = ready.Priority
|
|
|
|
|
|
|
|
} else if eligiblePriority > ready.Priority {
|
|
|
|
continue
|
|
|
|
|
|
|
|
} else if eligiblePriority == ready.Priority {
|
|
|
|
eligibleSched = append(eligibleSched, sched)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine behavior based on eligible work
|
|
|
|
switch n := len(eligibleSched); n {
|
|
|
|
case 0:
|
|
|
|
// No work to do!
|
2015-08-12 22:25:31 +00:00
|
|
|
return nil, "", nil
|
2015-07-24 00:31:08 +00:00
|
|
|
|
|
|
|
case 1:
|
|
|
|
// Only a single task, dequeue
|
|
|
|
return b.dequeueForSched(eligibleSched[0])
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Multiple tasks. We pick a random task so that we fairly
|
|
|
|
// distribute work.
|
2016-05-03 07:29:23 +00:00
|
|
|
offset := rand.Intn(n)
|
2015-07-24 00:31:08 +00:00
|
|
|
return b.dequeueForSched(eligibleSched[offset])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dequeueForSched is used to dequeue the next work item for a given scheduler.
|
|
|
|
// This assumes locks are held and that this scheduler has work
|
2015-08-12 22:25:31 +00:00
|
|
|
func (b *EvalBroker) dequeueForSched(sched string) (*structs.Evaluation, string, error) {
|
2015-07-24 00:31:08 +00:00
|
|
|
// Get the pending queue
|
|
|
|
pending := b.ready[sched]
|
2015-07-24 04:37:28 +00:00
|
|
|
raw := heap.Pop(&pending)
|
|
|
|
b.ready[sched] = pending
|
2015-07-24 00:31:08 +00:00
|
|
|
eval := raw.(*structs.Evaluation)
|
|
|
|
|
2015-08-12 22:25:31 +00:00
|
|
|
// Generate a UUID for the token
|
2017-09-29 16:58:48 +00:00
|
|
|
token := uuid.Generate()
|
2015-08-12 22:25:31 +00:00
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// Setup Nack timer
|
|
|
|
nackTimer := time.AfterFunc(b.nackTimeout, func() {
|
2015-08-12 22:25:31 +00:00
|
|
|
b.Nack(eval.ID, token)
|
2015-07-24 00:31:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Add to the unack queue
|
|
|
|
b.unack[eval.ID] = &unackEval{
|
|
|
|
Eval: eval,
|
2015-08-12 22:25:31 +00:00
|
|
|
Token: token,
|
2015-07-24 00:31:08 +00:00
|
|
|
NackTimer: nackTimer,
|
|
|
|
}
|
|
|
|
|
2015-08-16 17:55:55 +00:00
|
|
|
// Increment the dequeue count
|
|
|
|
b.evals[eval.ID] += 1
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// Update the stats
|
|
|
|
b.stats.TotalReady -= 1
|
|
|
|
b.stats.TotalUnacked += 1
|
2015-08-16 17:55:55 +00:00
|
|
|
bySched := b.stats.ByScheduler[sched]
|
2015-07-24 00:31:08 +00:00
|
|
|
bySched.Ready -= 1
|
|
|
|
bySched.Unacked += 1
|
|
|
|
|
2015-08-12 22:25:31 +00:00
|
|
|
return eval, token, nil
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// waitForSchedulers is used to wait for work on any of the scheduler or until a timeout.
|
|
|
|
// Returns if there is work waiting potentially.
|
|
|
|
func (b *EvalBroker) waitForSchedulers(schedulers []string, timeoutCh <-chan time.Time) bool {
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
readyCh := make(chan struct{}, 1)
|
|
|
|
defer close(doneCh)
|
|
|
|
|
|
|
|
// Start all the watchers
|
|
|
|
b.l.Lock()
|
|
|
|
for _, sched := range schedulers {
|
|
|
|
waitCh, ok := b.waiting[sched]
|
|
|
|
if !ok {
|
|
|
|
waitCh = make(chan struct{}, 1)
|
|
|
|
b.waiting[sched] = waitCh
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a goroutine that either waits for the waitCh on this scheduler
|
|
|
|
// to unblock or for this waitForSchedulers call to return
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-waitCh:
|
|
|
|
select {
|
|
|
|
case readyCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
case <-doneCh:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
b.l.Unlock()
|
|
|
|
|
|
|
|
// Block until we have ready work and should scan, or until we timeout
|
|
|
|
// and should not make an attempt to scan for work
|
|
|
|
select {
|
|
|
|
case <-readyCh:
|
|
|
|
return true
|
|
|
|
case <-timeoutCh:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:58:13 +00:00
|
|
|
// Outstanding checks if an EvalID has been delivered but not acknowledged
|
2015-08-12 22:25:31 +00:00
|
|
|
// and returns the associated token for the evaluation.
|
|
|
|
func (b *EvalBroker) Outstanding(evalID string) (string, bool) {
|
2015-07-24 04:58:13 +00:00
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
2015-08-12 22:25:31 +00:00
|
|
|
unack, ok := b.unack[evalID]
|
|
|
|
if !ok {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
return unack.Token, true
|
2015-07-24 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 17:14:16 +00:00
|
|
|
// OutstandingReset resets the Nack timer for the EvalID if the
|
|
|
|
// token matches and the eval is outstanding
|
2015-10-23 17:22:17 +00:00
|
|
|
func (b *EvalBroker) OutstandingReset(evalID, token string) error {
|
2015-10-23 17:14:16 +00:00
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
unack, ok := b.unack[evalID]
|
|
|
|
if !ok {
|
2015-10-23 17:22:17 +00:00
|
|
|
return ErrNotOutstanding
|
2015-10-23 17:14:16 +00:00
|
|
|
}
|
|
|
|
if unack.Token != token {
|
2015-10-23 17:22:17 +00:00
|
|
|
return ErrTokenMismatch
|
2015-10-23 17:14:16 +00:00
|
|
|
}
|
2015-10-23 17:22:17 +00:00
|
|
|
if !unack.NackTimer.Reset(b.nackTimeout) {
|
|
|
|
return ErrNackTimeoutReached
|
|
|
|
}
|
|
|
|
return nil
|
2015-10-23 17:14:16 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// Ack is used to positively acknowledge handling an evaluation
|
2015-08-12 22:25:31 +00:00
|
|
|
func (b *EvalBroker) Ack(evalID, token string) error {
|
2015-07-24 00:31:08 +00:00
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
2016-05-27 23:53:10 +00:00
|
|
|
// Always delete the requeued evaluation. Either the Ack is successful and
|
|
|
|
// we requeue it or it isn't and we want to remove it.
|
|
|
|
defer delete(b.requeue, token)
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// Lookup the unack'd eval
|
|
|
|
unack, ok := b.unack[evalID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Evaluation ID not found")
|
|
|
|
}
|
2015-08-12 22:25:31 +00:00
|
|
|
if unack.Token != token {
|
|
|
|
return fmt.Errorf("Token does not match for Evaluation ID")
|
|
|
|
}
|
2015-08-06 00:55:15 +00:00
|
|
|
jobID := unack.Eval.JobID
|
2015-07-24 00:31:08 +00:00
|
|
|
|
|
|
|
// Ensure we were able to stop the timer
|
|
|
|
if !unack.NackTimer.Stop() {
|
|
|
|
return fmt.Errorf("Evaluation ID Ack'd after Nack timer expiration")
|
|
|
|
}
|
|
|
|
|
2015-08-16 17:55:55 +00:00
|
|
|
// Update the stats
|
|
|
|
b.stats.TotalUnacked -= 1
|
|
|
|
queue := unack.Eval.Type
|
2016-02-11 19:07:18 +00:00
|
|
|
if b.evals[evalID] > b.deliveryLimit {
|
2015-08-16 17:55:55 +00:00
|
|
|
queue = failedQueue
|
|
|
|
}
|
|
|
|
bySched := b.stats.ByScheduler[queue]
|
|
|
|
bySched.Unacked -= 1
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// Cleanup
|
|
|
|
delete(b.unack, evalID)
|
2015-08-06 00:06:02 +00:00
|
|
|
delete(b.evals, evalID)
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2018-03-13 18:23:11 +00:00
|
|
|
namespacedID := structs.NamespacedID{
|
2017-09-07 23:56:15 +00:00
|
|
|
ID: jobID,
|
|
|
|
Namespace: unack.Eval.Namespace,
|
|
|
|
}
|
2018-03-13 18:23:11 +00:00
|
|
|
delete(b.jobEvals, namespacedID)
|
2015-07-24 00:31:08 +00:00
|
|
|
|
2015-08-06 00:55:15 +00:00
|
|
|
// Check if there are any blocked evaluations
|
2018-03-13 18:23:11 +00:00
|
|
|
if blocked := b.blocked[namespacedID]; len(blocked) != 0 {
|
2022-11-16 21:10:11 +00:00
|
|
|
|
|
|
|
// Any blocked evaluations with ModifyIndexes older than the just-ack'd
|
|
|
|
// evaluation are no longer useful, so it's safe to drop them.
|
|
|
|
cancelable := blocked.MarkForCancel()
|
|
|
|
b.cancelable = append(b.cancelable, cancelable...)
|
|
|
|
b.stats.TotalCancelable = len(b.cancelable)
|
|
|
|
b.stats.TotalBlocked -= len(cancelable)
|
|
|
|
|
|
|
|
// If any remain, enqueue an eval
|
|
|
|
if len(blocked) > 0 {
|
|
|
|
raw := heap.Pop(&blocked)
|
|
|
|
eval := raw.(*structs.Evaluation)
|
|
|
|
b.stats.TotalBlocked -= 1
|
|
|
|
b.enqueueLocked(eval, eval.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up if there are no more after that
|
2015-08-06 00:55:15 +00:00
|
|
|
if len(blocked) > 0 {
|
2018-03-13 18:23:11 +00:00
|
|
|
b.blocked[namespacedID] = blocked
|
2015-08-06 00:55:15 +00:00
|
|
|
} else {
|
2018-03-13 18:23:11 +00:00
|
|
|
delete(b.blocked, namespacedID)
|
2015-08-06 00:55:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-27 23:53:10 +00:00
|
|
|
|
|
|
|
// Re-enqueue the evaluation.
|
|
|
|
if eval, ok := b.requeue[token]; ok {
|
|
|
|
b.processEnqueue(eval, "")
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nack is used to negatively acknowledge handling an evaluation
|
2015-08-12 22:25:31 +00:00
|
|
|
func (b *EvalBroker) Nack(evalID, token string) error {
|
2015-07-24 00:31:08 +00:00
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
|
2016-05-27 23:53:10 +00:00
|
|
|
// Always delete the requeued evaluation since the Nack means the requeue is
|
|
|
|
// invalid.
|
|
|
|
delete(b.requeue, token)
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// Lookup the unack'd eval
|
|
|
|
unack, ok := b.unack[evalID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Evaluation ID not found")
|
|
|
|
}
|
2015-08-12 22:25:31 +00:00
|
|
|
if unack.Token != token {
|
|
|
|
return fmt.Errorf("Token does not match for Evaluation ID")
|
|
|
|
}
|
2015-07-24 00:31:08 +00:00
|
|
|
|
|
|
|
// Stop the timer, doesn't matter if we've missed it
|
|
|
|
unack.NackTimer.Stop()
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
delete(b.unack, evalID)
|
|
|
|
|
|
|
|
// Update the stats
|
|
|
|
b.stats.TotalUnacked -= 1
|
|
|
|
bySched := b.stats.ByScheduler[unack.Eval.Type]
|
|
|
|
bySched.Unacked -= 1
|
|
|
|
|
2015-08-16 17:55:55 +00:00
|
|
|
// Check if we've hit the delivery limit, and re-enqueue
|
|
|
|
// in the failedQueue
|
2017-04-12 20:39:19 +00:00
|
|
|
if dequeues := b.evals[evalID]; dequeues >= b.deliveryLimit {
|
2015-09-07 20:00:45 +00:00
|
|
|
b.enqueueLocked(unack.Eval, failedQueue)
|
2015-08-16 17:55:55 +00:00
|
|
|
} else {
|
2017-04-12 20:39:19 +00:00
|
|
|
e := unack.Eval
|
|
|
|
e.Wait = b.nackReenqueueDelay(e, dequeues)
|
|
|
|
|
|
|
|
// See if there should be a delay before re-enqueuing
|
|
|
|
if e.Wait > 0 {
|
|
|
|
b.processWaitingEnqueue(e)
|
|
|
|
} else {
|
|
|
|
b.enqueueLocked(e, e.Type)
|
|
|
|
}
|
2015-08-16 17:55:55 +00:00
|
|
|
}
|
2017-04-12 20:39:19 +00:00
|
|
|
|
2015-09-07 20:00:45 +00:00
|
|
|
return nil
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 20:39:19 +00:00
|
|
|
// nackReenqueueDelay is used to determine the delay that should be applied on
|
|
|
|
// the evaluation given the number of previous attempts
|
|
|
|
func (b *EvalBroker) nackReenqueueDelay(eval *structs.Evaluation, prevDequeues int) time.Duration {
|
|
|
|
switch {
|
|
|
|
case prevDequeues <= 0:
|
2017-04-14 22:24:55 +00:00
|
|
|
return 0
|
2017-04-12 20:39:19 +00:00
|
|
|
case prevDequeues == 1:
|
2017-04-14 22:24:55 +00:00
|
|
|
return b.initialNackDelay
|
2017-04-12 20:39:19 +00:00
|
|
|
default:
|
|
|
|
// For each subsequent nack compound a delay
|
2017-04-14 22:24:55 +00:00
|
|
|
return time.Duration(prevDequeues-1) * b.subsequentNackDelay
|
2017-04-12 20:39:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 20:50:44 +00:00
|
|
|
// PauseNackTimeout is used to pause the Nack timeout for an eval that is making
|
|
|
|
// progress but is in a potentially unbounded operation such as the plan queue.
|
|
|
|
func (b *EvalBroker) PauseNackTimeout(evalID, token string) error {
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
unack, ok := b.unack[evalID]
|
|
|
|
if !ok {
|
|
|
|
return ErrNotOutstanding
|
|
|
|
}
|
|
|
|
if unack.Token != token {
|
|
|
|
return ErrTokenMismatch
|
|
|
|
}
|
|
|
|
if !unack.NackTimer.Stop() {
|
|
|
|
return ErrNackTimeoutReached
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResumeNackTimeout is used to resume the Nack timeout for an eval that was
|
|
|
|
// paused. It should be resumed after leaving an unbounded operation.
|
|
|
|
func (b *EvalBroker) ResumeNackTimeout(evalID, token string) error {
|
|
|
|
b.l.Lock()
|
|
|
|
defer b.l.Unlock()
|
|
|
|
unack, ok := b.unack[evalID]
|
|
|
|
if !ok {
|
|
|
|
return ErrNotOutstanding
|
|
|
|
}
|
|
|
|
if unack.Token != token {
|
|
|
|
return ErrTokenMismatch
|
|
|
|
}
|
|
|
|
unack.NackTimer.Reset(b.nackTimeout)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-14 11:26:56 +00:00
|
|
|
// Flush is used to clear the state of the broker. It must be called from within
|
|
|
|
// the lock.
|
2018-03-06 03:46:54 +00:00
|
|
|
func (b *EvalBroker) flush() {
|
2015-07-24 00:31:08 +00:00
|
|
|
// Unblock any waiters
|
|
|
|
for _, waitCh := range b.waiting {
|
|
|
|
close(waitCh)
|
|
|
|
}
|
|
|
|
b.waiting = make(map[string]chan struct{})
|
|
|
|
|
|
|
|
// Cancel any Nack timers
|
|
|
|
for _, unack := range b.unack {
|
|
|
|
unack.NackTimer.Stop()
|
|
|
|
}
|
|
|
|
|
2015-09-07 20:00:45 +00:00
|
|
|
// Cancel any time wait evals
|
|
|
|
for _, wait := range b.timeWait {
|
|
|
|
wait.Stop()
|
|
|
|
}
|
|
|
|
|
2018-03-02 00:28:02 +00:00
|
|
|
// Cancel the delayed evaluations goroutine
|
2018-03-12 19:32:43 +00:00
|
|
|
if b.delayedEvalCancelFunc != nil {
|
|
|
|
b.delayedEvalCancelFunc()
|
|
|
|
}
|
2018-03-02 00:28:02 +00:00
|
|
|
|
|
|
|
// Clear out the update channel for delayed evaluations
|
|
|
|
b.delayedEvalsUpdateCh = make(chan struct{}, 1)
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// Reset the broker
|
|
|
|
b.stats.TotalReady = 0
|
|
|
|
b.stats.TotalUnacked = 0
|
2015-08-06 00:55:15 +00:00
|
|
|
b.stats.TotalBlocked = 0
|
2015-09-07 20:00:45 +00:00
|
|
|
b.stats.TotalWaiting = 0
|
2022-11-16 21:10:11 +00:00
|
|
|
b.stats.TotalCancelable = 0
|
2022-01-06 20:57:40 +00:00
|
|
|
b.stats.DelayedEvals = make(map[string]*structs.Evaluation)
|
2015-07-24 00:31:08 +00:00
|
|
|
b.stats.ByScheduler = make(map[string]*SchedulerStats)
|
2015-08-16 17:55:55 +00:00
|
|
|
b.evals = make(map[string]int)
|
2017-09-07 23:56:15 +00:00
|
|
|
b.jobEvals = make(map[structs.NamespacedID]string)
|
2022-11-16 21:10:11 +00:00
|
|
|
b.blocked = make(map[structs.NamespacedID]BlockedEvaluations)
|
|
|
|
b.cancelable = make([]*structs.Evaluation, 0, structs.MaxUUIDsPerWriteRequest)
|
2015-07-24 00:31:08 +00:00
|
|
|
b.ready = make(map[string]PendingEvaluations)
|
|
|
|
b.unack = make(map[string]*unackEval)
|
2015-09-07 20:00:45 +00:00
|
|
|
b.timeWait = make(map[string]*time.Timer)
|
2018-08-18 00:26:43 +00:00
|
|
|
b.delayHeap = delayheap.NewDelayHeap()
|
2018-03-02 00:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// evalWrapper satisfies the HeapNode interface
|
|
|
|
type evalWrapper struct {
|
|
|
|
eval *structs.Evaluation
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *evalWrapper) Data() interface{} {
|
|
|
|
return d.eval
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *evalWrapper) ID() string {
|
|
|
|
return d.eval.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *evalWrapper) Namespace() string {
|
|
|
|
return d.eval.Namespace
|
|
|
|
}
|
|
|
|
|
2018-03-06 03:46:54 +00:00
|
|
|
// runDelayedEvalsWatcher is a long-lived function that waits till a time deadline is met for
|
|
|
|
// pending evaluations before enqueuing them
|
2018-12-19 20:26:57 +00:00
|
|
|
func (b *EvalBroker) runDelayedEvalsWatcher(ctx context.Context, updateCh <-chan struct{}) {
|
2018-03-02 00:28:02 +00:00
|
|
|
var timerChannel <-chan time.Time
|
2018-03-08 23:30:54 +00:00
|
|
|
var delayTimer *time.Timer
|
2018-03-06 03:46:54 +00:00
|
|
|
for {
|
2018-03-02 00:28:02 +00:00
|
|
|
eval, waitUntil := b.nextDelayedEval()
|
|
|
|
if waitUntil.IsZero() {
|
|
|
|
timerChannel = nil
|
|
|
|
} else {
|
|
|
|
launchDur := waitUntil.Sub(time.Now().UTC())
|
2018-03-12 19:32:43 +00:00
|
|
|
if delayTimer == nil {
|
2018-03-08 23:30:54 +00:00
|
|
|
delayTimer = time.NewTimer(launchDur)
|
|
|
|
} else {
|
|
|
|
delayTimer.Reset(launchDur)
|
|
|
|
}
|
|
|
|
timerChannel = delayTimer.C
|
2018-03-02 00:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-timerChannel:
|
|
|
|
// remove from the heap since we can enqueue it now
|
2018-03-06 03:46:54 +00:00
|
|
|
b.l.Lock()
|
2018-08-31 15:59:48 +00:00
|
|
|
b.delayHeap.Remove(&evalWrapper{eval})
|
2018-03-06 03:46:54 +00:00
|
|
|
b.stats.TotalWaiting -= 1
|
2022-01-06 20:57:40 +00:00
|
|
|
delete(b.stats.DelayedEvals, eval.ID)
|
2018-03-02 00:28:02 +00:00
|
|
|
b.enqueueLocked(eval, eval.Type)
|
2018-03-06 03:46:54 +00:00
|
|
|
b.l.Unlock()
|
2018-12-19 20:26:57 +00:00
|
|
|
case <-updateCh:
|
2018-03-02 00:28:02 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nextDelayedEval returns the next delayed eval to launch and when it should be enqueued.
|
2018-03-12 19:39:18 +00:00
|
|
|
// This peeks at the heap to return the top. If the heap is empty, this returns nil and zero time.
|
2018-03-02 00:28:02 +00:00
|
|
|
func (b *EvalBroker) nextDelayedEval() (*structs.Evaluation, time.Time) {
|
2018-09-04 16:45:05 +00:00
|
|
|
b.l.RLock()
|
2019-05-14 12:06:27 +00:00
|
|
|
defer b.l.RUnlock()
|
|
|
|
|
2018-03-02 00:28:02 +00:00
|
|
|
// If there is nothing wait for an update.
|
|
|
|
if b.delayHeap.Length() == 0 {
|
|
|
|
return nil, time.Time{}
|
|
|
|
}
|
|
|
|
nextEval := b.delayHeap.Peek()
|
|
|
|
if nextEval == nil {
|
|
|
|
return nil, time.Time{}
|
|
|
|
}
|
|
|
|
eval := nextEval.Node.Data().(*structs.Evaluation)
|
|
|
|
return eval, nextEval.WaitUntil
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stats is used to query the state of the broker
|
|
|
|
func (b *EvalBroker) Stats() *BrokerStats {
|
|
|
|
// Allocate a new stats struct
|
|
|
|
stats := new(BrokerStats)
|
2022-01-06 20:57:40 +00:00
|
|
|
stats.DelayedEvals = make(map[string]*structs.Evaluation)
|
2015-07-24 00:31:08 +00:00
|
|
|
stats.ByScheduler = make(map[string]*SchedulerStats)
|
|
|
|
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
|
|
|
|
// Copy all the stats
|
|
|
|
stats.TotalReady = b.stats.TotalReady
|
|
|
|
stats.TotalUnacked = b.stats.TotalUnacked
|
2015-08-06 00:55:15 +00:00
|
|
|
stats.TotalBlocked = b.stats.TotalBlocked
|
2015-09-07 20:00:45 +00:00
|
|
|
stats.TotalWaiting = b.stats.TotalWaiting
|
2022-11-16 21:10:11 +00:00
|
|
|
stats.TotalCancelable = b.stats.TotalCancelable
|
2022-01-06 20:57:40 +00:00
|
|
|
for id, eval := range b.stats.DelayedEvals {
|
|
|
|
evalCopy := *eval
|
|
|
|
stats.DelayedEvals[id] = &evalCopy
|
|
|
|
}
|
2015-07-24 00:31:08 +00:00
|
|
|
for sched, subStat := range b.stats.ByScheduler {
|
2022-01-06 20:57:40 +00:00
|
|
|
subStatCopy := *subStat
|
|
|
|
stats.ByScheduler[sched] = &subStatCopy
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
return stats
|
|
|
|
}
|
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
// Cancelable retrieves a batch of previously-blocked evaluations that are now
|
|
|
|
// stale and ready to mark for canceling. The eval RPC will call this with a
|
|
|
|
// batch size set to avoid sending overly large raft messages.
|
|
|
|
func (b *EvalBroker) Cancelable(batchSize int) []*structs.Evaluation {
|
|
|
|
b.l.RLock()
|
|
|
|
defer b.l.RUnlock()
|
|
|
|
|
|
|
|
if batchSize > len(b.cancelable) {
|
|
|
|
batchSize = len(b.cancelable)
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelable := b.cancelable[:batchSize]
|
|
|
|
b.cancelable = b.cancelable[batchSize:]
|
|
|
|
|
|
|
|
b.stats.TotalCancelable = len(b.cancelable)
|
|
|
|
return cancelable
|
|
|
|
}
|
|
|
|
|
2015-07-24 05:17:37 +00:00
|
|
|
// EmitStats is used to export metrics about the broker while enabled
|
2019-03-12 21:25:14 +00:00
|
|
|
func (b *EvalBroker) EmitStats(period time.Duration, stopCh <-chan struct{}) {
|
2022-02-02 16:59:53 +00:00
|
|
|
timer, stop := helper.NewSafeTimer(period)
|
|
|
|
defer stop()
|
|
|
|
|
2015-07-24 05:17:37 +00:00
|
|
|
for {
|
2022-02-02 16:59:53 +00:00
|
|
|
timer.Reset(period)
|
|
|
|
|
2015-08-05 23:45:50 +00:00
|
|
|
select {
|
2022-02-02 16:59:53 +00:00
|
|
|
case <-timer.C:
|
2015-08-05 23:45:50 +00:00
|
|
|
stats := b.Stats()
|
|
|
|
metrics.SetGauge([]string{"nomad", "broker", "total_ready"}, float32(stats.TotalReady))
|
|
|
|
metrics.SetGauge([]string{"nomad", "broker", "total_unacked"}, float32(stats.TotalUnacked))
|
2015-08-06 00:55:15 +00:00
|
|
|
metrics.SetGauge([]string{"nomad", "broker", "total_blocked"}, float32(stats.TotalBlocked))
|
2015-09-07 20:00:45 +00:00
|
|
|
metrics.SetGauge([]string{"nomad", "broker", "total_waiting"}, float32(stats.TotalWaiting))
|
2022-11-16 21:10:11 +00:00
|
|
|
metrics.SetGauge([]string{"nomad", "broker", "total_cancelable"}, float32(stats.TotalCancelable))
|
2022-01-06 20:57:40 +00:00
|
|
|
for _, eval := range stats.DelayedEvals {
|
|
|
|
metrics.SetGaugeWithLabels([]string{"nomad", "broker", "eval_waiting"},
|
|
|
|
float32(time.Until(eval.WaitUntil).Seconds()),
|
|
|
|
[]metrics.Label{
|
|
|
|
{Name: "eval_id", Value: eval.ID},
|
|
|
|
{Name: "job", Value: eval.JobID},
|
|
|
|
{Name: "namespace", Value: eval.Namespace},
|
|
|
|
})
|
|
|
|
}
|
2015-08-05 23:45:50 +00:00
|
|
|
for sched, schedStats := range stats.ByScheduler {
|
|
|
|
metrics.SetGauge([]string{"nomad", "broker", sched, "ready"}, float32(schedStats.Ready))
|
|
|
|
metrics.SetGauge([]string{"nomad", "broker", sched, "unacked"}, float32(schedStats.Unacked))
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
2015-07-24 05:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// BrokerStats returns all the stats about the broker
|
|
|
|
type BrokerStats struct {
|
2022-11-16 21:10:11 +00:00
|
|
|
TotalReady int
|
|
|
|
TotalUnacked int
|
|
|
|
TotalBlocked int
|
|
|
|
TotalWaiting int
|
|
|
|
TotalCancelable int
|
|
|
|
DelayedEvals map[string]*structs.Evaluation
|
|
|
|
ByScheduler map[string]*SchedulerStats
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SchedulerStats returns the stats per scheduler
|
|
|
|
type SchedulerStats struct {
|
|
|
|
Ready int
|
|
|
|
Unacked int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len is for the sorting interface
|
|
|
|
func (p PendingEvaluations) Len() int {
|
|
|
|
return len(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less is for the sorting interface. We flip the check
|
|
|
|
// so that the "min" in the min-heap is the element with the
|
|
|
|
// highest priority
|
|
|
|
func (p PendingEvaluations) Less(i, j int) bool {
|
2015-08-06 00:55:15 +00:00
|
|
|
if p[i].JobID != p[j].JobID && p[i].Priority != p[j].Priority {
|
2015-07-24 05:58:12 +00:00
|
|
|
return !(p[i].Priority < p[j].Priority)
|
|
|
|
}
|
|
|
|
return p[i].CreateIndex < p[j].CreateIndex
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Swap is for the sorting interface
|
|
|
|
func (p PendingEvaluations) Swap(i, j int) {
|
|
|
|
p[i], p[j] = p[j], p[i]
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:01:35 +00:00
|
|
|
// Push is used to add a new evaluation to the slice
|
2015-07-24 04:37:28 +00:00
|
|
|
func (p *PendingEvaluations) Push(e interface{}) {
|
|
|
|
*p = append(*p, e.(*structs.Evaluation))
|
2015-07-24 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pop is used to remove an evaluation from the slice
|
2015-07-24 04:37:28 +00:00
|
|
|
func (p *PendingEvaluations) Pop() interface{} {
|
|
|
|
n := len(*p)
|
|
|
|
e := (*p)[n-1]
|
|
|
|
(*p)[n-1] = nil
|
|
|
|
*p = (*p)[:n-1]
|
2015-07-24 00:31:08 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Peek is used to peek at the next element that would be popped
|
|
|
|
func (p PendingEvaluations) Peek() *structs.Evaluation {
|
|
|
|
n := len(p)
|
|
|
|
if n == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p[n-1]
|
|
|
|
}
|
2022-11-16 21:10:11 +00:00
|
|
|
|
|
|
|
// Len is for the sorting interface
|
|
|
|
func (p BlockedEvaluations) Len() int {
|
|
|
|
return len(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less is for the sorting interface. We flip the check
|
|
|
|
// so that the "min" in the min-heap is the element with the
|
|
|
|
// highest priority or highest modify index
|
|
|
|
func (p BlockedEvaluations) Less(i, j int) bool {
|
|
|
|
if p[i].Priority != p[j].Priority {
|
|
|
|
return !(p[i].Priority < p[j].Priority)
|
|
|
|
}
|
|
|
|
return !(p[i].ModifyIndex < p[j].ModifyIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap is for the sorting interface
|
|
|
|
func (p BlockedEvaluations) Swap(i, j int) {
|
|
|
|
p[i], p[j] = p[j], p[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push implements the heap interface and is used to add a new evaluation to the slice
|
|
|
|
func (p *BlockedEvaluations) Push(e interface{}) {
|
|
|
|
*p = append(*p, e.(*structs.Evaluation))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop implements the heap interface and is used to remove an evaluation from the slice
|
|
|
|
func (p *BlockedEvaluations) Pop() interface{} {
|
|
|
|
n := len(*p)
|
|
|
|
e := (*p)[n-1]
|
|
|
|
(*p)[n-1] = nil
|
|
|
|
*p = (*p)[:n-1]
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkForCancel is used to clear the blocked list of all but the one with the
|
|
|
|
// highest modify index and highest priority. It returns a slice of cancelable
|
|
|
|
// evals so that Eval.Ack RPCs can write batched raft entries to cancel
|
|
|
|
// them. This must be called inside the broker's lock.
|
|
|
|
func (p *BlockedEvaluations) MarkForCancel() []*structs.Evaluation {
|
|
|
|
|
|
|
|
// In pathological cases, we can have a large number of blocked evals but
|
|
|
|
// will want to cancel most of them. Using heap.Remove requires we re-sort
|
|
|
|
// for each eval we remove. Because we expect to have at most one remaining,
|
|
|
|
// we'll just create a new heap.
|
|
|
|
retain := BlockedEvaluations{(heap.Pop(p)).(*structs.Evaluation)}
|
|
|
|
|
|
|
|
cancelable := make([]*structs.Evaluation, len(*p))
|
|
|
|
copy(cancelable, *p)
|
|
|
|
|
|
|
|
*p = retain
|
|
|
|
return cancelable
|
|
|
|
}
|