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.
Add expiry monitor to adjust SQS viz timeout if processing has not ye…
…t completed Signed-off-by: Chase Engelbrecht <[email protected]>
- Loading branch information
Showing
8 changed files
with
180 additions
and
3 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
64 changes: 64 additions & 0 deletions
64
...epper-api/src/main/java/org/opensearch/dataprepper/model/acknowledgements/ExpiryItem.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,64 @@ | ||
package org.opensearch.dataprepper.model.acknowledgements; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.function.Consumer; | ||
|
||
public class ExpiryItem { | ||
private static final Logger LOG = LoggerFactory.getLogger(ExpiryItem.class); | ||
|
||
private final String itemId; | ||
private final Instant startTime; | ||
private final long pollSeconds; | ||
private final Consumer expiryCallback; | ||
private Instant expirationTime; | ||
private AcknowledgementSet acknowledgementSet; | ||
|
||
public ExpiryItem(final String itemId, final Instant startTime, final long pollSeconds, final Instant expirationTime, | ||
final Consumer<ExpiryItem> expiryCallback, final AcknowledgementSet acknowledgementSet) { | ||
this.itemId = itemId; | ||
this.startTime = startTime; | ||
if (pollSeconds <= 2) { | ||
throw new UnsupportedOperationException("The poll interval must be at least 3 seconds to enable expiry monitoring"); | ||
} | ||
this.pollSeconds = pollSeconds; | ||
this.expirationTime = expirationTime; | ||
this.expiryCallback = expiryCallback; | ||
this.acknowledgementSet = acknowledgementSet; | ||
} | ||
|
||
public long getPollSeconds() { | ||
return pollSeconds; | ||
} | ||
|
||
public String getItemId() { | ||
return itemId; | ||
} | ||
|
||
public boolean executeExpiryCallback() { | ||
try { | ||
expiryCallback.accept(this); | ||
return true; | ||
} catch (final Exception e) { | ||
LOG.error("Exception occurred when executing the expiry callback", e.getMessage()); | ||
return false; | ||
} | ||
} | ||
|
||
public boolean isCompleteOrExpired() { | ||
return acknowledgementSet.isDone() || Instant.now().isAfter(expirationTime); | ||
} | ||
|
||
public void updateExpirationTime() { | ||
LOG.info("Updating expiration time for item ID {} to {}", itemId, expirationTime); | ||
expirationTime = expirationTime.plusSeconds(pollSeconds); | ||
acknowledgementSet.setExpiryTime(expirationTime); | ||
} | ||
|
||
public long getSecondsBetweenStartAndExpiration() { | ||
return Duration.between(startTime, expirationTime).getSeconds(); | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
...prepper-core/src/main/java/org/opensearch/dataprepper/acknowledgements/ExpiryMonitor.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,48 @@ | ||
package org.opensearch.dataprepper.acknowledgements; | ||
|
||
import org.opensearch.dataprepper.model.acknowledgements.ExpiryItem; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ExpiryMonitor { | ||
private static final Logger LOG = LoggerFactory.getLogger(ExpiryMonitor.class); | ||
|
||
private static final long CANCEL_CHECK_INTERVAL_SECONDS = 15; | ||
private static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE = Executors.newSingleThreadScheduledExecutor(); | ||
private static final ConcurrentHashMap<ExpiryItem, ScheduledFuture> EXPIRY_MONITORS = new ConcurrentHashMap<>(); | ||
|
||
public ExpiryMonitor() { | ||
SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(() -> { | ||
EXPIRY_MONITORS.forEach((expiryItem, future) -> { | ||
final boolean isCompleteOrExpired = expiryItem.isCompleteOrExpired(); | ||
LOG.error("ExpiryItem with ID {} has completion/expiry status {}", expiryItem.getItemId(), isCompleteOrExpired); | ||
|
||
if (isCompleteOrExpired) { | ||
future.cancel(false); | ||
EXPIRY_MONITORS.remove(expiryItem); | ||
} | ||
}); | ||
}, 0, CANCEL_CHECK_INTERVAL_SECONDS, TimeUnit.SECONDS); | ||
} | ||
|
||
public void addExpiryItem(final ExpiryItem expiryItem) { | ||
final ScheduledFuture expiryMonitor = SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(monitorExpiration(expiryItem), | ||
0, expiryItem.getPollSeconds() - 2, TimeUnit.SECONDS); | ||
EXPIRY_MONITORS.put(expiryItem, expiryMonitor); | ||
} | ||
|
||
private Runnable monitorExpiration(final ExpiryItem expiryItem) { | ||
return () -> { | ||
final boolean callBackSuccess = expiryItem.executeExpiryCallback(); | ||
if (callBackSuccess) { | ||
expiryItem.updateExpirationTime(); | ||
} | ||
}; | ||
} | ||
} |
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