forked from opensearch-project/data-prepper
-
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.
Signed-off-by: Chase Engelbrecht <[email protected]>
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 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
83 changes: 83 additions & 0 deletions
83
...earch/src/main/java/org/opensearch/dataprepper/plugins/sink/opensearch/RequestSender.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,83 @@ | ||
package org.opensearch.dataprepper.plugins.sink.opensearch; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.CompletionService; | ||
import java.util.concurrent.ExecutorCompletionService; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public class RequestSender { | ||
private static final Logger LOG = LoggerFactory.getLogger(RequestSender.class); | ||
|
||
private final List<Future<Void>> pendingRequestFutures; | ||
private final ExecutorService requestExecutor; | ||
private final CompletionService<Void> completionService; | ||
private final int concurrentRequestCount; | ||
private final ReentrantLock reentrantLock; | ||
|
||
public RequestSender(final int concurrentRequestCount) { | ||
this.concurrentRequestCount = concurrentRequestCount; | ||
pendingRequestFutures = new ArrayList<>(); | ||
requestExecutor = Executors.newFixedThreadPool(concurrentRequestCount); | ||
completionService = new ExecutorCompletionService(requestExecutor); | ||
reentrantLock = new ReentrantLock(); | ||
} | ||
|
||
public void sendRequest(final Callable<Void> requestRunnable) { | ||
reentrantLock.lock(); | ||
|
||
if (pendingRequestFutures.size() >= concurrentRequestCount) { | ||
waitForRequestSlot(); | ||
} | ||
|
||
final Future<Void> future = completionService.submit(requestRunnable); | ||
pendingRequestFutures.add(future); | ||
|
||
reentrantLock.unlock(); | ||
} | ||
|
||
private void waitForRequestSlot() { | ||
do { | ||
checkFutureCompletion(); | ||
if (isRequestQueueFull()) { | ||
try { | ||
LOG.info("Request queue is full, waiting for slot to free up"); | ||
completionService.take(); | ||
} catch (InterruptedException e) { | ||
LOG.error("Interrupted while waiting for future completion"); | ||
} | ||
} | ||
} while (isRequestQueueFull()); | ||
} | ||
|
||
private boolean isRequestQueueFull() { | ||
return pendingRequestFutures.size() >= concurrentRequestCount; | ||
} | ||
|
||
private void checkFutureCompletion() { | ||
for (Iterator<Future<Void>> iterator = pendingRequestFutures.iterator(); iterator.hasNext();) { | ||
final Future<Void> future = iterator.next(); | ||
if (future.isCancelled()) { | ||
try { | ||
future.get(); | ||
} catch (final Exception e) { | ||
LOG.error("Indexing future was cancelled", e); | ||
iterator.remove(); | ||
return; | ||
} | ||
} | ||
|
||
if (future.isDone()) { | ||
iterator.remove(); | ||
} | ||
} | ||
} | ||
} |