@@ -1352,7 +1352,7 @@ public void externalRefresh(String source, ActionListener<Engine.RefreshResult>
13521352 * Returns how many bytes we are currently moving from heap to disk
13531353 */
13541354 public long getWritingBytes () {
1355- return withEngineOrNullIfBeingReset (engine -> {
1355+ return tryWithEngineOrNull (engine -> {
13561356 if (engine == null ) {
13571357 return 0L ;
13581358 }
@@ -1372,7 +1372,7 @@ public RefreshStats refreshStats() {
13721372 }
13731373
13741374 public FlushStats flushStats () {
1375- return withEngineOrNullIfBeingReset (
1375+ return tryWithEngineOrNull (
13761376 engine -> new FlushStats (
13771377 flushMetric .count (),
13781378 periodicFlushMetric .count (),
@@ -1404,7 +1404,7 @@ public SeqNoStats seqNoStats() {
14041404 }
14051405
14061406 public IndexingStats indexingStats () {
1407- return withEngineOrNullIfBeingReset (engine -> {
1407+ return tryWithEngineOrNull (engine -> {
14081408 final boolean throttled ;
14091409 final long throttleTimeInMillis ;
14101410 if (engine == null ) {
@@ -1528,7 +1528,7 @@ public boolean flush(FlushRequest request) {
15281528 * @param request the flush request
15291529 * @param listener to notify after full durability has been achieved.
15301530 * <code>false</code> if <code>waitIfOngoing==false</code>
1531- * and an ongoing request or engine reset is detected, else <code>true</code>.
1531+ * and an ongoing request is detected, else <code>true</code>.
15321532 * If <code>false</code> is returned, no flush happened.
15331533 */
15341534 public void flush (FlushRequest request , ActionListener <Boolean > listener ) {
@@ -1543,23 +1543,11 @@ public void flush(FlushRequest request, ActionListener<Boolean> listener) {
15431543 */
15441544 verifyNotClosed ();
15451545 final long startTime = System .nanoTime ();
1546- final var finalListener = ActionListener .runBefore (
1547- l .map (Engine .FlushResult ::flushPerformed ),
1548- () -> flushMetric .inc (System .nanoTime () - startTime )
1546+ getEngine ().flush (
1547+ force ,
1548+ waitIfOngoing ,
1549+ ActionListener .runBefore (l .map (Engine .FlushResult ::flushPerformed ), () -> flushMetric .inc (System .nanoTime () - startTime ))
15491550 );
1550- if (waitIfOngoing ) {
1551- getEngine ().flush (force , waitIfOngoing , finalListener );
1552- } else {
1553- withEngineOrNullIfBeingReset (engine -> {
1554- if (engine == null ) {
1555- logger .trace ("detected engine is closed or being reset, not blocking" );
1556- finalListener .onResponse (Engine .FlushResult .NO_FLUSH );
1557- } else {
1558- engine .flush (force , waitIfOngoing , finalListener );
1559- }
1560- return null ;
1561- });
1562- }
15631551 });
15641552 }
15651553
@@ -1590,12 +1578,8 @@ public long readGlobalCheckpointForRecovery(Map<String, String> commitUserData)
15901578 */
15911579 public void trimTranslog () {
15921580 verifyNotClosed ();
1593- withEngineOrNullIfBeingReset (engine -> {
1594- if (engine != null ) {
1595- engine .trimUnreferencedTranslogFiles ();
1596- }
1597- return null ;
1598- });
1581+ final Engine engine = getEngine ();
1582+ engine .trimUnreferencedTranslogFiles ();
15991583 }
16001584
16011585 /**
@@ -2539,7 +2523,7 @@ private void verifyNotClosed(Exception suppressed) throws IllegalIndexShardState
25392523 * Returns number of heap bytes used by the indexing buffer for this shard, or 0 if the shard is closed
25402524 */
25412525 public long getIndexBufferRAMBytesUsed () {
2542- return withEngineOrNullIfBeingReset (engine -> {
2526+ return tryWithEngineOrNull (engine -> {
25432527 if (engine == null ) {
25442528 return 0L ;
25452529 }
@@ -2560,37 +2544,33 @@ public void addShardFailureCallback(Consumer<ShardFailure> onShardFailure) {
25602544 * indexing operation, so we can flush the index.
25612545 */
25622546 public void flushOnIdle (long inactiveTimeNS ) {
2563- withEngineOrNullIfBeingReset (engineOrNull -> {
2564- if (engineOrNull != null && System .nanoTime () - engineOrNull .getLastWriteNanos () >= inactiveTimeNS ) {
2565- boolean wasActive = active .getAndSet (false );
2566- if (wasActive ) {
2567- logger .debug ("flushing shard on inactive" );
2568- threadPool .executor (ThreadPool .Names .FLUSH )
2569- .execute (() -> flush (new FlushRequest ().waitIfOngoing (false ).force (false ), new ActionListener <>() {
2570- @ Override
2571- public void onResponse (Boolean flushed ) {
2572- if (flushed == false ) {
2573- // In case an ongoing flush was detected, revert active flag so that a next flushOnIdle request
2574- // will retry (#87888)
2575- active .set (true );
2576- }
2577- periodicFlushMetric .inc ();
2547+ Engine engineOrNull = getEngineOrNull ();
2548+ if (engineOrNull != null && System .nanoTime () - engineOrNull .getLastWriteNanos () >= inactiveTimeNS ) {
2549+ boolean wasActive = active .getAndSet (false );
2550+ if (wasActive ) {
2551+ logger .debug ("flushing shard on inactive" );
2552+ threadPool .executor (ThreadPool .Names .FLUSH )
2553+ .execute (() -> flush (new FlushRequest ().waitIfOngoing (false ).force (false ), new ActionListener <>() {
2554+ @ Override
2555+ public void onResponse (Boolean flushed ) {
2556+ if (flushed == false ) {
2557+ // In case an ongoing flush was detected, revert active flag so that a next flushOnIdle request
2558+ // will retry (#87888)
2559+ active .set (true );
25782560 }
2561+ periodicFlushMetric .inc ();
2562+ }
25792563
2580- @ Override
2581- public void onFailure (Exception e ) {
2582- if (state != IndexShardState .CLOSED ) {
2583- active .set (true );
2584- logger .warn ("failed to flush shard on inactive" , e );
2585- }
2564+ @ Override
2565+ public void onFailure (Exception e ) {
2566+ if (state != IndexShardState .CLOSED ) {
2567+ active .set (true );
2568+ logger .warn ("failed to flush shard on inactive" , e );
25862569 }
2587- }));
2588- }
2589- } else {
2590- logger .trace (() -> "flush on idle skipped as it is either closed, engine being reset, or has been recently written to" );
2570+ }
2571+ }));
25912572 }
2592- return null ;
2593- });
2573+ }
25942574 }
25952575
25962576 public boolean isActive () {
@@ -2781,21 +2761,19 @@ public IndexEventListener getIndexEventListener() {
27812761 * setting is set to true, throttling will pause indexing completely. Otherwise, indexing will be throttled to one thread.
27822762 */
27832763 public void activateThrottling () {
2784- withEngineOrNullIfBeingReset (engine -> {
2785- if (engine != null ) {
2786- engine .activateThrottling ();
2787- }
2788- return null ;
2789- });
2764+ try {
2765+ getEngine ().activateThrottling ();
2766+ } catch (AlreadyClosedException ex ) {
2767+ // ignore
2768+ }
27902769 }
27912770
27922771 public void deactivateThrottling () {
2793- withEngineOrNullIfBeingReset (engine -> {
2794- if (engine != null ) {
2795- engine .deactivateThrottling ();
2796- }
2797- return null ;
2798- });
2772+ try {
2773+ getEngine ().deactivateThrottling ();
2774+ } catch (AlreadyClosedException ex ) {
2775+ // ignore
2776+ }
27992777 }
28002778
28012779 private void suspendThrottling () {
@@ -2840,16 +2818,12 @@ private void handleRefreshException(Exception e) {
28402818 * Called when our shard is using too much heap and should move buffered indexed/deleted documents to disk.
28412819 */
28422820 public void writeIndexingBuffer () {
2843- withEngineOrNullIfBeingReset (engine -> {
2844- if (engine != null ) {
2845- try {
2846- engine .writeIndexingBuffer ();
2847- } catch (Exception e ) {
2848- handleRefreshException (e );
2849- }
2850- }
2851- return null ;
2852- });
2821+ try {
2822+ Engine engine = getEngine ();
2823+ engine .writeIndexingBuffer ();
2824+ } catch (Exception e ) {
2825+ handleRefreshException (e );
2826+ }
28532827 }
28542828
28552829 /**
@@ -3436,7 +3410,7 @@ Engine getEngine() {
34363410 * NOTE: returns null if engine is not yet started (e.g. recovery phase 1, copying over index files, is still running), or if engine is
34373411 * closed.
34383412 *
3439- * @deprecated use the withEngine* family of functions instead, which consider the situation an engine may be reset / changed.
3413+ * @deprecated use the try/ withEngine* family of functions instead, which consider the situation an engine may be reset / changed.
34403414 */
34413415 @ Deprecated
34423416 public Engine getEngineOrNull () {
@@ -3471,7 +3445,7 @@ private Engine getAndSetCurrentEngine(Engine newEngine) {
34713445 * @return the result of the operation
34723446 * @param <R> the type of the result
34733447 */
3474- public <R > R withEngineOrNullIfBeingReset (Function <Engine , R > operation ) {
3448+ public <R > R tryWithEngineOrNull (Function <Engine , R > operation ) {
34753449 return withEngine (operation , true , true );
34763450 }
34773451
0 commit comments