Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/25340.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
scheduler: Use core ID when selecting cores. This fixes a panic in the scheduler when the `reservable_cores` is not a contiguous list of core IDs.
```
13 changes: 10 additions & 3 deletions scheduler/numa_ce.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
package scheduler

import (
"cmp"
"math/rand"
"slices"

"github.com/hashicorp/nomad/client/lib/idset"
"github.com/hashicorp/nomad/client/lib/numalib"
"github.com/hashicorp/nomad/client/lib/numalib/hw"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/nomad/structs"
)

Expand All @@ -28,10 +29,16 @@ type coreSelector struct {
func (cs *coreSelector) Select(ask *structs.Resources) ([]uint16, hw.MHz) {
cores := cs.availableCores.Slice()[0:ask.Cores]
mhz := hw.MHz(0)
ids := make([]uint16, 0, ask.Cores)
sortedTopologyCores := make([]numalib.Core, len(cs.topology.Cores))
copy(sortedTopologyCores, cs.topology.Cores)
slices.SortFunc(sortedTopologyCores, func(a, b numalib.Core) int { return cmp.Compare(a.ID, b.ID) })
Comment on lines +33 to +35
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know what you think about leaving it like this, or if it's better to rework how the cores are stored in the struct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine; we want to keep that data structure the same due to requirements in the enterprise version.

for _, core := range cores {
mhz += cs.topology.Cores[core].MHz()
if i, found := slices.BinarySearchFunc(sortedTopologyCores, core, func(c numalib.Core, id hw.CoreID) int { return cmp.Compare(c.ID, id) }); found {
mhz += cs.topology.Cores[i].MHz()
ids = append(ids, uint16(cs.topology.Cores[i].ID))
}
}
ids := helper.ConvertSlice(cores, func(id hw.CoreID) uint16 { return uint16(id) })
return ids, mhz
}

Expand Down
92 changes: 92 additions & 0 deletions scheduler/numa_ce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package scheduler

import (
"testing"

"github.com/hashicorp/nomad/client/lib/idset"
"github.com/hashicorp/nomad/client/lib/numalib"
"github.com/hashicorp/nomad/client/lib/numalib/hw"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/stretchr/testify/require"
)

func TestCoreSelectorSelect(t *testing.T) {
var (
totalCores = 46
maxSpeed = 100
coreIds = make([]uint16, totalCores)
cores = make([]numalib.Core, totalCores)
)
for i := 1; i < 24; i++ {
coreIds[i-1] = uint16(i)
cores[i-1] = numalib.Core{
SocketID: 0,
NodeID: 0,
ID: hw.CoreID(i),
Grade: false,
Disable: false,
BaseSpeed: 0,
MaxSpeed: hw.MHz(maxSpeed),
GuessSpeed: 0,
}
}
for i := 25; i < 48; i++ {
coreIds[i-2] = uint16(i)
cores[i-2] = numalib.Core{
SocketID: 0,
NodeID: 0,
ID: hw.CoreID(i),
Grade: false,
Disable: false,
BaseSpeed: 0,
MaxSpeed: hw.MHz(maxSpeed),
GuessSpeed: 0,
}
}
require.Equal(t, coreIds, []uint16{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47})

selector := &coreSelector{
topology: &numalib.Topology{
Cores: cores,
},
availableCores: idset.From[hw.CoreID](coreIds),
}

for _, test := range []struct {
name string
resources *structs.Resources
expectedIds []uint16
expectedMhz hw.MHz
}{
{
name: "request all cores",
resources: &structs.Resources{
Cores: totalCores,
},
expectedIds: coreIds,
expectedMhz: hw.MHz(totalCores * maxSpeed),
},
{
name: "request half the cores",
resources: &structs.Resources{
Cores: 10,
},
expectedIds: coreIds[:10],
expectedMhz: hw.MHz(10 * maxSpeed),
},
{
name: "request one core",
resources: &structs.Resources{
Cores: 1,
},
expectedIds: coreIds[:1],
expectedMhz: hw.MHz(1 * maxSpeed),
},
} {
t.Run(test.name, func(t *testing.T) {
ids, mhz := selector.Select(test.resources)
require.Equal(t, test.expectedIds, ids)
require.Equal(t, test.expectedMhz, mhz)
})
}
}
Loading