Skip to content

Commit a4d2bf3

Browse files
committed
Latch to ensure ordering
1 parent f04ff7e commit a4d2bf3

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

core/src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
import java.util.Objects;
132132
import java.util.Set;
133133
import java.util.concurrent.CopyOnWriteArrayList;
134+
import java.util.concurrent.CountDownLatch;
134135
import java.util.concurrent.TimeUnit;
135136
import java.util.concurrent.TimeoutException;
136137
import java.util.concurrent.atomic.AtomicBoolean;
@@ -361,14 +362,17 @@ public void updatePrimaryTerm(final long newPrimaryTerm) {
361362
"primary terms can only go up; current term [" + primaryTerm + "], new term [" + newPrimaryTerm + "]";
362363
/*
363364
* Before this call returns, we are guaranteed that all future operations are delayed and so this happens before we
364-
* increment the primary term.
365+
* increment the primary term. The latch is needed to ensure that we do not unblock operations before the primary term is
366+
* incremented.
365367
*/
368+
final CountDownLatch latch = new CountDownLatch(1);
366369
indexShardOperationPermits.asyncBlockOperations(
367370
30,
368371
TimeUnit.MINUTES,
369-
() -> {},
372+
latch::await,
370373
e -> failShard("exception during primary term transition", e));
371374
primaryTerm = newPrimaryTerm;
375+
latch.countDown();
372376
}
373377
}
374378
}

0 commit comments

Comments
 (0)