open-nomad/scheduler/util.go

169 lines
4.2 KiB
Go
Raw Normal View History

2015-08-13 23:25:59 +00:00
package scheduler
import (
"fmt"
"github.com/hashicorp/nomad/nomad/structs"
)
2015-08-14 01:16:32 +00:00
// allocTuple is a tuple of the allocation name and potential alloc ID
type allocTuple struct {
Name string
TaskGroup *structs.TaskGroup
Alloc *structs.Allocation
2015-08-13 23:25:59 +00:00
}
// materializeTaskGroups is used to materialize all the task groups
// a job requires. This is used to do the count expansion.
func materializeTaskGroups(job *structs.Job) map[string]*structs.TaskGroup {
out := make(map[string]*structs.TaskGroup)
for _, tg := range job.TaskGroups {
for i := 0; i < tg.Count; i++ {
name := fmt.Sprintf("%s.%s[%d]", job.Name, tg.Name, i)
out[name] = tg
}
}
return out
}
// diffAllocs is used to do a set difference between the target allocations
// and the existing allocations. This returns 5 sets of results, the list of
2015-08-13 23:25:59 +00:00
// named task groups that need to be placed (no existing allocation), the
// allocations that need to be updated (job definition is newer), allocs that
// need to be migrated (node is draining), the allocs that need to be evicted
// (no longer required), and those that should be ignored.
2015-08-13 23:25:59 +00:00
func diffAllocs(job *structs.Job,
taintedNodes map[string]bool,
2015-08-13 23:25:59 +00:00
required map[string]*structs.TaskGroup,
2015-08-14 01:18:32 +00:00
allocs []*structs.Allocation) (place, update, migrate, evict, ignore []allocTuple) {
2015-08-13 23:25:59 +00:00
// Scan the existing updates
2015-08-14 01:20:55 +00:00
existing := make(map[string]struct{})
for _, exist := range allocs {
// Index the existing node
name := exist.Name
existing[name] = struct{}{}
2015-08-13 23:25:59 +00:00
2015-08-14 01:20:55 +00:00
// Check for the definition in the required set
tg, ok := required[name]
2015-08-13 23:25:59 +00:00
2015-08-14 01:20:55 +00:00
// If not required, we evict
if !ok {
evict = append(evict, allocTuple{
Name: name,
TaskGroup: tg,
Alloc: exist,
})
continue
}
2015-08-14 01:20:55 +00:00
// If we are on a tainted node, we must migrate
if taintedNodes[exist.NodeID] {
migrate = append(migrate, allocTuple{
Name: name,
TaskGroup: tg,
Alloc: exist,
})
continue
}
2015-08-13 23:25:59 +00:00
2015-08-14 01:20:55 +00:00
// If the definition is updated we need to update
// XXX: This is an extremely conservative approach. We can check
// if the job definition has changed in a way that affects
// this allocation and potentially ignore it.
if job.ModifyIndex != exist.Job.ModifyIndex {
update = append(update, allocTuple{
2015-08-14 01:16:32 +00:00
Name: name,
TaskGroup: tg,
Alloc: exist,
})
2015-08-14 01:20:55 +00:00
continue
2015-08-13 23:25:59 +00:00
}
2015-08-14 01:20:55 +00:00
// Everything is up-to-date
ignore = append(ignore, allocTuple{
Name: name,
TaskGroup: tg,
Alloc: exist,
})
2015-08-13 23:25:59 +00:00
}
// Scan the required groups
2015-08-14 01:16:32 +00:00
for name, tg := range required {
2015-08-13 23:25:59 +00:00
// Check for an existing allocation
_, ok := existing[name]
// Require a placement if no existing allocation. If there
// is an existing allocation, we would have checked for a potential
// update or ignore above.
if !ok {
2015-08-14 01:16:32 +00:00
place = append(place, allocTuple{
Name: name,
TaskGroup: tg,
})
2015-08-13 23:25:59 +00:00
}
}
return
}
2015-08-14 00:19:09 +00:00
// readyNodesInDCs returns all the ready nodes in the given datacenters
func readyNodesInDCs(state State, dcs []string) ([]*structs.Node, error) {
var out []*structs.Node
for _, dc := range dcs {
iter, err := state.NodesByDatacenterStatus(dc, structs.NodeStatusReady)
if err != nil {
return nil, err
}
for {
raw := iter.Next()
if raw == nil {
break
}
out = append(out, raw.(*structs.Node))
}
}
return out, nil
}
2015-08-14 00:40:23 +00:00
// retryMax is used to retry a callback until it returns success or
// a maximum number of attempts is reached
func retryMax(max int, cb func() (bool, error)) error {
attempts := 0
for attempts < max {
done, err := cb()
if err != nil {
return err
}
if done {
return nil
}
attempts += 1
}
return fmt.Errorf("maximum attempts reached (%d)", max)
}
2015-08-14 00:51:31 +00:00
// taintedNodes is used to scan the allocations and then check if the
// underlying nodes are tainted, and should force a migration of the allocation.
func taintedNodes(state State, allocs []*structs.Allocation) (map[string]bool, error) {
out := make(map[string]bool)
for _, alloc := range allocs {
if _, ok := out[alloc.NodeID]; ok {
continue
}
node, err := state.GetNodeByID(alloc.NodeID)
if err != nil {
return nil, err
}
2015-08-14 01:05:31 +00:00
// If the node does not exist, we should migrate
if node == nil {
out[alloc.NodeID] = true
continue
}
2015-08-14 00:51:31 +00:00
out[alloc.NodeID] = structs.ShouldDrainNode(node.Status)
}
return out, nil
}