mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(lambda): honor MaximumRetryAttempts and DestinationConfig.OnFailure for Kinesis ESM (#6133)
This commit is contained in:
1 parent
023cb8341b
commit
bb32ba06eb
8 files changed
+1112
-110
No files matched your search
@@ -398,8 +398,9 @@ public class EmulatorLifecycle {
|
||||
// SIGTERM grace window and trigger SIGKILL; if the flush ran last it would be skipped and
|
||||
// in-memory (hybrid) data would be lost on an otherwise-graceful shutdown. shutdownAll()
|
||||
// still runs at the end to stop the flush schedulers and capture any shutdown-time writes.
|
||||
// DynamoDB stream consumers stop first, so the flush holds their final checkpoints.
|
||||
// Stream consumers stop first, so the flush holds their final checkpoints.
|
||||
runCleanup("DynamoDB Streams poller", dynamodbStreamsPoller::shutdown);
|
||||
runCleanup("Kinesis poller", kinesisPoller::shutdown);
|
||||
runCleanup("Redshift zero-ETL consumer", redshiftZeroEtlConsumer::shutdown);
|
||||
runCleanup("storage flush", storageFactory::flushAll);
|
||||
runCleanup("EC2 metadata server", () -> {
|
||||
|
||||
+274
-80
@@ -32,15 +32,18 @@ import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
@ApplicationScoped
|
||||
public class KinesisEventSourcePoller implements Resettable {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(KinesisEventSourcePoller.class);
|
||||
private static final String BATCH_INFO_NAME = "KinesisBatchInfo";
|
||||
|
||||
private final Vertx vertx;
|
||||
private final KinesisService kinesisService;
|
||||
@@ -50,8 +53,17 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
private final long pollIntervalMs;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final PipesFilterMatcher filterMatcher;
|
||||
private final StreamBatchFailures batchFailures;
|
||||
private final LongSupplier clock;
|
||||
private final ConcurrentHashMap<String, Long> timerIds = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Boolean> activePolls = new ConcurrentHashMap<>();
|
||||
final ConcurrentHashMap<String, Boolean> activePolls = new ConcurrentHashMap<>();
|
||||
/** Keyed by {@link StreamBatchFailures#batchStateKey}. */
|
||||
private final ConcurrentHashMap<String, Integer> retryCounts = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Long> retryNotBefore = new ConcurrentHashMap<>();
|
||||
/** Set during a reset and after shutdown: a failed invoke then counts for nothing. */
|
||||
private volatile boolean quiesced;
|
||||
/** Mappings stopped since their last start; a delete stops the mapping before the store forgets it. */
|
||||
final Set<String> stopped = ConcurrentHashMap.newKeySet();
|
||||
private final ExecutorService pollExecutor = Executors.newCachedThreadPool(r -> {
|
||||
Thread t = new Thread(r, "kinesis-esm-poller");
|
||||
t.setDaemon(true);
|
||||
@@ -64,7 +76,20 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
LambdaTargetResolver targetResolver,
|
||||
EsmStore esmStore, EmulatorConfig config,
|
||||
ObjectMapper objectMapper,
|
||||
PipesFilterMatcher filterMatcher) {
|
||||
PipesFilterMatcher filterMatcher,
|
||||
StreamBatchFailures batchFailures) {
|
||||
this(vertx, kinesisService, executorService, targetResolver, esmStore, config, objectMapper,
|
||||
filterMatcher, batchFailures, System::currentTimeMillis);
|
||||
}
|
||||
|
||||
KinesisEventSourcePoller(Vertx vertx, KinesisService kinesisService,
|
||||
LambdaExecutorService executorService,
|
||||
LambdaTargetResolver targetResolver,
|
||||
EsmStore esmStore, EmulatorConfig config,
|
||||
ObjectMapper objectMapper,
|
||||
PipesFilterMatcher filterMatcher,
|
||||
StreamBatchFailures batchFailures,
|
||||
LongSupplier clock) {
|
||||
this.vertx = vertx;
|
||||
this.kinesisService = kinesisService;
|
||||
this.executorService = executorService;
|
||||
@@ -73,6 +98,8 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
this.pollIntervalMs = config.services().lambda().pollIntervalMs();
|
||||
this.objectMapper = objectMapper;
|
||||
this.filterMatcher = filterMatcher;
|
||||
this.batchFailures = batchFailures;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
public void startPersistedPollers() {
|
||||
@@ -84,20 +111,36 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
/** Runs before the Lambda containers are stopped, so an invoke cut short by them is not taken for a failure. */
|
||||
@PreDestroy
|
||||
void shutdown() {
|
||||
public synchronized void shutdown() {
|
||||
quiesced = true;
|
||||
pollExecutor.shutdownNow();
|
||||
timerIds.values().forEach(vertx::cancelTimer);
|
||||
timerIds.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void beforeReset() {
|
||||
quiesced = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void afterReset() {
|
||||
quiesced = pollExecutor.isShutdown();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
timerIds.values().forEach(vertx::cancelTimer);
|
||||
timerIds.clear();
|
||||
activePolls.clear();
|
||||
retryCounts.clear();
|
||||
retryNotBefore.clear();
|
||||
stopped.clear();
|
||||
}
|
||||
|
||||
public void startPolling(EventSourceMapping esm) {
|
||||
public synchronized void startPolling(EventSourceMapping esm) {
|
||||
stopped.remove(esm.getUuid());
|
||||
if (timerIds.containsKey(esm.getUuid())) return;
|
||||
String uuid = esm.getUuid();
|
||||
String accountId = esm.getAccountId();
|
||||
@@ -110,9 +153,19 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
LOG.infov("Started Kinesis polling for ESM {0} → {1}", uuid, esm.getEventSourceArn());
|
||||
}
|
||||
|
||||
public void stopPolling(String uuid) {
|
||||
public synchronized void stopPolling(String uuid) {
|
||||
stopped.add(uuid);
|
||||
Long timerId = timerIds.remove(uuid);
|
||||
if (timerId != null) vertx.cancelTimer(timerId);
|
||||
if (timerId != null) {
|
||||
vertx.cancelTimer(timerId);
|
||||
}
|
||||
retryCounts.keySet().removeIf(k -> k.startsWith(uuid + ":"));
|
||||
retryNotBefore.keySet().removeIf(k -> k.startsWith(uuid + ":"));
|
||||
}
|
||||
|
||||
/** Drops the stop tombstone once the store has forgotten the mapping, which stillActive then sees. */
|
||||
void mappingDeleted(String uuid) {
|
||||
stopped.remove(uuid);
|
||||
}
|
||||
|
||||
/** Package-private (not private) only so unit tests can drive a single poll directly. */
|
||||
@@ -128,71 +181,7 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
esm.getAccountId(), streamName, esm.getRegion());
|
||||
|
||||
for (KinesisShard shard : stream.getShards()) {
|
||||
String lastSeq = esm.getShardSequenceNumbers().get(shard.getShardId());
|
||||
String iterator;
|
||||
if (lastSeq == null) {
|
||||
iterator = kinesisService.getShardIteratorForAccount(esm.getAccountId(),
|
||||
streamName, shard.getShardId(), "TRIM_HORIZON", null, esm.getRegion());
|
||||
} else {
|
||||
iterator = kinesisService.getShardIteratorForAccount(esm.getAccountId(),
|
||||
streamName, shard.getShardId(), "AFTER_SEQUENCE_NUMBER", lastSeq, esm.getRegion());
|
||||
}
|
||||
|
||||
Map<String, Object> result = kinesisService.getRecordsForAccount(
|
||||
esm.getAccountId(), iterator, esm.getBatchSize(), esm.getRegion());
|
||||
List<KinesisRecord> records = (List<KinesisRecord>) result.get("Records");
|
||||
|
||||
if (records.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// The checkpoint must advance to the newest FETCHED record whenever the batch is
|
||||
// disposed of, whether by a successful invoke or because a filter matched nothing,
|
||||
// so filtered-out records are consumed, not re-read forever. Only an invoke that was
|
||||
// attempted and failed leaves the checkpoint unmoved (the whole window retries), and
|
||||
// reported batch item failures move it only up to the lowest failed record.
|
||||
String newestFetchedSeq = records.get(records.size() - 1).getSequenceNumber();
|
||||
|
||||
List<KinesisRecord> matched = records;
|
||||
JsonNode filterParams = EsmFilterCriteriaUtils.matcherSourceParameters(objectMapper, esm.getFilterCriteria());
|
||||
if (filterParams != null) {
|
||||
List<JsonNode> filterNodes = new ArrayList<>(records.size());
|
||||
for (KinesisRecord rec : records) {
|
||||
filterNodes.add(buildKinesisFilterNode(rec));
|
||||
}
|
||||
matched = EsmFilterCriteriaUtils.selectMatched(
|
||||
records, filterNodes, filterMatcher.applyFilterCriteria(filterNodes, filterParams));
|
||||
}
|
||||
|
||||
if (matched.isEmpty()) {
|
||||
// Whole batch filtered out: consume it (advance past), do not invoke, do not retry.
|
||||
advanceCheckpoint(esm, shard.getShardId(), newestFetchedSeq);
|
||||
continue;
|
||||
}
|
||||
|
||||
String eventJson = buildKinesisEvent(matched, esm, shard.getShardId());
|
||||
InvokeResult invokeResult;
|
||||
try {
|
||||
String qualifier = LambdaTargetResolver.mappingQualifier(esm);
|
||||
invokeResult = qualifier == null
|
||||
? executorService.invoke(fn, eventJson.getBytes(), InvocationType.RequestResponse)
|
||||
: executorService.invoke(fn, eventJson.getBytes(), InvocationType.RequestResponse, 0, qualifier);
|
||||
} catch (AwsException e) {
|
||||
if ("TooManyRequestsException".equals(e.getErrorCode())) {
|
||||
LOG.infov("Kinesis ESM {0}: function {1} throttled, shard iterator not advanced",
|
||||
esm.getUuid(), fn.getFunctionName());
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
if (invokeResult.getFunctionError() == null) {
|
||||
String checkpoint = successfulInvocationCheckpoint(
|
||||
esm, invokeResult.getPayload(), lastSeq, records, matched);
|
||||
if (checkpoint != null && !checkpoint.equals(lastSeq)) {
|
||||
advanceCheckpoint(esm, shard.getShardId(), checkpoint);
|
||||
}
|
||||
}
|
||||
pollShard(esm, fn, streamName, shard.getShardId());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Kinesis ESM {0} error: {1}", esm.getUuid(), e.getMessage());
|
||||
@@ -202,6 +191,201 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
});
|
||||
}
|
||||
|
||||
private void pollShard(EventSourceMapping esm, LambdaFunction fn, String streamName, String shardId) {
|
||||
String lastSeq = esm.getShardSequenceNumbers().get(shardId);
|
||||
String batchKey = StreamBatchFailures.batchStateKey(esm.getUuid(), shardId, lastSeq);
|
||||
if (clock.getAsLong() < retryNotBefore.getOrDefault(batchKey, 0L)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String iterator;
|
||||
if (lastSeq == null) {
|
||||
iterator = kinesisService.getShardIteratorForAccount(esm.getAccountId(),
|
||||
streamName, shardId, "TRIM_HORIZON", null, esm.getRegion());
|
||||
} else {
|
||||
iterator = kinesisService.getShardIteratorForAccount(esm.getAccountId(),
|
||||
streamName, shardId, "AFTER_SEQUENCE_NUMBER", lastSeq, esm.getRegion());
|
||||
}
|
||||
|
||||
Map<String, Object> result = kinesisService.getRecordsForAccount(
|
||||
esm.getAccountId(), iterator, esm.getBatchSize(), esm.getRegion());
|
||||
List<KinesisRecord> records = (List<KinesisRecord>) result.get("Records");
|
||||
|
||||
if (records.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// The checkpoint must advance to the newest FETCHED record whenever the batch is
|
||||
// disposed of: by a successful invoke, because a filter matched nothing, or because the
|
||||
// batch was discarded after its retries or its record age ran out. Filtered-out records are
|
||||
// consumed, not re-read forever. A failed invoke that may still be retried leaves the
|
||||
// checkpoint unmoved, and reported batch item failures move it only up to the lowest failed
|
||||
// record.
|
||||
String newestFetchedSeq = records.get(records.size() - 1).getSequenceNumber();
|
||||
|
||||
List<KinesisRecord> matched = records;
|
||||
JsonNode filterParams = EsmFilterCriteriaUtils.matcherSourceParameters(objectMapper, esm.getFilterCriteria());
|
||||
if (filterParams != null) {
|
||||
List<JsonNode> filterNodes = new ArrayList<>(records.size());
|
||||
for (KinesisRecord rec : records) {
|
||||
filterNodes.add(buildKinesisFilterNode(rec));
|
||||
}
|
||||
matched = EsmFilterCriteriaUtils.selectMatched(
|
||||
records, filterNodes, filterMatcher.applyFilterCriteria(filterNodes, filterParams));
|
||||
}
|
||||
|
||||
if (matched.isEmpty()) {
|
||||
// Whole batch filtered out: consume it (advance past), do not invoke, do not retry.
|
||||
clearBatchState(batchKey);
|
||||
advanceCheckpoint(esm, shardId, newestFetchedSeq);
|
||||
return;
|
||||
}
|
||||
|
||||
String eventJson = buildKinesisEvent(matched, esm, shardId);
|
||||
InvokeResult invokeResult;
|
||||
try {
|
||||
String qualifier = LambdaTargetResolver.mappingQualifier(esm);
|
||||
invokeResult = qualifier == null
|
||||
? executorService.invoke(fn, eventJson.getBytes(), InvocationType.RequestResponse)
|
||||
: executorService.invoke(fn, eventJson.getBytes(), InvocationType.RequestResponse, 0, qualifier);
|
||||
} catch (AwsException e) {
|
||||
if (!"TooManyRequestsException".equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
// A throttled batch is retried until it is older than MaximumRecordAgeInSeconds, as on AWS.
|
||||
if (stillActive(esm) && StreamBatchFailures.maximumRecordAgeExceeded(
|
||||
esm, matched.get(0).getApproximateArrivalTimestamp(), clock.getAsLong())) {
|
||||
discard(esm, shardId, batchKey, matched, new InvokeResult(429, null, null, null, null),
|
||||
retryCounts.getOrDefault(batchKey, 0), StreamBatchFailures.MAXIMUM_RECORD_AGE_EXCEEDED,
|
||||
newestFetchedSeq);
|
||||
return;
|
||||
}
|
||||
LOG.infov("Kinesis ESM {0}: function {1} throttled, shard iterator not advanced",
|
||||
esm.getUuid(), fn.getFunctionName());
|
||||
return;
|
||||
}
|
||||
|
||||
CheckpointOutcome outcome = invokeResult.getFunctionError() == null
|
||||
? successfulInvocationCheckpoint(esm, invokeResult.getPayload(), lastSeq, records, matched)
|
||||
: new CheckpointOutcome(lastSeq, 0);
|
||||
if (outcome.lowestFailedIndex() == records.size()) {
|
||||
clearBatchState(batchKey);
|
||||
advanceCheckpoint(esm, shardId, outcome.checkpoint());
|
||||
return;
|
||||
}
|
||||
// Records before the lowest reported failure are done: the checkpoint moves up to them, as it
|
||||
// always did, and the attempts of the retried batch are counted under the new checkpoint.
|
||||
String retryKey = StreamBatchFailures.batchStateKey(esm.getUuid(), shardId, outcome.checkpoint());
|
||||
if (!retryKey.equals(batchKey)) {
|
||||
clearBatchState(batchKey);
|
||||
advanceCheckpoint(esm, shardId, outcome.checkpoint());
|
||||
}
|
||||
// A failure is counted, and may discard the batch, only for a mapping that is still polled.
|
||||
if (!stillActive(esm)) {
|
||||
LOG.debugv("Kinesis ESM {0} was stopped during a failed invocation, dropping its result", esm.getUuid());
|
||||
return;
|
||||
}
|
||||
|
||||
// The backoff and the record age count from the end of the failed invoke, so a slow
|
||||
// function is not retried at once.
|
||||
long now = clock.getAsLong();
|
||||
List<KinesisRecord> failed = failedRecords(records, matched, outcome.lowestFailedIndex());
|
||||
int invokeCount = retryCounts.getOrDefault(retryKey, 0) + 1;
|
||||
Optional<String> discardCondition = discardCondition(esm, failed, invokeCount, now);
|
||||
if (discardCondition.isPresent()) {
|
||||
discard(esm, shardId, retryKey, failed, invokeResult, invokeCount, discardCondition.get(),
|
||||
newestFetchedSeq);
|
||||
} else {
|
||||
scheduleRetry(esm, shardId, retryKey, failed.size(), invokeResult, invokeCount, now);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the discarded records to the OnFailure destination, then checkpoints past the batch. The
|
||||
* send can be slow, so the mapping is checked again under the monitor that stopPolling, a reset
|
||||
* and shutdown take: a mapping stopped meanwhile keeps no state and is not written back.
|
||||
*/
|
||||
private void discard(EventSourceMapping esm, String shardId, String stateKey, List<KinesisRecord> discarded,
|
||||
InvokeResult invokeResult, int invokeCount, String condition, String newestFetchedSeq) {
|
||||
LOG.warnv("Kinesis ESM {0}: {1} after {2} invocation(s), discarding {3} record(s) of shard {4}",
|
||||
esm.getUuid(), condition, invokeCount, discarded.size(), shardId);
|
||||
batchFailures.sendToOnFailureDestination(esm, discardedBatch(esm, shardId, discarded), invokeResult,
|
||||
invokeCount, condition);
|
||||
synchronized (this) {
|
||||
if (stillActive(esm)) {
|
||||
clearBatchState(stateKey);
|
||||
advanceCheckpoint(esm, shardId, newestFetchedSeq);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Records the attempt and its backoff, unless stopPolling, a reset or shutdown came first. */
|
||||
private synchronized void scheduleRetry(EventSourceMapping esm, String shardId, String retryKey, int records,
|
||||
InvokeResult invokeResult, int invokeCount, long now) {
|
||||
if (!stillActive(esm)) {
|
||||
return;
|
||||
}
|
||||
long backoffMs = retryBackoffMs(invokeCount);
|
||||
retryCounts.put(retryKey, invokeCount);
|
||||
retryNotBefore.put(retryKey, now + backoffMs);
|
||||
LOG.warnv("Kinesis ESM {0}: invocation {1} failed [{2}], retrying {3} record(s) of shard {4} in {5,number,#} ms",
|
||||
esm.getUuid(), invokeCount,
|
||||
invokeResult.getFunctionError() != null ? invokeResult.getFunctionError() : "batchItemFailures",
|
||||
records, shardId, backoffMs);
|
||||
}
|
||||
|
||||
private record CheckpointOutcome(String checkpoint, int lowestFailedIndex) {}
|
||||
|
||||
long retryBackoffMs(int retries) {
|
||||
return StreamBatchFailures.retryBackoffMs(pollIntervalMs, retries);
|
||||
}
|
||||
|
||||
private static Optional<String> discardCondition(EventSourceMapping esm, List<KinesisRecord> failed,
|
||||
int invokeCount, long now) {
|
||||
if (StreamBatchFailures.maximumRecordAgeExceeded(esm, failed.get(0).getApproximateArrivalTimestamp(), now)) {
|
||||
return Optional.of(StreamBatchFailures.MAXIMUM_RECORD_AGE_EXCEEDED);
|
||||
}
|
||||
if (StreamBatchFailures.retryAttemptsExhausted(esm, invokeCount)) {
|
||||
return Optional.of(StreamBatchFailures.RETRY_ATTEMPTS_EXHAUSTED);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/** The delivered records from the lowest failed one on. A filtered-out record was never sent, so it did not fail. */
|
||||
private static List<KinesisRecord> failedRecords(List<KinesisRecord> fetched, List<KinesisRecord> delivered,
|
||||
int lowestFailedIndex) {
|
||||
Set<String> retried = new HashSet<>();
|
||||
for (KinesisRecord rec : fetched.subList(lowestFailedIndex, fetched.size())) {
|
||||
retried.add(rec.getSequenceNumber());
|
||||
}
|
||||
return delivered.stream().filter(rec -> retried.contains(rec.getSequenceNumber())).toList();
|
||||
}
|
||||
|
||||
private StreamBatchFailures.DiscardedBatch discardedBatch(EventSourceMapping esm, String shardId,
|
||||
List<KinesisRecord> records) {
|
||||
KinesisRecord first = records.get(0);
|
||||
KinesisRecord last = records.get(records.size() - 1);
|
||||
return new StreamBatchFailures.DiscardedBatch(BATCH_INFO_NAME, shardId,
|
||||
first.getSequenceNumber(), last.getSequenceNumber(),
|
||||
AsyncInvokeDestinationRouter.formatRecordTimestamp(first.getApproximateArrivalTimestamp()),
|
||||
AsyncInvokeDestinationRouter.formatRecordTimestamp(last.getApproximateArrivalTimestamp()),
|
||||
records.size(), streamArnFromArn(esm.getEventSourceArn()),
|
||||
() -> buildKinesisEvent(records, esm, shardId));
|
||||
}
|
||||
|
||||
/** False once the mapping is stopped, deleted or disabled, or the poller is reset or shut down. */
|
||||
private boolean stillActive(EventSourceMapping esm) {
|
||||
return !quiesced && !stopped.contains(esm.getUuid())
|
||||
&& esmStore.getForAccount(esm.getAccountId(), esm.getUuid())
|
||||
.map(EventSourceMapping::isEnabled)
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
private void clearBatchState(String key) {
|
||||
retryCounts.remove(key);
|
||||
retryNotBefore.remove(key);
|
||||
}
|
||||
|
||||
private String buildKinesisEvent(List<KinesisRecord> records, EventSourceMapping esm, String shardId) {
|
||||
try {
|
||||
ArrayNode recordsArray = objectMapper.createArrayNode();
|
||||
@@ -258,22 +442,25 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
}
|
||||
|
||||
/**
|
||||
* The sequence number to checkpoint after a successful invocation. With
|
||||
* The sequence number to checkpoint after a successful invocation, and the fetched index of the
|
||||
* lowest failed record ({@code fetched.size()} when none failed). With
|
||||
* {@code ReportBatchItemFailures} the shard resumes at the lowest reported failure; Floci
|
||||
* resumes {@code AFTER_SEQUENCE_NUMBER}, so that is the fetched record just before it. A
|
||||
* malformed {@code batchItemFailures} response retries the whole batch, as on AWS.
|
||||
*/
|
||||
private String successfulInvocationCheckpoint(EventSourceMapping esm, byte[] payload, String previousCheckpoint,
|
||||
List<KinesisRecord> fetched, List<KinesisRecord> delivered) {
|
||||
private CheckpointOutcome successfulInvocationCheckpoint(EventSourceMapping esm, byte[] payload,
|
||||
String previousCheckpoint,
|
||||
List<KinesisRecord> fetched,
|
||||
List<KinesisRecord> delivered) {
|
||||
String newestFetchedSeq = fetched.get(fetched.size() - 1).getSequenceNumber();
|
||||
if (!esm.isReportBatchItemFailures() || payload == null || payload.length == 0) {
|
||||
return newestFetchedSeq;
|
||||
return new CheckpointOutcome(newestFetchedSeq, fetched.size());
|
||||
}
|
||||
|
||||
try {
|
||||
JsonNode failures = objectMapper.readTree(payload).get("batchItemFailures");
|
||||
if (failures == null || failures.isNull()) {
|
||||
return newestFetchedSeq;
|
||||
return new CheckpointOutcome(newestFetchedSeq, fetched.size());
|
||||
}
|
||||
if (!failures.isArray()) {
|
||||
return retryWholeBatch(esm, previousCheckpoint, "batchItemFailures is not an array");
|
||||
@@ -305,22 +492,23 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
}
|
||||
|
||||
if (lowestFailedIndex == fetched.size()) {
|
||||
return newestFetchedSeq;
|
||||
return new CheckpointOutcome(newestFetchedSeq, fetched.size());
|
||||
}
|
||||
LOG.warnv("Kinesis ESM {0}: function reported batch item failures, resuming at {1}",
|
||||
esm.getUuid(), fetched.get(lowestFailedIndex).getSequenceNumber());
|
||||
return lowestFailedIndex == 0
|
||||
String checkpoint = lowestFailedIndex == 0
|
||||
? previousCheckpoint
|
||||
: fetched.get(lowestFailedIndex - 1).getSequenceNumber();
|
||||
return new CheckpointOutcome(checkpoint, lowestFailedIndex);
|
||||
} catch (Exception e) {
|
||||
return retryWholeBatch(esm, previousCheckpoint, "response is not valid JSON: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private String retryWholeBatch(EventSourceMapping esm, String previousCheckpoint, String reason) {
|
||||
private CheckpointOutcome retryWholeBatch(EventSourceMapping esm, String previousCheckpoint, String reason) {
|
||||
LOG.warnv("Kinesis ESM {0}: malformed batchItemFailures response ({1}), retrying the whole batch",
|
||||
esm.getUuid(), reason);
|
||||
return previousCheckpoint;
|
||||
return new CheckpointOutcome(previousCheckpoint, 0);
|
||||
}
|
||||
|
||||
private void advanceCheckpoint(EventSourceMapping esm, String shardId, String newestSeq) {
|
||||
@@ -328,6 +516,12 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
esmStore.saveForAccount(esm.getAccountId(), esm);
|
||||
}
|
||||
|
||||
/** The stream ARN of a stream or stream-consumer ARN: an enhanced fan-out mapping reads its stream. */
|
||||
static String streamArnFromArn(String arn) {
|
||||
int consumer = arn.indexOf("/consumer/");
|
||||
return consumer >= 0 ? arn.substring(0, consumer) : arn;
|
||||
}
|
||||
|
||||
/**
|
||||
* The stream name from a stream ARN ({@code .../stream/S}) or a stream-consumer ARN
|
||||
* ({@code .../stream/S/consumer/C:123}): the segment right after {@code stream/}. Only a
|
||||
|
||||
@@ -2231,6 +2231,8 @@ public class LambdaService implements ResourceProvider {
|
||||
esmStore.delete(uuid);
|
||||
if (esm.getEventSourceArn() != null && esm.getEventSourceArn().contains(":dynamodb:")) {
|
||||
dynamodbStreamsPoller.mappingDeleted(uuid);
|
||||
} else if (esm.getEventSourceArn() != null && esm.getEventSourceArn().contains(":kinesis:")) {
|
||||
kinesisPoller.mappingDeleted(uuid);
|
||||
}
|
||||
LOG.infov("Deleted ESM {0}", uuid);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user