Skip to content

Commit

Permalink
Merge pull request #2486 from hashicorp/b-periodic-state
Browse files Browse the repository at this point in the history
Fix periodic job state
  • Loading branch information
dadgar committed Mar 28, 2017
2 parents 1f41efc + 231c0b9 commit b7cf08b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
21 changes: 18 additions & 3 deletions nomad/state/state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,15 @@ func (s *StateStore) EvalsByJob(ws memdb.WatchSet, jobID string) ([]*structs.Eva
if raw == nil {
break
}
out = append(out, raw.(*structs.Evaluation))

e := raw.(*structs.Evaluation)

// Filter non-exact matches
if e.JobID != jobID {
continue
}

out = append(out, e)
}
return out, nil
}
Expand Down Expand Up @@ -1619,9 +1627,16 @@ func (s *StateStore) getJobStatus(txn *memdb.Txn, job *structs.Job, evalDelete b
}

hasEval := false
for eval := evals.Next(); eval != nil; eval = evals.Next() {
for raw := evals.Next(); raw != nil; raw = evals.Next() {
e := raw.(*structs.Evaluation)

// Filter non-exact matches
if e.JobID != job.ID {
continue
}

hasEval = true
if !eval.(*structs.Evaluation).TerminalStatus() {
if !e.TerminalStatus() {
return structs.JobStatusPending, nil
}
}
Expand Down
55 changes: 55 additions & 0 deletions nomad/state/state_store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package state

import (
"fmt"
"os"
"reflect"
"sort"
Expand Down Expand Up @@ -494,6 +495,60 @@ func TestStateStore_UpdateUpsertJob_Job(t *testing.T) {
}
}

func TestStateStore_UpdateUpsertJob_PeriodicJob(t *testing.T) {
state := testStateStore(t)
job := mock.PeriodicJob()

// Create a watchset so we can test that upsert fires the watch
ws := memdb.NewWatchSet()
_, err := state.JobByID(ws, job.ID)
if err != nil {
t.Fatalf("bad: %v", err)
}

if err := state.UpsertJob(1000, job); err != nil {
t.Fatalf("err: %v", err)
}

// Create a child and an evaluation
job2 := job.Copy()
job2.Periodic = nil
job2.ID = fmt.Sprintf("%v/%s-1490635020", job.ID, structs.PeriodicLaunchSuffix)
err = state.UpsertJob(1001, job2)
if err != nil {
t.Fatalf("err: %v", err)
}

eval := mock.Eval()
eval.JobID = job2.ID
err = state.UpsertEvals(1002, []*structs.Evaluation{eval})
if err != nil {
t.Fatalf("err: %v", err)
}

job3 := job.Copy()
job3.TaskGroups[0].Tasks[0].Name = "new name"
err = state.UpsertJob(1003, job3)
if err != nil {
t.Fatalf("err: %v", err)
}

if !watchFired(ws) {
t.Fatalf("bad")
}

ws = memdb.NewWatchSet()
out, err := state.JobByID(ws, job.ID)
if err != nil {
t.Fatalf("err: %v", err)
}

if s, e := out.Status, structs.JobStatusRunning; s != e {
t.Fatalf("got status %v; want %v", s, e)
}

}

// This test ensures that UpsertJob creates the EphemeralDisk is a job doesn't have
// one and clear out the task's disk resource asks
// COMPAT 0.4.1 -> 0.5
Expand Down

0 comments on commit b7cf08b

Please sign in to comment.