Skip to content

Commit f0f75ab

Browse files
authored
Merge pull request #3372 from hashicorp/b-versions
Fix sorting of job versions
2 parents be053a4 + 3dd2810 commit f0f75ab

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ IMPROVEMENTS:
2121
BUG FIXES:
2222
* core: Fix restoration of stopped periodic jobs [GH-3201]
2323
* core: Run deployment garbage collector on an interval [GH-3267]
24+
* core: Fix issue in which job versions above a threshold potentially wouldn't
25+
be stored [GH-3372]
2426
* core: Fix issue where node-drain with complete batch allocation would create
2527
replacement [GH-3217]
2628
* core: Fix issue in which batch allocations from previous job versions may not

nomad/state/state_store.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"log"
8+
"sort"
89

910
"github.com/hashicorp/go-memdb"
1011
multierror "github.com/hashicorp/go-multierror"
@@ -1017,10 +1018,10 @@ func (s *StateStore) jobVersionByID(txn *memdb.Txn, ws *memdb.WatchSet, namespac
10171018
all = append(all, j)
10181019
}
10191020

1020-
// Reverse so that highest versions first
1021-
for i, j := 0, len(all)-1; i < j; i, j = i+1, j-1 {
1022-
all[i], all[j] = all[j], all[i]
1023-
}
1021+
// Sort in reverse order so that the highest version is first
1022+
sort.Slice(all, func(i, j int) bool {
1023+
return all[i].Version > all[j].Version
1024+
})
10241025

10251026
return all, nil
10261027
}

nomad/state/state_store_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ func TestStateStore_UpdateUpsertJob_JobVersion(t *testing.T) {
12261226
// Create a job and mark it as stable
12271227
job := mock.Job()
12281228
job.Stable = true
1229-
job.Priority = 0
1229+
job.Name = "0"
12301230

12311231
// Create a watchset so we can test that upsert fires the watch
12321232
ws := memdb.NewWatchSet()
@@ -1244,10 +1244,10 @@ func TestStateStore_UpdateUpsertJob_JobVersion(t *testing.T) {
12441244
}
12451245

12461246
var finalJob *structs.Job
1247-
for i := 1; i < 20; i++ {
1247+
for i := 1; i < 300; i++ {
12481248
finalJob = mock.Job()
12491249
finalJob.ID = job.ID
1250-
finalJob.Priority = i
1250+
finalJob.Name = fmt.Sprintf("%d", i)
12511251
err = state.UpsertJob(uint64(1000+i), finalJob)
12521252
if err != nil {
12531253
t.Fatalf("err: %v", err)
@@ -1267,18 +1267,18 @@ func TestStateStore_UpdateUpsertJob_JobVersion(t *testing.T) {
12671267
if out.CreateIndex != 1000 {
12681268
t.Fatalf("bad: %#v", out)
12691269
}
1270-
if out.ModifyIndex != 1019 {
1270+
if out.ModifyIndex != 1299 {
12711271
t.Fatalf("bad: %#v", out)
12721272
}
1273-
if out.Version != 19 {
1273+
if out.Version != 299 {
12741274
t.Fatalf("bad: %#v", out)
12751275
}
12761276

12771277
index, err := state.Index("job_version")
12781278
if err != nil {
12791279
t.Fatalf("err: %v", err)
12801280
}
1281-
if index != 1019 {
1281+
if index != 1299 {
12821282
t.Fatalf("bad: %d", index)
12831283
}
12841284

@@ -1288,19 +1288,19 @@ func TestStateStore_UpdateUpsertJob_JobVersion(t *testing.T) {
12881288
t.Fatalf("err: %v", err)
12891289
}
12901290
if len(allVersions) != structs.JobTrackedVersions {
1291-
t.Fatalf("got %d; want 1", len(allVersions))
1291+
t.Fatalf("got %d; want %d", len(allVersions), structs.JobTrackedVersions)
12921292
}
12931293

1294-
if a := allVersions[0]; a.ID != job.ID || a.Version != 19 || a.Priority != 19 {
1294+
if a := allVersions[0]; a.ID != job.ID || a.Version != 299 || a.Name != "299" {
12951295
t.Fatalf("bad: %+v", a)
12961296
}
1297-
if a := allVersions[1]; a.ID != job.ID || a.Version != 18 || a.Priority != 18 {
1297+
if a := allVersions[1]; a.ID != job.ID || a.Version != 298 || a.Name != "298" {
12981298
t.Fatalf("bad: %+v", a)
12991299
}
13001300

13011301
// Ensure we didn't delete the stable job
13021302
if a := allVersions[structs.JobTrackedVersions-1]; a.ID != job.ID ||
1303-
a.Version != 0 || a.Priority != 0 || !a.Stable {
1303+
a.Version != 0 || a.Name != "0" || !a.Stable {
13041304
t.Fatalf("bad: %+v", a)
13051305
}
13061306

0 commit comments

Comments
 (0)