2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-07-28 23:03:15 +00:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-09-15 23:23:13 +00:00
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
2015-07-28 23:03:15 +00:00
|
|
|
|
2019-01-15 19:46:12 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2022-03-07 18:40:57 +00:00
|
|
|
"github.com/hashicorp/go-version"
|
2017-10-13 21:36:02 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2015-07-28 23:03:15 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2016-10-26 21:52:48 +00:00
|
|
|
const (
|
|
|
|
// SchedulerVersion is the version of the scheduler. Changes to the
|
|
|
|
// scheduler that are incompatible with prior schedulers will increment this
|
|
|
|
// version. It is used to disallow dequeueing when the versions do not match
|
|
|
|
// across the leader and the dequeueing scheduler.
|
|
|
|
SchedulerVersion uint16 = 1
|
|
|
|
)
|
|
|
|
|
2015-07-28 23:03:15 +00:00
|
|
|
// BuiltinSchedulers contains the built in registered schedulers
|
|
|
|
// which are available
|
2015-08-07 00:25:14 +00:00
|
|
|
var BuiltinSchedulers = map[string]Factory{
|
2020-10-09 21:31:38 +00:00
|
|
|
"service": NewServiceScheduler,
|
|
|
|
"batch": NewBatchScheduler,
|
|
|
|
"system": NewSystemScheduler,
|
|
|
|
"sysbatch": NewSysBatchScheduler,
|
2015-08-07 00:25:14 +00:00
|
|
|
}
|
2015-07-28 23:03:15 +00:00
|
|
|
|
|
|
|
// NewScheduler is used to instantiate and return a new scheduler
|
|
|
|
// given the scheduler name, initial state, and planner.
|
2022-01-15 01:09:14 +00:00
|
|
|
func NewScheduler(name string, logger log.Logger, eventsCh chan<- interface{}, state State, planner Planner) (Scheduler, error) {
|
2015-07-28 23:03:15 +00:00
|
|
|
// Lookup the factory function
|
|
|
|
factory, ok := BuiltinSchedulers[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown scheduler '%s'", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiate the scheduler
|
2022-01-15 01:09:14 +00:00
|
|
|
sched := factory(logger, eventsCh, state, planner)
|
2015-07-28 23:03:15 +00:00
|
|
|
return sched, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Factory is used to instantiate a new Scheduler
|
2022-01-15 01:09:14 +00:00
|
|
|
type Factory func(log.Logger, chan<- interface{}, State, Planner) Scheduler
|
2015-07-28 23:03:15 +00:00
|
|
|
|
|
|
|
// Scheduler is the top level instance for a scheduler. A scheduler is
|
|
|
|
// meant to only encapsulate business logic, pushing the various plumbing
|
|
|
|
// into Nomad itself. They are invoked to process a single evaluation at
|
|
|
|
// a time. The evaluation may result in task allocations which are computed
|
|
|
|
// optimistically, as there are many concurrent evaluations being processed.
|
|
|
|
// The task allocations are submitted as a plan, and the current leader will
|
2018-03-11 17:47:45 +00:00
|
|
|
// coordinate the commits to prevent oversubscription or improper allocations
|
2015-07-28 23:03:15 +00:00
|
|
|
// based on stale state.
|
|
|
|
type Scheduler interface {
|
|
|
|
// Process is used to handle a new evaluation. The scheduler is free to
|
|
|
|
// apply any logic necessary to make the task placements. The state and
|
|
|
|
// planner will be provided prior to any invocations of process.
|
|
|
|
Process(*structs.Evaluation) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// State is an immutable view of the global state. This allows schedulers
|
|
|
|
// to make intelligent decisions based on allocations of other schedulers
|
|
|
|
// and to enforce complex constraints that require more information than
|
|
|
|
// is available to a local state scheduler.
|
|
|
|
type State interface {
|
2017-10-13 21:36:02 +00:00
|
|
|
// Config returns the configuration of the state store
|
|
|
|
Config() *state.StateStoreConfig
|
|
|
|
|
2015-07-28 23:03:15 +00:00
|
|
|
// Nodes returns an iterator over all the nodes.
|
|
|
|
// The type of each result is *structs.Node
|
2017-02-08 04:31:23 +00:00
|
|
|
Nodes(ws memdb.WatchSet) (memdb.ResultIterator, error)
|
2015-08-07 00:46:14 +00:00
|
|
|
|
2023-06-07 14:39:03 +00:00
|
|
|
// NodesByNodePool returns an iterator over all nodes in the node pool
|
|
|
|
NodesByNodePool(ws memdb.WatchSet, poolName string) (memdb.ResultIterator, error)
|
|
|
|
|
2015-08-07 00:46:14 +00:00
|
|
|
// AllocsByJob returns the allocations by JobID
|
2017-09-07 23:56:15 +00:00
|
|
|
AllocsByJob(ws memdb.WatchSet, namespace, jobID string, all bool) ([]*structs.Allocation, error)
|
2015-08-11 21:04:04 +00:00
|
|
|
|
2015-08-13 18:54:59 +00:00
|
|
|
// AllocsByNode returns all the allocations by node
|
2017-02-08 04:31:23 +00:00
|
|
|
AllocsByNode(ws memdb.WatchSet, node string) ([]*structs.Allocation, error)
|
2015-08-13 18:54:59 +00:00
|
|
|
|
2020-03-24 01:21:04 +00:00
|
|
|
// AllocByID returns the allocation
|
|
|
|
AllocByID(ws memdb.WatchSet, allocID string) (*structs.Allocation, error)
|
|
|
|
|
2016-02-20 19:29:15 +00:00
|
|
|
// AllocsByNodeTerminal returns all the allocations by node filtering by terminal status
|
2017-02-08 04:31:23 +00:00
|
|
|
AllocsByNodeTerminal(ws memdb.WatchSet, node string, terminal bool) ([]*structs.Allocation, error)
|
2016-02-20 19:29:15 +00:00
|
|
|
|
2015-08-11 21:04:04 +00:00
|
|
|
// GetNodeByID is used to lookup a node by ID
|
2017-02-08 04:31:23 +00:00
|
|
|
NodeByID(ws memdb.WatchSet, nodeID string) (*structs.Node, error)
|
2015-08-11 23:41:48 +00:00
|
|
|
|
|
|
|
// GetJobByID is used to lookup a job by ID
|
2017-09-07 23:56:15 +00:00
|
|
|
JobByID(ws memdb.WatchSet, namespace, id string) (*structs.Job, error)
|
2017-05-18 19:36:04 +00:00
|
|
|
|
2020-08-13 13:35:09 +00:00
|
|
|
// DeploymentsByJobID returns the deployments associated with the job
|
|
|
|
DeploymentsByJobID(ws memdb.WatchSet, namespace, jobID string, all bool) ([]*structs.Deployment, error)
|
|
|
|
|
|
|
|
// JobByIDAndVersion returns the job associated with id and specific version
|
|
|
|
JobByIDAndVersion(ws memdb.WatchSet, namespace, id string, version uint64) (*structs.Job, error)
|
|
|
|
|
2017-05-18 19:36:04 +00:00
|
|
|
// LatestDeploymentByJobID returns the latest deployment matching the given
|
|
|
|
// job ID
|
2017-09-07 23:56:15 +00:00
|
|
|
LatestDeploymentByJobID(ws memdb.WatchSet, namespace, jobID string) (*structs.Deployment, error)
|
2018-09-28 04:44:01 +00:00
|
|
|
|
|
|
|
// SchedulerConfig returns config options for the scheduler
|
|
|
|
SchedulerConfig() (uint64, *structs.SchedulerConfiguration, error)
|
2020-01-31 15:13:21 +00:00
|
|
|
|
|
|
|
// CSIVolumeByID fetch CSI volumes, containing controller jobs
|
2020-03-17 15:35:34 +00:00
|
|
|
CSIVolumeByID(memdb.WatchSet, string, string) (*structs.CSIVolume, error)
|
2020-03-31 21:16:47 +00:00
|
|
|
|
|
|
|
// CSIVolumeByID fetch CSI volumes, containing controller jobs
|
2021-03-18 18:32:40 +00:00
|
|
|
CSIVolumesByNodeID(memdb.WatchSet, string, string) (memdb.ResultIterator, error)
|
2022-02-08 17:16:33 +00:00
|
|
|
|
|
|
|
// LatestIndex returns the greatest index value for all indexes.
|
|
|
|
LatestIndex() (uint64, error)
|
2015-07-28 23:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Planner interface is used to submit a task allocation plan.
|
|
|
|
type Planner interface {
|
|
|
|
// SubmitPlan is used to submit a plan for consideration.
|
|
|
|
// This will return a PlanResult or an error. It is possible
|
|
|
|
// that this will result in a state refresh as well.
|
|
|
|
SubmitPlan(*structs.Plan) (*structs.PlanResult, State, error)
|
2015-08-15 21:25:00 +00:00
|
|
|
|
|
|
|
// UpdateEval is used to update an evaluation. This should update
|
|
|
|
// a copy of the input evaluation since that should be immutable.
|
|
|
|
UpdateEval(*structs.Evaluation) error
|
2015-09-07 21:26:29 +00:00
|
|
|
|
|
|
|
// CreateEval is used to create an evaluation. This should set the
|
|
|
|
// PreviousEval to that of the current evaluation.
|
|
|
|
CreateEval(*structs.Evaluation) error
|
2016-05-20 23:03:53 +00:00
|
|
|
|
|
|
|
// ReblockEval takes a blocked evaluation and re-inserts it into the blocked
|
|
|
|
// evaluation tracker. This update occurs only in-memory on the leader. The
|
|
|
|
// evaluation must exist in a blocked state prior to this being called such
|
|
|
|
// that on leader changes, the evaluation will be reblocked properly.
|
|
|
|
ReblockEval(*structs.Evaluation) error
|
2022-03-07 18:40:57 +00:00
|
|
|
|
2022-10-17 20:23:51 +00:00
|
|
|
// ServersMeetMinimumVersion returns whether the Nomad servers in the
|
|
|
|
// worker's region are at least on the given Nomad version. The
|
|
|
|
// checkFailedServers parameter specifies whether version for the failed
|
|
|
|
// servers should be verified.
|
2022-03-07 18:40:57 +00:00
|
|
|
ServersMeetMinimumVersion(minVersion *version.Version, checkFailedServers bool) bool
|
2015-07-28 23:03:15 +00:00
|
|
|
}
|