scheduler: fix panic in system jobs when nodes filtered by class (#11565)

In the system scheduler, if a subset of clients are filtered by class,
we hit a code path where the `AllocMetric` has been copied, but the
`Copy` method does not instantiate the various maps. This leads to an
assignment to a nil map. This changeset ensures that the maps are
non-nil before continuing.

The `Copy` method relies on functions in the `helper` package that all
return nil slices or maps when passed zero-length inputs. This
changeset to fix the panic bug intentionally defers updating those
functions because it'll have potential impact on memory usage. See
https://github.com/hashicorp/nomad/issues/11564 for more details.
This commit is contained in:
Tim Gross 2021-11-24 12:28:47 -05:00 committed by Luiz Aoqui
parent 728ec744bf
commit 74768eb7d3
No known key found for this signature in database
GPG Key ID: 29F459C0D9CBB573
2 changed files with 10 additions and 0 deletions

3
.changelog/11565.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
scheduler: Fix panic when system jobs are filtered by node class
```

View File

@ -287,9 +287,16 @@ func mergeNodeFiltered(acc, curr *structs.AllocMetric) *structs.AllocMetric {
acc.NodesEvaluated += curr.NodesEvaluated acc.NodesEvaluated += curr.NodesEvaluated
acc.NodesFiltered += curr.NodesFiltered acc.NodesFiltered += curr.NodesFiltered
if acc.ClassFiltered == nil {
acc.ClassFiltered = make(map[string]int)
}
for k, v := range curr.ClassFiltered { for k, v := range curr.ClassFiltered {
acc.ClassFiltered[k] += v acc.ClassFiltered[k] += v
} }
if acc.ConstraintFiltered == nil {
acc.ConstraintFiltered = make(map[string]int)
}
for k, v := range curr.ConstraintFiltered { for k, v := range curr.ConstraintFiltered {
acc.ConstraintFiltered[k] += v acc.ConstraintFiltered[k] += v
} }