示例#1
0
 internal void UpdateCurrentTerm(long term, string leader)
 {
     PersistentState.UpdateTermTo(this, term);
     SetState(RaftEngineState.Follower);
     _log.Debug("UpdateCurrentTerm() setting new leader : {0}", leader ?? "no leader currently");
     CurrentLeader = leader;
 }
示例#2
0
        public RaftEngine(RaftEngineOptions raftEngineOptions)
        {
            _raftEngineOptions = raftEngineOptions;
            EngineStatistics   = new RaftEngineStatistics(this);
            Debug.Assert(raftEngineOptions.Stopwatch != null);

            _log = LogManager.GetLogger(raftEngineOptions.Name + "." + GetType().FullName);

            _eventLoopCancellationTokenSource = new CancellationTokenSource();

            Name            = raftEngineOptions.Name;
            PersistentState = new PersistentState(raftEngineOptions.Name, raftEngineOptions.StorageOptions, _eventLoopCancellationTokenSource.Token)
            {
                CommandSerializer = new JsonCommandSerializer()
            };

            _currentTopology = PersistentState.GetCurrentTopology();

            //warm up to make sure that the serializer don't take too long and force election timeout
            PersistentState.CommandSerializer.Serialize(new NopCommand());

            var thereAreOthersInTheCluster = CurrentTopology.QuorumSize > 1;

            if (thereAreOthersInTheCluster == false && CurrentTopology.IsVoter(Name))
            {
                PersistentState.UpdateTermTo(this, PersistentState.CurrentTerm + 1);// restart means new term
                SetState(RaftEngineState.Leader);
            }
            else
            {
                SetState(RaftEngineState.Follower);
            }

            _eventLoopTask = Task.Factory.StartNew(EventLoop, TaskCreationOptions.LongRunning);
        }
示例#3
0
        public void CanProperlySnapshot()
        {
            using (var state = new PersistentState("self", StorageEnvironmentOptions.CreateMemoryOnly(), CancellationToken.None)
            {
                CommandSerializer = new JsonCommandSerializer()
            })
            {
                state.UpdateTermTo(null, 1);
                state.AppendToLeaderLog(new NopCommand());
                for (int i = 0; i < 5; i++)
                {
                    state.AppendToLeaderLog(new DictionaryCommand.Set
                    {
                        Key   = i.ToString(),
                        Value = i
                    });
                }

                state.MarkSnapshotFor(6, 1, 5);

                state.AppendToLeaderLog(new DictionaryCommand.Set
                {
                    Key   = "1",
                    Value = 4
                });

                var lastLogEntry = state.LastLogEntry();

                Assert.Equal(7, lastLogEntry.Index);
            }
        }
示例#4
0
        public RaftEngine(RaftEngineOptions raftEngineOptions)
        {
//#if DEBUG
//			Console.WriteLine("Press any key to continue loading Raft -> opportunity to attach debugger");
//			Console.ReadLine();
//#endif
            _raftEngineOptions = raftEngineOptions;
            Debug.Assert(raftEngineOptions.Stopwatch != null);

            _log = LogManager.GetLogger(raftEngineOptions.Name + "." + GetType().FullName);

            _eventLoopCancellationTokenSource = new CancellationTokenSource();

            Name            = raftEngineOptions.Name;
            PersistentState = new PersistentState(raftEngineOptions.Name, raftEngineOptions.StorageOptions, _eventLoopCancellationTokenSource.Token)
            {
                CommandSerializer = new JsonCommandSerializer()
            };

            _currentTopology = PersistentState.GetCurrentTopology();

            //warm up to make sure that the serializer don't take too long and force election timeout
            PersistentState.CommandSerializer.Serialize(new NopCommand());

            var thereAreOthersInTheCluster = CurrentTopology.QuorumSize > 1;

            if (thereAreOthersInTheCluster == false && CurrentTopology.IsVoter(Name))
            {
                PersistentState.UpdateTermTo(this, PersistentState.CurrentTerm + 1);                // restart means new term
                SetState(RaftEngineState.Leader);
            }
            else
            {
                SetState(RaftEngineState.Follower);
            }

            _commitIndex   = StateMachine.LastAppliedIndex;
            _eventLoopTask = Task.Run(() => EventLoop());
        }