forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make reroute iteration time-bound for large shard allocations (opense…
…arch-project#14848) * Make reroute iteration time-bound for large shard allocations Signed-off-by: Bukhtawar Khan <[email protected]> Co-authored-by: Rishab Nahata <[email protected]>
- Loading branch information
1 parent
7661078
commit 681087a
Showing
15 changed files
with
645 additions
and
16 deletions.
There are no files selected for viewing
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
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
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
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
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
66 changes: 66 additions & 0 deletions
66
server/src/main/java/org/opensearch/common/util/BatchRunnableExecutor.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,66 @@ | ||
/* | ||
* 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.common.util; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.common.Randomness; | ||
import org.opensearch.common.unit.TimeValue; | ||
import org.opensearch.common.util.concurrent.TimeoutAwareRunnable; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A {@link Runnable} that iteratively executes a batch of {@link TimeoutAwareRunnable}s. If the elapsed time exceeds the timeout defined by {@link TimeValue} timeout, then all subsequent {@link TimeoutAwareRunnable}s will have their {@link TimeoutAwareRunnable#onTimeout} method invoked and will not be run. | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class BatchRunnableExecutor implements Runnable { | ||
|
||
private final Supplier<TimeValue> timeoutSupplier; | ||
|
||
private final List<TimeoutAwareRunnable> timeoutAwareRunnables; | ||
|
||
private static final Logger logger = LogManager.getLogger(BatchRunnableExecutor.class); | ||
|
||
public BatchRunnableExecutor(List<TimeoutAwareRunnable> timeoutAwareRunnables, Supplier<TimeValue> timeoutSupplier) { | ||
this.timeoutSupplier = timeoutSupplier; | ||
this.timeoutAwareRunnables = timeoutAwareRunnables; | ||
} | ||
|
||
// for tests | ||
public List<TimeoutAwareRunnable> getTimeoutAwareRunnables() { | ||
return this.timeoutAwareRunnables; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
logger.debug("Starting execution of runnable of size [{}]", timeoutAwareRunnables.size()); | ||
long startTime = System.nanoTime(); | ||
if (timeoutAwareRunnables.isEmpty()) { | ||
return; | ||
} | ||
Randomness.shuffle(timeoutAwareRunnables); | ||
for (TimeoutAwareRunnable runnable : timeoutAwareRunnables) { | ||
if (timeoutSupplier.get().nanos() < 0 || System.nanoTime() - startTime < timeoutSupplier.get().nanos()) { | ||
runnable.run(); | ||
} else { | ||
logger.debug("Executing timeout for runnable of size [{}]", timeoutAwareRunnables.size()); | ||
runnable.onTimeout(); | ||
} | ||
} | ||
logger.debug( | ||
"Time taken to execute timed runnables in this cycle:[{}ms]", | ||
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) | ||
); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
server/src/main/java/org/opensearch/common/util/concurrent/TimeoutAwareRunnable.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,19 @@ | ||
/* | ||
* 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.common.util.concurrent; | ||
|
||
/** | ||
* Runnable that is aware of a timeout | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public interface TimeoutAwareRunnable extends Runnable { | ||
|
||
void onTimeout(); | ||
} |
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
Oops, something went wrong.