Closed killme2008 closed 1 year ago
Hello, I am interested in this issue. My specific implementation process is as follows:
(1)we can declare a setAutoCommit (boolean status)
method in the Iterator interface.
(2)In the IteratorImpl class, we need to declare two variables of Boolean type. The first is autoCommit
, which is used to declare whether to enable the autocommit function, and the second is lastCommit
, which indicates whether the previous iterator has been committed.
(3)So the code for the modified hasNext
method is shown in the following figure:
Each time the hasNext method is called in the loop, the method will perform additional checks:
<3.1> Whether automatic submission is enabled;
<3.2> Whether the last loop has been submitted (the lastCommit field defaults to true in the first loop);
<3.3> Whether there were errors in the last loop
<3.4>Then obtain whether the iterator has any elements that have not been traversed
<3.5>Finally, set lastCommit
to false, indicating that this loop has not been submitted yet
(4)We also need to modify the commit method as follows: This means that after we submit, the previous submission status will become submitted
@1294566108 Cool, please create a PR, thanks.
Okay, I have submitted the PR, could you please help review it
The current log Iterator is used to apply a batch of raft logs to state machine. After the batch is applied to state machine and returns from
StateMachine#onApply
method, these logs are considered committed.And while iterating the logs in iterator:
Iteartor#setErrorAndRollback
to rollback applied logs and halt the state machine.Iteartor#commit
to commit the logs already applied.If we want to commit every log while applying, we should call
commit
method everytime:I think we can add an auto-commit mode to simplify the code:
After enabling auto-commit mode by
it.setAutoCommit(true)
, the iterator will callcommit
automatically when callinghasNext()
that returnstrue
.