2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-08-14 01:51:08 +00:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
2016-01-26 21:58:07 +00:00
|
|
|
"fmt"
|
2015-08-14 01:51:08 +00:00
|
|
|
"reflect"
|
2015-12-08 16:24:51 +00:00
|
|
|
"runtime"
|
2015-08-14 01:51:08 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2015-08-14 01:51:08 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2015-08-14 04:55:37 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-01-19 16:09:30 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-08-14 01:51:08 +00:00
|
|
|
)
|
|
|
|
|
2016-01-26 21:58:07 +00:00
|
|
|
func BenchmarkServiceStack_With_ComputedClass(b *testing.B) {
|
|
|
|
// Key doesn't escape computed node class.
|
|
|
|
benchmarkServiceStack_MetaKeyConstraint(b, "key", 5000, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkServiceStack_WithOut_ComputedClass(b *testing.B) {
|
|
|
|
// Key escapes computed node class.
|
|
|
|
benchmarkServiceStack_MetaKeyConstraint(b, "unique.key", 5000, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
// benchmarkServiceStack_MetaKeyConstraint creates the passed number of nodes
|
|
|
|
// and sets the meta data key to have nodePartitions number of values. It then
|
|
|
|
// benchmarks the stack by selecting a job that constrains against one of the
|
|
|
|
// partitions.
|
|
|
|
func benchmarkServiceStack_MetaKeyConstraint(b *testing.B, key string, numNodes, nodePartitions int) {
|
|
|
|
_, ctx := testContext(b)
|
|
|
|
stack := NewGenericStack(false, ctx)
|
|
|
|
|
|
|
|
// Create 4 classes of nodes.
|
|
|
|
nodes := make([]*structs.Node, numNodes)
|
|
|
|
for i := 0; i < numNodes; i++ {
|
|
|
|
n := mock.Node()
|
|
|
|
n.Meta[key] = fmt.Sprintf("%d", i%nodePartitions)
|
|
|
|
nodes[i] = n
|
|
|
|
}
|
|
|
|
stack.SetNodes(nodes)
|
|
|
|
|
|
|
|
// Create a job whose constraint meets two node classes.
|
|
|
|
job := mock.Job()
|
|
|
|
job.Constraints[0] = &structs.Constraint{
|
2016-02-05 00:50:20 +00:00
|
|
|
LTarget: fmt.Sprintf("${meta.%v}", key),
|
2016-01-26 21:58:07 +00:00
|
|
|
RTarget: "1",
|
|
|
|
Operand: "<",
|
|
|
|
}
|
|
|
|
stack.SetJob(job)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2018-01-14 22:47:21 +00:00
|
|
|
selectOptions := &SelectOptions{}
|
2016-01-26 21:58:07 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2018-01-14 22:47:21 +00:00
|
|
|
stack.Select(job.TaskGroups[0], selectOptions)
|
2016-01-26 21:58:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-07 18:30:13 +00:00
|
|
|
func TestServiceStack_SetNodes(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-09-07 18:30:13 +00:00
|
|
|
_, ctx := testContext(t)
|
2015-10-17 00:05:23 +00:00
|
|
|
stack := NewGenericStack(false, ctx)
|
2015-09-07 18:30:13 +00:00
|
|
|
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
2015-09-11 19:03:41 +00:00
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
2015-09-07 18:30:13 +00:00
|
|
|
}
|
|
|
|
stack.SetNodes(nodes)
|
|
|
|
|
2015-09-11 19:03:41 +00:00
|
|
|
// Check that our scan limit is updated
|
|
|
|
if stack.limit.limit != 3 {
|
|
|
|
t.Fatalf("bad limit %d", stack.limit.limit)
|
|
|
|
}
|
|
|
|
|
2015-09-07 18:30:13 +00:00
|
|
|
out := collectFeasible(stack.source)
|
|
|
|
if !reflect.DeepEqual(out, nodes) {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:51:08 +00:00
|
|
|
func TestServiceStack_SetJob(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-08-14 01:51:08 +00:00
|
|
|
_, ctx := testContext(t)
|
2015-10-17 00:05:23 +00:00
|
|
|
stack := NewGenericStack(false, ctx)
|
2015-08-14 01:51:08 +00:00
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
stack.SetJob(job)
|
|
|
|
|
|
|
|
if stack.binPack.priority != job.Priority {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(stack.jobConstraint.constraints, job.Constraints) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceStack_Select_Size(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-08-14 04:55:37 +00:00
|
|
|
_, ctx := testContext(t)
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
}
|
2015-10-17 00:05:23 +00:00
|
|
|
stack := NewGenericStack(false, ctx)
|
|
|
|
stack.SetNodes(nodes)
|
2015-08-14 04:55:37 +00:00
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
stack.SetJob(job)
|
2018-01-14 22:47:21 +00:00
|
|
|
selectOptions := &SelectOptions{}
|
2018-10-02 20:36:04 +00:00
|
|
|
node := stack.Select(job.TaskGroups[0], selectOptions)
|
2015-08-14 04:55:37 +00:00
|
|
|
if node == nil {
|
|
|
|
t.Fatalf("missing node %#v", ctx.Metrics())
|
|
|
|
}
|
2015-08-14 04:58:55 +00:00
|
|
|
|
2015-12-08 16:24:51 +00:00
|
|
|
// Note: On Windows time.Now currently has a best case granularity of 1ms.
|
|
|
|
// We skip the following assertion on Windows because this test usually
|
|
|
|
// runs too fast to measure an allocation time on Windows.
|
2015-08-14 04:58:55 +00:00
|
|
|
met := ctx.Metrics()
|
2015-12-08 16:24:51 +00:00
|
|
|
if runtime.GOOS != "windows" && met.AllocationTime == 0 {
|
2015-08-14 04:58:55 +00:00
|
|
|
t.Fatalf("missing time")
|
|
|
|
}
|
2015-08-14 01:51:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 22:36:30 +00:00
|
|
|
func TestServiceStack_Select_PreferringNodes(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2016-08-30 22:36:30 +00:00
|
|
|
_, ctx := testContext(t)
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
}
|
|
|
|
stack := NewGenericStack(false, ctx)
|
|
|
|
stack.SetNodes(nodes)
|
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
stack.SetJob(job)
|
|
|
|
|
|
|
|
// Create a preferred node
|
|
|
|
preferredNode := mock.Node()
|
2018-01-19 16:09:30 +00:00
|
|
|
prefNodes := []*structs.Node{preferredNode}
|
|
|
|
selectOptions := &SelectOptions{PreferredNodes: prefNodes}
|
2018-10-02 20:36:04 +00:00
|
|
|
option := stack.Select(job.TaskGroups[0], selectOptions)
|
2016-08-30 22:36:30 +00:00
|
|
|
if option == nil {
|
|
|
|
t.Fatalf("missing node %#v", ctx.Metrics())
|
|
|
|
}
|
|
|
|
if option.Node.ID != preferredNode.ID {
|
|
|
|
t.Fatalf("expected: %v, actual: %v", option.Node.ID, preferredNode.ID)
|
|
|
|
}
|
|
|
|
|
2018-01-19 16:09:30 +00:00
|
|
|
// Make sure select doesn't have a side effect on preferred nodes
|
|
|
|
require.Equal(t, prefNodes, selectOptions.PreferredNodes)
|
|
|
|
|
2016-08-30 22:36:30 +00:00
|
|
|
// Change the preferred node's kernel to windows and ensure the allocations
|
|
|
|
// are placed elsewhere
|
|
|
|
preferredNode1 := preferredNode.Copy()
|
|
|
|
preferredNode1.Attributes["kernel.name"] = "windows"
|
|
|
|
preferredNode1.ComputeClass()
|
2018-01-19 16:09:30 +00:00
|
|
|
prefNodes1 := []*structs.Node{preferredNode1}
|
|
|
|
selectOptions = &SelectOptions{PreferredNodes: prefNodes1}
|
2018-10-02 20:36:04 +00:00
|
|
|
option = stack.Select(job.TaskGroups[0], selectOptions)
|
2016-08-30 22:36:30 +00:00
|
|
|
if option == nil {
|
|
|
|
t.Fatalf("missing node %#v", ctx.Metrics())
|
|
|
|
}
|
|
|
|
|
|
|
|
if option.Node.ID != nodes[0].ID {
|
|
|
|
t.Fatalf("expected: %#v, actual: %#v", nodes[0], option.Node)
|
|
|
|
}
|
2018-01-19 16:09:30 +00:00
|
|
|
require.Equal(t, prefNodes1, selectOptions.PreferredNodes)
|
2016-08-30 22:36:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 01:51:08 +00:00
|
|
|
func TestServiceStack_Select_MetricsReset(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-08-14 04:55:37 +00:00
|
|
|
_, ctx := testContext(t)
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
}
|
2015-10-17 00:05:23 +00:00
|
|
|
stack := NewGenericStack(false, ctx)
|
|
|
|
stack.SetNodes(nodes)
|
2015-08-14 04:55:37 +00:00
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
stack.SetJob(job)
|
2018-01-14 22:47:21 +00:00
|
|
|
selectOptions := &SelectOptions{}
|
2018-10-02 20:36:04 +00:00
|
|
|
n1 := stack.Select(job.TaskGroups[0], selectOptions)
|
2015-08-14 04:55:37 +00:00
|
|
|
m1 := ctx.Metrics()
|
|
|
|
if n1 == nil {
|
|
|
|
t.Fatalf("missing node %#v", m1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if m1.NodesEvaluated != 2 {
|
|
|
|
t.Fatalf("should only be 2")
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:36:04 +00:00
|
|
|
n2 := stack.Select(job.TaskGroups[0], selectOptions)
|
2015-08-14 04:55:37 +00:00
|
|
|
m2 := ctx.Metrics()
|
|
|
|
if n2 == nil {
|
|
|
|
t.Fatalf("missing node %#v", m2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't reset, this would be 4
|
|
|
|
if m2.NodesEvaluated != 2 {
|
|
|
|
t.Fatalf("should only be 2")
|
|
|
|
}
|
2015-08-14 01:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceStack_Select_DriverFilter(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-08-14 04:55:37 +00:00
|
|
|
_, ctx := testContext(t)
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
}
|
|
|
|
zero := nodes[0]
|
|
|
|
zero.Attributes["driver.foo"] = "1"
|
2016-01-26 18:07:33 +00:00
|
|
|
if err := zero.ComputeClass(); err != nil {
|
|
|
|
t.Fatalf("ComputedClass() failed: %v", err)
|
|
|
|
}
|
2015-08-14 04:55:37 +00:00
|
|
|
|
2015-10-17 00:05:23 +00:00
|
|
|
stack := NewGenericStack(false, ctx)
|
|
|
|
stack.SetNodes(nodes)
|
2015-08-14 04:55:37 +00:00
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
job.TaskGroups[0].Tasks[0].Driver = "foo"
|
|
|
|
stack.SetJob(job)
|
|
|
|
|
2018-01-14 22:47:21 +00:00
|
|
|
selectOptions := &SelectOptions{}
|
2018-10-02 20:36:04 +00:00
|
|
|
node := stack.Select(job.TaskGroups[0], selectOptions)
|
2015-08-14 04:55:37 +00:00
|
|
|
if node == nil {
|
|
|
|
t.Fatalf("missing node %#v", ctx.Metrics())
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Node != zero {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-08-14 01:51:08 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 15:13:21 +00:00
|
|
|
func TestServiceStack_Select_CSI(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2020-01-31 15:13:21 +00:00
|
|
|
state, ctx := testContext(t)
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a volume in the state store
|
2020-02-03 16:59:00 +00:00
|
|
|
index := uint64(999)
|
2021-03-18 19:35:11 +00:00
|
|
|
v := structs.NewCSIVolume("foo[0]", index)
|
2020-01-31 15:13:21 +00:00
|
|
|
v.Namespace = structs.DefaultNamespace
|
|
|
|
v.AccessMode = structs.CSIVolumeAccessModeMultiNodeSingleWriter
|
|
|
|
v.AttachmentMode = structs.CSIVolumeAttachmentModeFilesystem
|
|
|
|
v.PluginID = "bar"
|
2022-03-07 16:06:59 +00:00
|
|
|
err := state.UpsertCSIVolume(999, []*structs.CSIVolume{v})
|
2020-01-31 15:13:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Create a node with healthy fingerprints for both controller and node plugins
|
|
|
|
zero := nodes[0]
|
|
|
|
zero.CSIControllerPlugins = map[string]*structs.CSIInfo{"bar": {
|
|
|
|
PluginID: "bar",
|
|
|
|
Healthy: true,
|
|
|
|
RequiresTopologies: false,
|
|
|
|
ControllerInfo: &structs.CSIControllerInfo{
|
|
|
|
SupportsReadOnlyAttach: true,
|
|
|
|
SupportsListVolumes: true,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
zero.CSINodePlugins = map[string]*structs.CSIInfo{"bar": {
|
|
|
|
PluginID: "bar",
|
|
|
|
Healthy: true,
|
|
|
|
RequiresTopologies: false,
|
|
|
|
NodeInfo: &structs.CSINodeInfo{
|
|
|
|
ID: zero.ID,
|
|
|
|
MaxVolumes: 2,
|
|
|
|
AccessibleTopology: nil,
|
|
|
|
RequiresNodeStageVolume: false,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
// Add the node to the state store to index the healthy plugins and mark the volume "foo" healthy
|
2020-10-19 13:30:15 +00:00
|
|
|
err = state.UpsertNode(structs.MsgTypeTestSetup, 1000, zero)
|
2020-01-31 15:13:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Use the node to build the stack and test
|
|
|
|
if err := zero.ComputeClass(); err != nil {
|
|
|
|
t.Fatalf("ComputedClass() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stack := NewGenericStack(false, ctx)
|
|
|
|
stack.SetNodes(nodes)
|
|
|
|
|
|
|
|
job := mock.Job()
|
2021-03-18 19:35:11 +00:00
|
|
|
job.TaskGroups[0].Count = 2
|
2020-01-31 15:13:21 +00:00
|
|
|
job.TaskGroups[0].Volumes = map[string]*structs.VolumeRequest{"foo": {
|
|
|
|
Name: "bar",
|
|
|
|
Type: structs.VolumeTypeCSI,
|
|
|
|
Source: "foo",
|
|
|
|
ReadOnly: true,
|
2021-03-18 19:35:11 +00:00
|
|
|
PerAlloc: true,
|
2020-01-31 15:13:21 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
stack.SetJob(job)
|
|
|
|
|
2021-03-18 19:35:11 +00:00
|
|
|
selectOptions := &SelectOptions{
|
|
|
|
AllocName: structs.AllocName(job.Name, job.TaskGroups[0].Name, 0)}
|
2020-01-31 15:13:21 +00:00
|
|
|
node := stack.Select(job.TaskGroups[0], selectOptions)
|
|
|
|
if node == nil {
|
|
|
|
t.Fatalf("missing node %#v", ctx.Metrics())
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Node != zero {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:51:08 +00:00
|
|
|
func TestServiceStack_Select_ConstraintFilter(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-08-14 04:55:37 +00:00
|
|
|
_, ctx := testContext(t)
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
}
|
|
|
|
zero := nodes[0]
|
2015-08-28 08:30:47 +00:00
|
|
|
zero.Attributes["kernel.name"] = "freebsd"
|
2016-01-26 18:07:33 +00:00
|
|
|
if err := zero.ComputeClass(); err != nil {
|
|
|
|
t.Fatalf("ComputedClass() failed: %v", err)
|
|
|
|
}
|
2015-08-14 04:55:37 +00:00
|
|
|
|
2015-10-17 00:05:23 +00:00
|
|
|
stack := NewGenericStack(false, ctx)
|
|
|
|
stack.SetNodes(nodes)
|
2015-08-14 04:55:37 +00:00
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
job.Constraints[0].RTarget = "freebsd"
|
|
|
|
stack.SetJob(job)
|
2018-01-14 22:47:21 +00:00
|
|
|
selectOptions := &SelectOptions{}
|
2018-10-02 20:36:04 +00:00
|
|
|
node := stack.Select(job.TaskGroups[0], selectOptions)
|
2015-08-14 04:55:37 +00:00
|
|
|
if node == nil {
|
|
|
|
t.Fatalf("missing node %#v", ctx.Metrics())
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Node != zero {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
met := ctx.Metrics()
|
2015-08-14 04:58:55 +00:00
|
|
|
if met.NodesFiltered != 1 {
|
|
|
|
t.Fatalf("bad: %#v", met)
|
|
|
|
}
|
|
|
|
if met.ClassFiltered["linux-medium-pci"] != 1 {
|
|
|
|
t.Fatalf("bad: %#v", met)
|
|
|
|
}
|
2016-02-05 00:50:20 +00:00
|
|
|
if met.ConstraintFiltered["${attr.kernel.name} = freebsd"] != 1 {
|
2015-08-14 04:55:37 +00:00
|
|
|
t.Fatalf("bad: %#v", met)
|
|
|
|
}
|
2015-08-14 01:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceStack_Select_BinPack_Overflow(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-08-14 04:55:37 +00:00
|
|
|
_, ctx := testContext(t)
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
}
|
|
|
|
zero := nodes[0]
|
|
|
|
one := nodes[1]
|
2018-10-03 16:47:18 +00:00
|
|
|
one.ReservedResources = &structs.NodeReservedResources{
|
|
|
|
Cpu: structs.NodeReservedCpuResources{
|
2018-10-04 21:33:09 +00:00
|
|
|
CpuShares: one.NodeResources.Cpu.CpuShares,
|
2018-10-03 16:47:18 +00:00
|
|
|
},
|
|
|
|
}
|
2015-08-14 04:55:37 +00:00
|
|
|
|
2015-10-17 00:05:23 +00:00
|
|
|
stack := NewGenericStack(false, ctx)
|
|
|
|
stack.SetNodes(nodes)
|
2015-08-14 04:55:37 +00:00
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
stack.SetJob(job)
|
2018-01-14 22:47:21 +00:00
|
|
|
selectOptions := &SelectOptions{}
|
2018-10-02 20:36:04 +00:00
|
|
|
node := stack.Select(job.TaskGroups[0], selectOptions)
|
2018-08-08 14:41:56 +00:00
|
|
|
ctx.Metrics().PopulateScoreMetaData()
|
2015-08-14 04:55:37 +00:00
|
|
|
if node == nil {
|
|
|
|
t.Fatalf("missing node %#v", ctx.Metrics())
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Node != zero {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
met := ctx.Metrics()
|
|
|
|
if met.NodesExhausted != 1 {
|
|
|
|
t.Fatalf("bad: %#v", met)
|
|
|
|
}
|
2015-08-14 04:58:55 +00:00
|
|
|
if met.ClassExhausted["linux-medium-pci"] != 1 {
|
|
|
|
t.Fatalf("bad: %#v", met)
|
|
|
|
}
|
2018-08-17 23:44:00 +00:00
|
|
|
// Expect score metadata for one node
|
|
|
|
if len(met.ScoreMetaData) != 1 {
|
2015-08-14 04:58:55 +00:00
|
|
|
t.Fatalf("bad: %#v", met)
|
|
|
|
}
|
2015-08-14 01:51:08 +00:00
|
|
|
}
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
func TestSystemStack_SetNodes(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-10-14 23:43:06 +00:00
|
|
|
_, ctx := testContext(t)
|
2020-10-09 21:31:38 +00:00
|
|
|
stack := NewSystemStack(false, ctx)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
}
|
|
|
|
stack.SetNodes(nodes)
|
|
|
|
|
|
|
|
out := collectFeasible(stack.source)
|
|
|
|
if !reflect.DeepEqual(out, nodes) {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemStack_SetJob(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-10-14 23:43:06 +00:00
|
|
|
_, ctx := testContext(t)
|
2020-10-09 21:31:38 +00:00
|
|
|
stack := NewSystemStack(false, ctx)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
stack.SetJob(job)
|
|
|
|
|
|
|
|
if stack.binPack.priority != job.Priority {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(stack.jobConstraint.constraints, job.Constraints) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemStack_Select_Size(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-10-14 23:43:06 +00:00
|
|
|
_, ctx := testContext(t)
|
2015-10-17 00:05:23 +00:00
|
|
|
nodes := []*structs.Node{mock.Node()}
|
2020-10-09 21:31:38 +00:00
|
|
|
stack := NewSystemStack(false, ctx)
|
2015-10-17 00:05:23 +00:00
|
|
|
stack.SetNodes(nodes)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
stack.SetJob(job)
|
2018-01-14 22:47:21 +00:00
|
|
|
selectOptions := &SelectOptions{}
|
2018-10-02 20:36:04 +00:00
|
|
|
node := stack.Select(job.TaskGroups[0], selectOptions)
|
2015-10-14 23:43:06 +00:00
|
|
|
if node == nil {
|
|
|
|
t.Fatalf("missing node %#v", ctx.Metrics())
|
|
|
|
}
|
|
|
|
|
2015-12-08 16:24:51 +00:00
|
|
|
// Note: On Windows time.Now currently has a best case granularity of 1ms.
|
|
|
|
// We skip the following assertion on Windows because this test usually
|
|
|
|
// runs too fast to measure an allocation time on Windows.
|
2015-10-14 23:43:06 +00:00
|
|
|
met := ctx.Metrics()
|
2015-12-08 16:24:51 +00:00
|
|
|
if runtime.GOOS != "windows" && met.AllocationTime == 0 {
|
2015-10-14 23:43:06 +00:00
|
|
|
t.Fatalf("missing time")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemStack_Select_MetricsReset(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-10-14 23:43:06 +00:00
|
|
|
_, ctx := testContext(t)
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
}
|
2020-10-09 21:31:38 +00:00
|
|
|
stack := NewSystemStack(false, ctx)
|
2015-10-17 00:05:23 +00:00
|
|
|
stack.SetNodes(nodes)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
stack.SetJob(job)
|
2018-01-14 22:47:21 +00:00
|
|
|
selectOptions := &SelectOptions{}
|
2018-10-02 20:36:04 +00:00
|
|
|
n1 := stack.Select(job.TaskGroups[0], selectOptions)
|
2015-10-14 23:43:06 +00:00
|
|
|
m1 := ctx.Metrics()
|
|
|
|
if n1 == nil {
|
|
|
|
t.Fatalf("missing node %#v", m1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if m1.NodesEvaluated != 1 {
|
|
|
|
t.Fatalf("should only be 1")
|
|
|
|
}
|
|
|
|
|
2018-10-02 20:36:04 +00:00
|
|
|
n2 := stack.Select(job.TaskGroups[0], selectOptions)
|
2015-10-14 23:43:06 +00:00
|
|
|
m2 := ctx.Metrics()
|
|
|
|
if n2 == nil {
|
|
|
|
t.Fatalf("missing node %#v", m2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't reset, this would be 2
|
|
|
|
if m2.NodesEvaluated != 1 {
|
|
|
|
t.Fatalf("should only be 2")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemStack_Select_DriverFilter(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-10-14 23:43:06 +00:00
|
|
|
_, ctx := testContext(t)
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
}
|
|
|
|
zero := nodes[0]
|
|
|
|
zero.Attributes["driver.foo"] = "1"
|
|
|
|
|
2020-10-09 21:31:38 +00:00
|
|
|
stack := NewSystemStack(false, ctx)
|
2015-10-17 00:05:23 +00:00
|
|
|
stack.SetNodes(nodes)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
job.TaskGroups[0].Tasks[0].Driver = "foo"
|
|
|
|
stack.SetJob(job)
|
|
|
|
|
2018-01-14 22:47:21 +00:00
|
|
|
selectOptions := &SelectOptions{}
|
2018-10-02 20:36:04 +00:00
|
|
|
node := stack.Select(job.TaskGroups[0], selectOptions)
|
2015-10-14 23:43:06 +00:00
|
|
|
if node == nil {
|
|
|
|
t.Fatalf("missing node %#v", ctx.Metrics())
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Node != zero {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
zero.Attributes["driver.foo"] = "0"
|
2016-01-26 18:07:33 +00:00
|
|
|
if err := zero.ComputeClass(); err != nil {
|
|
|
|
t.Fatalf("ComputedClass() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-10-09 21:31:38 +00:00
|
|
|
stack = NewSystemStack(false, ctx)
|
2015-10-17 00:05:23 +00:00
|
|
|
stack.SetNodes(nodes)
|
2015-10-14 23:43:06 +00:00
|
|
|
stack.SetJob(job)
|
2018-10-02 20:36:04 +00:00
|
|
|
node = stack.Select(job.TaskGroups[0], selectOptions)
|
2015-10-14 23:43:06 +00:00
|
|
|
if node != nil {
|
|
|
|
t.Fatalf("node not filtered %#v", node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemStack_Select_ConstraintFilter(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-10-14 23:43:06 +00:00
|
|
|
_, ctx := testContext(t)
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
}
|
|
|
|
zero := nodes[1]
|
|
|
|
zero.Attributes["kernel.name"] = "freebsd"
|
2016-01-26 18:07:33 +00:00
|
|
|
if err := zero.ComputeClass(); err != nil {
|
|
|
|
t.Fatalf("ComputedClass() failed: %v", err)
|
|
|
|
}
|
2015-10-14 23:43:06 +00:00
|
|
|
|
2020-10-09 21:31:38 +00:00
|
|
|
stack := NewSystemStack(false, ctx)
|
2015-10-17 00:05:23 +00:00
|
|
|
stack.SetNodes(nodes)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
job.Constraints[0].RTarget = "freebsd"
|
|
|
|
stack.SetJob(job)
|
|
|
|
|
2018-01-14 22:47:21 +00:00
|
|
|
selectOptions := &SelectOptions{}
|
2018-10-02 20:36:04 +00:00
|
|
|
node := stack.Select(job.TaskGroups[0], selectOptions)
|
2015-10-14 23:43:06 +00:00
|
|
|
if node == nil {
|
|
|
|
t.Fatalf("missing node %#v", ctx.Metrics())
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Node != zero {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
met := ctx.Metrics()
|
|
|
|
if met.NodesFiltered != 1 {
|
|
|
|
t.Fatalf("bad: %#v", met)
|
|
|
|
}
|
|
|
|
if met.ClassFiltered["linux-medium-pci"] != 1 {
|
|
|
|
t.Fatalf("bad: %#v", met)
|
|
|
|
}
|
2016-02-05 00:50:20 +00:00
|
|
|
if met.ConstraintFiltered["${attr.kernel.name} = freebsd"] != 1 {
|
2015-10-14 23:43:06 +00:00
|
|
|
t.Fatalf("bad: %#v", met)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemStack_Select_BinPack_Overflow(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2015-10-14 23:43:06 +00:00
|
|
|
_, ctx := testContext(t)
|
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
}
|
|
|
|
zero := nodes[0]
|
2018-10-03 16:47:18 +00:00
|
|
|
zero.ReservedResources = &structs.NodeReservedResources{
|
|
|
|
Cpu: structs.NodeReservedCpuResources{
|
2018-10-04 21:33:09 +00:00
|
|
|
CpuShares: zero.NodeResources.Cpu.CpuShares,
|
2018-10-03 16:47:18 +00:00
|
|
|
},
|
|
|
|
}
|
2015-10-14 23:43:06 +00:00
|
|
|
one := nodes[1]
|
|
|
|
|
2020-10-09 21:31:38 +00:00
|
|
|
stack := NewSystemStack(false, ctx)
|
2015-10-17 00:05:23 +00:00
|
|
|
stack.SetNodes(nodes)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
stack.SetJob(job)
|
|
|
|
|
2018-01-14 22:47:21 +00:00
|
|
|
selectOptions := &SelectOptions{}
|
2018-10-02 20:36:04 +00:00
|
|
|
node := stack.Select(job.TaskGroups[0], selectOptions)
|
2018-08-08 14:41:56 +00:00
|
|
|
ctx.Metrics().PopulateScoreMetaData()
|
2015-10-14 23:43:06 +00:00
|
|
|
if node == nil {
|
|
|
|
t.Fatalf("missing node %#v", ctx.Metrics())
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Node != one {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
met := ctx.Metrics()
|
|
|
|
if met.NodesExhausted != 1 {
|
|
|
|
t.Fatalf("bad: %#v", met)
|
|
|
|
}
|
|
|
|
if met.ClassExhausted["linux-medium-pci"] != 1 {
|
|
|
|
t.Fatalf("bad: %#v", met)
|
|
|
|
}
|
2018-07-16 13:47:18 +00:00
|
|
|
// Should have two scores, one from bin packing and one from normalization
|
2018-08-17 23:44:00 +00:00
|
|
|
if len(met.ScoreMetaData) != 1 {
|
2015-10-14 23:43:06 +00:00
|
|
|
t.Fatalf("bad: %#v", met)
|
|
|
|
}
|
|
|
|
}
|