From 8f0d2a2775dbe35162e8beccb07215003f02197e Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Fri, 22 Jul 2016 14:53:49 -0700 Subject: [PATCH] Fixed some more tests --- scheduler/context_test.go | 4 ++++ scheduler/generic_sched.go | 17 +---------------- scheduler/generic_sched_test.go | 3 +++ scheduler/rank_test.go | 8 ++++++++ scheduler/system_sched.go | 17 +---------------- scheduler/system_sched_test.go | 3 +++ scheduler/util.go | 21 +++++++++++++++++++++ scheduler/util_test.go | 11 ++++++++--- 8 files changed, 49 insertions(+), 35 deletions(-) diff --git a/scheduler/context_test.go b/scheduler/context_test.go index 4b0e4011f..00027c779 100644 --- a/scheduler/context_test.go +++ b/scheduler/context_test.go @@ -64,6 +64,7 @@ func TestEvalContext_ProposedAlloc(t *testing.T) { }, DesiredStatus: structs.AllocDesiredStatusRun, ClientStatus: structs.AllocClientStatusPending, + TaskGroup: "web", } alloc2 := &structs.Allocation{ ID: structs.GenerateUUID(), @@ -76,7 +77,10 @@ func TestEvalContext_ProposedAlloc(t *testing.T) { }, DesiredStatus: structs.AllocDesiredStatusRun, ClientStatus: structs.AllocClientStatusPending, + TaskGroup: "web", } + noErr(t, state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID))) + noErr(t, state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))) noErr(t, state.UpsertAllocs(1000, []*structs.Allocation{alloc1, alloc2})) // Add a planned eviction to alloc1 diff --git a/scheduler/generic_sched.go b/scheduler/generic_sched.go index 0e1aaf746..8f863b5dc 100644 --- a/scheduler/generic_sched.go +++ b/scheduler/generic_sched.go @@ -247,22 +247,7 @@ func (s *GenericScheduler) process() (bool, error) { // Decrement the number of allocations pending per task group based on the // number of allocations successfully placed - if result != nil { - for _, allocations := range result.NodeAllocation { - for _, allocation := range allocations { - // Ensure that the allocation is newly created - if allocation.CreateIndex != result.AllocIndex { - continue - } - - if _, ok := s.queuedAllocs[allocation.TaskGroup]; ok { - s.queuedAllocs[allocation.TaskGroup] -= 1 - } else { - s.logger.Printf("[ERR] sched: allocation %q placed but not in list of unplaced allocations", allocation.TaskGroup) - } - } - } - } + adjustQueuedAllocations(s.logger, result, s.queuedAllocs) // If we got a state refresh, try again since we have stale data if newState != nil { diff --git a/scheduler/generic_sched_test.go b/scheduler/generic_sched_test.go index af35c21ac..9c00f1dba 100644 --- a/scheduler/generic_sched_test.go +++ b/scheduler/generic_sched_test.go @@ -1118,6 +1118,9 @@ func TestServiceSched_JobDeregister(t *testing.T) { alloc.JobID = job.ID allocs = append(allocs, alloc) } + for _, alloc := range allocs { + h.State.UpsertJobSummary(h.NextIndex(), mock.JobSummary(alloc.JobID)) + } noErr(t, h.State.UpsertAllocs(h.NextIndex(), allocs)) // Create a mock evaluation to deregister the job diff --git a/scheduler/rank_test.go b/scheduler/rank_test.go index 63af11541..f33cd5521 100644 --- a/scheduler/rank_test.go +++ b/scheduler/rank_test.go @@ -204,6 +204,7 @@ func TestBinPackIterator_ExistingAlloc(t *testing.T) { }, DesiredStatus: structs.AllocDesiredStatusRun, ClientStatus: structs.AllocClientStatusPending, + TaskGroup: "web", } alloc2 := &structs.Allocation{ ID: structs.GenerateUUID(), @@ -216,7 +217,10 @@ func TestBinPackIterator_ExistingAlloc(t *testing.T) { }, DesiredStatus: structs.AllocDesiredStatusRun, ClientStatus: structs.AllocClientStatusPending, + TaskGroup: "web", } + noErr(t, state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID))) + noErr(t, state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))) noErr(t, state.UpsertAllocs(1000, []*structs.Allocation{alloc1, alloc2})) task := &structs.Task{ @@ -280,6 +284,7 @@ func TestBinPackIterator_ExistingAlloc_PlannedEvict(t *testing.T) { }, DesiredStatus: structs.AllocDesiredStatusRun, ClientStatus: structs.AllocClientStatusPending, + TaskGroup: "web", } alloc2 := &structs.Allocation{ ID: structs.GenerateUUID(), @@ -292,7 +297,10 @@ func TestBinPackIterator_ExistingAlloc_PlannedEvict(t *testing.T) { }, DesiredStatus: structs.AllocDesiredStatusRun, ClientStatus: structs.AllocClientStatusPending, + TaskGroup: "web", } + noErr(t, state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID))) + noErr(t, state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))) noErr(t, state.UpsertAllocs(1000, []*structs.Allocation{alloc1, alloc2})) // Add a planned eviction to alloc1 diff --git a/scheduler/system_sched.go b/scheduler/system_sched.go index 746470374..76979af38 100644 --- a/scheduler/system_sched.go +++ b/scheduler/system_sched.go @@ -149,22 +149,7 @@ func (s *SystemScheduler) process() (bool, error) { // Decrement the number of allocations pending per task group based on the // number of allocations successfully placed - if result != nil { - for _, allocations := range result.NodeAllocation { - for _, allocation := range allocations { - // Ensure that the allocation is newly created - if allocation.CreateIndex != result.AllocIndex { - continue - } - - if _, ok := s.queuedAllocs[allocation.TaskGroup]; ok { - s.queuedAllocs[allocation.TaskGroup] -= 1 - } else { - s.logger.Printf("[ERR] sched: allocation %q placed but not in list of unplaced allocations", allocation.TaskGroup) - } - } - } - } + adjustQueuedAllocations(s.logger, result, s.queuedAllocs) // If we got a state refresh, try again since we have stale data if newState != nil { diff --git a/scheduler/system_sched_test.go b/scheduler/system_sched_test.go index 9ee69d48a..46865cccf 100644 --- a/scheduler/system_sched_test.go +++ b/scheduler/system_sched_test.go @@ -644,6 +644,9 @@ func TestSystemSched_JobDeregister(t *testing.T) { alloc.Name = "my-job.web[0]" allocs = append(allocs, alloc) } + for _, alloc := range allocs { + noErr(t, h.State.UpsertJobSummary(h.NextIndex(), mock.JobSummary(alloc.JobID))) + } noErr(t, h.State.UpsertAllocs(h.NextIndex(), allocs)) // Create a mock evaluation to deregister the job diff --git a/scheduler/util.go b/scheduler/util.go index 0a6a6c649..fbed1ea3b 100644 --- a/scheduler/util.go +++ b/scheduler/util.go @@ -596,3 +596,24 @@ func desiredUpdates(diff *diffResult, inplaceUpdates, return desiredTgs } + +// adjustQueuedAllocations decrements the number of allocations pending per task +// group based on the number of allocations successfully placed +func adjustQueuedAllocations(logger *log.Logger, result *structs.PlanResult, queuedAllocs map[string]int) { + if result != nil { + for _, allocations := range result.NodeAllocation { + for _, allocation := range allocations { + // Ensure that the allocation is newly created + if allocation.CreateIndex != result.AllocIndex { + continue + } + + if _, ok := queuedAllocs[allocation.TaskGroup]; ok { + queuedAllocs[allocation.TaskGroup] -= 1 + } else { + logger.Printf("[ERR] sched: allocation %q placed but not in list of unplaced allocations", allocation.TaskGroup) + } + } + } + } +} diff --git a/scheduler/util_test.go b/scheduler/util_test.go index 9bd16ee83..7484f301f 100644 --- a/scheduler/util_test.go +++ b/scheduler/util_test.go @@ -573,7 +573,7 @@ func TestInplaceUpdate_ChangedTaskGroup(t *testing.T) { job := mock.Job() node := mock.Node() - noErr(t, state.UpsertNode(1000, node)) + noErr(t, state.UpsertNode(900, node)) // Register an alloc alloc := &structs.Allocation{ @@ -587,8 +587,10 @@ func TestInplaceUpdate_ChangedTaskGroup(t *testing.T) { MemoryMB: 2048, }, DesiredStatus: structs.AllocDesiredStatusRun, + TaskGroup: "web", } alloc.TaskResources = map[string]*structs.Resources{"web": alloc.Resources} + noErr(t, state.UpsertJobSummary(1000, mock.JobSummary(alloc.JobID))) noErr(t, state.UpsertAllocs(1001, []*structs.Allocation{alloc})) // Create a new task group that prevents in-place updates. @@ -619,7 +621,7 @@ func TestInplaceUpdate_NoMatch(t *testing.T) { job := mock.Job() node := mock.Node() - noErr(t, state.UpsertNode(1000, node)) + noErr(t, state.UpsertNode(900, node)) // Register an alloc alloc := &structs.Allocation{ @@ -633,8 +635,10 @@ func TestInplaceUpdate_NoMatch(t *testing.T) { MemoryMB: 2048, }, DesiredStatus: structs.AllocDesiredStatusRun, + TaskGroup: "web", } alloc.TaskResources = map[string]*structs.Resources{"web": alloc.Resources} + noErr(t, state.UpsertJobSummary(1000, mock.JobSummary(alloc.JobID))) noErr(t, state.UpsertAllocs(1001, []*structs.Allocation{alloc})) // Create a new task group that requires too much resources. @@ -664,7 +668,7 @@ func TestInplaceUpdate_Success(t *testing.T) { job := mock.Job() node := mock.Node() - noErr(t, state.UpsertNode(1000, node)) + noErr(t, state.UpsertNode(900, node)) // Register an alloc alloc := &structs.Allocation{ @@ -681,6 +685,7 @@ func TestInplaceUpdate_Success(t *testing.T) { DesiredStatus: structs.AllocDesiredStatusRun, } alloc.TaskResources = map[string]*structs.Resources{"web": alloc.Resources} + noErr(t, state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID))) noErr(t, state.UpsertAllocs(1001, []*structs.Allocation{alloc})) // Create a new task group that updates the resources.