2016-03-28 20:28:57 -04:00
|
|
|
package networkdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-06-13 21:30:31 -04:00
|
|
|
"net"
|
2016-03-28 20:28:57 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-05-17 00:42:35 -04:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2016-03-28 20:28:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type delegate struct {
|
|
|
|
nDB *NetworkDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *delegate) NodeMeta(limit int) []byte {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
|
2016-05-17 00:42:35 -04:00
|
|
|
func (nDB *NetworkDB) handleNetworkEvent(nEvent *NetworkEvent) bool {
|
2016-03-28 20:28:57 -04:00
|
|
|
// Update our local clock if the received messages has newer
|
|
|
|
// time.
|
|
|
|
nDB.networkClock.Witness(nEvent.LTime)
|
|
|
|
|
|
|
|
nDB.Lock()
|
|
|
|
defer nDB.Unlock()
|
|
|
|
|
2016-07-26 07:31:54 -04:00
|
|
|
if nEvent.NodeName == nDB.config.NodeName {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
nodeNetworks, ok := nDB.networks[nEvent.NodeName]
|
|
|
|
if !ok {
|
|
|
|
// We haven't heard about this node at all. Ignore the leave
|
2016-05-17 00:42:35 -04:00
|
|
|
if nEvent.Type == NetworkEventTypeLeave {
|
2016-03-28 20:28:57 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeNetworks = make(map[string]*network)
|
|
|
|
nDB.networks[nEvent.NodeName] = nodeNetworks
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, ok := nodeNetworks[nEvent.NetworkID]; ok {
|
|
|
|
// We have the latest state. Ignore the event
|
|
|
|
// since it is stale.
|
|
|
|
if n.ltime >= nEvent.LTime {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
n.ltime = nEvent.LTime
|
2016-05-17 00:42:35 -04:00
|
|
|
n.leaving = nEvent.Type == NetworkEventTypeLeave
|
2016-03-28 20:28:57 -04:00
|
|
|
if n.leaving {
|
|
|
|
n.leaveTime = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-05-17 00:42:35 -04:00
|
|
|
if nEvent.Type == NetworkEventTypeLeave {
|
2016-03-28 20:28:57 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// This remote network join is being seen the first time.
|
|
|
|
nodeNetworks[nEvent.NetworkID] = &network{
|
|
|
|
id: nEvent.NetworkID,
|
|
|
|
ltime: nEvent.LTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
nDB.networkNodes[nEvent.NetworkID] = append(nDB.networkNodes[nEvent.NetworkID], nEvent.NodeName)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-05-17 00:42:35 -04:00
|
|
|
func (nDB *NetworkDB) handleTableEvent(tEvent *TableEvent) bool {
|
2016-03-28 20:28:57 -04:00
|
|
|
// Update our local clock if the received messages has newer
|
|
|
|
// time.
|
|
|
|
nDB.tableClock.Witness(tEvent.LTime)
|
|
|
|
|
2016-08-10 15:44:05 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
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.
|
|
|
|
if entry.ltime >= tEvent.LTime {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &entry{
|
|
|
|
ltime: tEvent.LTime,
|
|
|
|
node: tEvent.NodeName,
|
|
|
|
value: tEvent.Value,
|
2016-05-17 00:42:35 -04:00
|
|
|
deleting: tEvent.Type == TableEventTypeDelete,
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if entry.deleting {
|
|
|
|
entry.deleteTime = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
nDB.Lock()
|
|
|
|
nDB.indexes[byTable].Insert(fmt.Sprintf("/%s/%s/%s", tEvent.TableName, tEvent.NetworkID, tEvent.Key), entry)
|
|
|
|
nDB.indexes[byNetwork].Insert(fmt.Sprintf("/%s/%s/%s", tEvent.NetworkID, tEvent.TableName, tEvent.Key), entry)
|
|
|
|
nDB.Unlock()
|
|
|
|
|
|
|
|
var op opType
|
2016-05-17 00:42:35 -04:00
|
|
|
switch tEvent.Type {
|
|
|
|
case TableEventTypeCreate:
|
2016-03-28 20:28:57 -04:00
|
|
|
op = opCreate
|
2016-05-17 00:42:35 -04:00
|
|
|
case TableEventTypeUpdate:
|
2016-03-28 20:28:57 -04:00
|
|
|
op = opUpdate
|
2016-05-17 00:42:35 -04:00
|
|
|
case TableEventTypeDelete:
|
2016-03-28 20:28:57 -04:00
|
|
|
op = opDelete
|
|
|
|
}
|
|
|
|
|
|
|
|
nDB.broadcaster.Write(makeEvent(op, tEvent.TableName, tEvent.NetworkID, tEvent.Key, tEvent.Value))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-06-12 23:19:40 -04:00
|
|
|
func (nDB *NetworkDB) handleCompound(buf []byte, isBulkSync bool) {
|
2016-03-28 20:28:57 -04:00
|
|
|
// Decode the parts
|
2016-05-17 00:42:35 -04:00
|
|
|
parts, err := decodeCompoundMessage(buf)
|
2016-03-28 20:28:57 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to decode compound request: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle each message
|
|
|
|
for _, part := range parts {
|
2016-06-12 23:19:40 -04:00
|
|
|
nDB.handleMessage(part, isBulkSync)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 23:19:40 -04:00
|
|
|
func (nDB *NetworkDB) handleTableMessage(buf []byte, isBulkSync bool) {
|
2016-05-17 00:42:35 -04:00
|
|
|
var tEvent TableEvent
|
|
|
|
if err := proto.Unmarshal(buf, &tEvent); err != nil {
|
2016-03-28 20:28:57 -04:00
|
|
|
logrus.Errorf("Error decoding table event message: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-30 21:24:13 -04:00
|
|
|
// Ignore messages that this node generated.
|
|
|
|
if tEvent.NodeName == nDB.config.NodeName {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-12 23:19:40 -04:00
|
|
|
// Do not rebroadcast a bulk sync
|
|
|
|
if rebroadcast := nDB.handleTableEvent(&tEvent); rebroadcast && !isBulkSync {
|
2016-05-17 00:42:35 -04:00
|
|
|
var err error
|
|
|
|
buf, err = encodeRawMessage(MessageTypeTableEvent, buf)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Error marshalling gossip message for network event rebroadcast: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
nDB.RLock()
|
|
|
|
n, ok := nDB.networks[nDB.config.NodeName][tEvent.NetworkID]
|
|
|
|
nDB.RUnlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
broadcastQ := n.tableBroadcasts
|
2016-06-02 23:29:37 -04:00
|
|
|
|
|
|
|
if broadcastQ == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
broadcastQ.QueueBroadcast(&tableEventMessage{
|
2016-05-17 00:42:35 -04:00
|
|
|
msg: buf,
|
2016-03-28 20:28:57 -04:00
|
|
|
id: tEvent.NetworkID,
|
|
|
|
tname: tEvent.TableName,
|
|
|
|
key: tEvent.Key,
|
|
|
|
node: nDB.config.NodeName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nDB *NetworkDB) handleNetworkMessage(buf []byte) {
|
2016-05-17 00:42:35 -04:00
|
|
|
var nEvent NetworkEvent
|
|
|
|
if err := proto.Unmarshal(buf, &nEvent); err != nil {
|
2016-03-28 20:28:57 -04:00
|
|
|
logrus.Errorf("Error decoding network event message: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if rebroadcast := nDB.handleNetworkEvent(&nEvent); rebroadcast {
|
2016-05-17 00:42:35 -04:00
|
|
|
var err error
|
|
|
|
buf, err = encodeRawMessage(MessageTypeNetworkEvent, buf)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Error marshalling gossip message for network event rebroadcast: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
nDB.networkBroadcasts.QueueBroadcast(&networkEventMessage{
|
2016-05-17 00:42:35 -04:00
|
|
|
msg: buf,
|
2016-03-28 20:28:57 -04:00
|
|
|
id: nEvent.NetworkID,
|
|
|
|
node: nEvent.NodeName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nDB *NetworkDB) handleBulkSync(buf []byte) {
|
2016-05-17 00:42:35 -04:00
|
|
|
var bsm BulkSyncMessage
|
|
|
|
if err := proto.Unmarshal(buf, &bsm); err != nil {
|
2016-03-28 20:28:57 -04:00
|
|
|
logrus.Errorf("Error decoding bulk sync message: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if bsm.LTime > 0 {
|
|
|
|
nDB.tableClock.Witness(bsm.LTime)
|
|
|
|
}
|
|
|
|
|
2016-06-12 23:19:40 -04:00
|
|
|
nDB.handleMessage(bsm.Payload, true)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
// Don't respond to a bulk sync which was not unsolicited
|
|
|
|
if !bsm.Unsolicited {
|
|
|
|
nDB.RLock()
|
|
|
|
ch, ok := nDB.bulkSyncAckTbl[bsm.NodeName]
|
|
|
|
nDB.RUnlock()
|
|
|
|
if ok {
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-13 21:30:31 -04:00
|
|
|
var nodeAddr net.IP
|
2016-08-05 16:54:17 -04:00
|
|
|
nDB.RLock()
|
2016-06-13 21:30:31 -04:00
|
|
|
if node, ok := nDB.nodes[bsm.NodeName]; ok {
|
|
|
|
nodeAddr = node.Addr
|
|
|
|
}
|
2016-08-05 16:54:17 -04:00
|
|
|
nDB.RUnlock()
|
2016-06-13 21:30:31 -04:00
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
if err := nDB.bulkSyncNode(bsm.Networks, bsm.NodeName, false); err != nil {
|
2016-06-13 21:30:31 -04:00
|
|
|
logrus.Errorf("Error in responding to bulk sync from node %s: %v", nodeAddr, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 23:19:40 -04:00
|
|
|
func (nDB *NetworkDB) handleMessage(buf []byte, isBulkSync bool) {
|
2016-05-17 00:42:35 -04:00
|
|
|
mType, data, err := decodeMessage(buf)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Error decoding gossip message to get message type: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch mType {
|
|
|
|
case MessageTypeNetworkEvent:
|
|
|
|
nDB.handleNetworkMessage(data)
|
|
|
|
case MessageTypeTableEvent:
|
2016-06-12 23:19:40 -04:00
|
|
|
nDB.handleTableMessage(data, isBulkSync)
|
2016-05-17 00:42:35 -04:00
|
|
|
case MessageTypeBulkSync:
|
|
|
|
nDB.handleBulkSync(data)
|
|
|
|
case MessageTypeCompound:
|
2016-06-12 23:19:40 -04:00
|
|
|
nDB.handleCompound(data, isBulkSync)
|
2016-03-28 20:28:57 -04:00
|
|
|
default:
|
2016-05-17 00:42:35 -04:00
|
|
|
logrus.Errorf("%s: unknown message type %d", nDB.config.NodeName, mType)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *delegate) NotifyMsg(buf []byte) {
|
|
|
|
if len(buf) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-12 23:19:40 -04:00
|
|
|
d.nDB.handleMessage(buf, false)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *delegate) GetBroadcasts(overhead, limit int) [][]byte {
|
|
|
|
return d.nDB.networkBroadcasts.GetBroadcasts(overhead, limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *delegate) LocalState(join bool) []byte {
|
|
|
|
d.nDB.RLock()
|
|
|
|
defer d.nDB.RUnlock()
|
|
|
|
|
2016-05-17 00:42:35 -04:00
|
|
|
pp := NetworkPushPull{
|
2016-03-28 20:28:57 -04:00
|
|
|
LTime: d.nDB.networkClock.Time(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, nn := range d.nDB.networks {
|
|
|
|
for _, n := range nn {
|
2016-05-17 00:42:35 -04:00
|
|
|
pp.Networks = append(pp.Networks, &NetworkEntry{
|
|
|
|
LTime: n.ltime,
|
|
|
|
NetworkID: n.id,
|
|
|
|
NodeName: name,
|
|
|
|
Leaving: n.leaving,
|
2016-03-28 20:28:57 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 00:42:35 -04:00
|
|
|
buf, err := encodeMessage(MessageTypePushPull, &pp)
|
2016-03-28 20:28:57 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to encode local network state: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *delegate) MergeRemoteState(buf []byte, isJoin bool) {
|
|
|
|
if len(buf) == 0 {
|
|
|
|
logrus.Error("zero byte remote network state received")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-17 00:42:35 -04:00
|
|
|
var gMsg GossipMessage
|
|
|
|
err := proto.Unmarshal(buf, &gMsg)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Error unmarshalling push pull messsage: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if gMsg.Type != MessageTypePushPull {
|
2016-03-28 20:28:57 -04:00
|
|
|
logrus.Errorf("Invalid message type %v received from remote", buf[0])
|
|
|
|
}
|
|
|
|
|
2016-05-17 00:42:35 -04:00
|
|
|
pp := NetworkPushPull{}
|
|
|
|
if err := proto.Unmarshal(gMsg.Data, &pp); err != nil {
|
2016-03-28 20:28:57 -04:00
|
|
|
logrus.Errorf("Failed to decode remote network state: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if pp.LTime > 0 {
|
|
|
|
d.nDB.networkClock.Witness(pp.LTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range pp.Networks {
|
2016-05-17 00:42:35 -04:00
|
|
|
nEvent := &NetworkEvent{
|
2016-03-28 20:28:57 -04:00
|
|
|
LTime: n.LTime,
|
|
|
|
NodeName: n.NodeName,
|
2016-05-17 00:42:35 -04:00
|
|
|
NetworkID: n.NetworkID,
|
|
|
|
Type: NetworkEventTypeJoin,
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if n.Leaving {
|
2016-05-17 00:42:35 -04:00
|
|
|
nEvent.Type = NetworkEventTypeLeave
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
d.nDB.handleNetworkEvent(nEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|