Beispiel #1
0
        async Task StartTcpListener()
        {
            try
            {
                if (server == null)
                {
                    server = new TcpListener(IPAddress.Any, this.port); //to telnet dev.tiesky.com 27751
                }
                server.Start();

                log.Log(new WarningLogEntry()
                {
                    LogType     = WarningLogEntry.eLogType.DEBUG,
                    Description = $"Started TcpNode on port {server.LocalEndpoint.ToString()}"
                });

                while (true)
                {
                    var peer = await server.AcceptTcpClientAsync();//.ConfigureAwait(false);

                    spider.AddTcpClient(peer);
                }
            }
            catch (Exception ex)
            {
                if (log != null)
                {
                    log.Log(new WarningLogEntry()
                    {
                        Exception = ex
                    });
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// If this action works, it can mean that Node can give a bid to be the candidate after specified time interval
        /// Starts Election timer only in case if it's not running yet
        /// </summary>
        /// <param name="userToken"></param>
        void LeaderHeartbeatTimeout(object userToken)
        {
            try
            {
                lock (lock_Operations)
                {
                    if (NodeState == eNodeState.Leader) //me is the leader
                    {
                        RemoveLeaderHeartbeatWaitingTimer();
                        return;
                    }

                    if (DateTime.Now.Subtract(this.LeaderHeartbeatArrivalTime).TotalMilliseconds < this.entitySettings.LeaderHeartbeatMs)
                    {
                        return; //Early to elect, we receive completely heartbeat from the leader
                    }
                    VerbosePrint("Node {0} LeaderHeartbeatTimeout", NodeAddress.NodeAddressId);

                    RunElectionTimer();
                }
            }
            catch (Exception ex)
            {
                Log.Log(new WarningLogEntry()
                {
                    Exception = ex, Method = "Raft.RaftNode.LeaderHeartbeatTimeout"
                });
            }
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="jsonConfiguration">json representation of Raft.NodeSettings</param>
        /// <param name="dbreezePath"></param>
        /// <param name="port"></param>
        /// <param name="log"></param>
        /// <param name="OnCommit"></param>
        /// <returns></returns>
        public static TcpRaftNode GetFromConfig(string jsonConfiguration, string dbreezePath, int port, IWarningLog log, Func <string, ulong, byte[], bool> OnCommit)
        {
            try
            {
                TcpRaftNode  rn = null;
                NodeSettings ns = NodeSettings.BiserJsonDecode(jsonConfiguration);

                //Biser.JsonEncoder encc = new Biser.JsonEncoder(new NodeSettings() { TcpClusterEndPoints = eps, RaftEntitiesSettings = reSettings });
                //var str = encc.GetJSON(Biser.JsonSettings.JsonStringStyle.Prettify);

                //NodeSettings nhz = NodeSettings.BiserJsonDecode(str);

                //encc = new Biser.JsonEncoder(nhz);
                //str = encc.GetJSON(Biser.JsonSettings.JsonStringStyle.Prettify);

                rn = new TcpRaftNode(ns, dbreezePath, OnCommit, port, log);
                return(rn);
            }
            catch (Exception ex)
            {
                if (log != null)
                {
                    log.Log(new WarningLogEntry {
                        LogType = WarningLogEntry.eLogType.ERROR, Exception = ex, Method = "TcpRaftNode.GetFromConfig JSON"
                    });
                }
            }
            return(null);
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="address">Address of the node who sent the signal</param>
        /// <param name="signalType"></param>
        /// <param name="data"></param>
        public void HandleRaftSignal(NodeRaftAddress address, eRaftSignalType signalType, object data)
        {
            try
            {
                if (GlobalConfig.Verbose)
                {
                    Console.WriteLine("mesage from:" + address.EndPointSID + " signal:" + signalType);
                }
                lock (lock_Operations)
                {
                    switch (signalType)
                    {
                    case eRaftSignalType.LeaderHearthbeat:
                        ParseLeaderHeartbeat(address, data);
                        break;

                    case eRaftSignalType.CandidateRequest:
                        ParseCandidateRequest(address, data);
                        break;

                    case eRaftSignalType.VoteOfCandidate:
                        ParseVoteOfCandidate(address, data);
                        break;

                    case eRaftSignalType.StateLogEntrySuggestion:
                        ParseStateLogEntrySuggestion(address, data);
                        break;

                    case eRaftSignalType.StateLogEntryRequest:
                        ParseStateLogEntryRequest(address, data);
                        break;

                    case eRaftSignalType.StateLogEntryAccepted:
                        this.logHandler.ParseStateLogEntryAccepted(address, data);
                        break;

                    case eRaftSignalType.StateLogRedirectRequest:     //Not a leader node tries to add command
                        this.logHandler.ParseStateLogRedirectRequest(address, data);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Log(new WarningLogEntry()
                {
                    Exception = ex, Method = "Raft.RaftNode.IncomingSignalHandler"
                });
            }
        }
Beispiel #5
0
        void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            tmr.Stop();
            tmr.Enabled = false;

            if (disposed)
            {
                return;
            }

            try
            {
                List <ulong> ids2remove = new List <ulong>();

                _sync.EnterWriteLock();
                try
                {
                    var    now      = DateTime.UtcNow;
                    double msPassed = 0;

                    foreach (var el in Events)
                    {
                        msPassed = (now - el.Value.ElapseAt).TotalMilliseconds;

                        if (msPassed > 0)
                        {
                            //if (el.Value.Name == "LEADER")
                            //{
                            //    //Console.WriteLine("Elapsed: " + now.ToString("HH:mm:ss.ms"));
                            //    Console.WriteLine("Must elapse: " + el.Value.ElapseAt);
                            //}

                            if (el.Value.RepeatOnce)
                            {
                                ids2remove.Add(el.Key);
                            }
                            else
                            {
                                el.Value.ElapseAt = now.AddMilliseconds(el.Value.Milliseconds);
                                //if (el.Value.Name == "LEADER")
                                //{
                                //    //Console.WriteLine("Elapsed: " + now.ToString("HH:mm:ss.ms"));
                                //    Console.WriteLine("Next elapse: " + el.Value.ElapseAt);
                                //}
                            }

                            Task.Run(() =>
                            {
                                el.Value.Action(el.Value.UserToken);
                            });
                        }
                    }

                    if (ids2remove.Count() > 0)
                    {
                        ids2remove.ForEach(r => Events.Remove(r));
                    }

                    this.RecalculateTimer();
                }
                finally
                {
                    _sync.ExitWriteLock();
                }
            }
            catch (Exception ex)
            {
                Log.Log(new WarningLogEntry()
                {
                    Exception = ex, Method = "Raft.TimeMaster.tmr_Elapsed"
                });
            }


            tmr.Enabled = true;
            tmr.Start();
        }