Skip to content

Commit 5142d3d

Browse files
authored
Acquire the engine reset write lock during post_recovery refresh (#132531)
Acquire the engine reset write lock during post_recovery refresh to ensure the refresh is always executed on some engine implementations. Relates ES-12565
1 parent 716bff8 commit 5142d3d

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,10 +1891,19 @@ public void postRecovery(String reason, ActionListener<Void> listener) throws In
18911891
postRecoveryComplete = subscribableListener;
18921892
final ActionListener<Void> finalListener = ActionListener.runBefore(listener, () -> subscribableListener.onResponse(null));
18931893
try {
1894-
getEngine().refresh("post_recovery");
1895-
// we need to refresh again to expose all operations that were index until now. Otherwise
1896-
// we may not expose operations that were indexed with a refresh listener that was immediately
1897-
// responded to in addRefreshListener. The refresh must happen under the same mutex used in addRefreshListener
1894+
// Some engine implementations try to acquire the engine reset write lock during refresh: in case something else is holding the
1895+
// engine read lock at the same time then the refresh is a no-op for those engines. Here we acquire the engine reset write lock
1896+
// upfront to guarantee that the next refresh will be executed because some logic (like ShardFieldStats and
1897+
// RefreshShardFieldStatsListener) depend on the success of this "post_recovery" refresh.
1898+
engineResetLock.writeLock().lock();
1899+
try {
1900+
// we need to refresh again to expose all operations that were index until now. Otherwise
1901+
// we may not expose operations that were indexed with a refresh listener that was immediately
1902+
// responded to in addRefreshListener.
1903+
withEngine(engine -> engine.refresh("post_recovery"));
1904+
} finally {
1905+
engineResetLock.writeLock().unlock();
1906+
}
18981907
synchronized (mutex) {
18991908
if (state == IndexShardState.CLOSED) {
19001909
throw new IndexShardClosedException(shardId);

0 commit comments

Comments
 (0)