diff --git a/nomad/state/state_store.go b/nomad/state/state_store.go index 754b1aa1c..fa6cb0840 100644 --- a/nomad/state/state_store.go +++ b/nomad/state/state_store.go @@ -679,8 +679,13 @@ func (s *StateStore) UpsertNode(index uint64, node *structs.Node) error { // DeleteNode deregisters a batch of nodes func (s *StateStore) DeleteNode(index uint64, nodes []string) error { + if len(nodes) == 0 { + return nil + } + txn := s.db.Txn(true) defer txn.Abort() + for _, nodeID := range nodes { existing, err := txn.First("nodes", "id", nodeID) if err != nil { @@ -694,10 +699,12 @@ func (s *StateStore) DeleteNode(index uint64, nodes []string) error { if err := txn.Delete("nodes", existing); err != nil { return fmt.Errorf("node delete failed: %s: %v", nodeID, err) } - if err := txn.Insert("index", &IndexEntry{"nodes", index}); err != nil { - return fmt.Errorf("index update failed: %s: %v", nodeID, err) - } } + + if err := txn.Insert("index", &IndexEntry{"nodes", index}); err != nil { + return fmt.Errorf("index update failed: %v", err) + } + txn.Commit() return nil } diff --git a/nomad/state/state_store_test.go b/nomad/state/state_store_test.go index f1822ce7b..68e6d4ab3 100644 --- a/nomad/state/state_store_test.go +++ b/nomad/state/state_store_test.go @@ -809,49 +809,45 @@ func TestStateStore_UpsertNode_Node(t *testing.T) { func TestStateStore_DeleteNode_Node(t *testing.T) { state := testStateStore(t) - node := mock.Node() - err := state.UpsertNode(1000, node) - if err != nil { - t.Fatalf("err: %v", err) - } + // Create and insert two nodes, which we'll delete + node0 := mock.Node() + node1 := mock.Node() + err := state.UpsertNode(1000, node0) + require.NoError(t, err) + err = state.UpsertNode(1001, node1) + require.NoError(t, err) // Create a watchset so we can test that delete fires the watch ws := memdb.NewWatchSet() - if _, err := state.NodeByID(ws, node.ID); err != nil { - t.Fatalf("bad: %v", err) - } - err = state.DeleteNode(1001, []string{node.ID}) - if err != nil { - t.Fatalf("err: %v", err) - } + // Check that both nodes are not nil + out, err := state.NodeByID(ws, node0.ID) + require.NoError(t, err) + require.NotNil(t, out) + out, err = state.NodeByID(ws, node1.ID) + require.NoError(t, err) + require.NotNil(t, out) - if !watchFired(ws) { - t.Fatalf("bad") - } + // Delete both nodes in a batch, fires the watch + err = state.DeleteNode(1002, []string{node0.ID, node1.ID}) + require.NoError(t, err) + require.True(t, watchFired(ws)) + // Check that both nodes are nil ws = memdb.NewWatchSet() - out, err := state.NodeByID(ws, node.ID) - if err != nil { - t.Fatalf("err: %v", err) - } - - if out != nil { - t.Fatalf("bad: %#v %#v", node, out) - } + out, err = state.NodeByID(ws, node0.ID) + require.NoError(t, err) + require.Nil(t, out) + out, err = state.NodeByID(ws, node1.ID) + require.NoError(t, err) + require.Nil(t, out) + // Ensure that the index is still at 1002, from DeleteNode index, err := state.Index("nodes") - if err != nil { - t.Fatalf("err: %v", err) - } - if index != 1001 { - t.Fatalf("bad: %d", index) - } - - if watchFired(ws) { - t.Fatalf("bad") - } + require.NoError(t, err) + require.Equal(t, uint64(1002), index) + require.False(t, watchFired(ws)) } func TestStateStore_UpdateNodeStatus_Node(t *testing.T) {