consul/state: node checks are removed with their associated nodes

This commit is contained in:
Ryan Uber 2015-08-29 11:10:32 -07:00 committed by James Phillips
parent 329c88d8b7
commit 5d63160bb6
2 changed files with 44 additions and 3 deletions

View File

@ -173,7 +173,7 @@ func (s *StateStore) deleteNodeTxn(idx uint64, nodeID string, tx *memdb.Txn) err
return fmt.Errorf("node lookup failed: %s", err)
}
// Delete all services associated with the node
// Delete all services associated with the node and update the service index
services, err := tx.Get("services", "node", nodeID)
if err != nil {
return fmt.Errorf("failed service lookup: %s", err)
@ -181,9 +181,27 @@ func (s *StateStore) deleteNodeTxn(idx uint64, nodeID string, tx *memdb.Txn) err
for service := services.Next(); service != nil; service = services.Next() {
svc := service.(*structs.ServiceNode)
if err := s.deleteNodeServiceTxn(idx, nodeID, svc.ServiceID, tx); err != nil {
return fmt.Errorf("failed removing node service: %s", err)
return err
}
}
if err := tx.Insert("index", &IndexEntry{"services", idx}); err != nil {
return fmt.Errorf("failed updating index: %s", err)
}
// Delete all checks associated with the node and update the check index
checks, err := tx.Get("checks", "node", nodeID)
if err != nil {
return fmt.Errorf("failed check lookup: %s", err)
}
for check := checks.Next(); check != nil; check = checks.Next() {
chk := check.(*structs.HealthCheck)
if err := s.deleteCheckTxn(idx, nodeID, chk.CheckID, tx); err != nil {
return err
}
}
if err := tx.Insert("index", &IndexEntry{"checks", idx}); err != nil {
return fmt.Errorf("failed updating index: %s", err)
}
// Delete the node and update the index
if err := tx.Delete("nodes", node); err != nil {

View File

@ -154,12 +154,26 @@ func TestStateStore_DeleteNode(t *testing.T) {
t.Fatalf("bad: %#v (err: %s)", services.Services, err)
}
// Register a check with the node service
chk := &structs.HealthCheck{
Node: "node1",
CheckID: "check1",
}
if err := s.EnsureCheck(3, chk); err != nil {
t.Fatalf("err: %s", err)
}
// Check exists
if checks, err := s.NodeChecks("node1"); err != nil || len(checks) != 1 {
t.Fatalf("bad: %#v (err: %s)", checks, err)
}
// Delete the node
if err := s.DeleteNode(3, "node1"); err != nil {
t.Fatalf("err: %s", err)
}
// The node and service are gone and the index was updated
// The node was removed
if n, err := s.GetNode("node1"); err != nil || n != nil {
t.Fatalf("bad: %#v (err: %#v)", node, err)
}
@ -176,6 +190,15 @@ func TestStateStore_DeleteNode(t *testing.T) {
t.Fatalf("bad: %#v", s)
}
// Associated health check was removed.
checks, err := tx.Get("checks", "id", "node1", "check1")
if err != nil {
t.Fatalf("err: %s", err)
}
if c := checks.Next(); c != nil {
t.Fatalf("bad: %#v", c)
}
// Indexes were updated.
for _, tbl := range []string{"nodes", "services"} {
if idx := s.maxIndex(tbl); idx != 3 {