fix(kinesis): resolve LATEST shard iterator position at GetShardIterator time (#1901)

* fix(kinesis): resolve LATEST shard iterator position at GetShardIterator time

A LATEST iterator recomputed its start position as the shard tip on
every GetRecords call, so records written between obtaining the iterator
and the first poll were skipped forever. Snapshot the tip once when the
iterator is created (matching DynamoDbStreamService) and encode it in
the iterator's index slot. GetShardIterator now also fails fast with
ResourceNotFoundException for an unknown shard, as on AWS.

Fixes #1900

* test(kinesis): add regression tests for tailing with a LATEST iterator

Reproduce issue #1900: a record written after the iterator is obtained
must be returned, both on the first GetRecords call and when polling
via NextShardIterator.

* test(kinesis): cover unknown shard validation for LATEST iterators

GetShardIterator must return ResourceNotFoundException when the shard
does not exist instead of deferring the failure to GetRecords.

* docs(kinesis): document shard iterator semantics

Describe the supported iterator types and the LATEST tip-snapshot
behavior; remove a stray empty Examples heading.

* fix(kinesis): validate shard existence for all iterator types in GetShardIterator

AWS rejects an unknown shard at GetShardIterator time for every
iterator type, not just LATEST. Apply the shard lookup unconditionally
instead of deferring the failure to GetRecords.

Addresses review feedback on #1901.

* test(kinesis): cover unknown shard validation across all iterator types
This commit is contained in:
Feyikemi Ogunsanya authored and GitHub committed 2026-08-09 00:10:16 -05:00
1 parent c4f4181c3d
commit 3b760bd8e5
3 files changed
+73 -13

No files matched your search

@@ -378,14 +378,16 @@ public class KinesisService {
public String getShardIterator(String streamName, String shardId, String type, String sequenceNumber,
Long timestampMillis, String region) {
resolveStream(streamName, region); // validate exists
KinesisStream stream = resolveStream(streamName, region);
// Format: streamName|shardId|type|sequenceNumber|index|timestampMillis
// The 6th slot was added for AT_TIMESTAMP; empty for other iterator types.
// Old 5-part iterators still decode via split(-1) compatibility in getRecords.
// For LATEST the index slot carries the shard tip at iterator creation time,
// so records written afterwards are visible to getRecords.
String raw = String.format("%s|%s|%s|%s|%d|%s",
streamName, shardId, type,
sequenceNumber != null ? sequenceNumber : "",
0,
iteratorStartIndex(stream, shardId, type),
timestampMillis != null ? timestampMillis.toString() : "");
return Base64.getEncoder().encodeToString(raw.getBytes(StandardCharsets.UTF_8));
}
@@ -407,6 +409,16 @@ public class KinesisService {
return parts;
}
private int iteratorStartIndex(KinesisStream stream, String shardId, String type) {
// AWS validates the shard at GetShardIterator time for every iterator type.
KinesisShard shard = stream.getShards().stream()
.filter(s -> s.getShardId().equals(shardId))
.findFirst()
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Shard not found", 400));
// LATEST resumes from the tip snapshot taken now; other types resolve in getRecords.
return "LATEST".equals(type) ? shard.getRecords().size() : 0;
}
private int parseIteratorIndex(String value) {
try {
return Integer.parseInt(value);
@@ -441,11 +453,11 @@ public class KinesisService {
List<KinesisRecord> allRecords = shard.getRecords();
int startIndex = 0;
// Simple implementation of iterator types
if ("TRIM_HORIZON".equals(type)) {
// Simple implementation of iterator types.
// LATEST resumes from the shard tip snapshot encoded at GetShardIterator time,
// so records appended after the iterator was obtained are returned.
if ("TRIM_HORIZON".equals(type) || "LATEST".equals(type)) {
startIndex = lastIndex;
} else if ("LATEST".equals(type)) {
startIndex = allRecords.size();
} else if ("AT_SEQUENCE_NUMBER".equals(type)) {
for (int i = 0; i < allRecords.size(); i++) {
if (allRecords.get(i).getSequenceNumber().equals(startSeq)) {
@@ -531,10 +543,11 @@ public class KinesisService {
public String getShardIteratorForAccount(String accountId, String streamName, String shardId,
String type, String sequenceNumber, String region) {
resolveStreamForAccount(accountId, streamName, region);
KinesisStream stream = resolveStreamForAccount(accountId, streamName, region);
String raw = String.format("%s|%s|%s|%s|%d|",
streamName, shardId, type,
sequenceNumber != null ? sequenceNumber : "", 0);
sequenceNumber != null ? sequenceNumber : "",
iteratorStartIndex(stream, shardId, type));
return Base64.getEncoder().encodeToString(raw.getBytes(StandardCharsets.UTF_8));
}
@@ -555,10 +568,9 @@ public class KinesisService {
List<KinesisRecord> allRecords = shard.getRecords();
int startIndex = 0;
if ("TRIM_HORIZON".equals(type)) {
// LATEST resumes from the shard tip snapshot encoded at GetShardIterator time.
if ("TRIM_HORIZON".equals(type) || "LATEST".equals(type)) {
startIndex = lastIndex;
} else if ("LATEST".equals(type)) {
startIndex = allRecords.size();
} else if ("AFTER_SEQUENCE_NUMBER".equals(type)) {
for (int i = 0; i < allRecords.size(); i++) {
if (allRecords.get(i).getSequenceNumber().equals(startSeq)) {