diff --git a/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java b/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java index 881f4602be1c7..36d34ffc3d938 100644 --- a/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java +++ b/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java @@ -3950,8 +3950,8 @@ ReplicationTracker getReplicationTracker() { /** * Executes a scheduled refresh if necessary. Completes the listener with true if a refresh was performed otherwise false. */ - public void scheduledRefresh(ActionListener listener) { - ActionListener.run(listener, l -> { + public boolean scheduledRefresh(ActionListener listener) { + try { verifyNotClosed(); boolean listenerNeedsRefresh = refreshListeners.refreshNeeded(); final Engine engine = getEngine(); @@ -3967,18 +3967,22 @@ && isSearchIdle() logger.trace("scheduledRefresh: search-idle, skipping refresh"); engine.maybePruneDeletes(); // try to prune the deletes in the engine if we accumulated some setRefreshPending(engine); - l.onResponse(false); - return; + listener.onResponse(false); + return false; } else { logger.trace("scheduledRefresh: refresh with source [schedule]"); - engine.maybeRefresh("schedule", l.map(Engine.RefreshResult::refreshed)); - return; + engine.maybeRefresh("schedule", listener.map(Engine.RefreshResult::refreshed)); + return true; } } logger.trace("scheduledRefresh: no refresh needed"); engine.maybePruneDeletes(); // try to prune the deletes in the engine if we accumulated some - l.onResponse(false); - }); + listener.onResponse(false); + return false; + } catch (Exception e) { + listener.onFailure(e); + return false; + } } /** diff --git a/server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java b/server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java index d002ada47f742..85ff509067ff2 100644 --- a/server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java +++ b/server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java @@ -3926,11 +3926,7 @@ public void testScheduledRefresh() throws Exception { assertTrue(primary.searchIdleTime() >= TimeValue.ZERO.millis()); primary.flushOnIdle(0); logger.info("--> scheduledRefresh(future5)"); - assertBusy(() -> { - PlainActionFuture future5 = new PlainActionFuture<>(); - primary.scheduledRefresh(future5); - assertTrue(future5.actionGet()); // make sure we refresh once the shard is inactive - }); + assertTrue(primary.scheduledRefresh(ActionListener.noop())); // make sure we refresh once the shard is inactive try (Engine.Searcher searcher = primary.acquireSearcher("test")) { assertEquals(3, searcher.getIndexReader().numDocs()); }