fix: honor Kinesis ExplicitHashKey routing (#3069)

* fix: honor kinesis explicit hash keys

* fix(kinesis): reject numeric ExplicitHashKey and migrate legacy shard ranges (#3069)

Address the two review findings on explicit-hash-key routing:

- Reject a non-string ExplicitHashKey JSON node with SerializationException
  in PutRecord and PutRecords, instead of letting asText coerce a JSON number
  into an accepted decimal string, preserving AWS wire-protocol fidelity.
- Repartition legacy multi-shard streams whose open shards still carry the old
  full-span hash range into disjoint contiguous ranges before explicit-hash
  routing, so an explicit key no longer collapses onto the first shard after an
  upgrade. Already-disjoint (new/split/merged) streams are left untouched.

RED -> GREEN: 3 new tests failed on the prior HEAD
(putRecordRejectsNumericExplicitHashKey, putRecordsRejectNumericExplicitHashKey,
putRecordMigratesLegacyOverlappingShardRangesForExplicitHashKey), all green
after the fix. Kinesis suite: 118 run, 0 failures.

---------

Co-authored-by: Ben Younes <2910651+ousamabenyounes@users.noreply.github.com>
This commit is contained in:
Ben YounesandBen Younes authored and GitHub committed 2026-09-05 12:35:06 -04:00
1 parent b6489ac933
commit a006b27694
5 files changed
+327 -13

No files matched your search

@@ -23,6 +23,10 @@ import static org.junit.jupiter.api.Assertions.*;
class KinesisIntegrationTest {
private static final String KINESIS_CONTENT_TYPE = "application/x-amz-json-1.1";
private static final String MIN_HASH_KEY = "0";
private static final String FIRST_SHARD_ENDING_HASH_KEY = "170141183460469231731687303715884105727";
private static final String SECOND_SHARD_STARTING_HASH_KEY = "170141183460469231731687303715884105728";
private static final String MAX_HASH_KEY = "340282366920938463463374607431768211455";
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final Pattern CREATION_TIMESTAMP_PATTERN = Pattern.compile(
"\\\"StreamCreationTimestamp\\\"\\s*:\\s*(-?\\d+(?:\\.\\d+)?)(?=\\s*[,}])");
@@ -64,7 +68,9 @@ class KinesisIntegrationTest {
.body("Shards[0].ShardId", equalTo("shardId-000000000000"))
.body("Shards[1].ShardId", equalTo("shardId-000000000001"))
.body("Shards[0].HashKeyRange.StartingHashKey", notNullValue())
.body("Shards[0].HashKeyRange.EndingHashKey", equalTo("340282366920938463463374607431768211455"))
.body("Shards[0].HashKeyRange.EndingHashKey", equalTo(FIRST_SHARD_ENDING_HASH_KEY))
.body("Shards[1].HashKeyRange.StartingHashKey", equalTo(SECOND_SHARD_STARTING_HASH_KEY))
.body("Shards[1].HashKeyRange.EndingHashKey", equalTo(MAX_HASH_KEY))
.body("Shards[0].SequenceNumberRange.StartingSequenceNumber", notNullValue());
}
@@ -254,7 +260,7 @@ class KinesisIntegrationTest {
{
"StreamName": "list-shards-test",
"ShardToSplit": "shardId-000000000000",
"NewStartingHashKey": "170141183460469231731687303715884105728"
"NewStartingHashKey": "85070591730234615865843651857942052864"
}
""")
.when()
@@ -790,6 +796,64 @@ class KinesisIntegrationTest {
@Test
@Order(44)
void putRecordHonorsExplicitHashKey() {
given()
.header("X-Amz-Target", "Kinesis_20131202.CreateStream")
.contentType(KINESIS_CONTENT_TYPE)
.body("""
{"StreamName": "explicit-hash-put-test", "ShardCount": 2}
""")
.when().post("/").then().statusCode(200);
given()
.header("X-Amz-Target", "Kinesis_20131202.PutRecord")
.contentType(KINESIS_CONTENT_TYPE)
.body("{\"StreamName\": \"explicit-hash-put-test\", \"Data\": \"dGVzdA==\","
+ "\"PartitionKey\": \"same-key\", \"ExplicitHashKey\": \"" + MIN_HASH_KEY + "\"}")
.when().post("/")
.then().statusCode(200)
.body("ShardId", equalTo("shardId-000000000000"));
given()
.header("X-Amz-Target", "Kinesis_20131202.PutRecord")
.contentType(KINESIS_CONTENT_TYPE)
.body("{\"StreamName\": \"explicit-hash-put-test\", \"Data\": \"dGVzdA==\","
+ "\"PartitionKey\": \"same-key\", \"ExplicitHashKey\": \"" + MAX_HASH_KEY + "\"}")
.when().post("/")
.then().statusCode(200)
.body("ShardId", equalTo("shardId-000000000001"));
}
@Test
@Order(45)
void putRecordsHonorsExplicitHashKeyPerEntry() {
given()
.header("X-Amz-Target", "Kinesis_20131202.CreateStream")
.contentType(KINESIS_CONTENT_TYPE)
.body("""
{"StreamName": "explicit-hash-putrecords-test", "ShardCount": 2}
""")
.when().post("/").then().statusCode(200);
given()
.header("X-Amz-Target", "Kinesis_20131202.PutRecords")
.contentType(KINESIS_CONTENT_TYPE)
.body("{\"StreamName\": \"explicit-hash-putrecords-test\","
+ "\"Records\": ["
+ "{\"Data\": \"dGVzdA==\", \"PartitionKey\": \"same-key\", \"ExplicitHashKey\": \""
+ MIN_HASH_KEY + "\"},"
+ "{\"Data\": \"dGVzdA==\", \"PartitionKey\": \"same-key\", \"ExplicitHashKey\": \""
+ MAX_HASH_KEY + "\"}"
+ "]}")
.when().post("/")
.then().statusCode(200)
.body("FailedRecordCount", equalTo(0))
.body("Records[0].ShardId", equalTo("shardId-000000000000"))
.body("Records[1].ShardId", equalTo("shardId-000000000001"));
}
@Test
@Order(46)
void putRecordPreservesShardForNegativeHashCode() {
given()
.header("X-Amz-Target", "Kinesis_20131202.CreateStream")
@@ -7,6 +7,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.kinesis.model.KinesisShard;
import io.github.hectorvent.floci.services.kinesis.model.KinesisStream;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -29,6 +31,14 @@ class KinesisJsonHandlerTest {
private static final String REGION = "us-east-1";
private static final String ACCOUNT = "123456789012";
private static final String STREAM_ARN = "arn:aws:kinesis:us-east-1:123456789012:stream/test-stream";
private static final String MAX_HASH_KEY_PLUS_ONE = "340282366920938463463374607431768211456";
private static final String NON_DECIMAL_HASH_KEY = "1.5";
private static final long NUMERIC_EXPLICIT_HASH_KEY = 12345L;
private static final String MIN_HASH_KEY = "0";
private static final String MAX_HASH_KEY = "340282366920938463463374607431768211455";
private static final int TWO_SHARDS = 2;
private static final String FIRST_SHARD_ID = "shardId-000000000000";
private static final String SECOND_SHARD_ID = "shardId-000000000001";
private static final ObjectMapper MAPPER = new ObjectMapper();
private KinesisService service;
@@ -45,9 +55,13 @@ class KinesisJsonHandlerTest {
}
private void createStream(String name) {
createStream(name, 1);
}
private void createStream(String name, int shardCount) {
ObjectNode req = MAPPER.createObjectNode();
req.put("StreamName", name);
req.put("ShardCount", 1);
req.put("ShardCount", shardCount);
assertThat(handler.handle("CreateStream", req, REGION).getStatus(), is(200));
}
@@ -421,6 +435,97 @@ class KinesisJsonHandlerTest {
assertEquals("InvalidArgumentException", ex.getErrorCode());
}
@Test
void putRecordRejectsExplicitHashKeyOutsideHashKeySpace() {
createStream("test-stream");
ObjectNode req = MAPPER.createObjectNode();
req.put("StreamName", "test-stream");
req.put("Data", "dGVzdA==");
req.put("PartitionKey", "pk1");
req.put("ExplicitHashKey", MAX_HASH_KEY_PLUS_ONE);
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("PutRecord", req, REGION));
assertEquals("InvalidArgumentException", ex.getErrorCode());
}
@Test
void putRecordsRejectsNonDecimalExplicitHashKey() {
createStream("test-stream");
ObjectNode req = MAPPER.createObjectNode();
req.put("StreamName", "test-stream");
ArrayNode records = req.putArray("Records");
records.addObject()
.put("Data", "dGVzdA==")
.put("PartitionKey", "pk1")
.put("ExplicitHashKey", NON_DECIMAL_HASH_KEY);
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("PutRecords", req, REGION));
assertEquals("InvalidArgumentException", ex.getErrorCode());
}
@Test
void putRecordRejectsNumericExplicitHashKey() {
createStream("test-stream");
ObjectNode req = MAPPER.createObjectNode();
req.put("StreamName", "test-stream");
req.put("Data", "dGVzdA==");
req.put("PartitionKey", "pk1");
// A JSON number is not the string-shaped field AWS accepts; asText coercion must not
// silently turn it into a valid decimal hash key.
req.put("ExplicitHashKey", NUMERIC_EXPLICIT_HASH_KEY);
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("PutRecord", req, REGION));
assertEquals("SerializationException", ex.getErrorCode());
}
@Test
void putRecordsRejectNumericExplicitHashKey() {
createStream("test-stream");
ObjectNode req = MAPPER.createObjectNode();
req.put("StreamName", "test-stream");
ArrayNode records = req.putArray("Records");
records.addObject()
.put("Data", "dGVzdA==")
.put("PartitionKey", "pk1")
.put("ExplicitHashKey", NUMERIC_EXPLICIT_HASH_KEY);
AwsException ex = assertThrows(AwsException.class,
() -> handler.handle("PutRecords", req, REGION));
assertEquals("SerializationException", ex.getErrorCode());
}
@Test
void putRecordMigratesLegacyOverlappingShardRangesForExplicitHashKey() {
// A multi-shard stream persisted before disjoint ranges existed carries the full 128-bit
// span on every open shard, so an explicit key matches all of them. Naive first-match
// routing would then collapse every explicit key onto the first shard.
createStream("legacy-stream", TWO_SHARDS);
KinesisStream stream = service.describeStream("legacy-stream", REGION);
for (KinesisShard shard : stream.getShards()) {
shard.setHashKeyRange(new KinesisShard.HashKeyRange(MIN_HASH_KEY, MAX_HASH_KEY));
}
ObjectNode req = MAPPER.createObjectNode();
req.put("StreamName", "legacy-stream");
req.put("Data", "dGVzdA==");
req.put("PartitionKey", "pk1");
req.put("ExplicitHashKey", MAX_HASH_KEY);
ObjectNode resp = responseEntity(handler.handle("PutRecord", req, REGION));
assertEquals(SECOND_SHARD_ID, resp.get("ShardId").asText());
req.put("ExplicitHashKey", MIN_HASH_KEY);
ObjectNode respMin = responseEntity(handler.handle("PutRecord", req, REGION));
assertEquals(FIRST_SHARD_ID, respMin.get("ShardId").asText());
}
@Test
void putRecordAcceptsRecordAtSizeLimit() {
createStream("test-stream");