File tree Expand file tree Collapse file tree 2 files changed +18
-5
lines changed
ratis-server/src/main/java/org/apache/ratis/server/impl Expand file tree Collapse file tree 2 files changed +18
-5
lines changed Original file line number Diff line number Diff 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 ();
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments