consul/state: cleanup
This commit is contained in:
parent
e114b5040f
commit
e6a9db17d7
|
@ -180,13 +180,10 @@ 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 {
|
||||
if err := s.deleteServiceTxn(idx, nodeID, svc.ServiceID, tx); err != nil {
|
||||
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)
|
||||
|
@ -199,9 +196,6 @@ func (s *StateStore) deleteNodeTxn(idx uint64, nodeID string, tx *memdb.Txn) err
|
|||
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 {
|
||||
|
@ -327,14 +321,13 @@ func (s *StateStore) NodeServices(nodeID string) (*structs.NodeServices, error)
|
|||
return ns, nil
|
||||
}
|
||||
|
||||
// DeleteNodeService is used to delete a given service associated
|
||||
// with the given node.
|
||||
func (s *StateStore) DeleteNodeService(idx uint64, nodeID, serviceID string) error {
|
||||
// DeleteService is used to delete a given service associated with a node.
|
||||
func (s *StateStore) DeleteService(idx uint64, nodeID, serviceID string) error {
|
||||
tx := s.db.Txn(true)
|
||||
defer tx.Abort()
|
||||
|
||||
// Call the service deletion
|
||||
if err := s.deleteNodeServiceTxn(idx, nodeID, serviceID, tx); err != nil {
|
||||
if err := s.deleteServiceTxn(idx, nodeID, serviceID, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -342,9 +335,9 @@ func (s *StateStore) DeleteNodeService(idx uint64, nodeID, serviceID string) err
|
|||
return nil
|
||||
}
|
||||
|
||||
// deleteNodeServiceTxn is the inner method called to remove a service
|
||||
// deleteServiceTxn is the inner method called to remove a service
|
||||
// registration within an existing transaction.
|
||||
func (s *StateStore) deleteNodeServiceTxn(idx uint64, nodeID, serviceID string, tx *memdb.Txn) error {
|
||||
func (s *StateStore) deleteServiceTxn(idx uint64, nodeID, serviceID string, tx *memdb.Txn) error {
|
||||
// Look up the service
|
||||
service, err := tx.First("services", "id", nodeID, serviceID)
|
||||
if err != nil {
|
||||
|
|
|
@ -304,7 +304,7 @@ func TestStateStore_EnsureService_NodeServices(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestStateStore_DeleteNodeService(t *testing.T) {
|
||||
func TestStateStore_DeleteService(t *testing.T) {
|
||||
s := testStateStore(t)
|
||||
|
||||
// Register a node with one service
|
||||
|
@ -321,7 +321,7 @@ func TestStateStore_DeleteNodeService(t *testing.T) {
|
|||
testRegisterCheck(t, s, 3, "node1", "service1", "check1")
|
||||
|
||||
// Delete the service
|
||||
if err := s.DeleteNodeService(4, "node1", "service1"); err != nil {
|
||||
if err := s.DeleteService(4, "node1", "service1"); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -14,23 +14,6 @@ func testStateStore() (*StateStore, error) {
|
|||
return NewStateStore(nil, os.Stderr)
|
||||
}
|
||||
|
||||
func testNodeRegister(t *testing.T, idx uint64, db *StateStore, nodeID string) {
|
||||
node := &structs.Node{Node: nodeID}
|
||||
if err := db.EnsureNode(idx, node); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
tx := db.Txn(false)
|
||||
defer tx.Abort()
|
||||
n, err := tx.First("nodes", "id", nodeID)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if result := n.(*structs.Node); result != node {
|
||||
t.Fatalf("bad node: %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureRegistration(t *testing.T) {
|
||||
store, err := testStateStore()
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue