mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 08:09:43 -05:00
fix(lambda): split a failed Kinesis batch with BisectBatchOnFunctionError (#6523)
* fix(lambda): split a failed Kinesis batch with BisectBatchOnFunctionError * refactor(lambda): share the stream batch split rule between the pollers
This commit is contained in:
1 parent
a4196c19e3
commit
d1b9baeb4a
6 files changed
+142
-13
No files matched your search
+5
-11
@@ -279,7 +279,7 @@ public class DynamoDbStreamsEventSourcePoller implements Resettable {
|
||||
DynamoDbStreamReader.RecordsPage page;
|
||||
try {
|
||||
page = streamReader.readAfter(stream, shardId, lastSeq,
|
||||
Math.min(bisectLimit(esm, persistedKey), esm.getBatchSize()));
|
||||
Math.min(StreamBatchFailures.bisectLimit(esm, bisectLimits, persistedKey), esm.getBatchSize()));
|
||||
} catch (AwsException e) {
|
||||
if (!TRIMMED_DATA_ACCESS_EXCEPTION.equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
@@ -296,7 +296,8 @@ public class DynamoDbStreamsEventSourcePoller implements Resettable {
|
||||
+ "trim horizon; records between were dropped from the stream",
|
||||
esm.getUuid(), lastSeq);
|
||||
page = streamReader.readAfter(stream, shardId, null, Math.min(esm.getBatchSize(),
|
||||
bisectLimit(esm, StreamBatchFailures.batchStateKey(esm.getUuid(), shardId, null))));
|
||||
StreamBatchFailures.bisectLimit(esm, bisectLimits,
|
||||
StreamBatchFailures.batchStateKey(esm.getUuid(), shardId, null))));
|
||||
}
|
||||
List<DynamoDbStreamReader.Record> records = page.records();
|
||||
|
||||
@@ -420,13 +421,12 @@ public class DynamoDbStreamsEventSourcePoller implements Resettable {
|
||||
clearBatchState(batchKey);
|
||||
dispose(esm, shardId, persistedKey, new PendingFailure(matched, invokeResult,
|
||||
currentRetries, StreamBatchFailures.MAXIMUM_RECORD_AGE_EXCEEDED, newestFetchedSeq));
|
||||
} else if (invokeResult.getFunctionError() != null
|
||||
&& Boolean.TRUE.equals(esm.getBisectBatchOnFunctionError()) && matched.size() > 1) {
|
||||
} else if (StreamBatchFailures.splitsOnFunctionError(esm, invokeResult, matched.size())) {
|
||||
// ponytail: the halved limit is keyed by checkpoint, so once a good half advances the
|
||||
// rest is refetched at full batch size and split again. One window converges on the
|
||||
// poison record in O(log n) invocations, a busy stream in O(log^2 n) worst case;
|
||||
// carrying the right half's end sequence forward would match AWS exactly.
|
||||
int limit = (records.size() + 1) / 2;
|
||||
int limit = StreamBatchFailures.splitBatchSize(records.size());
|
||||
bisectLimits.put(batchKey, limit);
|
||||
LOG.infov("DynamoDB Streams ESM {0}: function error on {1} record(s), bisecting to {2}",
|
||||
esm.getUuid(), records.size(), limit);
|
||||
@@ -471,12 +471,6 @@ public class DynamoDbStreamsEventSourcePoller implements Resettable {
|
||||
bisectLimits.remove(key);
|
||||
}
|
||||
|
||||
private int bisectLimit(EventSourceMapping esm, String key) {
|
||||
return Boolean.TRUE.equals(esm.getBisectBatchOnFunctionError())
|
||||
? bisectLimits.getOrDefault(key, Integer.MAX_VALUE)
|
||||
: Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
private static String finishedShardKey(String uuid, String shardId) {
|
||||
return uuid + ":" + shardId;
|
||||
}
|
||||
|
||||
+27
-1
@@ -60,6 +60,8 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
/** Keyed by {@link StreamBatchFailures#batchStateKey}. */
|
||||
private final ConcurrentHashMap<String, Integer> retryCounts = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Long> retryNotBefore = new ConcurrentHashMap<>();
|
||||
/** The batch size a split batch is refetched at, keyed like {@link #retryCounts}. */
|
||||
private final ConcurrentHashMap<String, Integer> bisectLimits = 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. */
|
||||
@@ -136,6 +138,7 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
activePolls.clear();
|
||||
retryCounts.clear();
|
||||
retryNotBefore.clear();
|
||||
bisectLimits.clear();
|
||||
stopped.clear();
|
||||
}
|
||||
|
||||
@@ -161,6 +164,7 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
}
|
||||
retryCounts.keySet().removeIf(k -> k.startsWith(uuid + ":"));
|
||||
retryNotBefore.keySet().removeIf(k -> k.startsWith(uuid + ":"));
|
||||
bisectLimits.keySet().removeIf(k -> k.startsWith(uuid + ":"));
|
||||
}
|
||||
|
||||
/** Drops the stop tombstone once the store has forgotten the mapping, which stillActive then sees. */
|
||||
@@ -208,7 +212,8 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
}
|
||||
|
||||
Map<String, Object> result = kinesisService.getRecordsForAccount(
|
||||
esm.getAccountId(), iterator, esm.getBatchSize(), esm.getRegion());
|
||||
esm.getAccountId(), iterator, Math.min(esm.getBatchSize(), StreamBatchFailures.bisectLimit(esm, bisectLimits, batchKey)),
|
||||
esm.getRegion());
|
||||
List<KinesisRecord> records = (List<KinesisRecord>) result.get("Records");
|
||||
|
||||
if (records.isEmpty()) {
|
||||
@@ -290,6 +295,11 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
// function is not retried at once.
|
||||
long now = clock.getAsLong();
|
||||
List<KinesisRecord> failed = failedRecords(records, matched, outcome.lowestFailedIndex());
|
||||
if (StreamBatchFailures.splitsOnFunctionError(esm, invokeResult, failed.size())
|
||||
&& !StreamBatchFailures.maximumRecordAgeExceeded(esm, failed.get(0).getApproximateArrivalTimestamp(), now)) {
|
||||
bisect(esm, shardId, retryKey, records.size());
|
||||
return;
|
||||
}
|
||||
int invokeCount = retryCounts.getOrDefault(retryKey, 0) + 1;
|
||||
Optional<String> discardCondition = discardCondition(esm, failed, invokeCount, now);
|
||||
if (discardCondition.isPresent()) {
|
||||
@@ -334,6 +344,21 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
records, shardId, backoffMs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Refetches the failed batch at half its size, unless stopPolling, a reset or shutdown came first. The
|
||||
* limit is keyed by checkpoint, so once the first half succeeds the rest is fetched at the full batch
|
||||
* size and split again if it fails.
|
||||
*/
|
||||
private synchronized void bisect(EventSourceMapping esm, String shardId, String retryKey, int fetched) {
|
||||
if (!stillActive(esm)) {
|
||||
return;
|
||||
}
|
||||
int limit = StreamBatchFailures.splitBatchSize(fetched);
|
||||
bisectLimits.put(retryKey, limit);
|
||||
LOG.infov("Kinesis ESM {0}: function error on {1} record(s) of shard {2}, bisecting to {3}",
|
||||
esm.getUuid(), fetched, shardId, limit);
|
||||
}
|
||||
|
||||
private record CheckpointOutcome(String checkpoint, int lowestFailedIndex) {}
|
||||
|
||||
long retryBackoffMs(int retries) {
|
||||
@@ -384,6 +409,7 @@ public class KinesisEventSourcePoller implements Resettable {
|
||||
private void clearBatchState(String key) {
|
||||
retryCounts.remove(key);
|
||||
retryNotBefore.remove(key);
|
||||
bisectLimits.remove(key);
|
||||
}
|
||||
|
||||
private String buildKinesisEvent(List<KinesisRecord> records, EventSourceMapping esm, String shardId) {
|
||||
|
||||
@@ -89,6 +89,27 @@ public class StreamBatchFailures {
|
||||
return maximum != null && maximum > 0 && nowMillis - oldestArrival.toEpochMilli() >= maximum * 1_000L;
|
||||
}
|
||||
|
||||
/**
|
||||
* With {@code BisectBatchOnFunctionError}, a batch that failed with a function error is split while
|
||||
* it holds more than one record. Splitting does not consume the retry quota.
|
||||
*/
|
||||
static boolean splitsOnFunctionError(EventSourceMapping esm, InvokeResult invokeResult, int records) {
|
||||
return invokeResult.getFunctionError() != null
|
||||
&& Boolean.TRUE.equals(esm.getBisectBatchOnFunctionError()) && records > 1;
|
||||
}
|
||||
|
||||
/** A split batch is refetched at its first half, rounded up. */
|
||||
static int splitBatchSize(int fetched) {
|
||||
return (fetched + 1) / 2;
|
||||
}
|
||||
|
||||
/** The most records to fetch after the checkpoint {@code key}: its split size, while bisecting. */
|
||||
static int bisectLimit(EventSourceMapping esm, Map<String, Integer> splitSizes, String key) {
|
||||
return Boolean.TRUE.equals(esm.getBisectBatchOnFunctionError())
|
||||
? splitSizes.getOrDefault(key, Integer.MAX_VALUE)
|
||||
: Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
/** The retry state of a failing batch is keyed by the checkpoint it is read after. */
|
||||
static String batchStateKey(String esmUuid, String shardId, String checkpoint) {
|
||||
return esmUuid + ":" + shardId + ":" + (checkpoint == null ? "TRIM_HORIZON" : checkpoint);
|
||||
|
||||
Reference in new issue
Block a user