// 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 func evaluatePlan(snap *state.StateSnapshot, plan *structs.Plan) (*structs.PlanResult, error) { defer metrics.MeasureSince([]string{"nomad", "plan", "evaluate"}, time.Now()) // Create a result holder for the plan result := &structs.PlanResult{ NodeUpdate: make(map[string][]*structs.Allocation), NodeAllocation: make(map[string][]*structs.Allocation), FailedAllocs: plan.FailedAllocs, } // Collect all the nodeIDs nodeIDs := make(map[string]struct{}) for nodeID := range plan.NodeUpdate { nodeIDs[nodeID] = struct{}{} } for nodeID := range plan.NodeAllocation { nodeIDs[nodeID] = struct{}{} } // Check each allocation to see if it should be allowed for nodeID := range nodeIDs { // Evaluate the plan for this node fit, err := evaluateNodePlan(snap, plan, nodeID) if err != nil { return nil, err } if !fit { // Scheduler must have stale data, RefreshIndex should force // the latest view of allocations and nodes allocIndex, err := snap.Index("allocs") if err != nil { return nil, err } nodeIndex, err := snap.Index("nodes") if err != nil { return nil, err } result.RefreshIndex = maxUint64(nodeIndex, allocIndex) // If we require all-at-once scheduling, there is no point // to continue the evaluation, as we've already failed. if plan.AllAtOnce { result.NodeUpdate = nil result.NodeAllocation = nil return result, nil } // Skip this node, since it cannot be used. continue } // Add this to the plan result if nodeUpdate := plan.NodeUpdate[nodeID]; len(nodeUpdate) > 0 { result.NodeUpdate[nodeID] = nodeUpdate } if nodeAlloc := plan.NodeAllocation[nodeID]; len(nodeAlloc) > 0 { result.NodeAllocation[nodeID] = nodeAlloc } } return result, nil }
// 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 func evaluatePlan(pool *EvaluatePool, snap *state.StateSnapshot, plan *structs.Plan) (*structs.PlanResult, error) { defer metrics.MeasureSince([]string{"nomad", "plan", "evaluate"}, time.Now()) // Create a result holder for the plan result := &structs.PlanResult{ NodeUpdate: make(map[string][]*structs.Allocation), NodeAllocation: make(map[string][]*structs.Allocation), } // Collect all the nodeIDs nodeIDs := make(map[string]struct{}) nodeIDList := make([]string, 0, len(plan.NodeUpdate)+len(plan.NodeAllocation)) for nodeID := range plan.NodeUpdate { if _, ok := nodeIDs[nodeID]; !ok { nodeIDs[nodeID] = struct{}{} nodeIDList = append(nodeIDList, nodeID) } } for nodeID := range plan.NodeAllocation { if _, ok := nodeIDs[nodeID]; !ok { nodeIDs[nodeID] = struct{}{} nodeIDList = append(nodeIDList, nodeID) } } // Setup a multierror to handle potentially getting many // errors since we are processing in parallel. var mErr multierror.Error partialCommit := false // handleResult is used to process the result of evaluateNodePlan handleResult := func(nodeID string, fit bool, err error) (cancel bool) { // Evaluate the plan for this node if err != nil { mErr.Errors = append(mErr.Errors, err) return true } if !fit { // Set that this is a partial commit partialCommit = true // If we require all-at-once scheduling, there is no point // to continue the evaluation, as we've already failed. if plan.AllAtOnce { result.NodeUpdate = nil result.NodeAllocation = nil return true } // Skip this node, since it cannot be used. return } // Add this to the plan result if nodeUpdate := plan.NodeUpdate[nodeID]; len(nodeUpdate) > 0 { result.NodeUpdate[nodeID] = nodeUpdate } if nodeAlloc := plan.NodeAllocation[nodeID]; len(nodeAlloc) > 0 { result.NodeAllocation[nodeID] = nodeAlloc } return } // Get the pool channels req := pool.RequestCh() resp := pool.ResultCh() outstanding := 0 didCancel := false // Evalute each node in the plan, handling results as they are ready to // avoid blocking. for len(nodeIDList) > 0 { nodeID := nodeIDList[0] select { case req <- evaluateRequest{snap, plan, nodeID}: outstanding++ nodeIDList = nodeIDList[1:] case r := <-resp: outstanding-- // Handle a result that allows us to cancel evaluation, // which may save time processing additional entries. if cancel := handleResult(r.nodeID, r.fit, r.err); cancel { didCancel = true break } } } // Drain the remaining results for outstanding > 0 { r := <-resp if !didCancel { if cancel := handleResult(r.nodeID, r.fit, r.err); cancel { didCancel = true } } outstanding-- } // If the plan resulted in a partial commit, we need to determine // a minimum refresh index to force the scheduler to work on a more // up-to-date state to avoid the failures. if partialCommit { allocIndex, err := snap.Index("allocs") if err != nil { mErr.Errors = append(mErr.Errors, err) } nodeIndex, err := snap.Index("nodes") if err != nil { mErr.Errors = append(mErr.Errors, err) } result.RefreshIndex = maxUint64(nodeIndex, allocIndex) if result.RefreshIndex == 0 { err := fmt.Errorf("partialCommit with RefreshIndex of 0 (%d node, %d alloc)", nodeIndex, allocIndex) mErr.Errors = append(mErr.Errors, err) } } return result, mErr.ErrorOrNil() }