From 2bab9b6bdbb50a691a23fa4f803fd5f7d6e12e8c Mon Sep 17 00:00:00 2001 From: Santhosh Manohar Date: Wed, 10 Aug 2016 12:44:05 -0700 Subject: [PATCH] Cleanup networkdb state when the network is deleted locally Signed-off-by: Santhosh Manohar --- libnetwork/networkdb/cluster.go | 5 ++++- libnetwork/networkdb/delegate.go | 9 +++++++++ libnetwork/networkdb/networkdb.go | 33 ++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/libnetwork/networkdb/cluster.go b/libnetwork/networkdb/cluster.go index 1424303e01..50661e1743 100644 --- a/libnetwork/networkdb/cluster.go +++ b/libnetwork/networkdb/cluster.go @@ -305,7 +305,10 @@ func (nDB *NetworkDB) gossip() { func (nDB *NetworkDB) bulkSyncTables() { var networks []string nDB.RLock() - for nid := range nDB.networks[nDB.config.NodeName] { + for nid, network := range nDB.networks[nDB.config.NodeName] { + if network.leaving { + continue + } networks = append(networks, nid) } nDB.RUnlock() diff --git a/libnetwork/networkdb/delegate.go b/libnetwork/networkdb/delegate.go index a33ec58b6d..98007516d4 100644 --- a/libnetwork/networkdb/delegate.go +++ b/libnetwork/networkdb/delegate.go @@ -75,6 +75,15 @@ func (nDB *NetworkDB) handleTableEvent(tEvent *TableEvent) bool { // time. nDB.tableClock.Witness(tEvent.LTime) + // Ignore the table events for networks that are in the process of going away + nDB.RLock() + networks := nDB.networks[nDB.config.NodeName] + network, ok := networks[tEvent.NetworkID] + nDB.RUnlock() + if !ok || network.leaving { + return true + } + if entry, err := nDB.getEntry(tEvent.TableName, tEvent.NetworkID, tEvent.Key); err == nil { // We have the latest state. Ignore the event // since it is stale. diff --git a/libnetwork/networkdb/networkdb.go b/libnetwork/networkdb/networkdb.go index 9859ab76d2..f21e572e69 100644 --- a/libnetwork/networkdb/networkdb.go +++ b/libnetwork/networkdb/networkdb.go @@ -398,7 +398,8 @@ func (nDB *NetworkDB) JoinNetwork(nid string) error { // this event across the cluster. This triggers this node leaving the // sub-cluster of this network and as a result will no longer // participate in the network-scoped gossip and bulk sync for this -// network. +// network. Also remove all the table entries for this network from +// networkdb func (nDB *NetworkDB) LeaveNetwork(nid string) error { ltime := nDB.networkClock.Increment() if err := nDB.sendNetworkEvent(nid, NetworkEventTypeLeave, ltime); err != nil { @@ -407,6 +408,36 @@ func (nDB *NetworkDB) LeaveNetwork(nid string) error { nDB.Lock() defer nDB.Unlock() + var ( + paths []string + entries []*entry + ) + + nwWalker := func(path string, v interface{}) bool { + entry, ok := v.(*entry) + if !ok { + return false + } + paths = append(paths, path) + entries = append(entries, entry) + return false + } + + nDB.indexes[byNetwork].WalkPrefix(fmt.Sprintf("/%s", nid), nwWalker) + for _, path := range paths { + params := strings.Split(path[1:], "/") + tname := params[1] + key := params[2] + + if _, ok := nDB.indexes[byTable].Delete(fmt.Sprintf("/%s/%s/%s", tname, nid, key)); !ok { + logrus.Errorf("Could not delete entry in table %s with network id %s and key %s as it does not exist", tname, nid, key) + } + + if _, ok := nDB.indexes[byNetwork].Delete(fmt.Sprintf("/%s/%s/%s", nid, tname, key)); !ok { + logrus.Errorf("Could not delete entry in network %s with table name %s and key %s as it does not exist", nid, tname, key) + } + } + nodeNetworks, ok := nDB.networks[nDB.config.NodeName] if !ok { return fmt.Errorf("could not find self node for network %s while trying to leave", nid)