Skip to content

Commit

Permalink
Fix sorting of job versions
Browse files Browse the repository at this point in the history
Fixes an issue in which the versions were improperly sorted which would
cause pruning of the wrong job version. This essentially meant that job
versions above 255 would be dropped from the job version table (note
this was due to the prefix walk crossing from the 1-byte to 2-byte
threshold).

Fixes #3357
  • Loading branch information
dadgar committed Oct 12, 2017
1 parent 45a16a4 commit 0664625
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
9 changes: 5 additions & 4 deletions nomad/state/state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"log"
"sort"

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

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

return all, nil
}
Expand Down
20 changes: 10 additions & 10 deletions nomad/state/state_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ func TestStateStore_UpdateUpsertJob_JobVersion(t *testing.T) {
// Create a job and mark it as stable
job := mock.Job()
job.Stable = true
job.Priority = 0
job.Name = "0"

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

var finalJob *structs.Job
for i := 1; i < 20; i++ {
for i := 1; i < 300; i++ {
finalJob = mock.Job()
finalJob.ID = job.ID
finalJob.Priority = i
finalJob.Name = fmt.Sprintf("%d", i)
err = state.UpsertJob(uint64(1000+i), finalJob)
if err != nil {
t.Fatalf("err: %v", err)
Expand All @@ -1267,18 +1267,18 @@ func TestStateStore_UpdateUpsertJob_JobVersion(t *testing.T) {
if out.CreateIndex != 1000 {
t.Fatalf("bad: %#v", out)
}
if out.ModifyIndex != 1019 {
if out.ModifyIndex != 1299 {
t.Fatalf("bad: %#v", out)
}
if out.Version != 19 {
if out.Version != 299 {
t.Fatalf("bad: %#v", out)
}

index, err := state.Index("job_version")
if err != nil {
t.Fatalf("err: %v", err)
}
if index != 1019 {
if index != 1299 {
t.Fatalf("bad: %d", index)
}

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

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

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

Expand Down

0 comments on commit 0664625

Please sign in to comment.