Exemple #1
0
        public void ApplyCommits(long from, long to)
        {
            Debug.Assert(to >= from);
            foreach (var entry in PersistentState.LogEntriesAfter(from, to))
            {
                try
                {
                    var oldCommitIndex = CommitIndex;
                    var command        = PersistentState.CommandSerializer.Deserialize(entry.Data);

                    StateMachine.Apply(entry, command);

                    Debug.Assert(entry.Index == StateMachine.LastAppliedIndex);
                    if (_log.IsDebugEnabled)
                    {
                        _log.Debug("Committing entry #{0}", entry.Index);
                    }

                    var tcc = command as TopologyChangeCommand;
                    if (tcc != null)
                    {
                        if (_log.IsInfoEnabled)
                        {
                            _log.Info("ApplyCommits for TopologyChangedCommand, tcc.Requested = {0}, Name = {1}",
                                      tcc.Requested, Name);
                        }

                        // StartTopologyChange(tcc); - not sure why it was needed, see RavenDB-3808 for details
                        CommitTopologyChange(tcc);
                    }

                    OnCommitIndexChanged(oldCommitIndex, CommitIndex);
                    OnCommitApplied(command);
                }
                catch (Exception e)
                {
                    _log.Error("Failed to apply commit", e);
                    throw;
                }
            }

            if (StateMachine.SupportSnapshots == false)
            {
                return;
            }

            var commitedEntriesCount = PersistentState.GetCommitedEntriesCount(to);

            if (commitedEntriesCount >= Options.MaxLogLengthBeforeCompaction)
            {
                // we need to pass this to the state machine so it can signal us
                // when it is fine to continue modifications again, otherwise
                // we might start the snapshot in a different term / index than we
                // are actually saying we did.
                var allowFurtherModifications = new ManualResetEventSlim();
                SnapshotAndTruncateLog(to, allowFurtherModifications);
                allowFurtherModifications.Wait(CancellationToken);
            }
        }