// evaluateNodePlan is used to evalute the plan for a single node, // returning if the plan is valid or if an error is encountered func evaluateNodePlan(snap *state.StateSnapshot, plan *structs.Plan, nodeID string) (bool, error) { // If this is an evict-only plan, it always 'fits' since we are removing things. if len(plan.NodeAllocation[nodeID]) == 0 { return true, nil } // Get the node itself node, err := snap.NodeByID(nodeID) if err != nil { return false, fmt.Errorf("failed to get node '%s': %v", nodeID, err) } // If the node does not exist or is not ready for schduling it is not fit // XXX: There is a potential race between when we do this check and when // the Raft commit happens. if node == nil || node.Status != structs.NodeStatusReady || node.Drain { 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", nodeID, err) } // Filter on alloc state existingAlloc = structs.FilterTerminalAllocs(existingAlloc) // Determine the proposed allocation by first removing allocations // that are planned evictions and adding the new allocations. proposed := existingAlloc var remove []*structs.Allocation if update := plan.NodeUpdate[nodeID]; len(update) > 0 { remove = append(remove, update...) } if updated := plan.NodeAllocation[nodeID]; len(updated) > 0 { for _, alloc := range updated { remove = append(remove, alloc) } } proposed = structs.RemoveAllocs(existingAlloc, remove) proposed = append(proposed, plan.NodeAllocation[nodeID]...) // Check if these allocations fit fit, _, _, err := structs.AllocsFit(node, proposed, nil) return fit, err }
func (iter *BinPackIterator) Next() *RankedNode { OUTER: for { // Get the next potential option option := iter.source.Next() if option == nil { return nil } // Get the proposed allocations proposed, err := option.ProposedAllocs(iter.ctx) if err != nil { iter.ctx.Logger().Printf( "[ERR] sched.binpack: failed to get proposed allocations: %v", err) continue } // Index the existing network usage netIdx := structs.NewNetworkIndex() netIdx.SetNode(option.Node) netIdx.AddAllocs(proposed) // Assign the resources for each task total := new(structs.Resources) for _, task := range iter.tasks { taskResources := task.Resources.Copy() // Check if we need a network resource if len(taskResources.Networks) > 0 { ask := taskResources.Networks[0] offer, err := netIdx.AssignNetwork(ask) if offer == nil { iter.ctx.Metrics().ExhaustedNode(option.Node, fmt.Sprintf("network: %s", err)) continue OUTER } // Reserve this to prevent another task from colliding netIdx.AddReserved(offer) // Update the network ask to the offer taskResources.Networks = []*structs.NetworkResource{offer} } // Store the task resource option.SetTaskResources(task, taskResources) // Accumulate the total resource requirement total.Add(taskResources) } // Add the resources we are trying to fit proposed = append(proposed, &structs.Allocation{Resources: total}) // Check if these allocations fit, if they do not, simply skip this node fit, dim, util, _ := structs.AllocsFit(option.Node, proposed, netIdx) if !fit { iter.ctx.Metrics().ExhaustedNode(option.Node, dim) continue } // XXX: For now we completely ignore evictions. We should use that flag // to determine if its possible to evict other lower priority allocations // to make room. This explodes the search space, so it must be done // carefully. // Score the fit normally otherwise fitness := structs.ScoreFit(option.Node, util) option.Score += fitness iter.ctx.Metrics().ScoreNode(option.Node, "binpack", fitness) return option } }