Merge pull request #1505 from hashicorp/f-fix-summary

Fix the logic for updating summary
This commit is contained in:
Diptanu Choudhury 2016-08-02 16:24:51 -07:00 committed by GitHub
commit c02f9a52df
2 changed files with 63 additions and 9 deletions

View file

@ -889,11 +889,6 @@ func (s *StateStore) nestedUpdateAllocFromClient(txn *memdb.Txn, watcher watch.I
return nil
}
exist := existing.(*structs.Allocation)
if err := s.updateSummaryWithAlloc(index, alloc, exist, watcher, txn); err != nil {
return fmt.Errorf("error updating job summary: %v", err)
}
// Trigger the watcher
watcher.Add(watch.Item{Alloc: alloc.ID})
watcher.Add(watch.Item{AllocEval: exist.EvalID})
@ -912,6 +907,10 @@ func (s *StateStore) nestedUpdateAllocFromClient(txn *memdb.Txn, watcher watch.I
// Update the modify index
copyAlloc.ModifyIndex = index
if err := s.updateSummaryWithAlloc(index, copyAlloc, exist, watcher, txn); err != nil {
return fmt.Errorf("error updating job summary: %v", err)
}
// Update the allocation
if err := txn.Insert("allocs", copyAlloc); err != nil {
return fmt.Errorf("alloc insert failed: %v", err)
@ -947,10 +946,6 @@ func (s *StateStore) UpsertAllocs(index uint64, allocs []*structs.Allocation) er
}
exist, _ := existing.(*structs.Allocation)
if err := s.updateSummaryWithAlloc(index, alloc, exist, watcher, txn); err != nil {
return fmt.Errorf("error updating job summary: %v", err)
}
if exist == nil {
alloc.CreateIndex = index
alloc.ModifyIndex = index
@ -962,6 +957,11 @@ func (s *StateStore) UpsertAllocs(index uint64, allocs []*structs.Allocation) er
alloc.ClientStatus = exist.ClientStatus
alloc.ClientDescription = exist.ClientDescription
}
if err := s.updateSummaryWithAlloc(index, alloc, exist, watcher, txn); err != nil {
return fmt.Errorf("error updating job summary: %v", err)
}
if err := txn.Insert("allocs", alloc); err != nil {
return fmt.Errorf("alloc insert failed: %v", err)
}
@ -1404,6 +1404,7 @@ func (s *StateStore) updateSummaryWithAlloc(index uint64, alloc *structs.Allocat
case structs.AllocClientStatusLost:
tgSummary.Lost -= 1
case structs.AllocClientStatusFailed, structs.AllocClientStatusComplete:
default:
s.logger.Printf("[ERR] state_store: invalid old state of allocation with id: %v, and state: %v",
existingAlloc.ID, existingAlloc.ClientStatus)
}

View file

@ -2026,6 +2026,59 @@ func TestStateStore_UpdateAlloc_NoJob(t *testing.T) {
}
}
func TestStateStore_JobSummary(t *testing.T) {
state := testStateStore(t)
// Add a job
job := mock.Job()
state.UpsertJob(900, job)
// Upser an allocation
alloc := mock.Alloc()
alloc.JobID = job.ID
alloc.Job = job
state.UpsertAllocs(910, []*structs.Allocation{alloc})
// Update the alloc from client
alloc1 := alloc.Copy()
alloc1.ClientStatus = structs.AllocClientStatusPending
alloc1.DesiredStatus = ""
state.UpdateAllocsFromClient(920, []*structs.Allocation{alloc})
alloc3 := alloc.Copy()
alloc3.ClientStatus = structs.AllocClientStatusRunning
alloc3.DesiredStatus = ""
state.UpdateAllocsFromClient(930, []*structs.Allocation{alloc3})
// Upsert the alloc
alloc4 := alloc.Copy()
alloc4.ClientStatus = structs.AllocClientStatusPending
alloc4.DesiredStatus = structs.AllocDesiredStatusRun
state.UpsertAllocs(950, []*structs.Allocation{alloc4})
// Again upsert the alloc
alloc5 := alloc.Copy()
alloc5.ClientStatus = structs.AllocClientStatusPending
alloc5.DesiredStatus = structs.AllocDesiredStatusRun
state.UpsertAllocs(970, []*structs.Allocation{alloc5})
expectedSummary := structs.JobSummary{
JobID: job.ID,
Summary: map[string]structs.TaskGroupSummary{
"web": structs.TaskGroupSummary{
Running: 1,
},
},
CreateIndex: 900,
ModifyIndex: 930,
}
summary, _ := state.JobSummaryByID(job.ID)
if !reflect.DeepEqual(&expectedSummary, summary) {
t.Fatalf("expected: %#v, actual: %v", expectedSummary, summary)
}
}
func TestStateStore_EvictAlloc_Alloc(t *testing.T) {
state := testStateStore(t)
alloc := mock.Alloc()