2015-07-27 22:31:09 +00:00
|
|
|
package nomad
|
|
|
|
|
2015-08-04 23:32:46 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2015-08-04 23:35:49 +00:00
|
|
|
"time"
|
2015-08-04 23:32:46 +00:00
|
|
|
|
2015-08-04 23:35:49 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2015-08-11 21:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2015-08-04 23:32:46 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
2015-07-27 22:31:09 +00:00
|
|
|
|
|
|
|
// planApply is a long lived goroutine that reads plan allocations from
|
|
|
|
// the plan queue, determines if they can be applied safely and applies
|
|
|
|
// them via Raft.
|
|
|
|
func (s *Server) planApply() {
|
|
|
|
for {
|
|
|
|
// Pull the next pending plan, exit if we are no longer leader
|
|
|
|
pending, err := s.planQueue.Dequeue(0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-12 22:44:36 +00:00
|
|
|
// Verify the evaluation is outstanding, and that the tokens match.
|
|
|
|
token, ok := s.evalBroker.Outstanding(pending.plan.EvalID)
|
|
|
|
if !ok {
|
|
|
|
s.logger.Printf("[ERR] nomad: plan received for non-outstanding evaluation %s",
|
|
|
|
pending.plan.EvalID)
|
|
|
|
pending.respond(nil, fmt.Errorf("evaluation is not outstanding"))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if pending.plan.EvalToken != token {
|
|
|
|
s.logger.Printf("[ERR] nomad: plan received for evaluation %s with wrong token",
|
|
|
|
pending.plan.EvalID)
|
|
|
|
pending.respond(nil, fmt.Errorf("evaluation token does not match"))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-08-05 01:30:05 +00:00
|
|
|
// Snapshot the state so that we have a consistent view of the world
|
|
|
|
snap, err := s.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Printf("[ERR] nomad: failed to snapshot state: %v", err)
|
|
|
|
pending.respond(nil, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-08-04 23:32:46 +00:00
|
|
|
// Evaluate the plan
|
2015-08-05 01:30:05 +00:00
|
|
|
result, err := evaluatePlan(snap, pending.plan)
|
2015-08-04 23:32:46 +00:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Printf("[ERR] nomad: failed to evaluate plan: %v", err)
|
|
|
|
pending.respond(nil, err)
|
|
|
|
continue
|
|
|
|
}
|
2015-07-27 22:31:09 +00:00
|
|
|
|
2015-08-04 23:32:46 +00:00
|
|
|
// Apply the plan if there is anything to do
|
2015-08-26 00:36:52 +00:00
|
|
|
if !result.IsNoOp() {
|
2015-08-04 23:32:46 +00:00
|
|
|
allocIndex, err := s.applyPlan(result)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Printf("[ERR] nomad: failed to apply plan: %v", err)
|
|
|
|
pending.respond(nil, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result.AllocIndex = allocIndex
|
2015-07-27 22:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Respond to the plan
|
|
|
|
pending.respond(result, nil)
|
|
|
|
}
|
|
|
|
}
|
2015-08-04 23:32:46 +00:00
|
|
|
|
2015-08-05 01:30:05 +00:00
|
|
|
// applyPlan is used to apply the plan result and to return the alloc index
|
|
|
|
func (s *Server) applyPlan(result *structs.PlanResult) (uint64, error) {
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "plan", "apply"}, time.Now())
|
|
|
|
req := structs.AllocUpdateRequest{}
|
2015-08-26 00:36:52 +00:00
|
|
|
for _, updateList := range result.NodeUpdate {
|
|
|
|
req.Alloc = append(req.Alloc, updateList...)
|
2015-08-05 01:30:05 +00:00
|
|
|
}
|
|
|
|
for _, allocList := range result.NodeAllocation {
|
|
|
|
req.Alloc = append(req.Alloc, allocList...)
|
|
|
|
}
|
2015-08-15 20:33:20 +00:00
|
|
|
req.Alloc = append(req.Alloc, result.FailedAllocs...)
|
2015-08-05 01:30:05 +00:00
|
|
|
|
|
|
|
_, index, err := s.raftApply(structs.AllocUpdateRequestType, &req)
|
|
|
|
return index, err
|
|
|
|
}
|
|
|
|
|
2015-08-04 23:32:46 +00:00
|
|
|
// evaluatePlan is used to determine what portions of a plan
|
|
|
|
// can be applied if any. Returns if there should be a plan application
|
|
|
|
// which may be partial or if there was an error
|
2015-08-11 21:27:14 +00:00
|
|
|
func evaluatePlan(snap *state.StateSnapshot, plan *structs.Plan) (*structs.PlanResult, error) {
|
2015-08-04 23:35:49 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "plan", "evaluate"}, time.Now())
|
2015-08-05 01:10:57 +00:00
|
|
|
|
2015-08-04 23:32:46 +00:00
|
|
|
// Create a result holder for the plan
|
|
|
|
result := &structs.PlanResult{
|
2015-08-26 00:36:52 +00:00
|
|
|
NodeUpdate: make(map[string][]*structs.Allocation),
|
2015-08-04 23:32:46 +00:00
|
|
|
NodeAllocation: make(map[string][]*structs.Allocation),
|
2015-08-15 20:33:20 +00:00
|
|
|
FailedAllocs: plan.FailedAllocs,
|
2015-08-04 23:32:46 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 00:36:52 +00:00
|
|
|
// Collect all the nodeIDs
|
|
|
|
nodeIDs := make(map[string]struct{})
|
|
|
|
for nodeID := range plan.NodeUpdate {
|
|
|
|
nodeIDs[nodeID] = struct{}{}
|
|
|
|
}
|
2015-08-05 01:10:57 +00:00
|
|
|
for nodeID := range plan.NodeAllocation {
|
2015-08-26 00:36:52 +00:00
|
|
|
nodeIDs[nodeID] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check each allocation to see if it should be allowed
|
|
|
|
for nodeID := range nodeIDs {
|
2015-08-05 01:10:57 +00:00
|
|
|
// Evaluate the plan for this node
|
|
|
|
fit, err := evaluateNodePlan(snap, plan, nodeID)
|
2015-08-04 23:32:46 +00:00
|
|
|
if err != nil {
|
2015-08-05 01:10:57 +00:00
|
|
|
return nil, err
|
2015-08-04 23:32:46 +00:00
|
|
|
}
|
2015-08-05 01:10:57 +00:00
|
|
|
if !fit {
|
2015-08-04 23:32:46 +00:00
|
|
|
// Scheduler must have stale data, RefreshIndex should force
|
|
|
|
// the latest view of allocations and nodes
|
2015-09-07 03:56:38 +00:00
|
|
|
allocIndex, err := snap.Index("allocs")
|
2015-08-04 23:32:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-07 03:56:38 +00:00
|
|
|
nodeIndex, err := snap.Index("nodes")
|
2015-08-04 23:32:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-05 00:13:40 +00:00
|
|
|
result.RefreshIndex = maxUint64(nodeIndex, allocIndex)
|
2015-08-04 23:32:46 +00:00
|
|
|
|
|
|
|
// If we require all-at-once scheduling, there is no point
|
|
|
|
// to continue the evaluation, as we've already failed.
|
|
|
|
if plan.AllAtOnce {
|
2015-08-26 00:36:52 +00:00
|
|
|
result.NodeUpdate = nil
|
2015-08-05 01:30:05 +00:00
|
|
|
result.NodeAllocation = nil
|
2015-08-04 23:32:46 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip this node, since it cannot be used.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add this to the plan result
|
2015-08-26 00:36:52 +00:00
|
|
|
if nodeUpdate := plan.NodeUpdate[nodeID]; len(nodeUpdate) > 0 {
|
|
|
|
result.NodeUpdate[nodeID] = nodeUpdate
|
2015-08-07 16:37:45 +00:00
|
|
|
}
|
|
|
|
if nodeAlloc := plan.NodeAllocation[nodeID]; len(nodeAlloc) > 0 {
|
|
|
|
result.NodeAllocation[nodeID] = nodeAlloc
|
|
|
|
}
|
2015-08-04 23:32:46 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2015-08-05 01:10:57 +00:00
|
|
|
// evaluateNodePlan is used to evalute the plan for a single node,
|
|
|
|
// returning if the plan is valid or if an error is encountered
|
2015-08-11 21:27:14 +00:00
|
|
|
func evaluateNodePlan(snap *state.StateSnapshot, plan *structs.Plan, nodeID string) (bool, error) {
|
2015-08-07 16:37:45 +00:00
|
|
|
// If this is an evict-only plan, it always 'fits' since we are removing things.
|
|
|
|
if len(plan.NodeAllocation[nodeID]) == 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2015-08-05 01:10:57 +00:00
|
|
|
// Get the node itself
|
2015-09-07 03:56:38 +00:00
|
|
|
node, err := snap.NodeByID(nodeID)
|
2015-08-05 01:10:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to get node '%s': %v", node, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the node does not exist or is not ready for schduling it is not fit
|
2015-08-16 01:03:05 +00:00
|
|
|
// XXX: There is a potential race between when we do this check and when
|
|
|
|
// the Raft commit happens.
|
2015-09-07 02:47:02 +00:00
|
|
|
if node == nil || node.Status != structs.NodeStatusReady || node.Drain {
|
2015-08-05 01:10:57 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the existing allocations
|
|
|
|
existingAlloc, err := snap.AllocsByNode(nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to get existing allocations for '%s': %v", node, err)
|
|
|
|
}
|
|
|
|
|
2015-08-23 01:30:49 +00:00
|
|
|
// Filter on alloc state
|
|
|
|
existingAlloc = structs.FilterTerminalAllocs(existingAlloc)
|
|
|
|
|
2015-08-05 01:10:57 +00:00
|
|
|
// Determine the proposed allocation by first removing allocations
|
|
|
|
// that are planned evictions and adding the new allocations.
|
|
|
|
proposed := existingAlloc
|
2015-08-26 00:36:52 +00:00
|
|
|
var remove []*structs.Allocation
|
|
|
|
if update := plan.NodeUpdate[nodeID]; len(update) > 0 {
|
|
|
|
remove = append(remove, update...)
|
2015-08-05 01:10:57 +00:00
|
|
|
}
|
2015-08-23 02:37:21 +00:00
|
|
|
if updated := plan.NodeAllocation[nodeID]; len(updated) > 0 {
|
|
|
|
for _, alloc := range updated {
|
2015-08-26 00:36:52 +00:00
|
|
|
remove = append(remove, alloc)
|
2015-08-23 02:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
proposed = structs.RemoveAllocs(existingAlloc, remove)
|
2015-08-05 01:10:57 +00:00
|
|
|
proposed = append(proposed, plan.NodeAllocation[nodeID]...)
|
|
|
|
|
|
|
|
// Check if these allocations fit
|
2015-09-14 01:46:40 +00:00
|
|
|
fit, _, _, err := structs.AllocsFit(node, proposed, nil)
|
2015-08-13 18:54:59 +00:00
|
|
|
return fit, err
|
2015-08-05 01:10:57 +00:00
|
|
|
}
|