// TODO: Make this use same buffer and event as RpcLogWriter. public void OnNext(ApplyCommandRequested data, long sequence, bool endOfBatch) { var appliedDifference = data.LogIdx - _node.Data.LastApplied; var logsToApply = EnumerableUtilities.Range(_node.Data.LastApplied + 1, (int)appliedDifference); foreach (var logIdx in logsToApply) { var command = _commandRegister.Get(_node.Data.CurrentTerm, logIdx); // The term may have been increased before the command was applied. In which case, rely on log matching to fix. if (command == null) { continue; } command.Execute(_serviceLocator); _nodePublisher.PublishEvent(new NodeCommandScheduled { Command = new ApplyEntry { EntryIdx = logIdx } }).Wait(); } }
public void DoesNotRemoveCommandsForTermsMatchingNewTermWhenTermChanges() { // Arrange const long term = 2L; const long logIdx = 3L; var command = new TestCommand(); var commandRegister = new CommandRegister(); commandRegister.Add(term, logIdx, command); commandRegister.Get(term, logIdx).Should().Be(command); // Act commandRegister.Handle(new TermChanged(term)); // Assert commandRegister.Get(term, logIdx).Should().Be(command); }
public void RemovesCommandsForOlderTermsWhenTermChanges() { // Arrange const long term = 1L; const long logIdx = 3L; var command = new TestCommand(); var commandRegister = new CommandRegister(); commandRegister.Add(term, logIdx, command); commandRegister.Get(term, logIdx).Should().Be(command); // Act commandRegister.Handle(new TermChanged(term + 1)); // Assert commandRegister.Get(term, logIdx).Should().BeNull(); }