Skip to content

Commit

Permalink
Merge pull request #753 from hashicorp/b-computed-class
Browse files Browse the repository at this point in the history
Fix computed class when the job has multiple task groups
  • Loading branch information
dadgar committed Feb 4, 2016
2 parents 3562233 + d487295 commit 6af88ac
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
9 changes: 9 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,15 @@ type TaskGroup struct {
Meta map[string]string
}

func (tg *TaskGroup) Copy() *TaskGroup {
i, err := copystructure.Copy(tg)
if err != nil {
panic(err)
}

return i.(*TaskGroup)
}

// InitFields is used to initialize fields in the TaskGroup.
func (tg *TaskGroup) InitFields(job *Job) {
// Set the default restart policy.
Expand Down
69 changes: 69 additions & 0 deletions scheduler/generic_sched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,75 @@ func TestServiceSched_JobRegister_BlockedEval(t *testing.T) {
h.AssertEvalStatus(t, structs.EvalStatusComplete)
}

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

// Create one node
node := mock.Node()
node.NodeClass = "class_0"
noErr(t, node.ComputeClass())
noErr(t, h.State.UpsertNode(h.NextIndex(), node))

// Create a job that constrains on a node class
job := mock.Job()
job.TaskGroups[0].Count = 2
job.TaskGroups[0].Constraints = append(job.Constraints,
&structs.Constraint{
LTarget: "$node.class",
RTarget: "class_0",
Operand: "=",
},
)
tg2 := job.TaskGroups[0].Copy()
tg2.Name = "web2"
tg2.Constraints[1].RTarget = "class_1"
job.TaskGroups = append(job.TaskGroups, tg2)
noErr(t, h.State.UpsertJob(h.NextIndex(), job))

// Create a mock evaluation to register the job
eval := &structs.Evaluation{
ID: structs.GenerateUUID(),
Priority: job.Priority,
TriggeredBy: structs.EvalTriggerJobRegister,
JobID: job.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 allocated
var planned []*structs.Allocation
for _, allocList := range plan.NodeAllocation {
planned = append(planned, allocList...)
}
if len(planned) != 2 {
t.Fatalf("bad: %#v", plan)
}
if len(plan.FailedAllocs) != 1 {
t.Fatalf("bad: %#v", plan)
}

// Lookup the allocations by JobID
out, err := h.State.AllocsByJob(job.ID)
noErr(t, err)

// Ensure all allocations placed
if len(out) != 3 {
t.Fatalf("bad: %#v", out)
}

h.AssertEvalStatus(t, structs.EvalStatusComplete)
}

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

Expand Down
2 changes: 2 additions & 0 deletions scheduler/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func (s *GenericStack) Select(tg *structs.TaskGroup) (*RankedNode, *structs.Reso
s.taskGroupDrivers.SetDrivers(tgConstr.drivers)
s.taskGroupConstraint.SetConstraints(tgConstr.constraints)
s.proposedAllocConstraint.SetTaskGroup(tg)
s.wrappedChecks.SetTaskGroup(tg.Name)
s.binPack.SetTasks(tg.Tasks)

// Find the node with the max score
Expand Down Expand Up @@ -242,6 +243,7 @@ func (s *SystemStack) Select(tg *structs.TaskGroup) (*RankedNode, *structs.Resou
s.taskGroupDrivers.SetDrivers(tgConstr.drivers)
s.taskGroupConstraint.SetConstraints(tgConstr.constraints)
s.binPack.SetTasks(tg.Tasks)
s.wrappedChecks.SetTaskGroup(tg.Name)

// Get the next option that satisfies the constraints.
option := s.binPack.Next()
Expand Down

0 comments on commit 6af88ac

Please sign in to comment.