Skip to content

Commit 76b913c

Browse files
committed
Handling allocations with client state pending
1 parent da591a2 commit 76b913c

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

nomad/node_endpoint.go

+9
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func (n *Node) Register(args *structs.NodeRegisterRequest, reply *structs.NodeUp
6666
return fmt.Errorf("invalid status for node")
6767
}
6868

69+
// Set the timestamp when the node is registered
70+
args.Node.StatusUpdatedAt = time.Now().Unix()
71+
6972
// Compute the node class
7073
if err := args.Node.ComputeClass(); err != nil {
7174
return fmt.Errorf("failed to computed node class: %v", err)
@@ -218,6 +221,9 @@ func (n *Node) UpdateStatus(args *structs.NodeUpdateStatusRequest, reply *struct
218221
return fmt.Errorf("node not found")
219222
}
220223

224+
// Update the timestamp of when the node status was updated
225+
node.StatusUpdatedAt = time.Now().Unix()
226+
221227
// Commit this update via Raft
222228
var index uint64
223229
if node.Status != args.Status {
@@ -291,6 +297,9 @@ func (n *Node) UpdateDrain(args *structs.NodeUpdateDrainRequest,
291297
return fmt.Errorf("node not found")
292298
}
293299

300+
// Update the timestamp to
301+
node.StatusUpdatedAt = time.Now().Unix()
302+
294303
// Commit this update via Raft
295304
var index uint64
296305
if node.Drain != args.Drain {

nomad/state/state_store.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ func (s *StateStore) UpdateNodeStatus(index uint64, nodeID, status string) error
201201
for _, alloc := range allocs {
202202
copyAlloc := new(structs.Allocation)
203203
*copyAlloc = *alloc
204-
if alloc.ClientStatus == structs.AllocClientStatusRunning {
204+
if alloc.ClientStatus == structs.AllocClientStatusPending ||
205+
alloc.ClientStatus == structs.AllocClientStatusRunning {
205206
copyAlloc.ClientStatus = structs.AllocClientStatusLost
206207
if err := txn.Insert("allocs", copyAlloc); err != nil {
207208
return fmt.Errorf("alloc insert failed: %v", err)

nomad/state/state_store_test.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,15 @@ func TestStateStore_UpdateNodeStatus_Node(t *testing.T) {
138138

139139
alloc := mock.Alloc()
140140
alloc1 := mock.Alloc()
141+
alloc2 := mock.Alloc()
141142
alloc.NodeID = node.ID
142143
alloc1.NodeID = node.ID
144+
alloc2.NodeID = node.ID
143145
alloc.ClientStatus = structs.AllocClientStatusRunning
144146
alloc1.ClientStatus = structs.AllocClientStatusFailed
147+
alloc2.ClientStatus = structs.AllocClientStatusPending
145148

146-
if err = state.UpsertAllocs(1002, []*structs.Allocation{alloc, alloc1}); err != nil {
149+
if err = state.UpsertAllocs(1002, []*structs.Allocation{alloc, alloc1, alloc2}); err != nil {
147150
t.Fatalf("err: %v", err)
148151
}
149152
if err = state.UpdateNodeStatus(1003, node.ID, structs.NodeStatusDown); err != nil {
@@ -163,7 +166,15 @@ func TestStateStore_UpdateNodeStatus_Node(t *testing.T) {
163166
t.Fatalf("err: %v", err)
164167
}
165168
if alloc1Out.ClientStatus != structs.AllocClientStatusFailed {
166-
t.Fatalf("expected alloc status: %v, actual: %v", structs.AllocClientStatusLost, allocOut.ClientStatus)
169+
t.Fatalf("expected alloc status: %v, actual: %v", structs.AllocClientStatusFailed, alloc1Out.ClientStatus)
170+
}
171+
172+
alloc2Out, err := state.AllocByID(alloc2.ID)
173+
if err != nil {
174+
t.Fatalf("err: %v", err)
175+
}
176+
if alloc2Out.ClientStatus != structs.AllocClientStatusLost {
177+
t.Fatalf("expected alloc status: %v, actual: %v", structs.AllocClientStatusLost, alloc2Out.ClientStatus)
167178
}
168179

169180
notify.verify(t)

nomad/structs/structs.go

+4
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,10 @@ type Node struct {
634634
// StatusDescription is meant to provide more human useful information
635635
StatusDescription string
636636

637+
// StatusUpdatedAt is the time stamp at which the state of the node was
638+
// updated
639+
StatusUpdatedAt int64
640+
637641
// Raft Indexes
638642
CreateIndex uint64
639643
ModifyIndex uint64

0 commit comments

Comments
 (0)