diff --git a/api/go.mod b/api/go.mod index a5e1ac306..bda68d40e 100644 --- a/api/go.mod +++ b/api/go.mod @@ -13,7 +13,7 @@ require ( github.com/mitchellh/go-testing-interface v1.14.1 github.com/mitchellh/mapstructure v1.5.0 github.com/shoenig/test v0.6.6 - golang.org/x/exp v0.0.0-20230108222341-4b8118a2686a + golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 ) require ( diff --git a/api/go.sum b/api/go.sum index a9f09bd2c..8a826edf1 100644 --- a/api/go.sum +++ b/api/go.sum @@ -36,8 +36,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -golang.org/x/exp v0.0.0-20230108222341-4b8118a2686a h1:tlXy25amD5A7gOfbXdqCGN5k8ESEed/Ee1E5RcrYnqU= -golang.org/x/exp v0.0.0-20230108222341-4b8118a2686a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 h1:/yRP+0AN7mf5DkD3BAI6TOFnd51gEoDEb8o35jIFtgw= +golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/client/allocrunner/taskrunner/getter/params.go b/client/allocrunner/taskrunner/getter/params.go index d2e1b8a23..29f1b5cba 100644 --- a/client/allocrunner/taskrunner/getter/params.go +++ b/client/allocrunner/taskrunner/getter/params.go @@ -109,13 +109,19 @@ func (p *parameters) Equal(o *parameters) bool { return false case p.TaskDir != o.TaskDir: return false - case !maps.EqualFunc(p.Headers, o.Headers, slices.Equal[string]): + case !maps.EqualFunc(p.Headers, o.Headers, headersCompareFn): return false } return true } +func headersCompareFn(a []string, b []string) bool { + slices.Sort(a) + slices.Sort(b) + return slices.Equal(a, b) +} + const ( // stop privilege escalation via setuid/setgid // https://github.com/hashicorp/nomad/issues/6176 diff --git a/client/allocrunner/taskrunner/getter/params_test.go b/client/allocrunner/taskrunner/getter/params_test.go index 15f9b77c8..560767c8b 100644 --- a/client/allocrunner/taskrunner/getter/params_test.go +++ b/client/allocrunner/taskrunner/getter/params_test.go @@ -135,6 +135,14 @@ func TestParameters_Equal_headers(t *testing.T) { // equal must.Equal(t, p1, p2) + // equal + must.Equal(t, p1, ¶meters{ + Headers: map[string][]string{ + "West": {"California"}, + "East": {"Florida", "New York"}, + }, + }) + // not equal p2.Headers["East"] = []string{"New York"} must.NotEqual(t, p1, p2) diff --git a/nomad/job_endpoint_test.go b/nomad/job_endpoint_test.go index cab6dec9e..79ba20506 100644 --- a/nomad/job_endpoint_test.go +++ b/nomad/job_endpoint_test.go @@ -267,8 +267,22 @@ func TestJobEndpoint_Register_NonOverlapping(t *testing.T) { return false, fmt.Errorf("expected 2 allocs but found %d:\n%v", n, allocResp.Allocations) } - slices.SortFunc(allocResp.Allocations, func(a, b *structs.AllocListStub) bool { - return a.CreateIndex < b.CreateIndex + slices.SortFunc(allocResp.Allocations, func(a, b *structs.AllocListStub) int { + var result int + // cmp(a, b) should return + // a positive number when a > b + if a.CreateIndex > b.CreateIndex { + result = 1 + } + // a negative number when a < b, + if a.CreateIndex < b.CreateIndex { + result = -1 + } + // zero when a == b. + result = 0 + + // invert the comparison to sort descending. + return result * -1 }) if alloc.ID != allocResp.Allocations[0].ID { diff --git a/nomad/state/state_store.go b/nomad/state/state_store.go index 463486a3f..445849fd0 100644 --- a/nomad/state/state_store.go +++ b/nomad/state/state_store.go @@ -5521,8 +5521,18 @@ func (s *StateStore) pruneJobSubmissions(namespace, jobID string, txn *txn) erro } // sort by job modify index descending so we can just keep the first N - slices.SortFunc(stored, func(a, b lang.Pair[uint64, uint64]) bool { - return a.First > b.First + slices.SortFunc(stored, func(a, b lang.Pair[uint64, uint64]) int { + var cmp int = 0 + if a.First < b.First { + cmp = -1 + } + if a.First > b.First { + cmp = +1 + } + + // Convert the sort into a descending sort by inverting the sign + cmp = cmp * -1 + return cmp }) // remove the outdated submission versions