Skip to content

Commit

Permalink
Merge pull request #750 from hashicorp/b-node-drain
Browse files Browse the repository at this point in the history
Fix node drain not working properly
  • Loading branch information
dadgar committed Feb 4, 2016
2 parents d395361 + c55ea4e commit c9716f5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion scheduler/generic_sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (s *GenericScheduler) computeJobAllocs() error {
s.limitReached = evictAndPlace(s.ctx, diff, diff.migrate, allocMigrating, &limit)

// Treat non in-place updates as an eviction and new placement.
s.limitReached = evictAndPlace(s.ctx, diff, diff.update, allocUpdating, &limit)
s.limitReached = s.limitReached || evictAndPlace(s.ctx, diff, diff.update, allocUpdating, &limit)

// Nothing remaining to do if placement is not required
if len(diff.place) == 0 {
Expand Down
80 changes: 79 additions & 1 deletion scheduler/generic_sched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ func TestServiceSched_NodeDrain(t *testing.T) {
noErr(t, h.State.UpsertNode(h.NextIndex(), node))
}

// Generate a fake job with allocations
// Generate a fake job with allocations and an update policy.
job := mock.Job()
noErr(t, h.State.UpsertJob(h.NextIndex(), job))

Expand Down Expand Up @@ -651,6 +651,84 @@ func TestServiceSched_NodeDrain(t *testing.T) {
h.AssertEvalStatus(t, structs.EvalStatusComplete)
}

func TestServiceSched_NodeDrain_UpdateStrategy(t *testing.T) {
h := NewHarness(t)

// Register a draining node
node := mock.Node()
node.Drain = true
noErr(t, h.State.UpsertNode(h.NextIndex(), node))

// Create some nodes
for i := 0; i < 10; i++ {
node := mock.Node()
noErr(t, h.State.UpsertNode(h.NextIndex(), node))
}

// Generate a fake job with allocations and an update policy.
job := mock.Job()
mp := 5
job.Update = structs.UpdateStrategy{
Stagger: time.Second,
MaxParallel: mp,
}
noErr(t, h.State.UpsertJob(h.NextIndex(), job))

var allocs []*structs.Allocation
for i := 0; i < 10; i++ {
alloc := mock.Alloc()
alloc.Job = job
alloc.JobID = job.ID
alloc.NodeID = node.ID
alloc.Name = fmt.Sprintf("my-job.web[%d]", i)
allocs = append(allocs, alloc)
}
noErr(t, h.State.UpsertAllocs(h.NextIndex(), allocs))

// Create a mock evaluation to deal with drain
eval := &structs.Evaluation{
ID: structs.GenerateUUID(),
Priority: 50,
TriggeredBy: structs.EvalTriggerNodeUpdate,
JobID: job.ID,
NodeID: node.ID,
}

// Process the evaluation
err := h.Process(NewServiceScheduler, eval)
if err != nil {
t.Fatalf("err: %v", err)
}

// Ensure a single plan
if len(h.Plans) != 1 {
t.Fatalf("bad: %#v", h.Plans)
}
plan := h.Plans[0]

// Ensure the plan evicted all allocs
if len(plan.NodeUpdate[node.ID]) != mp {
t.Fatalf("bad: %#v", plan)
}

// Ensure the plan allocated
var planned []*structs.Allocation
for _, allocList := range plan.NodeAllocation {
planned = append(planned, allocList...)
}
if len(planned) != mp {
t.Fatalf("bad: %#v", plan)
}

// Ensure there is a followup eval.
if len(h.CreateEvals) != 1 ||
h.CreateEvals[0].TriggeredBy != structs.EvalTriggerRollingUpdate {
t.Fatalf("bad: %#v", h.CreateEvals)
}

h.AssertEvalStatus(t, structs.EvalStatusComplete)
}

func TestServiceSched_RetryLimit(t *testing.T) {
h := NewHarness(t)
h.Planner = &RejectPlan{h}
Expand Down

0 comments on commit c9716f5

Please sign in to comment.