Exemple #1
0
        private PreciseTime GetCandidateFailedElectionTimeout()
        {
            PreciseTimeSpan delta;

            lock (localRandom)
                delta = PreciseTimeSpan.FromMilliseconds(localRandom.Next(350) + 150);
            //			LogMinorEvent("Election timeout at " + (DateTime.Now + delta.TimeSpan).ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture));
            return(PreciseTime.Now + delta);
        }
Exemple #2
0
        private void ThreadConsensus()
        {
            lock (localRandom)
                nextActionAt = PreciseTime.Now + PreciseTimeSpan.FromMilliseconds(localRandom.NextDouble() * 100);
            LogMinorEvent("Started consensus engine: Next action in " + (PreciseTime.Now - nextActionAt));

            while (!disposing)
            {
                myAddress = GetAddress(MemberID.Identifier);
                if (myAddress != Address.None && myAddress != BoundAddress)
                {
                    //GetAddress(MemberID.Identifier);
                    OnAddressMismatchDispose();
                    Dispose();
                    return;
                }

                try
                {
                    if (state == State.Leader)
                    {
                        CheckTimeouts(false);
                    }
                    CheckCommittedTimeouts();

                    var dl = nextActionAt;
                    if (PreciseTime.Now < dl)
                    {
                        //LogEvent(GetNanoTime()+"/"+nextActionAt);
                        lock (localRandom)
                            Thread.Sleep(TimeSpan.FromMilliseconds(localRandom.Next(10)));
                        continue;
                    }
                    dl = nextActionAt;
                    if (PreciseTime.Now < dl)
                    {
                        continue;
                    }
                    switch (state)
                    {
                    case State.Leader:
                        DoSerialized(() =>
                        {
                            //CheckTimeouts(true);
                            Broadcast(new AppendEntries(this));
                            if (IsLeader)
                            {
                                nextActionAt = NextHeartbeat;
                            }
                        });
                        break;

                    case State.Candidate:
                    case State.Follower:
                        if (MemberID.CanBeLeader)
                        {
                            InitElection();
                        }
                        else
                        {
                            state = State.Follower;
                        }
                        break;
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }
Exemple #3
0
 internal PreciseTime GetAppendMessageTimeout()
 {
     return(PreciseTime.Now + PreciseTimeSpan.FromMilliseconds(300));
 }
Exemple #4
0
 private PreciseTimeSpan GetElectionTimeoutNS()
 {
     lock (localRandom)
         return(PreciseTimeSpan.FromMilliseconds(localRandom.Next(350) + 150));
 }