2016-03-28 20:28:57 -04:00
|
|
|
package networkdb
|
|
|
|
|
2016-05-17 00:42:35 -04:00
|
|
|
//go:generate protoc -I.:../Godeps/_workspace/src/github.com/gogo/protobuf --gogo_out=import_path=github.com/docker/libnetwork/networkdb,Mgogoproto/gogo.proto=github.com/gogo/protobuf/gogoproto:. networkdb.proto
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/armon/go-radix"
|
|
|
|
"github.com/docker/go-events"
|
2016-08-21 01:55:00 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2016-03-28 20:28:57 -04:00
|
|
|
"github.com/hashicorp/memberlist"
|
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
byTable int = 1 + iota
|
|
|
|
byNetwork
|
|
|
|
)
|
|
|
|
|
|
|
|
// NetworkDB instance drives the networkdb cluster and acts the broker
|
|
|
|
// for cluster-scoped and network-scoped gossip and watches.
|
|
|
|
type NetworkDB struct {
|
|
|
|
sync.RWMutex
|
|
|
|
|
|
|
|
// NetworkDB configuration.
|
|
|
|
config *Config
|
|
|
|
|
|
|
|
// All the tree index (byTable, byNetwork) that we maintain
|
|
|
|
// the db.
|
|
|
|
indexes map[int]*radix.Tree
|
|
|
|
|
|
|
|
// Memberlist we use to drive the cluster.
|
|
|
|
memberlist *memberlist.Memberlist
|
|
|
|
|
|
|
|
// List of all peer nodes in the cluster not-limited to any
|
|
|
|
// network.
|
2016-09-15 01:24:14 -04:00
|
|
|
nodes map[string]*node
|
|
|
|
|
|
|
|
// List of all peer nodes which have failed
|
|
|
|
failedNodes map[string]*node
|
|
|
|
|
|
|
|
// List of all peer nodes which have left
|
|
|
|
leftNodes map[string]*node
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
// A multi-dimensional map of network/node attachmemts. The
|
|
|
|
// first key is a node name and the second key is a network ID
|
|
|
|
// for the network that node is participating in.
|
|
|
|
networks map[string]map[string]*network
|
|
|
|
|
|
|
|
// A map of nodes which are participating in a given
|
|
|
|
// network. The key is a network ID.
|
|
|
|
networkNodes map[string][]string
|
|
|
|
|
|
|
|
// A table of ack channels for every node from which we are
|
|
|
|
// waiting for an ack.
|
|
|
|
bulkSyncAckTbl map[string]chan struct{}
|
|
|
|
|
|
|
|
// Global lamport clock for node network attach events.
|
|
|
|
networkClock serf.LamportClock
|
|
|
|
|
|
|
|
// Global lamport clock for table events.
|
|
|
|
tableClock serf.LamportClock
|
|
|
|
|
|
|
|
// Broadcast queue for network event gossip.
|
|
|
|
networkBroadcasts *memberlist.TransmitLimitedQueue
|
|
|
|
|
2016-09-15 01:24:14 -04:00
|
|
|
// Broadcast queue for node event gossip.
|
|
|
|
nodeBroadcasts *memberlist.TransmitLimitedQueue
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
// A central stop channel to stop all go routines running on
|
|
|
|
// behalf of the NetworkDB instance.
|
|
|
|
stopCh chan struct{}
|
|
|
|
|
|
|
|
// A central broadcaster for all local watchers watching table
|
|
|
|
// events.
|
|
|
|
broadcaster *events.Broadcaster
|
|
|
|
|
|
|
|
// List of all tickers which needed to be stopped when
|
|
|
|
// cleaning up.
|
|
|
|
tickers []*time.Ticker
|
2016-06-04 05:10:19 -04:00
|
|
|
|
|
|
|
// Reference to the memberlist's keyring to add & remove keys
|
|
|
|
keyring *memberlist.Keyring
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
2016-10-25 17:52:36 -04:00
|
|
|
// PeerInfo represents the peer (gossip cluster) nodes of a network
|
|
|
|
type PeerInfo struct {
|
|
|
|
Name string
|
|
|
|
IP string
|
|
|
|
}
|
|
|
|
|
2016-09-15 01:24:14 -04:00
|
|
|
type node struct {
|
|
|
|
memberlist.Node
|
|
|
|
ltime serf.LamportTime
|
2016-09-30 17:03:10 -04:00
|
|
|
// Number of hours left before the reaper removes the node
|
|
|
|
reapTime time.Duration
|
2016-09-15 01:24:14 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
// network describes the node/network attachment.
|
|
|
|
type network struct {
|
|
|
|
// Network ID
|
|
|
|
id string
|
|
|
|
|
|
|
|
// Lamport time for the latest state of the entry.
|
|
|
|
ltime serf.LamportTime
|
|
|
|
|
|
|
|
// Node leave is in progress.
|
|
|
|
leaving bool
|
|
|
|
|
2016-09-27 19:38:47 -04:00
|
|
|
// Number of seconds still left before a deleted network entry gets
|
|
|
|
// removed from networkDB
|
|
|
|
reapTime time.Duration
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
// The broadcast queue for table event gossip. This is only
|
|
|
|
// initialized for this node's network attachment entries.
|
|
|
|
tableBroadcasts *memberlist.TransmitLimitedQueue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config represents the configuration of the networdb instance and
|
|
|
|
// can be passed by the caller.
|
|
|
|
type Config struct {
|
|
|
|
// NodeName is the cluster wide unique name for this node.
|
|
|
|
NodeName string
|
|
|
|
|
2016-09-22 14:38:35 -04:00
|
|
|
// BindAddr is the IP on which networkdb listens. It can be
|
|
|
|
// 0.0.0.0 to listen on all addresses on the host.
|
|
|
|
BindAddr string
|
|
|
|
|
2016-07-19 21:17:30 -04:00
|
|
|
// AdvertiseAddr is the node's IP address that we advertise for
|
2016-03-28 20:28:57 -04:00
|
|
|
// cluster communication.
|
2016-07-19 21:17:30 -04:00
|
|
|
AdvertiseAddr string
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
// BindPort is the local node's port to which we bind to for
|
|
|
|
// cluster communication.
|
|
|
|
BindPort int
|
2016-06-04 05:10:19 -04:00
|
|
|
|
|
|
|
// Keys to be added to the Keyring of the memberlist. Key at index
|
|
|
|
// 0 is the primary key
|
|
|
|
Keys [][]byte
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// entry defines a table entry
|
|
|
|
type entry struct {
|
|
|
|
// node from which this entry was learned.
|
|
|
|
node string
|
|
|
|
|
|
|
|
// Lamport time for the most recent update to the entry
|
|
|
|
ltime serf.LamportTime
|
|
|
|
|
|
|
|
// Opaque value store in the entry
|
|
|
|
value []byte
|
|
|
|
|
|
|
|
// Deleting the entry is in progress. All entries linger in
|
|
|
|
// the cluster for certain amount of time after deletion.
|
|
|
|
deleting bool
|
|
|
|
|
2016-09-27 19:38:47 -04:00
|
|
|
// Number of seconds still left before a deleted table entry gets
|
|
|
|
// removed from networkDB
|
|
|
|
reapTime time.Duration
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new instance of NetworkDB using the Config passed by
|
|
|
|
// the caller.
|
|
|
|
func New(c *Config) (*NetworkDB, error) {
|
|
|
|
nDB := &NetworkDB{
|
|
|
|
config: c,
|
|
|
|
indexes: make(map[int]*radix.Tree),
|
|
|
|
networks: make(map[string]map[string]*network),
|
2016-09-15 01:24:14 -04:00
|
|
|
nodes: make(map[string]*node),
|
|
|
|
failedNodes: make(map[string]*node),
|
|
|
|
leftNodes: make(map[string]*node),
|
2016-03-28 20:28:57 -04:00
|
|
|
networkNodes: make(map[string][]string),
|
|
|
|
bulkSyncAckTbl: make(map[string]chan struct{}),
|
|
|
|
broadcaster: events.NewBroadcaster(),
|
|
|
|
}
|
|
|
|
|
|
|
|
nDB.indexes[byTable] = radix.New()
|
|
|
|
nDB.indexes[byNetwork] = radix.New()
|
|
|
|
|
|
|
|
if err := nDB.clusterInit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nDB, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join joins this NetworkDB instance with a list of peer NetworkDB
|
|
|
|
// instances passed by the caller in the form of addr:port
|
|
|
|
func (nDB *NetworkDB) Join(members []string) error {
|
|
|
|
return nDB.clusterJoin(members)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close destroys this NetworkDB instance by leave the cluster,
|
|
|
|
// stopping timers, canceling goroutines etc.
|
|
|
|
func (nDB *NetworkDB) Close() {
|
|
|
|
if err := nDB.clusterLeave(); err != nil {
|
|
|
|
logrus.Errorf("Could not close DB %s: %v", nDB.config.NodeName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:52:36 -04:00
|
|
|
// Peers returns the gossip peers for a given network.
|
|
|
|
func (nDB *NetworkDB) Peers(nid string) []PeerInfo {
|
|
|
|
nDB.RLock()
|
|
|
|
defer nDB.RUnlock()
|
|
|
|
peers := make([]PeerInfo, 0, len(nDB.networkNodes[nid]))
|
|
|
|
for _, nodeName := range nDB.networkNodes[nid] {
|
|
|
|
peers = append(peers, PeerInfo{
|
|
|
|
Name: nDB.nodes[nodeName].Name,
|
|
|
|
IP: nDB.nodes[nodeName].Addr.String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return peers
|
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
// GetEntry retrieves the value of a table entry in a given (network,
|
|
|
|
// table, key) tuple
|
|
|
|
func (nDB *NetworkDB) GetEntry(tname, nid, key string) ([]byte, error) {
|
|
|
|
entry, err := nDB.getEntry(tname, nid, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry.value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nDB *NetworkDB) getEntry(tname, nid, key string) (*entry, error) {
|
|
|
|
nDB.RLock()
|
|
|
|
defer nDB.RUnlock()
|
|
|
|
|
|
|
|
e, ok := nDB.indexes[byTable].Get(fmt.Sprintf("/%s/%s/%s", tname, nid, key))
|
|
|
|
if !ok {
|
2016-08-21 01:55:00 -04:00
|
|
|
return nil, types.NotFoundErrorf("could not get entry in table %s with network id %s and key %s", tname, nid, key)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return e.(*entry), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateEntry creates a table entry in NetworkDB for given (network,
|
|
|
|
// table, key) tuple and if the NetworkDB is part of the cluster
|
|
|
|
// propogates this event to the cluster. It is an error to create an
|
|
|
|
// entry for the same tuple for which there is already an existing
|
2016-08-21 01:55:00 -04:00
|
|
|
// entry unless the current entry is deleting state.
|
2016-03-28 20:28:57 -04:00
|
|
|
func (nDB *NetworkDB) CreateEntry(tname, nid, key string, value []byte) error {
|
2016-08-21 01:55:00 -04:00
|
|
|
oldEntry, err := nDB.getEntry(tname, nid, key)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(types.NotFoundError); !ok {
|
|
|
|
return fmt.Errorf("cannot create entry in table %s with network id %s and key %s: %v", tname, nid, key, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if oldEntry != nil && !oldEntry.deleting {
|
|
|
|
return fmt.Errorf("cannot create entry in table %s with network id %s and key %s, already exists", tname, nid, key)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
entry := &entry{
|
|
|
|
ltime: nDB.tableClock.Increment(),
|
|
|
|
node: nDB.config.NodeName,
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
|
2016-05-17 00:42:35 -04:00
|
|
|
if err := nDB.sendTableEvent(TableEventTypeCreate, nid, tname, key, entry); err != nil {
|
2016-03-28 20:28:57 -04:00
|
|
|
return fmt.Errorf("cannot send table create event: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nDB.Lock()
|
|
|
|
nDB.indexes[byTable].Insert(fmt.Sprintf("/%s/%s/%s", tname, nid, key), entry)
|
|
|
|
nDB.indexes[byNetwork].Insert(fmt.Sprintf("/%s/%s/%s", nid, tname, key), entry)
|
|
|
|
nDB.Unlock()
|
|
|
|
|
|
|
|
nDB.broadcaster.Write(makeEvent(opCreate, tname, nid, key, value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateEntry updates a table entry in NetworkDB for given (network,
|
|
|
|
// table, key) tuple and if the NetworkDB is part of the cluster
|
|
|
|
// propogates this event to the cluster. It is an error to update a
|
|
|
|
// non-existent entry.
|
|
|
|
func (nDB *NetworkDB) UpdateEntry(tname, nid, key string, value []byte) error {
|
|
|
|
if _, err := nDB.GetEntry(tname, nid, key); err != nil {
|
|
|
|
return fmt.Errorf("cannot update entry as the entry in table %s with network id %s and key %s does not exist", tname, nid, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &entry{
|
|
|
|
ltime: nDB.tableClock.Increment(),
|
|
|
|
node: nDB.config.NodeName,
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
|
2016-05-17 00:42:35 -04:00
|
|
|
if err := nDB.sendTableEvent(TableEventTypeUpdate, nid, tname, key, entry); err != nil {
|
2016-03-28 20:28:57 -04:00
|
|
|
return fmt.Errorf("cannot send table update event: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nDB.Lock()
|
|
|
|
nDB.indexes[byTable].Insert(fmt.Sprintf("/%s/%s/%s", tname, nid, key), entry)
|
|
|
|
nDB.indexes[byNetwork].Insert(fmt.Sprintf("/%s/%s/%s", nid, tname, key), entry)
|
|
|
|
nDB.Unlock()
|
|
|
|
|
|
|
|
nDB.broadcaster.Write(makeEvent(opUpdate, tname, nid, key, value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteEntry deletes a table entry in NetworkDB for given (network,
|
|
|
|
// table, key) tuple and if the NetworkDB is part of the cluster
|
|
|
|
// propogates this event to the cluster.
|
|
|
|
func (nDB *NetworkDB) DeleteEntry(tname, nid, key string) error {
|
|
|
|
value, err := nDB.GetEntry(tname, nid, key)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot delete entry as the entry in table %s with network id %s and key %s does not exist", tname, nid, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &entry{
|
2016-09-27 19:38:47 -04:00
|
|
|
ltime: nDB.tableClock.Increment(),
|
|
|
|
node: nDB.config.NodeName,
|
|
|
|
value: value,
|
|
|
|
deleting: true,
|
|
|
|
reapTime: reapInterval,
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
2016-05-17 00:42:35 -04:00
|
|
|
if err := nDB.sendTableEvent(TableEventTypeDelete, nid, tname, key, entry); err != nil {
|
2016-03-28 20:28:57 -04:00
|
|
|
return fmt.Errorf("cannot send table delete event: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nDB.Lock()
|
|
|
|
nDB.indexes[byTable].Insert(fmt.Sprintf("/%s/%s/%s", tname, nid, key), entry)
|
|
|
|
nDB.indexes[byNetwork].Insert(fmt.Sprintf("/%s/%s/%s", nid, tname, key), entry)
|
|
|
|
nDB.Unlock()
|
|
|
|
|
|
|
|
nDB.broadcaster.Write(makeEvent(opDelete, tname, nid, key, value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-15 01:24:14 -04:00
|
|
|
func (nDB *NetworkDB) deleteNetworkEntriesForNode(deletedNode string) {
|
2016-06-14 15:39:38 -04:00
|
|
|
nDB.Lock()
|
|
|
|
for nid, nodes := range nDB.networkNodes {
|
|
|
|
updatedNodes := make([]string, 0, len(nodes))
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node == deletedNode {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedNodes = append(updatedNodes, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
nDB.networkNodes[nid] = updatedNodes
|
|
|
|
}
|
2016-09-15 01:24:14 -04:00
|
|
|
|
|
|
|
delete(nDB.networks, deletedNode)
|
2016-06-14 15:39:38 -04:00
|
|
|
nDB.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
func (nDB *NetworkDB) deleteNodeTableEntries(node string) {
|
|
|
|
nDB.Lock()
|
|
|
|
nDB.indexes[byTable].Walk(func(path string, v interface{}) bool {
|
|
|
|
oldEntry := v.(*entry)
|
|
|
|
if oldEntry.node != node {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
params := strings.Split(path[1:], "/")
|
|
|
|
tname := params[0]
|
|
|
|
nid := params[1]
|
|
|
|
key := params[2]
|
|
|
|
|
|
|
|
entry := &entry{
|
2016-09-27 19:38:47 -04:00
|
|
|
ltime: oldEntry.ltime,
|
|
|
|
node: node,
|
|
|
|
value: oldEntry.value,
|
|
|
|
deleting: true,
|
|
|
|
reapTime: reapInterval,
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
nDB.indexes[byTable].Insert(fmt.Sprintf("/%s/%s/%s", tname, nid, key), entry)
|
|
|
|
nDB.indexes[byNetwork].Insert(fmt.Sprintf("/%s/%s/%s", nid, tname, key), entry)
|
2016-08-18 16:57:24 -04:00
|
|
|
|
|
|
|
nDB.broadcaster.Write(makeEvent(opDelete, tname, nid, key, entry.value))
|
2016-03-28 20:28:57 -04:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
nDB.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WalkTable walks a single table in NetworkDB and invokes the passed
|
|
|
|
// function for each entry in the table passing the network, key,
|
|
|
|
// value. The walk stops if the passed function returns a true.
|
|
|
|
func (nDB *NetworkDB) WalkTable(tname string, fn func(string, string, []byte) bool) error {
|
|
|
|
nDB.RLock()
|
|
|
|
values := make(map[string]interface{})
|
|
|
|
nDB.indexes[byTable].WalkPrefix(fmt.Sprintf("/%s", tname), func(path string, v interface{}) bool {
|
|
|
|
values[path] = v
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
nDB.RUnlock()
|
|
|
|
|
|
|
|
for k, v := range values {
|
|
|
|
params := strings.Split(k[1:], "/")
|
|
|
|
nid := params[1]
|
|
|
|
key := params[2]
|
|
|
|
if fn(nid, key, v.(*entry).value) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinNetwork joins this node to a given network and propogates this
|
|
|
|
// event across the cluster. This triggers this node joining the
|
|
|
|
// sub-cluster of this network and participates in the network-scoped
|
|
|
|
// gossip and bulk sync for this network.
|
|
|
|
func (nDB *NetworkDB) JoinNetwork(nid string) error {
|
|
|
|
ltime := nDB.networkClock.Increment()
|
|
|
|
|
|
|
|
nDB.Lock()
|
|
|
|
nodeNetworks, ok := nDB.networks[nDB.config.NodeName]
|
|
|
|
if !ok {
|
|
|
|
nodeNetworks = make(map[string]*network)
|
|
|
|
nDB.networks[nDB.config.NodeName] = nodeNetworks
|
|
|
|
}
|
|
|
|
nodeNetworks[nid] = &network{id: nid, ltime: ltime}
|
|
|
|
nodeNetworks[nid].tableBroadcasts = &memberlist.TransmitLimitedQueue{
|
|
|
|
NumNodes: func() int {
|
2016-08-05 16:54:17 -04:00
|
|
|
nDB.RLock()
|
|
|
|
num := len(nDB.networkNodes[nid])
|
|
|
|
nDB.RUnlock()
|
|
|
|
return num
|
2016-03-28 20:28:57 -04:00
|
|
|
},
|
|
|
|
RetransmitMult: 4,
|
|
|
|
}
|
|
|
|
nDB.networkNodes[nid] = append(nDB.networkNodes[nid], nDB.config.NodeName)
|
2016-06-14 15:39:38 -04:00
|
|
|
networkNodes := nDB.networkNodes[nid]
|
2016-03-28 20:28:57 -04:00
|
|
|
nDB.Unlock()
|
|
|
|
|
2016-05-17 00:42:35 -04:00
|
|
|
if err := nDB.sendNetworkEvent(nid, NetworkEventTypeJoin, ltime); err != nil {
|
2016-04-23 16:26:34 -04:00
|
|
|
return fmt.Errorf("failed to send leave network event for %s: %v", nid, err)
|
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
logrus.Debugf("%s: joined network %s", nDB.config.NodeName, nid)
|
2016-09-15 01:24:14 -04:00
|
|
|
if _, err := nDB.bulkSync(networkNodes, true); err != nil {
|
2016-03-28 20:28:57 -04:00
|
|
|
logrus.Errorf("Error bulk syncing while joining network %s: %v", nid, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LeaveNetwork leaves this node from a given network and propogates
|
|
|
|
// 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
|
2016-08-10 15:44:05 -04:00
|
|
|
// network. Also remove all the table entries for this network from
|
|
|
|
// networkdb
|
2016-03-28 20:28:57 -04:00
|
|
|
func (nDB *NetworkDB) LeaveNetwork(nid string) error {
|
|
|
|
ltime := nDB.networkClock.Increment()
|
2016-05-17 00:42:35 -04:00
|
|
|
if err := nDB.sendNetworkEvent(nid, NetworkEventTypeLeave, ltime); err != nil {
|
2016-03-28 20:28:57 -04:00
|
|
|
return fmt.Errorf("failed to send leave network event for %s: %v", nid, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nDB.Lock()
|
|
|
|
defer nDB.Unlock()
|
2016-08-10 15:44:05 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
n, ok := nodeNetworks[nid]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("could not find network %s while trying to leave", nid)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.ltime = ltime
|
|
|
|
n.leaving = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-19 20:18:15 -04:00
|
|
|
// addNetworkNode adds the node to the list of nodes which participate
|
|
|
|
// in the passed network only if it is not already present. Caller
|
|
|
|
// should hold the NetworkDB lock while calling this
|
|
|
|
func (nDB *NetworkDB) addNetworkNode(nid string, nodeName string) {
|
|
|
|
nodes := nDB.networkNodes[nid]
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node == nodeName {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nDB.networkNodes[nid] = append(nDB.networkNodes[nid], nodeName)
|
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
// Deletes the node from the list of nodes which participate in the
|
|
|
|
// passed network. Caller should hold the NetworkDB lock while calling
|
|
|
|
// this
|
|
|
|
func (nDB *NetworkDB) deleteNetworkNode(nid string, nodeName string) {
|
|
|
|
nodes := nDB.networkNodes[nid]
|
2016-10-11 18:20:15 -04:00
|
|
|
newNodes := make([]string, 0, len(nodes)-1)
|
|
|
|
for _, name := range nodes {
|
2016-03-28 20:28:57 -04:00
|
|
|
if name == nodeName {
|
2016-10-11 18:20:15 -04:00
|
|
|
continue
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
2016-10-11 18:20:15 -04:00
|
|
|
newNodes = append(newNodes, name)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
2016-10-11 18:20:15 -04:00
|
|
|
nDB.networkNodes[nid] = newNodes
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// findCommonnetworks find the networks that both this node and the
|
|
|
|
// passed node have joined.
|
|
|
|
func (nDB *NetworkDB) findCommonNetworks(nodeName string) []string {
|
|
|
|
nDB.RLock()
|
|
|
|
defer nDB.RUnlock()
|
|
|
|
|
|
|
|
var networks []string
|
|
|
|
for nid := range nDB.networks[nDB.config.NodeName] {
|
2016-09-15 01:24:14 -04:00
|
|
|
if n, ok := nDB.networks[nodeName][nid]; ok {
|
|
|
|
if !n.leaving {
|
|
|
|
networks = append(networks, nid)
|
|
|
|
}
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return networks
|
|
|
|
}
|
2016-09-15 01:24:14 -04:00
|
|
|
|
2016-09-23 17:42:17 -04:00
|
|
|
func (nDB *NetworkDB) updateLocalNetworkTime() {
|
2016-09-15 01:24:14 -04:00
|
|
|
nDB.Lock()
|
|
|
|
defer nDB.Unlock()
|
|
|
|
|
|
|
|
ltime := nDB.networkClock.Increment()
|
|
|
|
for _, n := range nDB.networks[nDB.config.NodeName] {
|
|
|
|
n.ltime = ltime
|
|
|
|
}
|
2016-09-23 17:42:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (nDB *NetworkDB) updateLocalTableTime() {
|
|
|
|
nDB.Lock()
|
|
|
|
defer nDB.Unlock()
|
2016-09-15 01:24:14 -04:00
|
|
|
|
2016-09-23 17:42:17 -04:00
|
|
|
ltime := nDB.tableClock.Increment()
|
2016-09-15 01:24:14 -04:00
|
|
|
nDB.indexes[byTable].Walk(func(path string, v interface{}) bool {
|
|
|
|
entry := v.(*entry)
|
|
|
|
if entry.node != nDB.config.NodeName {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
params := strings.Split(path[1:], "/")
|
|
|
|
tname := params[0]
|
|
|
|
nid := params[1]
|
|
|
|
key := params[2]
|
|
|
|
entry.ltime = ltime
|
|
|
|
|
|
|
|
nDB.indexes[byTable].Insert(fmt.Sprintf("/%s/%s/%s", tname, nid, key), entry)
|
|
|
|
nDB.indexes[byNetwork].Insert(fmt.Sprintf("/%s/%s/%s", nid, tname, key), entry)
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|