From 30b8f88467ee89fcd5de62d072bbe6437be0d992 Mon Sep 17 00:00:00 2001 From: Niels Kootstra <545768+nkootstra@users.noreply.github.com> Date: Wed, 2 Sep 2026 22:12:12 +0200 Subject: [PATCH] fix(kinesis): handle minimum partition key hash (#2950) * fix(kinesis): handle minimum partition key hash * fix(kinesis): preserve negative hash shard routing --- .../services/kinesis/KinesisService.java | 2 +- .../kinesis/KinesisIntegrationTest.java | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisService.java b/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisService.java index 72b4ba42d3..d4807bceb8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisService.java +++ b/src/main/java/io/github/hectorvent/floci/services/kinesis/KinesisService.java @@ -766,7 +766,7 @@ public class KinesisService implements ResourceProvider { private KinesisShard selectShard(KinesisStream stream, String partitionKey) { // Simple hash-based shard selection among ALL shards, then resolve to open one - int index = Math.abs(partitionKey.hashCode()) % stream.getShards().size(); + int index = (int) (Math.abs((long) partitionKey.hashCode()) % stream.getShards().size()); KinesisShard shard = stream.getShards().get(index); // If closed, find the first open child (simplified) diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisIntegrationTest.java index 7e025ceb4b..92e1ef55f1 100644 --- a/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/kinesis/KinesisIntegrationTest.java @@ -765,6 +765,53 @@ class KinesisIntegrationTest { .body("Records.size()", equalTo(0)); } + @Test + @Order(43) + void putRecordAcceptsPartitionKeyWithMinimumHashCode() { + given() + .header("X-Amz-Target", "Kinesis_20131202.CreateStream") + .contentType(KINESIS_CONTENT_TYPE) + .body(""" + {"StreamName": "minimum-hash-partition-key-test", "ShardCount": 3} + """) + .when().post("/").then().statusCode(200); + + given() + .header("X-Amz-Target", "Kinesis_20131202.PutRecord") + .contentType(KINESIS_CONTENT_TYPE) + .body(""" + {"StreamName": "minimum-hash-partition-key-test", "Data": "dGVzdA==", "PartitionKey": "polygenelubricants"} + """) + .when() + .post("/") + .then() + .statusCode(200); + } + + @Test + @Order(44) + void putRecordPreservesShardForNegativeHashCode() { + given() + .header("X-Amz-Target", "Kinesis_20131202.CreateStream") + .contentType(KINESIS_CONTENT_TYPE) + .body(""" + {"StreamName": "negative-hash-partition-key-test", "ShardCount": 3} + """) + .when().post("/").then().statusCode(200); + + given() + .header("X-Amz-Target", "Kinesis_20131202.PutRecord") + .contentType(KINESIS_CONTENT_TYPE) + .body(""" + {"StreamName": "negative-hash-partition-key-test", "Data": "dGVzdA==", "PartitionKey": "negative-hash"} + """) + .when() + .post("/") + .then() + .statusCode(200) + .body("ShardId", equalTo("shardId-000000000002")); + } + // --- AT_TIMESTAMP iterator coverage --- private String atTimestampCreateStream(String name) {