2016-03-28 20:28:57 -04:00
|
|
|
package networkdb
|
|
|
|
|
2017-01-31 12:13:08 -05:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/hashicorp/memberlist"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-01-31 12:13:08 -05:00
|
|
|
)
|
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-05-22 21:36:43 -04:00
|
|
|
logrus.Infof("Node %s/%s, joined gossip cluster", mn.Name, mn.Addr)
|
2017-01-31 12:13:08 -05:00
|
|
|
e.broadcastNodeEvent(mn.Addr, opCreate)
|
2016-03-28 20:28:57 -04:00
|
|
|
e.nDB.Lock()
|
2017-11-16 19:30:27 -05:00
|
|
|
defer e.nDB.Unlock()
|
2017-12-21 17:42:47 -05:00
|
|
|
|
2016-09-15 01:24:14 -04:00
|
|
|
// In case the node is rejoining after a failure or leave,
|
2017-12-21 17:42:47 -05:00
|
|
|
// just add the node back to active
|
|
|
|
if moved, _ := e.nDB.changeNodeState(mn.Name, nodeActiveState); moved {
|
2016-09-15 01:24:14 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-16 19:30:27 -05:00
|
|
|
// Every node has a unique ID
|
|
|
|
// Check on the base of the IP address if the new node that joined is actually a new incarnation of a previous
|
|
|
|
// failed or shutdown one
|
2017-12-12 11:23:49 -05:00
|
|
|
e.nDB.purgeReincarnation(mn)
|
2017-11-16 19:30:27 -05:00
|
|
|
|
2016-09-15 01:24:14 -04:00
|
|
|
e.nDB.nodes[mn.Name] = &node{Node: *mn}
|
2017-05-22 21:36:43 -04:00
|
|
|
logrus.Infof("Node %s/%s, added to nodes list", mn.Name, mn.Addr)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
2016-09-15 01:24:14 -04:00
|
|
|
func (e *eventDelegate) NotifyLeave(mn *memberlist.Node) {
|
2017-05-22 21:36:43 -04:00
|
|
|
logrus.Infof("Node %s/%s, left gossip cluster", mn.Name, mn.Addr)
|
2017-01-31 12:13:08 -05:00
|
|
|
e.broadcastNodeEvent(mn.Addr, opDelete)
|
2017-12-12 11:23:49 -05:00
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
e.nDB.Lock()
|
2017-11-16 19:30:27 -05:00
|
|
|
defer e.nDB.Unlock()
|
|
|
|
|
2017-12-12 11:23:49 -05:00
|
|
|
n, currState, _ := e.nDB.findNode(mn.Name)
|
|
|
|
if n == nil {
|
|
|
|
logrus.Errorf("Node %s/%s not found in the node lists", mn.Name, mn.Addr)
|
|
|
|
return
|
2016-09-15 01:24:14 -04:00
|
|
|
}
|
2017-12-12 11:23:49 -05:00
|
|
|
// if the node was active means that did not send the leave cluster message, so it's probable that
|
|
|
|
// failed. Else would be already in the left list so nothing else has to be done
|
|
|
|
if currState == nodeActiveState {
|
|
|
|
moved, err := e.nDB.changeNodeState(mn.Name, nodeFailedState)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Errorf("impossible condition, node %s/%s not present in the list", mn.Name, mn.Addr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if moved {
|
|
|
|
logrus.Infof("Node %s/%s, added to failed nodes list", mn.Name, mn.Addr)
|
|
|
|
}
|
2017-05-22 21:36:43 -04:00
|
|
|
}
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *eventDelegate) NotifyUpdate(n *memberlist.Node) {
|
|
|
|
}
|