mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(kinesis): signal closed shard exhaustion and return child shards (#6716)
Return a terminal iterator and direct child metadata from coherent shard snapshots. Preserve final-batch delivery and prevent replay in Pipes and Firehose without changing checkpoint commit boundaries.
This commit is contained in:
11 files changed
+615
-44
No files matched your search
@@ -987,12 +987,18 @@ public class FirehoseService implements ResourceProvider {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<KinesisRecord> records = (List<KinesisRecord>) page.get("Records");
|
||||
String next = (String) page.get("NextShardIterator");
|
||||
String advanced = next != null ? next : iterator;
|
||||
String advanced = next != null || records == null || records.isEmpty() ? next :
|
||||
kinesisService.getShardIteratorForAccount(sourceAccountId, sourceName, shard.getShardId(),
|
||||
"AFTER_SEQUENCE_NUMBER", records.getLast().getSequenceNumber(), region);
|
||||
if (advanced == null) {
|
||||
advanced = iterator;
|
||||
}
|
||||
String checkpoint = advanced;
|
||||
String shardId = shard.getShardId();
|
||||
if (records == null || records.isEmpty()) {
|
||||
// Nothing was buffered, so this advance owes S3 nothing and is safe to
|
||||
// park as pending on its own.
|
||||
advanceSourceIterator(streamKey, shardId, advanced);
|
||||
advanceSourceIterator(streamKey, shardId, checkpoint);
|
||||
continue;
|
||||
}
|
||||
// Through putRecordBatch so source records buffer, and trigger a flush, on
|
||||
@@ -1002,7 +1008,7 @@ public class FirehoseService implements ResourceProvider {
|
||||
putRecordBatch(streamKey, deliveryStreamName, records.stream()
|
||||
.map(record -> new Record(record.getData()))
|
||||
.toList(),
|
||||
() -> advanceSourceIterator(streamKey, shardId, advanced));
|
||||
() -> advanceSourceIterator(streamKey, shardId, checkpoint));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -417,7 +417,7 @@ public class KinesisJsonHandler {
|
||||
List<KinesisRecord> records = (List<KinesisRecord>) result.get("Records");
|
||||
long millisBehind = ((Number) result.get("MillisBehindLatest")).longValue();
|
||||
|
||||
String continuationSeqNo = records.isEmpty() ? null
|
||||
String continuationSeqNo = result.get("NextShardIterator") == null || records.isEmpty() ? null
|
||||
: records.get(records.size() - 1).getSequenceNumber();
|
||||
|
||||
ObjectNode eventPayload = objectMapper.createObjectNode();
|
||||
@@ -435,7 +435,7 @@ public class KinesisJsonHandler {
|
||||
eventPayload.put("ContinuationSequenceNumber", continuationSeqNo);
|
||||
}
|
||||
eventPayload.put("MillisBehindLatest", millisBehind);
|
||||
eventPayload.putArray("ChildShards");
|
||||
writeChildShards(eventPayload, result);
|
||||
|
||||
try {
|
||||
// The Go SDK (and other SDKs) expect an initial-response message before
|
||||
@@ -735,10 +735,29 @@ public class KinesisJsonHandler {
|
||||
}
|
||||
}
|
||||
response.put("NextShardIterator", (String) result.get("NextShardIterator"));
|
||||
writeChildShards(response, result);
|
||||
response.put("MillisBehindLatest", ((Number) result.get("MillisBehindLatest")).longValue());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void writeChildShards(ObjectNode response, Map<String, Object> result) {
|
||||
List<KinesisService.ChildShard> children = (List<KinesisService.ChildShard>) result.get("ChildShards");
|
||||
if (children == null) {
|
||||
return;
|
||||
}
|
||||
ArrayNode childNodes = response.putArray("ChildShards");
|
||||
for (KinesisService.ChildShard child : children) {
|
||||
ObjectNode node = childNodes.addObject();
|
||||
node.put("ShardId", child.shardId());
|
||||
ArrayNode parents = node.putArray("ParentShards");
|
||||
child.parentShards().forEach(parents::add);
|
||||
node.putObject("HashKeyRange")
|
||||
.put("StartingHashKey", child.hashKeyRange().startingHashKey())
|
||||
.put("EndingHashKey", child.hashKeyRange().endingHashKey());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleIncreaseStreamRetentionPeriod(JsonNode request, String region) {
|
||||
String streamName = resolveStreamName(request);
|
||||
int retentionPeriodHours = request.path("RetentionPeriodHours").asInt();
|
||||
|
||||
@@ -893,14 +893,8 @@ public class KinesisService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
KinesisStream stream = resolveStream(streamName, region);
|
||||
KinesisShard shard = stream.getShards().stream()
|
||||
.filter(s -> s.getShardId().equals(shardId))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Shard not found", 400));
|
||||
pruneExpiredRecords(stream, shard);
|
||||
|
||||
KinesisShard.RecordsSnapshot snapshot = shard.snapshotRecords();
|
||||
ShardReadSnapshot read = snapshotShard(null, streamName, shardId, region);
|
||||
KinesisShard.RecordsSnapshot snapshot = read.records();
|
||||
List<KinesisRecord> allRecords = snapshot.records();
|
||||
int startIndex = 0;
|
||||
|
||||
@@ -946,6 +940,45 @@ public class KinesisService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
return recordsPage(streamName, shardId, read, startIndex, limit);
|
||||
}
|
||||
|
||||
public record ChildShard(String shardId, List<String> parentShards, KinesisShard.HashKeyRange hashKeyRange) {}
|
||||
|
||||
private record ShardReadSnapshot(KinesisShard.RecordsSnapshot records, boolean closed,
|
||||
List<ChildShard> children) {}
|
||||
|
||||
private ShardReadSnapshot snapshotShard(String accountId, String streamName, String shardId, String region) {
|
||||
// Share the append/reshard monitor so a terminal snapshot includes the final append and topology.
|
||||
synchronized (lockFor(regionKey(region, streamName))) {
|
||||
KinesisStream stream = resolveStreamForAccount(accountId, streamName, region);
|
||||
KinesisShard shard = stream.getShards().stream()
|
||||
.filter(candidate -> candidate.getShardId().equals(shardId))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Shard not found", 400));
|
||||
pruneExpiredRecords(stream, shard);
|
||||
List<ChildShard> children = new ArrayList<>();
|
||||
if (shard.isClosed()) {
|
||||
for (KinesisShard child : stream.getShards()) {
|
||||
if (shardId.equals(child.getParentShardId()) || shardId.equals(child.getAdjacentParentShardId())) {
|
||||
List<String> parents = new ArrayList<>();
|
||||
if (child.getParentShardId() != null) {
|
||||
parents.add(child.getParentShardId());
|
||||
}
|
||||
if (child.getAdjacentParentShardId() != null) {
|
||||
parents.add(child.getAdjacentParentShardId());
|
||||
}
|
||||
children.add(new ChildShard(child.getShardId(), List.copyOf(parents), child.getHashKeyRange()));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ShardReadSnapshot(shard.snapshotRecords(), shard.isClosed(), List.copyOf(children));
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> recordsPage(String streamName, String shardId, ShardReadSnapshot read,
|
||||
int startIndex, Integer limit) {
|
||||
List<KinesisRecord> allRecords = read.records().records();
|
||||
int max = limit != null ? Math.min(limit, 1000) : 1000;
|
||||
List<KinesisRecord> result = new ArrayList<>();
|
||||
int nextIndex = startIndex;
|
||||
@@ -953,13 +986,15 @@ public class KinesisService implements ResourceProvider {
|
||||
result.add(allRecords.get(i));
|
||||
nextIndex = i + 1;
|
||||
}
|
||||
|
||||
String nextIterator = buildContinuationIterator(streamName, shardId, allRecords, nextIndex);
|
||||
|
||||
boolean terminal = read.closed() && nextIndex >= allRecords.size();
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("Records", result);
|
||||
response.put("NextShardIterator", nextIterator);
|
||||
response.put("NextShardIterator", terminal ? null
|
||||
: buildContinuationIterator(streamName, shardId, allRecords, nextIndex));
|
||||
response.put("MillisBehindLatest", computeMillisBehindLatest(allRecords, nextIndex));
|
||||
if (terminal) {
|
||||
response.put("ChildShards", read.children());
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -1149,14 +1184,8 @@ public class KinesisService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
KinesisStream stream = resolveStreamForAccount(accountId, streamName, region);
|
||||
KinesisShard shard = stream.getShards().stream()
|
||||
.filter(s -> s.getShardId().equals(shardId))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Shard not found", 400));
|
||||
pruneExpiredRecords(stream, shard);
|
||||
|
||||
KinesisShard.RecordsSnapshot snapshot = shard.snapshotRecords();
|
||||
ShardReadSnapshot read = snapshotShard(accountId, streamName, shardId, region);
|
||||
KinesisShard.RecordsSnapshot snapshot = read.records();
|
||||
List<KinesisRecord> allRecords = snapshot.records();
|
||||
int startIndex = 0;
|
||||
// LATEST resolves the same way as AFTER_SEQUENCE_NUMBER (see the longer explanation in
|
||||
@@ -1186,20 +1215,7 @@ public class KinesisService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
int max = limit != null ? Math.min(limit, 1000) : 1000;
|
||||
List<KinesisRecord> result = new ArrayList<>();
|
||||
int nextIndex = startIndex;
|
||||
for (int i = startIndex; i < allRecords.size() && result.size() < max; i++) {
|
||||
result.add(allRecords.get(i));
|
||||
nextIndex = i + 1;
|
||||
}
|
||||
|
||||
String nextIterator = buildContinuationIterator(streamName, shardId, allRecords, nextIndex);
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("Records", result);
|
||||
response.put("NextShardIterator", nextIterator);
|
||||
response.put("MillisBehindLatest", computeMillisBehindLatest(allRecords, nextIndex));
|
||||
return response;
|
||||
return recordsPage(streamName, shardId, read, startIndex, limit);
|
||||
}
|
||||
|
||||
private KinesisShard selectShard(KinesisStream stream, String partitionKey, String explicitHashKey) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbTtlIdentity;
|
||||
import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbStreamReader;
|
||||
import io.github.hectorvent.floci.services.kinesis.KinesisService;
|
||||
import io.github.hectorvent.floci.services.kinesis.model.KinesisRecord;
|
||||
import io.github.hectorvent.floci.services.pipes.model.DesiredState;
|
||||
import io.github.hectorvent.floci.services.pipes.model.Pipe;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
@@ -245,7 +246,7 @@ public class PipesPoller implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
private void pollKinesis(Pipe pipe, String region) {
|
||||
void pollKinesis(Pipe pipe, String region) {
|
||||
String pipeKey = pipeKey(pipe);
|
||||
String streamName = extractResourceName(pipe.getSource());
|
||||
String pipeAccountId = pipe.getAccountId();
|
||||
@@ -262,10 +263,17 @@ public class PipesPoller implements Resettable {
|
||||
? kinesisService.getRecordsForAccount(pipeAccountId, iterator, batchSize, region)
|
||||
: kinesisService.getRecords(iterator, batchSize, region);
|
||||
String nextIterator = (String) result.get("NextShardIterator");
|
||||
if (nextIterator != null) {
|
||||
kinesisIterators.put(pipeKey, nextIterator);
|
||||
}
|
||||
List<?> records = (List<?>) result.get("Records");
|
||||
if (nextIterator == null && records != null && !records.isEmpty()) {
|
||||
KinesisRecord last = (KinesisRecord) records.getLast();
|
||||
nextIterator = pipeAccountId != null
|
||||
? kinesisService.getShardIteratorForAccount(pipeAccountId, streamName,
|
||||
"shardId-000000000000", "AFTER_SEQUENCE_NUMBER", last.getSequenceNumber(), region)
|
||||
: kinesisService.getShardIterator(streamName, "shardId-000000000000",
|
||||
"AFTER_SEQUENCE_NUMBER", last.getSequenceNumber(), null, region);
|
||||
}
|
||||
// Remember even the initial exhausted cursor, before filtering or delivery.
|
||||
kinesisIterators.put(pipeKey, nextIterator != null ? nextIterator : iterator);
|
||||
if (records == null || records.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user