Ejemplo n.º 1
0
 public void AddLogEntryByFollower(StateLogEntrySuggestion suggestion)
 {
     this.log.AddLogEntryByFollower(suggestion);
     if (suggestion.IsCommitted)
     {
         this.stateMachine.logHandler.Commited();
     }
     if (stateMachine.States.LeaderHeartbeat != null && log.LastCommittedIndex < stateMachine.States.LeaderHeartbeat.LastStateLogCommittedIndex)
     {
         stateMachine.SyncronizeWithLeader(true);
     }
     else
     {
         stateMachine.States.LeaderSynchronizationIsActive = false;
     }
     if (stateMachine.States.LeaderHeartbeat != null &&
         log.LastCommittedIndex < stateMachine.States.LeaderHeartbeat.LastStateLogCommittedIndex)
     {
         stateMachine.SyncronizeWithLeader(true);
     }
     else
     {
         stateMachine.States.LeaderSynchronizationIsActive = false;
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="suggestion"></param>
        /// <returns></returns>
        public void AddToLogFollower(StateLogEntrySuggestion suggestion)
        {
            try
            {
                int pups = 0;
                lock (inMem.Sync)
                {
                    List <Tuple <ulong, ulong, StateLogEntry> > toDelete = new List <Tuple <ulong, ulong, StateLogEntry> >();

                    foreach (var el in inMem.SelectForwardFromTo(suggestion.StateLogEntry.Index, ulong.MinValue, true, ulong.MaxValue, ulong.MaxValue))
                    {
                        if (el.Item1 == suggestion.StateLogEntry.Index && el.Item2 == suggestion.StateLogEntry.Term)
                        {
                            if (pups > 0)
                            {
                                //!!!!!!!!!!!!!!! note, to be checked, it returns, but may be it could have smth to remove before (earlier term - ulong.MinValue is used )
                                throw new Exception("Pups more than 0");
                            }
                            return;
                        }
                        toDelete.Add(el);
                        pups++;
                    }
                    if (toDelete.Count > 0)
                    {
                        inMem.Remove(toDelete);
                    }
                    inMem.Add(suggestion.StateLogEntry.Index, suggestion.StateLogEntry.Term, suggestion.StateLogEntry);
                }

                //   rn.VerbosePrint($"{rn.NodeAddress.NodeAddressId}> AddToLogFollower (I/T): {suggestion.StateLogEntry.Index}/{suggestion.StateLogEntry.Term} -> Result:" +
                //      $" { (GetEntryByIndexTerm(suggestion.StateLogEntry.Index, suggestion.StateLogEntry.Term) != null)};");

                //Setting new internal LogId
                this.leaderState.tempPrevStateLogId   = StateLogId;
                this.leaderState.tempPrevStateLogTerm = StateLogTerm;
                StateLogId   = suggestion.StateLogEntry.Index;
                StateLogTerm = suggestion.StateLogEntry.Term;

                //tempPrevStateLogId = PreviousStateLogId;
                //tempPrevStateLogTerm = PreviousStateLogTerm;
                this.leaderState.tempStateLogId   = StateLogId;
                this.leaderState.tempStateLogTerm = StateLogTerm;

                if (suggestion.IsCommitted)
                {
                    if (
                        this.LastCommittedIndexTerm > suggestion.StateLogEntry.Term
                        ||
                        (
                            this.LastCommittedIndexTerm == suggestion.StateLogEntry.Term
                            &&
                            this.LastCommittedIndex > suggestion.StateLogEntry.Index
                        )
                        )
                    {
                        //Should be not possible
                    }
                    else
                    {
                        this.LastCommittedIndex     = suggestion.StateLogEntry.Index;
                        this.LastCommittedIndexTerm = suggestion.StateLogEntry.Term;

                        //this.rn.Commited(suggestion.StateLogEntry.Index);
                        this.rn.logHandler.Commited();
                    }
                }
            }
            catch (Exception ex)
            {
            }
            //if (this.LastCommittedIndex < rn.LeaderHeartbeat.LastStateLogCommittedIndex)
            if (rn.States.LeaderHeartbeat != null && this.LastCommittedIndex < rn.States.LeaderHeartbeat.LastStateLogCommittedIndex)
            {
                rn.SyncronizeWithLeader(true);
            }
            else
            {
                LeaderSynchronizationIsActive = false;
            }
        }