diff --git a/nomad/plan_apply.go b/nomad/plan_apply.go index b58f9e1ce..ae9f85b50 100644 --- a/nomad/plan_apply.go +++ b/nomad/plan_apply.go @@ -82,7 +82,7 @@ func (s *Server) planApply() { } // Dispatch the Raft transaction for the plan - future, err := s.applyPlan(result) + future, err := s.applyPlan(result, snap) if err != nil { s.logger.Printf("[ERR] nomad: failed to submit plan: %v", err) pending.respond(nil, err) @@ -97,7 +97,7 @@ func (s *Server) planApply() { } // applyPlan is used to apply the plan result and to return the alloc index -func (s *Server) applyPlan(result *structs.PlanResult) (raft.ApplyFuture, error) { +func (s *Server) applyPlan(result *structs.PlanResult, snap *state.StateSnapshot) (raft.ApplyFuture, error) { req := structs.AllocUpdateRequest{} for _, updateList := range result.NodeUpdate { req.Alloc = append(req.Alloc, updateList...) @@ -107,7 +107,20 @@ func (s *Server) applyPlan(result *structs.PlanResult) (raft.ApplyFuture, error) } req.Alloc = append(req.Alloc, result.FailedAllocs...) - return s.raftApplyFuture(structs.AllocUpdateRequestType, &req) + // Dispatch the Raft transaction + future, err := s.raftApplyFuture(structs.AllocUpdateRequestType, &req) + if err != nil { + return nil, err + } + + // Optimistically apply to our state view + if snap != nil { + nextIdx := s.raft.AppliedIndex() + 1 + if err := snap.UpsertAllocs(nextIdx, req.Alloc); err != nil { + return future, err + } + } + return future, nil } // asyncPlanWait is used to apply and respond to a plan async diff --git a/nomad/plan_apply_test.go b/nomad/plan_apply_test.go index 654e22f6d..d4c5dde0f 100644 --- a/nomad/plan_apply_test.go +++ b/nomad/plan_apply_test.go @@ -55,7 +55,7 @@ func TestPlanApply_applyPlan(t *testing.T) { } // Apply the plan - future, err := s1.applyPlan(plan) + future, err := s1.applyPlan(plan, nil) if err != nil { t.Fatalf("err: %v", err) } @@ -100,7 +100,7 @@ func TestPlanApply_applyPlan(t *testing.T) { } // Apply the plan - future, err = s1.applyPlan(plan) + future, err = s1.applyPlan(plan, nil) if err != nil { t.Fatalf("err: %v", err) }