Ejemplo n.º 1
0
        public override void Handle(CommandScheduled @event)
        {
            if (@event.LogEntry == null || @event.EncodedEntry == null)
            {
                throw new InvalidOperationException("Must set EncodedEntry on event before executing this step.");
            }

            _writeDataBlocks.WriteBlock(@event.EncodedEntry);
        }
Ejemplo n.º 2
0
        public void OnNext(AppendEntriesRequested data, long sequence, bool endOfBatch)
        {
            if (!data.PreviousLogIndex.HasValue || !data.PreviousLogTerm.HasValue)
            {
                return;
            }

            if (data.PreviousLogIndex.Equals(_node.Data.CommitIndex))
            {
                return;
            }

            if (data.PreviousLogIndex > _node.Data.CommitIndex ||
                data.PreviousLogTerm > _node.Data.CurrentTerm)
            {
                throw new InvalidOperationException(
                          "This command is invalid and should not be published to the buffer.");
            }

            var truncateCommandEntry = new TruncateLogCommandEntry
            {
                TruncateFromIndex = data.PreviousLogIndex.Value
            };

            using (var ms = new MemoryStream())
            {
                Serializer.SerializeWithLengthPrefix(ms, truncateCommandEntry, PrefixStyle.Base128);
                _writeDataBlocks.WriteBlock(ms.ToArray());
            }

            _nodePublisher.PublishEvent(
                new NodeCommandScheduled
            {
                Command = new TruncateLog
                {
                    TruncateFromIndex = data.PreviousLogIndex.Value
                }
            });
        }