proposal: Auto commit mode for applying log iterator #957
Closed
Description
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:
- We can call
Iteartor#setErrorAndRollback
to rollback applied logs and halt the state machine. - Or call
Iteartor#commit
to commit the logs already applied.
If we want to commit every log while applying, we should call commit
method everytime:
while(it.hasNext()) {
ByteBuffer data = it.getData();
// apply data to state machine
......
it.commit();
it.next();
}
I think we can add an auto-commit mode to simplify the code:
// Enable auto-commit mode
it.setAutoCommit(true);
while(it.hasNext()) {
ByteBuffer data = it.getData();
// apply data to state machine
......
it.next();
}
After enabling auto-commit mode by it.setAutoCommit(true)
, the iterator will call commit
automatically when calling hasNext()
that returns true
.