command/monitor: handle more alloc state changes

This commit is contained in:
Ryan Uber
2015-09-17 20:18:33 -07:00
parent c5674305de
commit 6719d22cd6
2 changed files with 62 additions and 22 deletions

View File

@@ -100,25 +100,32 @@ func (m *monitor) update(eval *api.Evaluation, allocs []*api.AllocationListStub)
// Check the allocations
for allocID, alloc := range update.allocs {
if existing, ok := existing.allocs[allocID]; !ok {
// Check if this is a failure indication allocation
if alloc.desired == structs.AllocDesiredStatusFailed {
m.output(fmt.Sprintf("Scheduling failed for task group %q",
switch {
case alloc.desired == structs.AllocDesiredStatusFailed:
// New allocs with desired state failed indicate
// scheduling failure.
m.output(fmt.Sprintf("Scheduling failed for group %q",
alloc.group))
} else {
if alloc.index < update.index {
m.output(fmt.Sprintf(
"Allocation %q updated (node %q, task group %q)",
alloc.id, alloc.node, alloc.group))
} else {
m.output(fmt.Sprintf(
"Allocation %q created on node %q for task group %q",
alloc.id, alloc.node, alloc.group))
}
case alloc.index < update.index:
// New alloc with create index lower than the eval
// create index indicates modification
m.output(fmt.Sprintf(
"Allocation %q modified: node %q, group %q",
alloc.id, alloc.node, alloc.group))
case alloc.desired == structs.AllocDesiredStatusRun:
// New allocation with desired status running
m.output(fmt.Sprintf(
"Allocation %q created: node %q, group %q",
alloc.id, alloc.node, alloc.group))
}
} else {
if existing.client != alloc.client {
switch {
case existing.client != alloc.client:
// Allocation status has changed
m.output(fmt.Sprintf(
"Allocation %q changed status from %q to %q",
"Allocation %q status changed: %q -> %q",
alloc.id, existing.client, alloc.client))
}
}
@@ -126,7 +133,7 @@ func (m *monitor) update(eval *api.Evaluation, allocs []*api.AllocationListStub)
// Check if the status changed
if existing.status != update.status {
m.output(fmt.Sprintf("Evaluation changed status from %q to %q",
m.output(fmt.Sprintf("Evaluation status changed: %q -> %q",
existing.status, eval.Status))
}

View File

@@ -16,9 +16,10 @@ func TestMonitor_Update(t *testing.T) {
// Basic eval updates work
eval := &api.Evaluation{
Status: "pending",
NodeID: "node1",
Wait: 10 * time.Second,
Status: "pending",
NodeID: "node1",
Wait: 10 * time.Second,
CreateIndex: 2,
}
mon.update(eval, nil)
@@ -50,14 +51,15 @@ func TestMonitor_Update(t *testing.T) {
}
ui.OutputWriter.Reset()
// Allocations write new logs
// New allocations write new logs
allocs := []*api.AllocationListStub{
&api.AllocationListStub{
ID: "alloc1",
TaskGroup: "group1",
NodeID: "node1",
DesiredStatus: "running",
ClientStatus: "pending",
DesiredStatus: structs.AllocDesiredStatusRun,
ClientStatus: structs.AllocClientStatusPending,
CreateIndex: 3,
},
}
mon.update(eval, allocs)
@@ -73,6 +75,9 @@ func TestMonitor_Update(t *testing.T) {
if !strings.Contains(out, "node1") {
t.Fatalf("missing node\n\n%s", out)
}
if !strings.Contains(out, "created") {
t.Fatalf("missing created\n\n%s", out)
}
ui.OutputWriter.Reset()
// No change yields no logs
@@ -99,8 +104,10 @@ func TestMonitor_Update(t *testing.T) {
// New allocs with desired status failed warns
allocs = append(allocs, &api.AllocationListStub{
ID: "alloc2",
TaskGroup: "group2",
DesiredStatus: structs.AllocDesiredStatusFailed,
CreateIndex: 4,
})
mon.update(eval, allocs)
@@ -112,6 +119,32 @@ func TestMonitor_Update(t *testing.T) {
if !strings.Contains(out, "Scheduling failed") {
t.Fatalf("missing failure\n\n%s", out)
}
ui.OutputWriter.Reset()
// New allocs with a create index lower than the
// eval create index are logged as modifications
allocs = append(allocs, &api.AllocationListStub{
ID: "alloc3",
NodeID: "node1",
TaskGroup: "group2",
CreateIndex: 1,
})
mon.update(eval, allocs)
// Modification was logged
out = ui.OutputWriter.String()
if !strings.Contains(out, "alloc3") {
t.Fatalf("missing alloc\n\n%s", out)
}
if !strings.Contains(out, "group2") {
t.Fatalf("missing group\n\n%s", out)
}
if !strings.Contains(out, "node1") {
t.Fatalf("missing node\n\n%s", out)
}
if !strings.Contains(out, "modified") {
t.Fatalf("missing modification\n\n%s", out)
}
}
func TestMonitor_Monitor(t *testing.T) {