Skip to content

Commit fcdefb0

Browse files
committed
ignore the second request with same first index
1 parent d44bd68 commit fcdefb0

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,10 @@ private CompletableFuture<Void> appendLog(ReferenceCountedObject<List<LogEntryPr
16941694
final List<ConsecutiveIndices> entriesTermIndices;
16951695
try(UncheckedAutoCloseableSupplier<List<LogEntryProto>> entries = entriesRef.retainAndReleaseOnClose()) {
16961696
entriesTermIndices = ConsecutiveIndices.convert(entries.get());
1697-
appendLogTermIndices.append(entriesTermIndices);
1697+
if (!appendLogTermIndices.append(entriesTermIndices)) {
1698+
// index already exists, return the last future
1699+
return appendLogFuture.get();
1700+
}
16981701
}
16991702

17001703
entriesRef.retain();

ratis-server/src/main/java/org/apache/ratis/server/impl/ServerImplUtils.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,26 @@ synchronized Long getTerm(long index) {
136136
return floorEntry.getValue().getTerm(index);
137137
}
138138

139-
synchronized void append(List<ConsecutiveIndices> entriesTermIndices) {
140-
for(ConsecutiveIndices indices : entriesTermIndices) {
141-
map.put(indices.startIndex, indices);
139+
synchronized boolean append(List<ConsecutiveIndices> entriesTermIndices) {
140+
for(int i = 0; i < entriesTermIndices.size(); i++) {
141+
final ConsecutiveIndices indices = entriesTermIndices.get(i);
142+
final ConsecutiveIndices previous = map.put(indices.startIndex, indices);
143+
if (previous != null) {
144+
// index already exists, revert this append
145+
map.put(previous.startIndex, previous);
146+
for(int j = 0; j < i; j++) {
147+
map.remove(entriesTermIndices.get(j).startIndex);
148+
}
149+
return false;
150+
}
142151
}
152+
return true;
143153
}
144154

145155
synchronized void removeExisting(List<ConsecutiveIndices> entriesTermIndices) {
146156
for(ConsecutiveIndices indices : entriesTermIndices) {
147157
final ConsecutiveIndices removed = map.remove(indices.startIndex);
148-
Preconditions.assertSame(indices.startIndex, removed.startIndex, "removed");
158+
Preconditions.assertSame(indices, removed, "removed");
149159
}
150160
}
151161
}

0 commit comments

Comments
 (0)