From 53a2cc9d996c52029c0f8ab9725b64f31b1334ff Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Mon, 25 Jul 2016 17:51:20 -0700 Subject: [PATCH] Setting the right indexes while creating Job Summary --- nomad/state/state_store.go | 13 ++++++++++--- nomad/state/state_store_test.go | 12 ++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/nomad/state/state_store.go b/nomad/state/state_store.go index 81586675b..779d54be1 100644 --- a/nomad/state/state_store.go +++ b/nomad/state/state_store.go @@ -1430,9 +1430,10 @@ type StateSnapshot struct { // restoring state by only using a single large transaction // instead of thousands of sub transactions type StateRestore struct { - txn *memdb.Txn - watch *stateWatch - items watch.Items + txn *memdb.Txn + watch *stateWatch + items watch.Items + latestIndex uint64 } // Abort is used to abort the restore operation @@ -1494,6 +1495,10 @@ func (r *StateRestore) IndexRestore(idx *IndexEntry) error { if err := r.txn.Insert("index", idx); err != nil { return fmt.Errorf("index insert failed: %v", err) } + + if idx.Value > r.latestIndex { + r.latestIndex = idx.Value + } return nil } @@ -1591,6 +1596,8 @@ func (r *StateRestore) CreateJobSummaries(jobs []*structs.Job) error { } // Insert the job summary + summary.CreateIndex = r.latestIndex + summary.ModifyIndex = r.latestIndex if err := r.txn.Insert("job_summary", summary); err != nil { return fmt.Errorf("error inserting job summary: %v", err) } diff --git a/nomad/state/state_store_test.go b/nomad/state/state_store_test.go index 247215476..fe996383d 100644 --- a/nomad/state/state_store_test.go +++ b/nomad/state/state_store_test.go @@ -1123,6 +1123,16 @@ func TestStateStore_CreateJobSummaries(t *testing.T) { t.Fatalf("err: %v", err) } + // Restore an Index + index := IndexEntry{ + Key: "Foo", + Value: 100, + } + + if err := restore.IndexRestore(&index); err != nil { + t.Fatalf("err: %v", err) + } + // Restore an allocation alloc := mock.Alloc() alloc.JobID = job.ID @@ -1148,6 +1158,8 @@ func TestStateStore_CreateJobSummaries(t *testing.T) { Starting: 1, }, }, + CreateIndex: 100, + ModifyIndex: 100, } if !reflect.DeepEqual(summary, &expected) {