github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/libnetwork/networkdb/event_delegate.go (about) 1 package networkdb 2 3 import ( 4 "encoding/json" 5 "net" 6 7 "github.com/hashicorp/memberlist" 8 "github.com/sirupsen/logrus" 9 ) 10 11 type eventDelegate struct { 12 nDB *NetworkDB 13 } 14 15 func (e *eventDelegate) broadcastNodeEvent(addr net.IP, op opType) { 16 value, err := json.Marshal(&NodeAddr{addr}) 17 if err == nil { 18 e.nDB.broadcaster.Write(makeEvent(op, NodeTable, "", "", value)) 19 } else { 20 logrus.Errorf("Error marshalling node broadcast event %s", addr.String()) 21 } 22 } 23 24 func (e *eventDelegate) NotifyJoin(mn *memberlist.Node) { 25 logrus.Infof("Node %s/%s, joined gossip cluster", mn.Name, mn.Addr) 26 e.broadcastNodeEvent(mn.Addr, opCreate) 27 e.nDB.Lock() 28 defer e.nDB.Unlock() 29 30 // In case the node is rejoining after a failure or leave, 31 // just add the node back to active 32 if moved, _ := e.nDB.changeNodeState(mn.Name, nodeActiveState); moved { 33 return 34 } 35 36 // Every node has a unique ID 37 // Check on the base of the IP address if the new node that joined is actually a new incarnation of a previous 38 // failed or shutdown one 39 e.nDB.purgeReincarnation(mn) 40 41 e.nDB.nodes[mn.Name] = &node{Node: *mn} 42 logrus.Infof("Node %s/%s, added to nodes list", mn.Name, mn.Addr) 43 } 44 45 func (e *eventDelegate) NotifyLeave(mn *memberlist.Node) { 46 logrus.Infof("Node %s/%s, left gossip cluster", mn.Name, mn.Addr) 47 e.broadcastNodeEvent(mn.Addr, opDelete) 48 49 e.nDB.Lock() 50 defer e.nDB.Unlock() 51 52 n, currState, _ := e.nDB.findNode(mn.Name) 53 if n == nil { 54 logrus.Errorf("Node %s/%s not found in the node lists", mn.Name, mn.Addr) 55 return 56 } 57 // if the node was active means that did not send the leave cluster message, so it's probable that 58 // failed. Else would be already in the left list so nothing else has to be done 59 if currState == nodeActiveState { 60 moved, err := e.nDB.changeNodeState(mn.Name, nodeFailedState) 61 if err != nil { 62 logrus.WithError(err).Errorf("impossible condition, node %s/%s not present in the list", mn.Name, mn.Addr) 63 return 64 } 65 if moved { 66 logrus.Infof("Node %s/%s, added to failed nodes list", mn.Name, mn.Addr) 67 } 68 } 69 } 70 71 func (e *eventDelegate) NotifyUpdate(n *memberlist.Node) { 72 }