Skip to content

Commit 34c422b

Browse files
committed
assertNoEngineResetLock
1 parent a80d07b commit 34c422b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,7 @@ public Store.MetadataSnapshot snapshotStoreMetadata() throws IOException {
16521652
Engine.IndexCommitRef indexCommit = null;
16531653
store.incRef();
16541654
try {
1655+
assert assertNoEngineResetLock();
16551656
synchronized (engineMutex) {
16561657
// if the engine is not running, we can access the store directly, but we need to make sure no one starts
16571658
// the engine on us. If the engine is running, we can get a snapshot via the deletion policy of the engine.
@@ -1812,6 +1813,7 @@ public CacheHelper getReaderCacheHelper() {
18121813
}
18131814

18141815
public void close(String reason, boolean flushEngine, Executor closeExecutor, ActionListener<Void> closeListener) throws IOException {
1816+
assert assertNoEngineResetLock();
18151817
synchronized (engineMutex) {
18161818
engineResetLock.readLock().lock(); // prevent engine resets while closing
18171819
try {
@@ -1977,6 +1979,7 @@ private void doLocalRecovery(
19771979
// First, start a temporary engine, recover the local translog up to the given checkpoint, and then close the engine again.
19781980
.<Void>newForked(l -> ActionListener.runWithResource(ActionListener.assertOnce(l), () -> () -> {
19791981
assert Thread.holdsLock(mutex) == false : "must not hold the mutex here";
1982+
assert assertNoEngineResetLock();
19801983
synchronized (engineMutex) {
19811984
engineResetLock.readLock().lock(); // prevent engine resets while closing
19821985
try {
@@ -2199,6 +2202,7 @@ public void openEngineAndSkipTranslogRecovery() throws IOException {
21992202

22002203
private void innerOpenEngineAndTranslog(LongSupplier globalCheckpointSupplier) throws IOException {
22012204
assert Thread.holdsLock(mutex) == false : "opening engine under mutex";
2205+
assert assertNoEngineResetLock();
22022206
if (state != IndexShardState.RECOVERING) {
22032207
throw new IndexShardNotRecoveringException(shardId, state);
22042208
}
@@ -2303,6 +2307,7 @@ private void onNewEngine(Engine newEngine) {
23032307
*/
23042308
public void performRecoveryRestart() throws IOException {
23052309
assert Thread.holdsLock(mutex) == false : "restart recovery under mutex";
2310+
assert assertNoEngineResetLock();
23062311
synchronized (engineMutex) {
23072312
assert refreshListeners.pendingCount() == 0 : "we can't restart with pending listeners";
23082313
engineResetLock.readLock().lock(); // prevent engine resets while closing
@@ -4443,6 +4448,7 @@ public void afterRefresh(boolean didRefresh) {
44434448
public void resetEngine(Consumer<Engine> postResetNewEngineConsumer) {
44444449
assert Thread.holdsLock(mutex) == false : "resetting engine under mutex";
44454450
assert waitForEngineOrClosedShardListeners.isDone();
4451+
assert assertNoEngineResetLock();
44464452
Engine previousEngine = null;
44474453
try {
44484454
synchronized (engineMutex) {
@@ -4489,6 +4495,7 @@ public void resetEngine(Consumer<Engine> postResetNewEngineConsumer) {
44894495
*/
44904496
void rollbackEngineToGlobalCheckpoint() throws IOException {
44914497
assert Thread.holdsLock(mutex) == false : "resetting engine under mutex";
4498+
assert assertNoEngineResetLock();
44924499
assert getActiveOperationsCount() == OPERATIONS_BLOCKED
44934500
: "engine rollback without blocking operations; active operations are [" + getActiveOperationsCount() + ']';
44944501
sync(); // persist the global checkpoint to disk
@@ -4517,6 +4524,7 @@ assert getActiveOperationsCount() == OPERATIONS_BLOCKED
45174524
) {
45184525
@Override
45194526
public IndexCommitRef acquireLastIndexCommit(boolean flushFirst) {
4527+
assert assertNoEngineResetLock();
45204528
synchronized (engineMutex) {
45214529
if (newEngineReference.get() == null) {
45224530
throw new AlreadyClosedException("engine was closed");
@@ -4528,6 +4536,7 @@ public IndexCommitRef acquireLastIndexCommit(boolean flushFirst) {
45284536

45294537
@Override
45304538
public IndexCommitRef acquireSafeIndexCommit() {
4539+
assert assertNoEngineResetLock();
45314540
synchronized (engineMutex) {
45324541
if (newEngineReference.get() == null) {
45334542
throw new AlreadyClosedException("engine was closed");
@@ -4539,6 +4548,7 @@ public IndexCommitRef acquireSafeIndexCommit() {
45394548
@Override
45404549
public void close() throws IOException {
45414550
Engine newEngine;
4551+
assert assertNoEngineResetLock();
45424552
synchronized (engineMutex) {
45434553
newEngine = newEngineReference.get();
45444554
if (newEngine == getCurrentEngine(true)) {
@@ -4692,4 +4702,16 @@ public void ensureMutable(ActionListener<Void> listener, boolean permitAcquired)
46924702
EngineResetLock getEngineResetLock() {
46934703
return engineResetLock;
46944704
}
4705+
4706+
private boolean assertNoEngineResetLock() {
4707+
assert engineResetLock.isReadLockedByCurrentThread()
4708+
: "Expected current thread ["
4709+
+ Thread.currentThread()
4710+
+ "] to not hold an engine read lock (lock ordering should be: engineMutex -> engineResetLock -> mutex)";
4711+
assert engineResetLock.isWriteLockedByCurrentThread()
4712+
: "Expected current thread ["
4713+
+ Thread.currentThread()
4714+
+ "] to not hold the engine write lock (lock ordering should be: engineMutex -> engineResetLock -> mutex)";
4715+
return true;
4716+
}
46954717
}

0 commit comments

Comments
 (0)