diff --git a/nomad/fsm.go b/nomad/fsm.go index 9a0884d07f1..4cdd93fae57 100644 --- a/nomad/fsm.go +++ b/nomad/fsm.go @@ -374,7 +374,7 @@ func (n *nomadFSM) applyAllocUpdate(buf []byte, index uint64) interface{} { // prior to being inserted into MemDB. if j := req.Job; j != nil { for _, alloc := range req.Alloc { - if alloc.Job == nil { + if alloc.Job == nil && !alloc.TerminalStatus() { alloc.Job = j } } diff --git a/nomad/fsm_test.go b/nomad/fsm_test.go index ddf92dd7724..34bb6310221 100644 --- a/nomad/fsm_test.go +++ b/nomad/fsm_test.go @@ -577,9 +577,12 @@ func TestFSM_UpsertAllocs_SharedJob(t *testing.T) { t.Fatalf("bad: %#v %#v", alloc, out) } + // Ensure that the original job is used evictAlloc := new(structs.Allocation) *evictAlloc = *alloc - job = evictAlloc.Job + job = mock.Job() + job.Priority = 123 + evictAlloc.Job = nil evictAlloc.DesiredStatus = structs.AllocDesiredStatusEvict req2 := structs.AllocUpdateRequest{ @@ -604,8 +607,8 @@ func TestFSM_UpsertAllocs_SharedJob(t *testing.T) { if out.DesiredStatus != structs.AllocDesiredStatusEvict { t.Fatalf("alloc found!") } - if out.Job == nil { - t.Fatalf("missing job") + if out.Job == nil || out.Job.Priority == 123 { + t.Fatalf("bad job") } } diff --git a/nomad/state/state_store.go b/nomad/state/state_store.go index e3b123b4717..83b0573d6ae 100644 --- a/nomad/state/state_store.go +++ b/nomad/state/state_store.go @@ -961,6 +961,11 @@ func (s *StateStore) UpsertAllocs(index uint64, allocs []*structs.Allocation) er alloc.AllocModifyIndex = index alloc.ClientStatus = exist.ClientStatus alloc.ClientDescription = exist.ClientDescription + + // The job has been denormalized so re-attach the original job + if alloc.Job == nil { + alloc.Job = exist.Job + } } if err := txn.Insert("allocs", alloc); err != nil { return fmt.Errorf("alloc insert failed: %v", err)