Exemple #1
0
        public ReceiveGossipThread(GossipNode gossipNode, GossipManager gossipManager)
        {
            _gossipManager = gossipManager;
            _gossipNode    = gossipNode;
            try
            {
                Logger.Debug($"Gossip service successfully initialized on {_gossipNode.GossipPeer.EndPoint}");
                Logger.Debug($"I am {_gossipNode.GossipPeer}");

                _cluster = _gossipNode.GossipPeer.ClusterName;

                if (_cluster == null)
                {
                    throw new ArgumentNullException("cluster was null");
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex);
                throw;
            }
            _keepRunning = true;

            TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
        }
        public SendGossipThread(GossipNode gossipNode, GossipManager gossipManager)
        {
            _gossipNode    = gossipNode;
            _random        = new Random();
            _gossipManager = gossipManager;

            _replyTimer     = new Timer(new TimerCallback(ReplyTimerHandle));
            _heartbeatTimer = new Timer(new TimerCallback(HeartbeatTimerHandle));
        }
Exemple #3
0
        public GossipManager(GossipNode gossipNode)
        {
            _gossipNode            = gossipNode;
            _cts                   = new CancellationTokenSource();
            _currentStateExtension = new CurrentStateExtension();

            _requests       = new ConcurrentDictionary <string, BaseMessage>();
            _helloTimer     = new Timer(new TimerCallback(HelloTimerHandle));
            _heartbeatTimer = new Timer(new TimerCallback(HeartbeatTimerHandle));

            _fsm = new AsyncPassiveStateMachine <NodeState, GossipEvent>("GossipManager");
            _fsm.AddExtension(new ConsoleLogExtension());
            _fsm.AddExtension(_currentStateExtension);

            _fsm.Initialize(NodeState.Initialized);

            _fsm.In(NodeState.Initialized)
            .On(GossipEvent.HelloSend)
            .Goto(NodeState.HelloSent)
            .Execute(async() =>
            {
                // set hello timer
                await SendHello();
            });

            _fsm.In(NodeState.HelloSent)
            .On(GossipEvent.HelloAnswer)
            .Goto(NodeState.Infected)
            .Execute <HelloResponse>((msg) =>
            {
                ReceiveHelloAnswer(msg);
            })
            .On(GossipEvent.HelloExpired)
            .Goto(NodeState.Initialized)
            .Execute(async() =>
            {
                // hello timer re-set
                await SendHello();
            });

            _fsm.In(NodeState.Infected)
            .On(GossipEvent.HeartbeatExpired)
            .Goto(NodeState.Susceptible)
            .Execute(async() =>
            {
                await SendHeartbeat();
            })
            .On(GossipEvent.HeartbeatAnswer)
            .Goto(NodeState.Infected)
            .Execute <HeartbeatResponse>((msg) => {
                IGossipPeer senderPeer = msg.Members?.FirstOrDefault();
                MergeLists(senderPeer, msg.Members);
            });

            _fsm.In(NodeState.Infected)
            .On(GossipEvent.HelloReceive)
            .Execute <HelloRequest>(async(msg) =>
            {
                await SendHelloAnswer(msg);
            });

            _fsm.In(NodeState.Susceptible)
            .On(GossipEvent.HelloReceive)
            .Execute <HelloRequest>(async(msg) =>
            {
                await SendHelloAnswer(msg);
            })
            .On(GossipEvent.HeartbeatReceive)
            .If <HeartbeatRequest>((msg) =>
            {
                return(_gossipNode.GossipPeer.Heartbeat < msg.Peer.Heartbeat);
            })
            .Goto(NodeState.Infected)
            .Execute <HeartbeatRequest>(async(msg) =>
            {
                await ReceiveHeartbeat(msg);
            })
            .On(GossipEvent.HeartbeatAnswer)
            .If <HeartbeatResponse>((msg) =>
            {
                return(msg.Members[0].Heartbeat > _gossipNode.GossipPeer.Heartbeat);
            }).Goto(NodeState.Infected);

            _fsm.Start();
        }