2015-06-01 15:49:10 +00:00
|
|
|
package structs
|
|
|
|
|
2015-06-05 22:21:17 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2015-06-05 22:41:03 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2015-06-05 22:21:17 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
|
|
|
)
|
|
|
|
|
2015-06-05 22:41:03 +00:00
|
|
|
var (
|
|
|
|
ErrNoLeader = fmt.Errorf("No cluster leader")
|
|
|
|
ErrNoRegionPath = fmt.Errorf("No path to region")
|
|
|
|
)
|
|
|
|
|
2015-06-01 15:49:10 +00:00
|
|
|
type MessageType uint8
|
|
|
|
|
|
|
|
const (
|
2015-07-07 16:51:42 +00:00
|
|
|
NodeRegisterRequestType MessageType = iota
|
|
|
|
NodeDeregisterRequestType
|
2015-07-04 01:41:36 +00:00
|
|
|
NodeUpdateStatusRequestType
|
2015-07-07 16:51:42 +00:00
|
|
|
JobRegisterRequestType
|
|
|
|
JobDeregisterRequestType
|
2015-07-23 22:52:38 +00:00
|
|
|
EvalUpdateRequestType
|
|
|
|
EvalDeleteRequestType
|
2015-08-04 21:04:33 +00:00
|
|
|
AllocUpdateRequestType
|
2015-06-01 15:49:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// IgnoreUnknownTypeFlag is set along with a MessageType
|
|
|
|
// to indicate that the message type can be safely ignored
|
|
|
|
// if it is not recognized. This is for future proofing, so
|
|
|
|
// that new commands can be added in a way that won't cause
|
|
|
|
// old servers to crash when the FSM attempts to process them.
|
|
|
|
IgnoreUnknownTypeFlag MessageType = 128
|
|
|
|
)
|
2015-06-05 22:21:17 +00:00
|
|
|
|
2015-06-05 22:41:03 +00:00
|
|
|
// RPCInfo is used to describe common information about query
|
|
|
|
type RPCInfo interface {
|
|
|
|
RequestRegion() string
|
|
|
|
IsRead() bool
|
|
|
|
AllowStaleRead() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryOptions is used to specify various flags for read queries
|
|
|
|
type QueryOptions struct {
|
2015-07-03 23:04:24 +00:00
|
|
|
// The target region for this query
|
|
|
|
Region string
|
|
|
|
|
2015-06-07 18:18:59 +00:00
|
|
|
// If set, wait until query exceeds given index. Must be provided
|
|
|
|
// with MaxQueryTime.
|
|
|
|
MinQueryIndex uint64
|
|
|
|
|
|
|
|
// Provided with MinQueryIndex to wait for change.
|
|
|
|
MaxQueryTime time.Duration
|
|
|
|
|
2015-06-05 22:41:03 +00:00
|
|
|
// If set, any follower can service the request. Results
|
|
|
|
// may be arbitrarily stale.
|
|
|
|
AllowStale bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q QueryOptions) RequestRegion() string {
|
|
|
|
return q.Region
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryOption only applies to reads, so always true
|
|
|
|
func (q QueryOptions) IsRead() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q QueryOptions) AllowStaleRead() bool {
|
|
|
|
return q.AllowStale
|
|
|
|
}
|
|
|
|
|
|
|
|
type WriteRequest struct {
|
2015-07-03 23:04:24 +00:00
|
|
|
// The target region for this write
|
2015-06-05 22:41:03 +00:00
|
|
|
Region string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WriteRequest) RequestRegion() string {
|
|
|
|
// The target region for this request
|
|
|
|
return w.Region
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteRequest only applies to writes, always false
|
|
|
|
func (w WriteRequest) IsRead() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WriteRequest) AllowStaleRead() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryMeta allows a query response to include potentially
|
|
|
|
// useful metadata about a query
|
|
|
|
type QueryMeta struct {
|
|
|
|
// This is the index associated with the read
|
|
|
|
Index uint64
|
|
|
|
|
|
|
|
// If AllowStale is used, this is time elapsed since
|
|
|
|
// last contact between the follower and leader. This
|
|
|
|
// can be used to gauge staleness.
|
|
|
|
LastContact time.Duration
|
|
|
|
|
|
|
|
// Used to indicate if there is a known leader node
|
|
|
|
KnownLeader bool
|
|
|
|
}
|
|
|
|
|
2015-07-03 23:04:24 +00:00
|
|
|
// WriteMeta allows a write response to includ e potentially
|
|
|
|
// useful metadata about the write
|
|
|
|
type WriteMeta struct {
|
|
|
|
// This is the index associated with the write
|
|
|
|
Index uint64
|
|
|
|
}
|
2015-06-07 18:18:59 +00:00
|
|
|
|
2015-07-07 16:51:42 +00:00
|
|
|
// NodeRegisterRequest is used for Client.Register endpoint
|
2015-06-07 18:18:59 +00:00
|
|
|
// to register a node as being a schedulable entity.
|
2015-07-07 16:51:42 +00:00
|
|
|
type NodeRegisterRequest struct {
|
2015-07-03 23:04:24 +00:00
|
|
|
Node *Node
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
2015-07-07 16:51:42 +00:00
|
|
|
// NodeDeregisterRequest is used for Client.Deregister endpoint
|
2015-07-04 01:41:36 +00:00
|
|
|
// to deregister a node as being a schedulable entity.
|
2015-07-07 16:51:42 +00:00
|
|
|
type NodeDeregisterRequest struct {
|
2015-07-04 01:41:36 +00:00
|
|
|
NodeID string
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateStatusRequest is used for Client.UpdateStatus endpoint
|
|
|
|
// to update the status of a node.
|
2015-07-07 16:51:42 +00:00
|
|
|
type NodeUpdateStatusRequest struct {
|
2015-07-04 01:41:36 +00:00
|
|
|
NodeID string
|
|
|
|
Status string
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
2015-07-06 21:23:15 +00:00
|
|
|
// NodeSpecificRequest is used when we just need to specify a target node
|
|
|
|
type NodeSpecificRequest struct {
|
|
|
|
NodeID string
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
2015-07-07 16:51:42 +00:00
|
|
|
// JobRegisterRequest is used for Job.Register endpoint
|
|
|
|
// to register a job as being a schedulable entity.
|
|
|
|
type JobRegisterRequest struct {
|
|
|
|
Job *Job
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
// JobDeregisterRequest is used for Job.Deregister endpoint
|
|
|
|
// to deregister a job as being a schedulable entity.
|
|
|
|
type JobDeregisterRequest struct {
|
2015-07-23 22:15:48 +00:00
|
|
|
JobID string
|
2015-07-07 16:51:42 +00:00
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
2015-07-23 21:41:18 +00:00
|
|
|
// JobSpecificRequest is used when we just need to specify a target job
|
|
|
|
type JobSpecificRequest struct {
|
2015-07-23 22:15:48 +00:00
|
|
|
JobID string
|
2015-07-23 21:41:18 +00:00
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
2015-08-06 21:51:15 +00:00
|
|
|
// EvalUpdateRequest is used for upserting evaluations.
|
2015-07-23 22:52:38 +00:00
|
|
|
type EvalUpdateRequest struct {
|
2015-08-06 21:51:15 +00:00
|
|
|
Evals []*Evaluation
|
2015-07-23 22:52:38 +00:00
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
// EvalDeleteRequest is used for deleting an evaluation.
|
|
|
|
type EvalDeleteRequest struct {
|
|
|
|
EvalID string
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
2015-07-23 23:00:19 +00:00
|
|
|
// EvalSpecificRequest is used when we just need to specify a target evaluation
|
|
|
|
type EvalSpecificRequest struct {
|
|
|
|
EvalID string
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:58:51 +00:00
|
|
|
// EvalDequeueRequest is used when we want to dequeue an evaluation
|
|
|
|
type EvalDequeueRequest struct {
|
|
|
|
Schedulers []string
|
|
|
|
Timeout time.Duration
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
2015-07-27 22:31:49 +00:00
|
|
|
// PlanRequest is used to submit an allocation plan to the leader
|
|
|
|
type PlanRequest struct {
|
|
|
|
Plan *Plan
|
|
|
|
WriteRequest
|
|
|
|
}
|
|
|
|
|
2015-08-04 21:04:33 +00:00
|
|
|
// AllocUpdateRequest is used to submit changes to allocations, either
|
|
|
|
// to cause evictions or to assign new allocaitons. Both can be done
|
|
|
|
// within a single transaction
|
|
|
|
type AllocUpdateRequest struct {
|
|
|
|
// Evict is the list of allocation IDs to evict
|
|
|
|
Evict []string
|
|
|
|
|
|
|
|
// Alloc is the list of new allocations to assign
|
|
|
|
Alloc []*Allocation
|
|
|
|
}
|
|
|
|
|
2015-07-04 01:41:36 +00:00
|
|
|
// GenericResponse is used to respond to a request where no
|
|
|
|
// specific response information is needed.
|
|
|
|
type GenericResponse struct {
|
2015-07-03 23:04:24 +00:00
|
|
|
WriteMeta
|
|
|
|
}
|
|
|
|
|
2015-08-06 18:48:44 +00:00
|
|
|
// JobRegisterResponse is used to respond to a job registration
|
|
|
|
type JobRegisterResponse struct {
|
|
|
|
EvalID string
|
|
|
|
EvalCreateIndex uint64
|
|
|
|
JobModifyIndex uint64
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2015-08-06 21:17:18 +00:00
|
|
|
// JobDeregisterResponse is used to respond to a job deregistration
|
|
|
|
type JobDeregisterResponse struct {
|
|
|
|
EvalID string
|
|
|
|
EvalCreateIndex uint64
|
|
|
|
JobModifyIndex uint64
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2015-08-06 23:39:20 +00:00
|
|
|
// NodeUpdateResponse is used to respond to a node update
|
|
|
|
type NodeUpdateResponse struct {
|
|
|
|
EvalIDs []string
|
|
|
|
EvalCreateIndex uint64
|
|
|
|
NodeModifyIndex uint64
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2015-07-06 21:23:15 +00:00
|
|
|
// SingleNodeResponse is used to return a single node
|
|
|
|
type SingleNodeResponse struct {
|
|
|
|
Node *Node
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2015-07-23 21:41:18 +00:00
|
|
|
// SingleJobResponse is used to return a single job
|
|
|
|
type SingleJobResponse struct {
|
|
|
|
Job *Job
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2015-07-23 23:00:19 +00:00
|
|
|
// SingleEvalResponse is used to return a single evaluation
|
|
|
|
type SingleEvalResponse struct {
|
|
|
|
Eval *Evaluation
|
|
|
|
QueryMeta
|
|
|
|
}
|
|
|
|
|
2015-07-27 22:31:49 +00:00
|
|
|
// PlanResponse is used to return from a PlanRequest
|
|
|
|
type PlanResponse struct {
|
|
|
|
Result *PlanResult
|
|
|
|
WriteMeta
|
|
|
|
}
|
|
|
|
|
2015-07-03 23:57:48 +00:00
|
|
|
const (
|
|
|
|
NodeStatusInit = "initializing"
|
|
|
|
NodeStatusReady = "ready"
|
|
|
|
NodeStatusMaint = "maintenance"
|
2015-08-06 23:39:20 +00:00
|
|
|
NodeStatusDrain = "drain"
|
2015-07-03 23:57:48 +00:00
|
|
|
NodeStatusDown = "down"
|
|
|
|
)
|
|
|
|
|
2015-08-06 23:39:20 +00:00
|
|
|
// ShouldEvaluateNode checks if a given node status should trigger an
|
|
|
|
// evaluation. Some states don't require any further action.
|
|
|
|
func ShouldEvaluateNode(status string) bool {
|
|
|
|
switch status {
|
|
|
|
case NodeStatusInit, NodeStatusReady, NodeStatusMaint:
|
|
|
|
return false
|
|
|
|
case NodeStatusDrain, NodeStatusDown:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unhandled node status %s", status))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidNodeStatus is used to check if a node status is valid
|
|
|
|
func ValidNodeStatus(status string) bool {
|
|
|
|
switch status {
|
|
|
|
case NodeStatusInit, NodeStatusReady,
|
|
|
|
NodeStatusMaint, NodeStatusDrain, NodeStatusDown:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 23:04:24 +00:00
|
|
|
// Node is a representation of a schedulable client node
|
|
|
|
type Node struct {
|
|
|
|
// ID is a unique identifier for the node. It can be constructed
|
|
|
|
// by doing a concatenation of the Name and Datacenter as a simple
|
|
|
|
// approach. Alternatively a UUID may be used.
|
|
|
|
ID string
|
|
|
|
|
2015-06-07 18:18:59 +00:00
|
|
|
// Datacenter for this node
|
|
|
|
Datacenter string
|
|
|
|
|
2015-06-07 19:14:41 +00:00
|
|
|
// Node name
|
2015-07-03 23:04:24 +00:00
|
|
|
Name string
|
2015-06-07 18:18:59 +00:00
|
|
|
|
|
|
|
// Attributes is an arbitrary set of key/value
|
|
|
|
// data that can be used for constraints. Examples
|
2015-07-03 23:04:24 +00:00
|
|
|
// include "os=linux", "arch=386", "driver.docker=1",
|
|
|
|
// "docker.runtime=1.8.3"
|
2015-07-06 20:01:10 +00:00
|
|
|
Attributes map[string]string
|
2015-06-07 18:18:59 +00:00
|
|
|
|
|
|
|
// Resources is the available resources on the client.
|
|
|
|
// For example 'cpu=2' 'memory=2048'
|
2015-07-06 20:01:10 +00:00
|
|
|
Resources *Resources
|
2015-06-07 18:18:59 +00:00
|
|
|
|
2015-07-03 23:57:48 +00:00
|
|
|
// Reserved is the set of resources that are reserved,
|
|
|
|
// and should be subtracted from the total resources for
|
|
|
|
// the purposes of scheduling. This may be provide certain
|
|
|
|
// high-watermark tolerances or because of external schedulers
|
|
|
|
// consuming resources.
|
|
|
|
Reserved *Resources
|
|
|
|
|
|
|
|
// Allocated is the set of resources that have been allocated
|
|
|
|
// as part of scheduling. They should also be excluded for the
|
|
|
|
// purposes of additional scheduling allocations.
|
|
|
|
Allocated *Resources
|
|
|
|
|
2015-06-07 18:18:59 +00:00
|
|
|
// Links are used to 'link' this client to external
|
|
|
|
// systems. For example 'consul=foo.dc1' 'aws=i-83212'
|
|
|
|
// 'ami=ami-123'
|
2015-07-06 20:01:10 +00:00
|
|
|
Links map[string]string
|
2015-06-07 18:18:59 +00:00
|
|
|
|
|
|
|
// Meta is used to associate arbitrary metadata with this
|
|
|
|
// client. This is opaque to Nomad.
|
|
|
|
Meta map[string]string
|
|
|
|
|
2015-07-04 00:37:01 +00:00
|
|
|
// NodeClass is an opaque identifier used to group nodes
|
|
|
|
// together for the purpose of determining scheduling pressure.
|
|
|
|
NodeClass string
|
|
|
|
|
2015-07-03 23:04:24 +00:00
|
|
|
// Status of this node
|
|
|
|
Status string
|
2015-07-04 00:50:54 +00:00
|
|
|
|
|
|
|
// Raft Indexes
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
2015-06-07 18:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resources is used to define the resources available
|
|
|
|
// on a client
|
|
|
|
type Resources struct {
|
2015-07-03 23:57:48 +00:00
|
|
|
CPU float64
|
|
|
|
MemoryMB int
|
|
|
|
DiskMB int
|
|
|
|
IOPS int
|
|
|
|
Networks []*NetworkResource
|
2015-06-07 18:18:59 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:23:42 +00:00
|
|
|
// NetIndexByCIDR scans the list of networks for a matching
|
|
|
|
// CIDR, returning the index. This currently ONLY handles
|
|
|
|
// an exact match and not a subset CIDR.
|
|
|
|
func (r *Resources) NetIndexByCIDR(cidr string) int {
|
|
|
|
for idx, net := range r.Networks {
|
|
|
|
if net.CIDR == cidr {
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2015-08-05 00:32:57 +00:00
|
|
|
// Superset checks if one set of resources is a superset
|
|
|
|
// of another.
|
|
|
|
func (r *Resources) Superset(other *Resources) bool {
|
|
|
|
if r.CPU < other.CPU {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r.MemoryMB < other.MemoryMB {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r.DiskMB < other.DiskMB {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r.IOPS < other.IOPS {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, net := range r.Networks {
|
|
|
|
idx := other.NetIndexByCIDR(net.CIDR)
|
|
|
|
if idx >= 0 {
|
|
|
|
if net.MBits < other.Networks[idx].MBits {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check that other does not have a network we are missing
|
|
|
|
for _, net := range other.Networks {
|
|
|
|
idx := r.NetIndexByCIDR(net.CIDR)
|
|
|
|
if idx == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-08-05 00:41:02 +00:00
|
|
|
// Add adds the resources of the delta to this, potentially
|
|
|
|
// returning an error if not possible.
|
|
|
|
func (r *Resources) Add(delta *Resources) error {
|
|
|
|
if delta == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.CPU += delta.CPU
|
|
|
|
r.MemoryMB += delta.MemoryMB
|
|
|
|
r.DiskMB += delta.DiskMB
|
|
|
|
r.IOPS += delta.IOPS
|
|
|
|
|
|
|
|
for _, net := range delta.Networks {
|
|
|
|
idx := r.NetIndexByCIDR(net.CIDR)
|
|
|
|
if idx == -1 {
|
|
|
|
return fmt.Errorf("missing network for CIDR %s", net.CIDR)
|
|
|
|
}
|
|
|
|
r.Networks[idx].Add(net)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-07 18:18:59 +00:00
|
|
|
// NetworkResource is used to represesent available network
|
2015-07-03 23:57:48 +00:00
|
|
|
// resources
|
2015-06-07 18:18:59 +00:00
|
|
|
type NetworkResource struct {
|
|
|
|
Public bool // Is this a public address?
|
|
|
|
CIDR string // CIDR block of addresses
|
|
|
|
ReservedPorts []int // Reserved ports
|
|
|
|
MBits int // Throughput
|
2015-07-03 23:57:48 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 00:41:02 +00:00
|
|
|
// Add adds the resources of the delta to this, potentially
|
|
|
|
// returning an error if not possible.
|
|
|
|
func (n *NetworkResource) Add(delta *NetworkResource) {
|
|
|
|
if len(delta.ReservedPorts) > 0 {
|
|
|
|
n.ReservedPorts = append(n.ReservedPorts, delta.ReservedPorts...)
|
|
|
|
}
|
|
|
|
n.MBits += delta.MBits
|
|
|
|
}
|
|
|
|
|
2015-07-03 23:57:48 +00:00
|
|
|
const (
|
2015-08-07 00:04:35 +00:00
|
|
|
// JobTypeSystem is reserved for internal system tasks and is
|
|
|
|
// always handled by the SystemScheduler.
|
|
|
|
JobTypeSystem = "system"
|
2015-07-03 23:57:48 +00:00
|
|
|
JobTypeService = "service"
|
|
|
|
JobTypeBatch = "batch"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
JobStatusPending = "pending" // Pending means the job is waiting on scheduling
|
|
|
|
JobStatusRunning = "running" // Running means the entire job is running
|
|
|
|
JobStatusComplete = "complete" // Complete means there was a clean termination
|
|
|
|
JobStatusDead = "dead" // Dead means there was abnormal termination
|
|
|
|
)
|
|
|
|
|
2015-08-06 18:48:44 +00:00
|
|
|
const (
|
|
|
|
// JobMinPriority is the minimum allowed priority
|
|
|
|
JobMinPriority = 1
|
|
|
|
|
|
|
|
// JobDefaultPriority is the default priority if not
|
|
|
|
// not specified.
|
|
|
|
JobDefaultPriority = 50
|
|
|
|
|
|
|
|
// JobMaxPriority is the maximum allowed priority
|
|
|
|
JobMaxPriority = 100
|
|
|
|
)
|
|
|
|
|
2015-07-03 23:57:48 +00:00
|
|
|
// Job is the scope of a scheduling request to Nomad. It is the largest
|
|
|
|
// scoped object, and is a named collection of task groups. Each task group
|
|
|
|
// is further composed of tasks. A task group (TG) is the unit of scheduling
|
|
|
|
// however.
|
|
|
|
type Job struct {
|
2015-07-23 22:15:48 +00:00
|
|
|
// ID is a unique identifier for the job. It can be the same as
|
|
|
|
// the job name, or alternatively a UUID may be used.
|
|
|
|
ID string
|
|
|
|
|
2015-07-03 23:57:48 +00:00
|
|
|
// Name is the logical name of the job used to refer to it. This is unique
|
|
|
|
// per region, but not unique globally.
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Type is used to control various behaviors about the job. Most jobs
|
|
|
|
// are service jobs, meaning they are expected to be long lived.
|
|
|
|
// Some jobs are batch oriented meaning they run and then terminate.
|
|
|
|
// This can be extended in the future to support custom schedulers.
|
|
|
|
Type string
|
|
|
|
|
|
|
|
// Priority is used to control scheduling importance and if this job
|
|
|
|
// can preempt other jobs.
|
|
|
|
Priority int
|
|
|
|
|
|
|
|
// AllAtOnce is used to control if incremental scheduling of task groups
|
|
|
|
// is allowed or if we must do a gang scheduling of the entire job. This
|
|
|
|
// can slow down larger jobs if resources are not available.
|
|
|
|
AllAtOnce bool
|
|
|
|
|
|
|
|
// Constraints can be specified at a job level and apply to
|
|
|
|
// all the task groups and tasks.
|
|
|
|
Constraints []*Constraint
|
|
|
|
|
|
|
|
// TaskGroups are the collections of task groups that this job needs
|
|
|
|
// to run. Each task group is an atomic unit of scheduling and placement.
|
|
|
|
TaskGroups []*TaskGroup
|
|
|
|
|
|
|
|
// Meta is used to associate arbitrary metadata with this
|
|
|
|
// job. This is opaque to Nomad.
|
|
|
|
Meta map[string]string
|
|
|
|
|
|
|
|
// Job status
|
|
|
|
Status string
|
2015-07-04 00:50:54 +00:00
|
|
|
|
|
|
|
// Raft Indexes
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
2015-07-03 23:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TaskGroup is an atomic unit of placement. Each task group belongs to
|
|
|
|
// a job and may contain any number of tasks. A task group support running
|
|
|
|
// in many replicas using the same configuration..
|
|
|
|
type TaskGroup struct {
|
|
|
|
// Name of the task group
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Count is the number of replicas of this task group that should
|
|
|
|
// be scheduled.
|
|
|
|
Count int
|
|
|
|
|
|
|
|
// Constraints can be specified at a task group level and apply to
|
|
|
|
// all the tasks contained.
|
|
|
|
Constraints []*Constraint
|
|
|
|
|
|
|
|
// Tasks are the collection of tasks that this task group needs to run
|
|
|
|
Tasks []*Task
|
|
|
|
|
|
|
|
// Meta is used to associate arbitrary metadata with this
|
|
|
|
// task group. This is opaque to Nomad.
|
|
|
|
Meta map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Task is a single process typically that is executed as part of a task group.
|
|
|
|
type Task struct {
|
|
|
|
// Name of the task
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Driver is used to control which driver is used
|
|
|
|
Driver string
|
|
|
|
|
|
|
|
// Config is provided to the driver to initialize
|
|
|
|
Config map[string]string
|
|
|
|
|
|
|
|
// Constraints can be specified at a task level and apply only to
|
|
|
|
// the particular task.
|
|
|
|
Constraints []*Constraint
|
|
|
|
|
|
|
|
// Resources is the resources needed by this task
|
|
|
|
Resources *Resources
|
|
|
|
|
|
|
|
// Meta is used to associate arbitrary metadata with this
|
|
|
|
// task. This is opaque to Nomad.
|
|
|
|
Meta map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constraints are used to restrict placement options in the case of
|
|
|
|
// a hard constraint, and used to prefer a placement in the case of
|
|
|
|
// a soft constraint.
|
|
|
|
type Constraint struct {
|
|
|
|
Hard bool // Hard or soft constraint
|
|
|
|
LTarget string // Left-hand target
|
|
|
|
RTarget string // Right-hand target
|
|
|
|
Operand string // Constraint operand (<=, <, =, !=, >, >=), contains, near
|
|
|
|
Weight int // Soft constraints can vary the weight
|
2015-06-07 18:18:59 +00:00
|
|
|
}
|
|
|
|
|
2015-07-04 00:11:53 +00:00
|
|
|
const (
|
|
|
|
AllocStatusPending = "pending"
|
|
|
|
AllocStatusInit = "initializing"
|
|
|
|
AllocStatusRunning = "running"
|
|
|
|
AllocStatusComplete = "complete"
|
|
|
|
AllocStatusDead = "dead"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Allocation is used to allocate the placement of a task group to a node.
|
|
|
|
type Allocation struct {
|
|
|
|
// ID of the allocation (UUID)
|
|
|
|
ID string
|
|
|
|
|
2015-08-11 23:34:06 +00:00
|
|
|
// Name is a logical name of the allocation.
|
|
|
|
Name string
|
|
|
|
|
2015-07-04 00:11:53 +00:00
|
|
|
// NodeID is the node this is being placed on
|
|
|
|
NodeID string
|
|
|
|
|
|
|
|
// Job is the parent job of the task group being allocated.
|
|
|
|
// This is copied at allocation time to avoid issues if the job
|
|
|
|
// definition is updated.
|
2015-07-23 22:15:48 +00:00
|
|
|
JobID string
|
|
|
|
Job *Job
|
2015-07-04 00:11:53 +00:00
|
|
|
|
|
|
|
// TaskGroup is the task being allocated to the node
|
|
|
|
// This is copied at allocation time to avoid issues if the job
|
|
|
|
// definition is updated.
|
|
|
|
TaskGroupName string
|
|
|
|
TaskGroup *TaskGroup
|
|
|
|
|
|
|
|
// Resources is the set of resources allocated as part
|
|
|
|
// of this allocation of the task group.
|
|
|
|
Resources *Resources
|
|
|
|
|
2015-07-04 00:37:01 +00:00
|
|
|
// Metrics associated with this allocation
|
|
|
|
Metrics *AllocMetric
|
|
|
|
|
2015-07-04 00:11:53 +00:00
|
|
|
// Status of the allocation
|
|
|
|
Status string
|
2015-07-04 00:50:54 +00:00
|
|
|
|
|
|
|
// Raft Indexes
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
2015-07-04 00:11:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-04 00:37:01 +00:00
|
|
|
// AllocMetric is used to track various metrics while attempting
|
|
|
|
// to make an allocation. These are used to debug a job, or to better
|
|
|
|
// understand the pressure within the system.
|
|
|
|
type AllocMetric struct {
|
|
|
|
// NodesEvaluated is the number of nodes that were evaluated
|
|
|
|
NodesEvaluated int
|
|
|
|
|
|
|
|
// NodesFiltered is the number of nodes filtered due to
|
|
|
|
// a hard constraint
|
|
|
|
NodesFiltered int
|
|
|
|
|
|
|
|
// ClassFiltered is the number of nodes filtered by class
|
|
|
|
ClassFiltered map[string]int
|
|
|
|
|
|
|
|
// ConstraintFiltered is the number of failures caused by constraint
|
|
|
|
ConstraintFiltered map[string]int
|
|
|
|
|
|
|
|
// NodesExhausted is the nubmer of nodes skipped due to being
|
|
|
|
// exhausted of at least one resource
|
|
|
|
NodesExhausted int
|
|
|
|
|
|
|
|
// ClassExhausted is the number of nodes exhausted by class
|
|
|
|
ClassExhausted map[string]int
|
|
|
|
|
|
|
|
// Preemptions is the number of preemptions considered.
|
|
|
|
// This indicates a relatively busy fleet if high.
|
|
|
|
Preemptions int
|
|
|
|
|
|
|
|
// Scores is the scores of the final few nodes remaining
|
|
|
|
// for placement. The top score is typically selected.
|
|
|
|
Scores map[string]int
|
|
|
|
|
|
|
|
// AllocationTime is a measure of how long the allocation
|
|
|
|
// attempt took. This can affect performance and SLAs.
|
|
|
|
AllocationTime time.Duration
|
|
|
|
}
|
|
|
|
|
2015-07-23 22:27:13 +00:00
|
|
|
const (
|
|
|
|
EvalStatusPending = "pending"
|
|
|
|
EvalStatusComplete = "complete"
|
|
|
|
EvalStatusCanceled = "canceled"
|
|
|
|
)
|
|
|
|
|
2015-08-06 18:48:44 +00:00
|
|
|
const (
|
2015-08-06 21:17:18 +00:00
|
|
|
EvalTriggerJobRegister = "job-register"
|
|
|
|
EvalTriggerJobDeregister = "job-deregister"
|
2015-08-06 23:39:20 +00:00
|
|
|
EvalTriggerNodeUpdate = "node-update"
|
2015-08-06 18:48:44 +00:00
|
|
|
)
|
|
|
|
|
2015-07-23 22:27:13 +00:00
|
|
|
// Evaluation is used anytime we need to apply business logic as a result
|
|
|
|
// of a change to our desired state (job specification) or the emergent state
|
|
|
|
// (registered nodes). When the inputs change, we need to "evaluate" them,
|
|
|
|
// potentially taking action (allocation of work) or doing nothing if the state
|
|
|
|
// of the world does not require it.
|
|
|
|
type Evaluation struct {
|
|
|
|
// ID is a randonly generated UUID used for this evaluation. This
|
|
|
|
// is assigned upon the creation of the evaluation.
|
|
|
|
ID string
|
|
|
|
|
2015-07-24 00:31:08 +00:00
|
|
|
// Priority is used to control scheduling importance and if this job
|
|
|
|
// can preempt other jobs.
|
|
|
|
Priority int
|
|
|
|
|
|
|
|
// Type is used to control which schedulers are available to handle
|
|
|
|
// this evaluation.
|
|
|
|
Type string
|
|
|
|
|
2015-07-24 05:30:08 +00:00
|
|
|
// TriggeredBy is used to give some insight into why this Eval
|
|
|
|
// was created. (Job change, node failure, alloc failure, etc).
|
|
|
|
TriggeredBy string
|
|
|
|
|
2015-08-06 00:55:15 +00:00
|
|
|
// JobID is the job this evaluation is scoped to. Evalutions cannot
|
|
|
|
// be run in parallel for a given JobID, so we serialize on this.
|
|
|
|
JobID string
|
|
|
|
|
2015-08-06 18:48:44 +00:00
|
|
|
// JobModifyIndex is the modify index of the job at the time
|
|
|
|
// the evaluation was created
|
|
|
|
JobModifyIndex uint64
|
|
|
|
|
2015-08-06 23:39:20 +00:00
|
|
|
// NodeID is the node that was affected triggering the evaluation.
|
|
|
|
NodeID string
|
|
|
|
|
|
|
|
// NodeModifyIndex is the modify index of the node at the time
|
|
|
|
// the evaluation was created
|
|
|
|
NodeModifyIndex uint64
|
|
|
|
|
2015-07-23 22:27:13 +00:00
|
|
|
// Status of the evaluation
|
|
|
|
Status string
|
|
|
|
|
|
|
|
// Raft Indexes
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
2015-08-06 18:28:55 +00:00
|
|
|
// ShouldEnqueue checks if a given evaluation should be enqueued
|
|
|
|
func (e *Evaluation) ShouldEnqueue() bool {
|
|
|
|
switch e.Status {
|
|
|
|
case EvalStatusPending:
|
|
|
|
return true
|
|
|
|
case EvalStatusComplete, EvalStatusCanceled:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unhandled evaluation (%s) status %s", e.ID, e.Status))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 23:34:06 +00:00
|
|
|
// MakePlan is used to make a plan from the given evaluation
|
|
|
|
// for a given Job
|
|
|
|
func (e *Evaluation) MakePlan(j *Job) *Plan {
|
|
|
|
p := &Plan{
|
|
|
|
EvalID: e.ID,
|
|
|
|
Priority: j.Priority,
|
|
|
|
AllAtOnce: j.AllAtOnce,
|
|
|
|
NodeEvict: make(map[string][]string),
|
|
|
|
NodeAllocation: make(map[string][]*Allocation),
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2015-07-27 21:59:16 +00:00
|
|
|
// Plan is used to submit a commit plan for task allocations. These
|
|
|
|
// are submitted to the leader which verifies that resources have
|
|
|
|
// not been overcommitted before admiting the plan.
|
|
|
|
type Plan struct {
|
2015-07-29 00:49:45 +00:00
|
|
|
// EvalID is the evaluation ID this plan is associated with
|
|
|
|
EvalID string
|
|
|
|
|
2015-07-27 21:59:16 +00:00
|
|
|
// Priority is the priority of the upstream job
|
|
|
|
Priority int
|
|
|
|
|
2015-07-29 00:49:45 +00:00
|
|
|
// AllAtOnce is used to control if incremental scheduling of task groups
|
|
|
|
// is allowed or if we must do a gang scheduling of the entire job.
|
|
|
|
// If this is false, a plan may be partially applied. Otherwise, the
|
|
|
|
// entire plan must be able to make progress.
|
|
|
|
AllAtOnce bool
|
|
|
|
|
|
|
|
// NodeEvict contains all the evictions for each node. For each node,
|
|
|
|
// this is a list of the allocation IDs to evict.
|
|
|
|
NodeEvict map[string][]string
|
|
|
|
|
|
|
|
// NodeAllocation contains all the allocations for each node.
|
|
|
|
// The evicts must be considered prior to the allocations.
|
2015-08-04 23:32:46 +00:00
|
|
|
NodeAllocation map[string][]*Allocation
|
2015-07-27 21:59:16 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 22:31:49 +00:00
|
|
|
// PlanResult is the result of a plan submitted to the leader.
|
2015-07-27 21:59:16 +00:00
|
|
|
type PlanResult struct {
|
2015-07-29 00:49:45 +00:00
|
|
|
// NodeEvict contains all the evictions that were committed.
|
|
|
|
NodeEvict map[string][]string
|
|
|
|
|
|
|
|
// NodeAllocation contains all the allocations that were committed.
|
2015-08-04 23:32:46 +00:00
|
|
|
NodeAllocation map[string][]*Allocation
|
2015-07-29 00:49:45 +00:00
|
|
|
|
|
|
|
// RefreshIndex is the index the worker should refresh state up to.
|
|
|
|
// This allows all evictions and allocations to be materialized.
|
|
|
|
// If any allocations were rejected due to stale data (node state,
|
|
|
|
// over committed) this can be used to force a worker refresh.
|
2015-07-28 23:36:15 +00:00
|
|
|
RefreshIndex uint64
|
|
|
|
|
2015-07-29 00:49:45 +00:00
|
|
|
// AllocIndex is the Raft index in which the evictions and
|
|
|
|
// allocations took place. This is used for the write index.
|
2015-07-27 22:31:49 +00:00
|
|
|
AllocIndex uint64
|
2015-07-27 21:59:16 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 22:21:17 +00:00
|
|
|
// msgpackHandle is a shared handle for encoding/decoding of structs
|
|
|
|
var msgpackHandle = &codec.MsgpackHandle{}
|
|
|
|
|
|
|
|
// Decode is used to decode a MsgPack encoded object
|
|
|
|
func Decode(buf []byte, out interface{}) error {
|
|
|
|
return codec.NewDecoder(bytes.NewReader(buf), msgpackHandle).Decode(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode is used to encode a MsgPack object with type prefix
|
|
|
|
func Encode(t MessageType, msg interface{}) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteByte(uint8(t))
|
|
|
|
err := codec.NewEncoder(&buf, msgpackHandle).Encode(msg)
|
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|