forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bukhtawar Khan <bukhtawa@amazon.com>
- Loading branch information
Showing
6 changed files
with
76 additions
and
59 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
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
/* | ||
* 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.unit.TimeValue; | ||
import org.opensearch.common.util.concurrent.TimeoutAwareRunnable; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* The executor that executes a batch of {@link TimeoutAwareRunnable} and triggers a timeout based on {@link TimeValue} timeout | ||
*/ | ||
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; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
logger.debug("Starting execution of runnable of size [{}]", timeoutAwareRunnables.size()); | ||
Collections.shuffle(timeoutAwareRunnables); | ||
long startTime = System.nanoTime(); | ||
for (TimeoutAwareRunnable workQueue : timeoutAwareRunnables) { | ||
if (System.nanoTime() - startTime > timeoutSupplier.get().nanos()) { | ||
workQueue.run(); | ||
} else { | ||
logger.debug("Executing timeout for runnable of size [{}]", timeoutAwareRunnables.size()); | ||
workQueue.onTimeout(); | ||
} | ||
} | ||
logger.debug("Time taken to execute timed runnables in this cycle:[{}ms]", | ||
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)); | ||
} | ||
|
||
} |
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