2019-09-10 02:01:41 +03:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2019-09-10 03:14:23 +03:00
|
|
|
"container/list"
|
2019-09-10 02:01:41 +03:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-09-10 20:32:25 +03:00
|
|
|
pb "github.com/micro/go-micro/network/proto"
|
2019-09-10 02:01:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// MaxDepth defines max depth of peer topology
|
2019-09-10 20:32:25 +03:00
|
|
|
MaxDepth uint = 3
|
2019-09-10 02:01:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// node is network node
|
|
|
|
type node struct {
|
|
|
|
sync.RWMutex
|
|
|
|
// id is node id
|
|
|
|
id string
|
|
|
|
// address is node address
|
|
|
|
address string
|
2019-09-10 03:14:23 +03:00
|
|
|
// peers are nodes with direct link to this node
|
|
|
|
peers map[string]*node
|
2019-09-10 02:01:41 +03:00
|
|
|
// network returns the node network
|
|
|
|
network Network
|
2019-09-10 03:14:23 +03:00
|
|
|
// lastSeen keeps track of node lifetime and updates
|
2019-09-10 02:01:41 +03:00
|
|
|
lastSeen time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// Id is node ide
|
|
|
|
func (n *node) Id() string {
|
|
|
|
return n.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns node address
|
|
|
|
func (n *node) Address() string {
|
|
|
|
return n.address
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network returns node network
|
|
|
|
func (n *node) Network() Network {
|
|
|
|
return n.network
|
|
|
|
}
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
// Nodes returns a slice if all nodes in node topology
|
|
|
|
func (n *node) Nodes() []Node {
|
|
|
|
// we need to freeze the network graph here
|
|
|
|
// otherwise we might get invalid results
|
|
|
|
n.RLock()
|
|
|
|
defer n.RUnlock()
|
|
|
|
|
2019-09-10 15:31:02 +03:00
|
|
|
//track the visited nodes
|
|
|
|
visited := make(map[string]*node)
|
|
|
|
// queue of the nodes to visit
|
|
|
|
queue := list.New()
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
// push node to the back of queue
|
|
|
|
queue.PushBack(n)
|
|
|
|
// mark the node as visited
|
|
|
|
visited[n.id] = n
|
|
|
|
|
|
|
|
// keep iterating over the queue until its empty
|
|
|
|
for queue.Len() > 0 {
|
|
|
|
// pop the node from the front of the queue
|
|
|
|
qnode := queue.Front()
|
|
|
|
// iterate through all of the node peers
|
|
|
|
// mark the visited nodes; enqueue the non-visted
|
|
|
|
for id, node := range qnode.Value.(*node).peers {
|
|
|
|
if _, ok := visited[id]; !ok {
|
|
|
|
visited[id] = node
|
|
|
|
queue.PushBack(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove the node from the queue
|
|
|
|
queue.Remove(qnode)
|
|
|
|
}
|
|
|
|
|
2019-09-10 02:01:41 +03:00
|
|
|
var nodes []Node
|
2019-09-10 03:14:23 +03:00
|
|
|
// collect all the nodes and return them
|
|
|
|
for _, node := range visited {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Peers returns node peers
|
|
|
|
func (n *node) Peers() []Node {
|
|
|
|
var peers []Node
|
2019-09-10 02:01:41 +03:00
|
|
|
n.RLock()
|
2019-09-10 03:14:23 +03:00
|
|
|
for _, peer := range n.peers {
|
2019-09-10 02:01:41 +03:00
|
|
|
// make a copy of the node
|
2019-09-10 03:14:23 +03:00
|
|
|
p := &node{
|
|
|
|
id: peer.id,
|
|
|
|
address: peer.address,
|
2019-09-11 02:23:37 +03:00
|
|
|
peers: make(map[string]*node),
|
2019-09-10 03:14:23 +03:00
|
|
|
network: peer.network,
|
2019-09-10 02:01:41 +03:00
|
|
|
}
|
2019-09-11 02:23:37 +03:00
|
|
|
// collect peer's peers aka pop (peer of peer)
|
|
|
|
for id, pop := range peer.peers {
|
|
|
|
p.peers[id] = pop
|
|
|
|
}
|
2019-09-10 03:14:23 +03:00
|
|
|
peers = append(peers, p)
|
2019-09-10 02:01:41 +03:00
|
|
|
}
|
|
|
|
n.RUnlock()
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
return peers
|
2019-09-10 02:01:41 +03:00
|
|
|
}
|
|
|
|
|
2019-09-11 02:23:37 +03:00
|
|
|
// topology returns network topology up to MaxDepth
|
|
|
|
func (n *node) Topology(depth uint) *node {
|
2019-09-10 20:32:25 +03:00
|
|
|
n.RLock()
|
2019-09-11 02:23:37 +03:00
|
|
|
// make a copy of yourself
|
|
|
|
node := &node{
|
|
|
|
id: n.id,
|
|
|
|
address: n.address,
|
|
|
|
peers: make(map[string]*node),
|
|
|
|
network: n.network,
|
|
|
|
}
|
|
|
|
|
|
|
|
// return if we reach requested depth or we have no more peers
|
|
|
|
if depth == 0 || len(n.peers) == 0 {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
depth--
|
|
|
|
|
|
|
|
for _, peer := range n.peers {
|
|
|
|
nodePeer := peer.Topology(depth)
|
|
|
|
node.peers[nodePeer.id] = nodePeer
|
2019-09-10 20:32:25 +03:00
|
|
|
}
|
|
|
|
n.RUnlock()
|
|
|
|
|
2019-09-11 02:23:37 +03:00
|
|
|
return node
|
2019-09-10 20:32:25 +03:00
|
|
|
}
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
// getProtoTopology returns node peers up to given depth encoded in protobufs
|
|
|
|
// NOTE: this method is NOT thread-safe, so make sure you serialize access to it
|
2019-09-10 20:32:25 +03:00
|
|
|
func (n *node) getProtoTopology(depth uint) (*pb.Peer, error) {
|
|
|
|
node := &pb.Node{
|
2019-09-10 02:01:41 +03:00
|
|
|
Id: n.id,
|
|
|
|
Address: n.address,
|
|
|
|
}
|
2019-09-10 03:14:23 +03:00
|
|
|
|
2019-09-10 20:32:25 +03:00
|
|
|
pbPeers := &pb.Peer{
|
2019-09-10 03:14:23 +03:00
|
|
|
Node: node,
|
2019-09-10 20:32:25 +03:00
|
|
|
Peers: make([]*pb.Peer, 0),
|
2019-09-10 02:01:41 +03:00
|
|
|
}
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
// return if have either reached the depth or have no more peers
|
|
|
|
if depth == 0 || len(n.peers) == 0 {
|
|
|
|
return pbPeers, nil
|
2019-09-10 02:01:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// decrement the depth
|
|
|
|
depth--
|
|
|
|
|
2019-09-10 20:32:25 +03:00
|
|
|
var peers []*pb.Peer
|
2019-09-10 03:14:23 +03:00
|
|
|
for _, peer := range n.peers {
|
|
|
|
// get peers of the node peers
|
2019-09-10 02:01:41 +03:00
|
|
|
// NOTE: this is [not] a recursive call
|
2019-09-10 03:14:23 +03:00
|
|
|
pbPeerPeer, err := peer.getProtoTopology(depth)
|
2019-09-10 02:01:41 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-10 03:14:23 +03:00
|
|
|
// add current peer to explored peers
|
|
|
|
peers = append(peers, pbPeerPeer)
|
2019-09-10 02:01:41 +03:00
|
|
|
}
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
// add peers to the parent topology
|
|
|
|
pbPeers.Peers = peers
|
2019-09-10 02:01:41 +03:00
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
return pbPeers, nil
|
2019-09-10 02:01:41 +03:00
|
|
|
}
|
|
|
|
|
2019-09-10 20:32:25 +03:00
|
|
|
// unpackPeer unpacks pb.Peer into node topology of given depth
|
2019-09-10 03:14:23 +03:00
|
|
|
// NOTE: this method is NOT thread-safe, so make sure you serialize access to it
|
2019-09-10 20:32:25 +03:00
|
|
|
func unpackPeer(pbPeer *pb.Peer, depth uint) *node {
|
2019-09-10 03:14:23 +03:00
|
|
|
peerNode := &node{
|
|
|
|
id: pbPeer.Node.Id,
|
|
|
|
address: pbPeer.Node.Address,
|
|
|
|
peers: make(map[string]*node),
|
2019-09-10 02:01:41 +03:00
|
|
|
}
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
// return if have either reached the depth or have no more peers
|
|
|
|
if depth == 0 || len(pbPeer.Peers) == 0 {
|
|
|
|
return peerNode
|
2019-09-10 02:01:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// decrement the depth
|
|
|
|
depth--
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
peers := make(map[string]*node)
|
|
|
|
for _, pbPeer := range pbPeer.Peers {
|
|
|
|
peer := unpackPeer(pbPeer, depth)
|
|
|
|
peers[pbPeer.Node.Id] = peer
|
2019-09-10 02:01:41 +03:00
|
|
|
}
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
peerNode.peers = peers
|
2019-09-10 02:01:41 +03:00
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
return peerNode
|
2019-09-10 02:01:41 +03:00
|
|
|
}
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
// updatePeer updates node peer up to given depth
|
2019-09-10 02:01:41 +03:00
|
|
|
// NOTE: this method is not thread safe, so make sure you serialize access to it
|
2019-09-10 20:32:25 +03:00
|
|
|
func (n *node) updatePeerTopology(pbPeer *pb.Peer, depth uint) error {
|
2019-09-10 03:14:23 +03:00
|
|
|
if pbPeer == nil {
|
|
|
|
return errors.New("peer not initialized")
|
2019-09-10 02:01:41 +03:00
|
|
|
}
|
|
|
|
|
2019-09-10 03:14:23 +03:00
|
|
|
// NOTE: we need MaxDepth-1 because node n is the parent adding which
|
|
|
|
// gives us the max peer topology we maintain and propagate
|
|
|
|
peer := unpackPeer(pbPeer, MaxDepth-1)
|
|
|
|
|
|
|
|
// update node peers with new topology
|
|
|
|
n.peers[pbPeer.Node.Id] = peer
|
2019-09-10 02:01:41 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|