2016-03-28 20:28:57 -04:00
|
|
|
package networkdb
|
|
|
|
|
2017-01-31 12:13:08 -05:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/hashicorp/memberlist"
|
|
|
|
)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
type eventDelegate struct {
|
|
|
|
nDB *NetworkDB
|
|
|
|
}
|
|
|
|
|
2017-01-31 12:13:08 -05:00
|
|
|
func (e *eventDelegate) broadcastNodeEvent(addr net.IP, op opType) {
|
|
|
|
value, err := json.Marshal(&NodeAddr{addr})
|
|
|
|
if err == nil {
|
|
|
|
e.nDB.broadcaster.Write(makeEvent(op, NodeTable, "", "", value))
|
|
|
|
} else {
|
|
|
|
logrus.Errorf("Error marshalling node broadcast event %s", addr.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 01:24:14 -04:00
|
|
|
func (e *eventDelegate) NotifyJoin(mn *memberlist.Node) {
|
2017-01-31 12:13:08 -05:00
|
|
|
e.broadcastNodeEvent(mn.Addr, opCreate)
|
2016-03-28 20:28:57 -04:00
|
|
|
e.nDB.Lock()
|
2016-09-15 01:24:14 -04:00
|
|
|
// In case the node is rejoining after a failure or leave,
|
|
|
|
// wait until an explicit join message arrives before adding
|
|
|
|
// it to the nodes just to make sure this is not a stale
|
|
|
|
// join. If you don't know about this node add it immediately.
|
|
|
|
_, fOk := e.nDB.failedNodes[mn.Name]
|
|
|
|
_, lOk := e.nDB.leftNodes[mn.Name]
|
|
|
|
if fOk || lOk {
|
|
|
|
e.nDB.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
e.nDB.nodes[mn.Name] = &node{Node: *mn}
|
2016-03-28 20:28:57 -04:00
|
|
|
e.nDB.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-09-15 01:24:14 -04:00
|
|
|
func (e *eventDelegate) NotifyLeave(mn *memberlist.Node) {
|
2017-01-31 12:13:08 -05:00
|
|
|
e.broadcastNodeEvent(mn.Addr, opDelete)
|
2016-09-15 01:24:14 -04:00
|
|
|
e.nDB.deleteNodeTableEntries(mn.Name)
|
|
|
|
e.nDB.deleteNetworkEntriesForNode(mn.Name)
|
2016-03-28 20:28:57 -04:00
|
|
|
e.nDB.Lock()
|
2016-09-15 01:24:14 -04:00
|
|
|
if n, ok := e.nDB.nodes[mn.Name]; ok {
|
|
|
|
delete(e.nDB.nodes, mn.Name)
|
2016-09-30 17:03:10 -04:00
|
|
|
|
|
|
|
n.reapTime = reapInterval
|
2016-09-15 01:24:14 -04:00
|
|
|
e.nDB.failedNodes[mn.Name] = n
|
|
|
|
}
|
2016-03-28 20:28:57 -04:00
|
|
|
e.nDB.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *eventDelegate) NotifyUpdate(n *memberlist.Node) {
|
|
|
|
}
|