Initialize task state in allocation sent by scheduler

This commit is contained in:
Alex Dadgar 2015-11-12 17:47:51 -08:00
parent 359251744f
commit bdf7497f1b
5 changed files with 42 additions and 1 deletions

View File

@ -1,8 +1,9 @@
package mock
import (
"github.com/hashicorp/nomad/nomad/structs"
"time"
"github.com/hashicorp/nomad/nomad/structs"
)
func Node() *structs.Node {
@ -221,6 +222,11 @@ func Alloc() *structs.Allocation {
},
},
},
TaskStates: map[string]*structs.TaskState{
"web": &structs.TaskState{
State: structs.TaskStatePending,
},
},
Job: Job(),
DesiredStatus: structs.AllocDesiredStatusRun,
ClientStatus: structs.AllocClientStatusPending,

View File

@ -285,11 +285,13 @@ func (s *GenericScheduler) computePlacements(place []allocTuple) error {
alloc.TaskResources = option.TaskResources
alloc.DesiredStatus = structs.AllocDesiredStatusRun
alloc.ClientStatus = structs.AllocClientStatusPending
alloc.TaskStates = initTaskState(missing.TaskGroup, structs.TaskStatePending)
s.plan.AppendAlloc(alloc)
} else {
alloc.DesiredStatus = structs.AllocDesiredStatusFailed
alloc.DesiredDescription = "failed to find a node for placement"
alloc.ClientStatus = structs.AllocClientStatusFailed
alloc.TaskStates = initTaskState(missing.TaskGroup, structs.TaskStateDead)
s.plan.AppendFailed(alloc)
failedTG[missing.TaskGroup] = alloc
}

View File

@ -252,11 +252,13 @@ func (s *SystemScheduler) computePlacements(place []allocTuple) error {
alloc.TaskResources = option.TaskResources
alloc.DesiredStatus = structs.AllocDesiredStatusRun
alloc.ClientStatus = structs.AllocClientStatusPending
alloc.TaskStates = initTaskState(missing.TaskGroup, structs.TaskStatePending)
s.plan.AppendAlloc(alloc)
} else {
alloc.DesiredStatus = structs.AllocDesiredStatusFailed
alloc.DesiredDescription = "failed to find a node for placement"
alloc.ClientStatus = structs.AllocClientStatusFailed
alloc.TaskStates = initTaskState(missing.TaskGroup, structs.TaskStateDead)
s.plan.AppendFailed(alloc)
failedTG[missing.TaskGroup] = alloc
}

View File

@ -445,3 +445,11 @@ func taskGroupConstraints(tg *structs.TaskGroup) tgConstrainTuple {
return c
}
func initTaskState(tg *structs.TaskGroup, state string) map[string]*structs.TaskState {
states := make(map[string]*structs.TaskState, len(tg.Tasks))
for _, task := range tg.Tasks {
states[task.Name] = &structs.TaskState{State: state}
}
return states
}

View File

@ -648,3 +648,26 @@ func TestTaskGroupConstraints(t *testing.T) {
}
}
func TestInitTaskState(t *testing.T) {
tg := &structs.TaskGroup{
Tasks: []*structs.Task{
&structs.Task{Name: "foo"},
&structs.Task{Name: "bar"},
},
}
expPending := map[string]*structs.TaskState{
"foo": &structs.TaskState{State: structs.TaskStatePending},
"bar": &structs.TaskState{State: structs.TaskStatePending},
}
expDead := map[string]*structs.TaskState{
"foo": &structs.TaskState{State: structs.TaskStateDead},
"bar": &structs.TaskState{State: structs.TaskStateDead},
}
actPending := initTaskState(tg, structs.TaskStatePending)
actDead := initTaskState(tg, structs.TaskStateDead)
if !(reflect.DeepEqual(expPending, actPending) && reflect.DeepEqual(expDead, actDead)) {
t.Fatal("Expected and actual not equal")
}
}