Пример #1
0
        private AppendEntriesResponse AppendEntries(string address, IHost host, ulong nextIndex)
        {
            AppendEntriesRequest request = new AppendEntriesRequest
            {
                LeaderId     = Id,
                Term         = _stateController.PersistentState.CurrentTerm,
                PrevLogIndex = _stateController.PersistentState.LastLogIndex,
                PrevLogTerm  = _stateController.PersistentState.LastLogTerm,
                LeaderCommit = _stateController.VolatileState.CommitIndex
            };

            if (_stateController.PersistentState.LastLogIndex >= nextIndex)
            {
                List <LogEntry> logEntries = _uncommittiedLogs.FindAll(w => w.Index >= nextIndex);
                if (logEntries != null && logEntries.Any())
                {
                    request.Entries = logEntries;
                }
            }

            AppendEntriesResponse response = host.AppendEntriesInvoke(request);

            if (!response.IsSuccess && _leaderVolatileStates[address].NextIndex != 0)
            {
                _leaderVolatileStates[address].NextIndex -= 1;
                response = AppendEntries(address, host, _leaderVolatileStates[address].NextIndex);
            }
            return(response);
        }