feat(kinesis): support AT_TIMESTAMP shard iterator type (#520)

Implement ShardIteratorType=AT_TIMESTAMP so consumers can resume a shard
from a specific arrival time. Previously AT_TIMESTAMP fell through the
type switch and silently behaved like TRIM_HORIZON.

- Bump iterator encoding to 6 parts (adds timestampMillis slot).
  5-part legacy iterators still decode via split(-1) compat.
- handleGetShardIterator parses and validates Timestamp (numeric,
  epoch seconds with fractional ms, converted via Math.round to long
  millis to avoid 1ms drift).
- AT_TIMESTAMP scan in getRecords uses inclusive >= match against each
  record's ApproximateArrivalTimestamp.
- Continuation iterator remains type=TRIM_HORIZON + resume-at-nextIndex
  to preserve the existing "resume by index" convention; inline comment
  documents the apparent contradiction.
- Reject AT_TIMESTAMP without Timestamp (or with non-numeric Timestamp)
  as InvalidArgumentException at the handler boundary.
This commit is contained in:
TiM authored and GitHub committed 2026-04-19 17:31:04 -05:00
1 parent bc951bf4bf
commit 0642a28e78
4 files changed
+298 -6

No files matched your search

@@ -386,8 +386,24 @@ public class KinesisJsonHandler {
String shardId = request.path("ShardId").asText();
String type = request.path("ShardIteratorType").asText();
String seq = request.has("StartingSequenceNumber") ? request.path("StartingSequenceNumber").asText() : null;
// AWS sends Timestamp as epoch seconds (double with fractional ms).
// Convert to long millis at the boundary; the emulator stores time in ms everywhere.
// Use Math.round to avoid 1ms drift from FP multiplication (e.g. X.999...).
Long timestampMillis = null;
if (request.has("Timestamp") && !request.path("Timestamp").isNull()) {
JsonNode tsNode = request.path("Timestamp");
if (!tsNode.isNumber()) {
throw new io.github.hectorvent.floci.core.common.AwsException("InvalidArgumentException",
"Timestamp must be a number (epoch seconds)", 400);
}
timestampMillis = Math.round(tsNode.asDouble() * 1000);
}
if ("AT_TIMESTAMP".equals(type) && timestampMillis == null) {
throw new io.github.hectorvent.floci.core.common.AwsException("InvalidArgumentException",
"ShardIteratorType AT_TIMESTAMP requires a Timestamp", 400);
}
String iterator = service.getShardIterator(streamName, shardId, type, seq, region);
String iterator = service.getShardIterator(streamName, shardId, type, seq, timestampMillis, region);
ObjectNode response = objectMapper.createObjectNode();
response.put("ShardIterator", iterator);
@@ -371,16 +371,27 @@ public class KinesisService {
}
public String getShardIterator(String streamName, String shardId, String type, String sequenceNumber, String region) {
return getShardIterator(streamName, shardId, type, sequenceNumber, null, region);
}
public String getShardIterator(String streamName, String shardId, String type, String sequenceNumber,
Long timestampMillis, String region) {
resolveStream(streamName, region); // validate exists
// Format: streamName|shardId|type|sequenceNumber|index
String raw = String.format("%s|%s|%s|%s|%d",
streamName, shardId, type, sequenceNumber != null ? sequenceNumber : "", 0);
// 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.
String raw = String.format("%s|%s|%s|%s|%d|%s",
streamName, shardId, type,
sequenceNumber != null ? sequenceNumber : "",
0,
timestampMillis != null ? timestampMillis.toString() : "");
return Base64.getEncoder().encodeToString(raw.getBytes(StandardCharsets.UTF_8));
}
public Map<String, Object> getRecords(String shardIterator, Integer limit, String region) {
byte[] decoded = Base64.getDecoder().decode(shardIterator);
String[] parts = new String(decoded, StandardCharsets.UTF_8).split(java.util.regex.Pattern.quote("|"), 5);
// Use limit=-1 so trailing empty slots round-trip and old 5-part iterators still work.
String[] parts = new String(decoded, StandardCharsets.UTF_8).split(java.util.regex.Pattern.quote("|"), -1);
if (parts.length < 5) throw new AwsException("InvalidArgumentException", "Invalid shard iterator", 400);
String streamName = parts[0];
@@ -388,6 +399,14 @@ public class KinesisService {
String type = parts[2];
String startSeq = parts[3];
int lastIndex = Integer.parseInt(parts[4]);
Long timestampMillis = null;
if (parts.length >= 6 && !parts[5].isEmpty()) {
try {
timestampMillis = Long.parseLong(parts[5]);
} catch (NumberFormatException e) {
throw new AwsException("InvalidArgumentException", "Invalid timestamp in shard iterator", 400);
}
}
KinesisStream stream = resolveStream(streamName, region);
KinesisShard shard = stream.getShards().stream()
@@ -417,6 +436,21 @@ public class KinesisService {
break;
}
}
} else if ("AT_TIMESTAMP".equals(type)) {
if (timestampMillis == null) {
throw new AwsException("InvalidArgumentException",
"AT_TIMESTAMP iterator requires a Timestamp", 400);
}
// First record with ApproximateArrivalTimestamp >= requested timestamp.
// If none match (all records predate timestamp or shard is empty), start past end (no records returned, caught up).
startIndex = allRecords.size();
for (int i = 0; i < allRecords.size(); i++) {
Instant arr = allRecords.get(i).getApproximateArrivalTimestamp();
if (arr != null && arr.toEpochMilli() >= timestampMillis) {
startIndex = i;
break;
}
}
}
int max = limit != null ? Math.min(limit, 1000) : 1000;
@@ -427,8 +461,11 @@ public class KinesisService {
nextIndex = i + 1;
}
// Continuation iterator: type=TRIM_HORIZON + resume-at-nextIndex is the existing
// "resume by index" convention (the type label is misleading but preserved for compat).
// Timestamp slot empty on continuation.
String nextIterator = Base64.getEncoder().encodeToString(
String.format("%s|%s|%s|%s|%d", streamName, shardId, "TRIM_HORIZON", "", nextIndex)
String.format("%s|%s|%s|%s|%d|", streamName, shardId, "TRIM_HORIZON", "", nextIndex)
.getBytes(StandardCharsets.UTF_8));
Map<String, Object> response = new HashMap<>();