forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Bukhtawar/ashish-8345
Await for in-flight segments to complete transfer before hand-off
- Loading branch information
Showing
2 changed files
with
108 additions
and
8 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
server/src/main/java/org/opensearch/index/shard/GatedDelegateRefreshListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.shard; | ||
|
||
import org.apache.lucene.search.ReferenceManager; | ||
import org.opensearch.common.Nullable; | ||
import org.opensearch.common.metrics.MeanMetric; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.concurrent.Semaphore; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* Delegate listener that delegates the call iff the required permits are obtained. Once the listener is closed, no | ||
* future calls to delgate should be allowed* | ||
*/ | ||
public class GatedDelegateRefreshListener implements ReferenceManager.RefreshListener, Closeable { | ||
|
||
static final int TOTAL_PERMITS = Integer.MAX_VALUE; | ||
final Semaphore semaphore = new Semaphore(TOTAL_PERMITS, true); | ||
final AtomicBoolean closed = new AtomicBoolean(); | ||
private ReferenceManager.RefreshListener delegateListener; | ||
@Nullable | ||
private MeanMetric refreshListenerMetrics; | ||
|
||
/** | ||
* The ctor for gated delegate listener* | ||
* @param delegateListener the delegate listener | ||
* @param refreshListenerMetrics an optional refresh listener metrics | ||
*/ | ||
GatedDelegateRefreshListener(ReferenceManager.RefreshListener delegateListener, @Nullable MeanMetric refreshListenerMetrics) { | ||
this.delegateListener = delegateListener; | ||
//TODO instrument metrics for listeners | ||
this.refreshListenerMetrics = refreshListenerMetrics; | ||
} | ||
|
||
@Override | ||
public void beforeRefresh() throws IOException { | ||
assert Thread.holdsLock(this); | ||
try { | ||
if (closed.get() == false) { | ||
if (semaphore.tryAcquire(1, 0, TimeUnit.SECONDS)) { | ||
try { | ||
delegateListener.beforeRefresh(); | ||
} catch (IOException e) { | ||
semaphore.release(1); | ||
} | ||
} | ||
} else { | ||
// this should never happen, if it does something is deeply wrong | ||
throw new IllegalStateException("failed to obtain permit but operations are not delayed"); | ||
} | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void afterRefresh(boolean didRefresh) throws IOException { | ||
try { | ||
delegateListener.afterRefresh(didRefresh); | ||
} finally { | ||
semaphore.release(1); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
try { | ||
if (semaphore.tryAcquire(TOTAL_PERMITS, 30, TimeUnit.SECONDS)) { | ||
boolean result = closed.compareAndSet(false, true); | ||
assert result; | ||
assert semaphore.availablePermits() == 0; | ||
} else { | ||
throw new IllegalStateException("timeout while blocking operations"); | ||
} | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters