Пример #1
0
        //protected override bool VoteRequest(Client client, VoteRequest request)
        //{
        //    if (client == null)
        //        client = new Client(_server, request.From);

        //    return base.VoteRequest(client, request);
        //}

        // should give its vote
        //protected override bool VoteRequest(Client client, VoteRequest request)
        //{
        //    return true;
        //}

        protected override bool AppendEntriesRequest(Client client, AppendEntriesRequest request)
        {
            return base.AppendEntriesRequest(client, request);
        }
Пример #2
0
        //protected override bool VoteRequest(Client client, VoteRequest request)
        //{
        //    return true;
        //}

        protected override bool AppendEntriesRequest(Client client, AppendEntriesRequest request)
        {
            return true;
        }
Пример #3
0
        //public AppendEntriesReply? AppendEntriesRequest2(AppendEntriesRequest request)
        //{
        //    var client = _server.GetClient(request.From);
        //    return AppendEntriesRequest2(client, request);
        //}

        public bool AppendEntriesRequest(AppendEntriesRequest request)
        {
            var client = _server.GetClient(request.From);
            return AppendEntriesRequest(client, request);
        }
Пример #4
0
        protected override bool AppendEntriesRequest(Client client, AppendEntriesRequest request)
        {
            var _persistedState = _server.PersistedStore;
            if (_persistedState.Term < request.Term)
                _persistedState.Term = request.Term;

            _server.ChangeState(new FollowerState(_server));
            return false;
        }
Пример #5
0
 //protected override bool VoteReply(Client client, VoteReply reply)
 //{
 //    return true;
 //}
 
 protected override bool AppendEntriesRequest(Client client, AppendEntriesRequest request)
 {
     client.SendRemoveServerRequest();
     return base.AppendEntriesRequest(client, request);
 }
Пример #6
0
 protected abstract bool AppendEntriesRequest(Client client, AppendEntriesRequest request);
Пример #7
0
        public void SendAppendEntriesRequest()
        {
            var _persistedState = _server.PersistedStore;
            //Console.WriteLine("Send heart beat");
            var prevIndex = _nextIndex - 1;
            var lastIndex = Math.Min(prevIndex + 1, _persistedState.Length);
            if (_matchIndex + 1 < _nextIndex)
                lastIndex = prevIndex;

            var entries = _persistedState.GetEntries(prevIndex, lastIndex);
            //if (entries != null && entries.Length > 0)
            //    Console.WriteLine("{0}: {4} - Send AppendEnties[{1}-{2}] to {3}", _server.ID, prevIndex, lastIndex, ID, _server.Tick);
            //else
            //    Console.WriteLine("{0}: Send heart beat to {1}", _server.ID, _id);

            _rpcDue = _server.Tick + _server.PersistedStore.RPC_TIMEOUT;
            _nextHeartBeat = _server.Tick + (_server.PersistedStore.ELECTION_TIMEOUT / 4);

            var message = new AppendEntriesRequest()
            {
                From = _server.ID,
                Term = _persistedState.Term,
                PrevIndex = prevIndex,
                PrevTerm = _persistedState.GetTerm(prevIndex),
                AgentIP = _server.AgentIP,
                Entries = entries,
                CommitIndex = Math.Min(_server.CommitIndex, lastIndex)
            };

            _lastMessage = message;
            _server.Transport.SendMessage(this, message);
            _rpcDue = _server.Tick + _server.PersistedStore.RPC_TIMEOUT;
        }
Пример #8
0
 public void SendRequest(Peer peer, AppendEntriesRequest request)
 {
     SendMessage(peer, request);
 }
Пример #9
0
 protected override bool AppendEntriesRequest(Client client, AppendEntriesRequest request)
 {
     LastMessage = request;
     LastClient = client;
     return true;
 }
Пример #10
0
        protected override bool AppendEntriesRequest(Client client, AppendEntriesRequest request)
        {
            var _persistedState = _server.PersistedStore;
            if (_persistedState.Term < request.Term)
                _persistedState.Term = request.Term;

            //Console.WriteLine("heatbeat");
            _leader = client;
            _leader.AgentIP = request.AgentIP;

            var success = false;
            var matchIndex = 0u;

            if (_persistedState.Term == request.Term)
            {
                resetHeartbeat();

                if (request.PrevIndex == 0 ||
                    (request.PrevIndex <= _persistedState.Length && _persistedState.GetTerm(request.PrevIndex) == request.PrevTerm))
                {
                    success = true;

                    var index = request.PrevIndex;
                    for (var i = 0; request.Entries != null && i < request.Entries.Length; i++)
                    {
                        index++;
                        if (_persistedState.GetTerm(index) != request.Entries[i].Index.Term)
                        {
                            while (_persistedState.Length > index - 1)
                            {
                                Console.WriteLine("{0}: Rolling back log {1}", _server.Name, _persistedState.Length - 1);
                                _persistedState.Pop(_server);
                            }

                            //Console.WriteLine("{0}: Writing log value {1}", _id, request.Entries[i].Offset);
                            var pushed = _persistedState.Push(_server, request.Entries[i]);
                            if (pushed)
                            {
                                var lastEntry = _persistedState.GetEntry(index).Value;
                                if (!LogEntry.AreEqual(lastEntry, request.Entries[i]))
                                {
                                    Console.WriteLine("{0}: Log entry push didn't match", _server.Name);
                                    Console.WriteLine("{0}: Expected: {1}", _server.Name, request.Entries[i]);
                                    Console.WriteLine("{0}: Got:      {1}", _server.Name, lastEntry);

                                    pushed = false;
                                }
                                //Console.WriteLine("{0}: Appending index: {1}, data length: {2}", _server.ID, index, request.Entries[i].Index.ChunkSize);                             
                            }

                            if(!pushed)
                            {
                                index--;
                                break;
                            }
                        }
                    }

                    matchIndex = index;
                    _server.AdvanceToCommit(Math.Max(_server.CommitIndex, request.CommitIndex));
                }
                else
                {
                    Console.WriteLine("{0}: Append unsuccessful {{ request.PrevIndex[{1}] == 0 || (request.PrevIndex[{1}] <= _persistedState.Length[{2}] && _persistedState.GetTerm(request.PrevIndex[{1}])[{3}] == request.PrevTerm[{4}])) }}", _server.Name, request.PrevIndex, _persistedState.Length, _persistedState.GetTerm(request.PrevIndex), request.PrevTerm);
                    matchIndex = _server.CommitIndex;
                }
            }
            else
            {
                Console.WriteLine("{0}: Append failed, terms didn't match", _server.Name);
                //matchIndex = _server.CommitIndex;
            }

            client.SendAppendEntriesReply(matchIndex, success);
            return true;
        }
Пример #11
0
        protected void handleAppendEntriesRequest(IConsensus model, AppendEntriesRequest request)
        {
            if (_persistedState.Term < request.Term)
                stepDown(model, request.Term);

            var peer = _peers.First(x => x.ID == request.From);
            var success = false;
            var matchIndex = 0u;

            if (_persistedState.Term == request.Term)
            {
                _state = ServerState.Follower;
                updateElectionAlarm(model);

                if (request.PrevIndex == 0 ||
                    (request.PrevIndex <= _persistedState.Length && _persistedState.GetTerm(request.PrevIndex) == request.PrevTerm))
                {
                    success = true;

                    var index = request.PrevIndex;
                    for (var i = 0; request.Entries != null && i < request.Entries.Length; i++)
                    {
                        index++;
                        if (_persistedState.GetTerm(index) != request.Entries[i].Index.Term)
                        {
                            while (_persistedState.Length > index - 1)
                            {
                                Console.WriteLine("{0}: Rolling back log {1}", _id, _persistedState.Length - 1);
                                _persistedState.Pop();
                            }

                            //Console.WriteLine("{0}: Writing log value {1}", _id, request.Entries[i].Offset);
                            _persistedState.Push(request.Entries[i]);
                        }
                    }

                    matchIndex = index;
                    commitIndex(Math.Max(_commitIndex, request.CommitIndex));
                }
            }

            model.SendReply(peer, new AppendEntriesReply() { From = _id, Term = _persistedState.Term, MatchIndex = matchIndex, Success = success });
        }