You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ratis.apache.org by "szetszwo (via GitHub)" <gi...@apache.org> on 2024/08/05 16:38:58 UTC
Re: [PR] RATIS-2134. `logMetadata` could miss appending the `metadataEntry` with the lastCommitIndex [ratis]
szetszwo commented on code in PR #1130:
URL: https://github.com/apache/ratis/pull/1130#discussion_r1704376160
##########
ratis-server/src/main/java/org/apache/ratis/server/raftlog/RaftLogBase.java:
##########
@@ -230,6 +230,9 @@ private long appendMetadataImpl(long term, long newCommitIndex) {
final long nextIndex;
try(AutoCloseableLock writeLock = writeLock()) {
nextIndex = getNextIndex();
+ if (nextIndex - 1 != newCommitIndex) {
+ return INVALID_LOG_INDEX;
+ }
Review Comment:
Let's add more information to your example. Suppose`entriesToCommit` are
- STATEMACHINELOGENTRY(1, 1)
- STATEMACHINELOGENTRY(1, 2)
- METADATAENTRY(term: 1, index: 3, commitIndex: 1)
and
- `nextIndex` is 4.
Then, `newCommitIndex` is 2 and `nextIndex - 1 != newCommitIndex` is false. It won't append the metadata entry in this case. Why checking the if-condition here?
##########
ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderStateImpl.java:
##########
@@ -960,7 +957,14 @@ private void updateCommit(LogEntryHeader[] entriesToCommit) {
}
hasConfiguration |= entry.getLogEntryBodyCase() == LogEntryBodyCase.CONFIGURATIONENTRY;
raftLog.getRaftLogMetrics().onLogEntryCommitted(entry);
+ if (entry.getLogEntryBodyCase() != LogEntryBodyCase.METADATAENTRY) {
+ lastCommitIndex = entry.getIndex();
+ }
}
+ if (logMetadataEnabled) {
Review Comment:
It should check if `lastCommitIndex != RaftLog.INVALID_LOG_INDEX`, i.e.
```java
if (logMetadataEnabled && lastCommitIndex != RaftLog.INVALID_LOG_INDEX) {
logMetadata(lastCommitIndex);
}
```
##########
ratis-server/src/main/java/org/apache/ratis/server/raftlog/RaftLogBase.java:
##########
@@ -242,25 +245,11 @@ private boolean shouldAppendMetadata(long newCommitIndex) {
// do not log the first conf entry
return false;
} else if (Optional.ofNullable(lastMetadataEntry.get())
- .filter(e -> e.getIndex() == newCommitIndex || e.getMetadataEntry().getCommitIndex() >= newCommitIndex)
+ .filter(e -> e.getMetadataEntry().getCommitIndex() >= newCommitIndex)
.isPresent()) {
- //log neither lastMetadataEntry, nor entries with a smaller commit index.
+ // do not log entries with a smaller commit index.
return false;
Review Comment:
Let's simplify the code as below:
```java
private boolean shouldAppendMetadata(long newCommitIndex) {
if (newCommitIndex <= 0) {
// do not log the first conf entry
return false;
}
final LogEntryProto last = lastMetadataEntry.get();
// do not log entries with a smaller commit index.
return newCommitIndex > last.getMetadataEntry().getCommitIndex();
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@ratis.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org