mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(kinesis): route partition keys by MD5 hash range (#8198)
* fix(kinesis): route partition keys by MD5 hash range * fix(kinesis): preserve reshard ranges and index writes
This commit is contained in:
1 parent
9bb384903a
commit
9ee8ba4e11
7 files changed
+411
-104
No files matched your search
+198
-21
@@ -12,6 +12,8 @@ import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -33,7 +35,7 @@ class KinesisClosedShardRoutingServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordFollowsAnOpenSplitChild() {
|
||||
void putRecordSelectsAnOpenSplitChildByHash() {
|
||||
KinesisStream stream = service.createStream(STREAM, 1, REGION);
|
||||
service.putRecord(STREAM, DATA, "c", REGION);
|
||||
split(stream.getShards().getFirst());
|
||||
@@ -42,55 +44,230 @@ class KinesisClosedShardRoutingServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordTraversesClosedSplitChildren() {
|
||||
void splitMigratesLegacyRangesBeforeDerivingChildRanges() {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
BigInteger half = BigInteger.ONE.shiftLeft(127);
|
||||
BigInteger quarter = BigInteger.ONE.shiftLeft(126);
|
||||
String max = BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).toString();
|
||||
stream.getShards().forEach(shard ->
|
||||
shard.setHashKeyRange(new KinesisShard.HashKeyRange("0", max)));
|
||||
|
||||
service.splitShard(STREAM, stream.getShards().getFirst().getShardId(), quarter.toString(), REGION);
|
||||
|
||||
assertEquals(new KinesisShard.HashKeyRange("0", half.subtract(BigInteger.ONE).toString()),
|
||||
stream.getShards().get(0).getHashKeyRange());
|
||||
assertEquals(new KinesisShard.HashKeyRange(half.toString(), max),
|
||||
stream.getShards().get(1).getHashKeyRange());
|
||||
assertEquals(new KinesisShard.HashKeyRange("0", quarter.subtract(BigInteger.ONE).toString()),
|
||||
stream.getShards().get(2).getHashKeyRange());
|
||||
assertEquals(new KinesisShard.HashKeyRange(quarter.toString(), half.subtract(BigInteger.ONE).toString()),
|
||||
stream.getShards().get(3).getHashKeyRange());
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, "c", stream.getShards().get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidLegacySplitDoesNotChangeShardRanges() {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
String max = BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).toString();
|
||||
String outsideNormalizedParent = BigInteger.ONE.shiftLeft(127)
|
||||
.add(BigInteger.ONE.shiftLeft(126)).toString();
|
||||
stream.getShards().forEach(shard ->
|
||||
shard.setHashKeyRange(new KinesisShard.HashKeyRange("0", max)));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.splitShard(STREAM, stream.getShards().getFirst().getShardId(),
|
||||
outsideNormalizedParent, REGION));
|
||||
|
||||
assertEquals("InvalidArgumentException", error.getErrorCode());
|
||||
assertEquals(2, stream.getShards().size());
|
||||
assertTrue(stream.getShards().stream().allMatch(shard ->
|
||||
new KinesisShard.HashKeyRange("0", max).equals(shard.getHashKeyRange())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mergeMigratesLegacyRangesBeforeDerivingChildRange() {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
BigInteger half = BigInteger.ONE.shiftLeft(127);
|
||||
String max = BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).toString();
|
||||
stream.getShards().forEach(shard ->
|
||||
shard.setHashKeyRange(new KinesisShard.HashKeyRange("0", max)));
|
||||
|
||||
service.mergeShards(STREAM, stream.getShards().get(0).getShardId(),
|
||||
stream.getShards().get(1).getShardId(), REGION);
|
||||
|
||||
assertEquals(new KinesisShard.HashKeyRange("0", half.subtract(BigInteger.ONE).toString()),
|
||||
stream.getShards().get(0).getHashKeyRange());
|
||||
assertEquals(new KinesisShard.HashKeyRange(half.toString(), max),
|
||||
stream.getShards().get(1).getHashKeyRange());
|
||||
assertEquals(new KinesisShard.HashKeyRange("0", max), stream.getShards().get(2).getHashKeyRange());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void nonAdjacentMergeDoesNotChangeShardTopology(boolean legacyRanges) {
|
||||
KinesisStream stream = service.createStream(STREAM, 3, REGION);
|
||||
if (legacyRanges) {
|
||||
String max = BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).toString();
|
||||
stream.getShards().forEach(shard ->
|
||||
shard.setHashKeyRange(new KinesisShard.HashKeyRange("0", max)));
|
||||
}
|
||||
List<KinesisShard.HashKeyRange> before = stream.getShards().stream()
|
||||
.map(KinesisShard::getHashKeyRange).toList();
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.mergeShards(STREAM, stream.getShards().get(0).getShardId(),
|
||||
stream.getShards().get(2).getShardId(), REGION));
|
||||
|
||||
assertEquals("InvalidArgumentException", error.getErrorCode());
|
||||
assertEquals(3, stream.getShards().size());
|
||||
assertTrue(stream.getShards().stream().noneMatch(KinesisShard::isClosed));
|
||||
assertEquals(before, stream.getShards().stream().map(KinesisShard::getHashKeyRange).toList());
|
||||
service.putRecordWithShardId(STREAM, DATA, "c", REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountMigratesLegacyRangesBeforeChoosingSplits() {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
String max = BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).toString();
|
||||
stream.getShards().forEach(shard ->
|
||||
shard.setHashKeyRange(new KinesisShard.HashKeyRange("0", max)));
|
||||
|
||||
service.updateShardCount(STREAM, 4, "UNIFORM_SCALING", REGION);
|
||||
|
||||
List<KinesisShard> open = stream.getShards().stream().filter(shard -> !shard.isClosed())
|
||||
.sorted((left, right) -> new BigInteger(left.getHashKeyRange().startingHashKey())
|
||||
.compareTo(new BigInteger(right.getHashKeyRange().startingHashKey())))
|
||||
.toList();
|
||||
assertEquals(4, open.size());
|
||||
assertEquals("0", open.getFirst().getHashKeyRange().startingHashKey());
|
||||
assertEquals(max, open.getLast().getHashKeyRange().endingHashKey());
|
||||
for (int i = 1; i < open.size(); i++) {
|
||||
assertEquals(new BigInteger(open.get(i - 1).getHashKeyRange().endingHashKey()).add(BigInteger.ONE),
|
||||
new BigInteger(open.get(i).getHashKeyRange().startingHashKey()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountLimitDoesNotNormalizeLegacyRanges() {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
String max = BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).toString();
|
||||
stream.getShards().forEach(shard ->
|
||||
shard.setHashKeyRange(new KinesisShard.HashKeyRange("0", max)));
|
||||
List<Instant> history = Collections.nCopies(10, Instant.now());
|
||||
stream.setShardCountUpdateTimestamps(history);
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.updateShardCount(STREAM, 4, "UNIFORM_SCALING", REGION));
|
||||
|
||||
assertEquals("LimitExceededException", error.getErrorCode());
|
||||
assertTrue(stream.getShards().stream().allMatch(shard ->
|
||||
new KinesisShard.HashKeyRange("0", max).equals(shard.getHashKeyRange())));
|
||||
assertEquals(history, stream.getShardCountUpdateTimestamps());
|
||||
}
|
||||
|
||||
@Test
|
||||
void alreadyReshardedLegacyOverlapDoesNotRewriteChildRanges() {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
String max = BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).toString();
|
||||
String quarter = BigInteger.ONE.shiftLeft(126).toString();
|
||||
service.splitShard(STREAM, stream.getShards().getFirst().getShardId(), quarter, REGION);
|
||||
stream.getShards().get(0).setHashKeyRange(new KinesisShard.HashKeyRange("0", max));
|
||||
stream.getShards().get(1).setHashKeyRange(new KinesisShard.HashKeyRange("0", max));
|
||||
stream.getShards().get(3).setHashKeyRange(new KinesisShard.HashKeyRange(quarter, max));
|
||||
List<KinesisShard.HashKeyRange> before = stream.getShards().stream()
|
||||
.map(KinesisShard::getHashKeyRange).toList();
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.putRecordWithShardId(STREAM, DATA, "c", REGION));
|
||||
|
||||
assertEquals("InvalidArgumentException", error.getErrorCode());
|
||||
assertEquals(before, stream.getShards().stream().map(KinesisShard::getHashKeyRange).toList());
|
||||
assertTrue(stream.getShards().stream().allMatch(shard -> shard.recordCount() == 0));
|
||||
|
||||
AwsException scalingError = assertThrows(AwsException.class,
|
||||
() -> service.updateShardCount(STREAM, 4, "UNIFORM_SCALING", REGION));
|
||||
assertEquals("InvalidArgumentException", scalingError.getErrorCode());
|
||||
assertTrue(stream.getShardCountUpdateTimestamps().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void partiallyOverlappingRootRangesAreNotRepartitioned() {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
String max = BigInteger.ONE.shiftLeft(128).subtract(BigInteger.ONE).toString();
|
||||
stream.getShards().get(1).setHashKeyRange(new KinesisShard.HashKeyRange("0", max));
|
||||
List<KinesisShard.HashKeyRange> before = stream.getShards().stream()
|
||||
.map(KinesisShard::getHashKeyRange).toList();
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.putRecordWithShardId(STREAM, DATA, "c", REGION));
|
||||
|
||||
assertEquals("InvalidArgumentException", error.getErrorCode());
|
||||
assertEquals(before, stream.getShards().stream().map(KinesisShard::getHashKeyRange).toList());
|
||||
assertTrue(stream.getShards().stream().allMatch(shard -> shard.recordCount() == 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordRoutesToOpenRangeAfterRepeatedSplits() {
|
||||
KinesisStream stream = splitTwoGenerations();
|
||||
|
||||
// "b" hashes to 98, selecting the original parent at index zero modulo seven.
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, "b", stream.getShards().get(3));
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, "b", stream.getShards().get(5));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {1, 2})
|
||||
void putRecordPrefersAnOpenDirectChildOverADeeperDescendant(int closedChildIndex) {
|
||||
void putRecordFindsHashRangeAcrossUnevenSplitDepth(int closedChildIndex) {
|
||||
KinesisStream stream = service.createStream(STREAM, 1, REGION);
|
||||
split(stream.getShards().getFirst());
|
||||
split(stream.getShards().get(closedChildIndex));
|
||||
|
||||
// "d" selects the root modulo five, with one direct child still open.
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, "d", stream.getShards().get(3 - closedChildIndex));
|
||||
KinesisShard expected = stream.getShards().get(closedChildIndex == 1 ? 2 : 3);
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, "d", expected);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"d", "e"})
|
||||
void putRecordTraversesEitherMergeParentThenAClosedChild(String partitionKey) {
|
||||
@ValueSource(strings = {"a", "b"})
|
||||
void putRecordRoutesAcrossMergedThenSplitRange(String partitionKey) {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
service.mergeShards(STREAM, stream.getShards().get(0).getShardId(),
|
||||
stream.getShards().get(1).getShardId(), REGION);
|
||||
split(stream.getShards().get(2));
|
||||
|
||||
// With five stored shards, these keys select each original merge parent.
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, partitionKey, stream.getShards().get(3));
|
||||
KinesisShard expected = stream.getShards().get("a".equals(partitionKey) ? 3 : 4);
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, partitionKey, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordTraversesSplitThenMergeThenSplit() {
|
||||
void cachedRoutingRebuildsAfterMerge() {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
service.putRecordWithShardId(STREAM, DATA, "a", "0", REGION);
|
||||
service.mergeShards(STREAM, stream.getShards().get(0).getShardId(),
|
||||
stream.getShards().get(1).getShardId(), REGION);
|
||||
|
||||
KinesisService.PutRecordResult result = service.putRecordWithShardId(STREAM, DATA, "b", "0", REGION);
|
||||
|
||||
assertEquals(stream.getShards().get(2).getShardId(), result.shardId());
|
||||
assertEquals(1, stream.getShards().get(0).recordCount());
|
||||
assertEquals(1, stream.getShards().get(2).recordCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordRoutesAcrossSplitThenMergeThenSplit() {
|
||||
KinesisStream stream = service.createStream(STREAM, 1, REGION);
|
||||
split(stream.getShards().getFirst());
|
||||
service.mergeShards(STREAM, stream.getShards().get(1).getShardId(),
|
||||
stream.getShards().get(2).getShardId(), REGION);
|
||||
split(stream.getShards().get(3));
|
||||
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, "f", stream.getShards().get(4));
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, "f", stream.getShards().get(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordUsesAReachableChildInsteadOfAnEarlierUnrelatedOpenShard() {
|
||||
void putRecordSelectsSplitChildInsteadOfUnrelatedOpenShard() {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
service.putRecordWithShardId(STREAM, DATA, "d", "0", REGION);
|
||||
split(stream.getShards().getFirst());
|
||||
|
||||
// "d" selects the closed parent modulo four; shard one is unrelated and open.
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, "d", stream.getShards().get(2));
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, "a", stream.getShards().get(2));
|
||||
|
||||
assertEquals(1, stream.getShards().get(0).recordCount());
|
||||
assertEquals(0, stream.getShards().get(1).recordCount());
|
||||
@@ -98,11 +275,11 @@ class KinesisClosedShardRoutingServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordRejectsAClosedShardWithoutDescendants() {
|
||||
void putRecordRejectsWhenNoOpenShardCoversHash() {
|
||||
KinesisStream stream = service.createStream(STREAM, 1, REGION);
|
||||
stream.getShards().getFirst().setClosed(true);
|
||||
|
||||
assertNoDestination(stream, "b");
|
||||
assertNoDestination(stream, "a");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -110,11 +287,11 @@ class KinesisClosedShardRoutingServiceTest {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
stream.getShards().getFirst().setClosed(true);
|
||||
|
||||
assertNoDestination(stream, "b");
|
||||
assertNoDestination(stream, "a");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordRejectsACycleWithoutHangingOrAppending() {
|
||||
void putRecordRejectsNoOpenShardDespiteLineageCycle() {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
KinesisShard first = stream.getShards().get(0);
|
||||
KinesisShard second = stream.getShards().get(1);
|
||||
@@ -143,7 +320,7 @@ class KinesisClosedShardRoutingServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitAccountProducerAlsoTraversesClosedDescendants() {
|
||||
void explicitAccountProducerAlsoRoutesToAnOpenHashRange() {
|
||||
KinesisStream stream = splitTwoGenerations();
|
||||
|
||||
String sequence = service.putRecordForAccount("000000000000", STREAM, DATA, "b", REGION);
|
||||
|
||||
@@ -671,7 +671,7 @@ class KinesisIntegrationTest {
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
Set<String> reported = new HashSet<>();
|
||||
// Probe enough partition keys that hash(pk) % 2 hits both shards. With 50 keys the
|
||||
// Probe enough partition-key MD5 values to cover both shard ranges. With 50 keys the
|
||||
// odds of single-shard routing are ~1 in 2^49, so this is effectively deterministic.
|
||||
for (int i = 0; i < 50 && reported.size() < 2; i++) {
|
||||
String pk = "pk-" + i;
|
||||
@@ -863,7 +863,7 @@ class KinesisIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(46)
|
||||
void putRecordPreservesShardForNegativeHashCode() {
|
||||
void putRecordUsesMd5RangeForPreviouslyNegativeJavaHashCode() {
|
||||
given()
|
||||
.header("X-Amz-Target", "Kinesis_20131202.CreateStream")
|
||||
.contentType(KINESIS_CONTENT_TYPE)
|
||||
@@ -882,7 +882,7 @@ class KinesisIntegrationTest {
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ShardId", equalTo("shardId-000000000002"));
|
||||
.body("ShardId", equalTo("shardId-000000000001"));
|
||||
}
|
||||
|
||||
// --- AT_TIMESTAMP iterator coverage ---
|
||||
|
||||
@@ -20,6 +20,7 @@ import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
@@ -28,6 +29,7 @@ import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
@@ -406,7 +408,7 @@ class KinesisJsonHandlerTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"PutRecord", "PutRecords"})
|
||||
void writesReportAnOpenDescendantAfterRepeatedSplits(String action) {
|
||||
void writesReportTheOpenHashRangeAfterRepeatedSplits(String action) {
|
||||
createStream("test-stream");
|
||||
KinesisStream stream = service.describeStream("test-stream", REGION);
|
||||
service.splitShard("test-stream", FIRST_SHARD_ID,
|
||||
@@ -422,19 +424,19 @@ class KinesisJsonHandlerTest {
|
||||
ObjectNode response = responseEntity(handler.handle(action, request, REGION));
|
||||
JsonNode result = "PutRecords".equals(action) ? response.path("Records").get(0) : response;
|
||||
|
||||
assertEquals(stream.getShards().get(3).getShardId(), result.path("ShardId").asText());
|
||||
assertEquals(stream.getShards().get(5).getShardId(), result.path("ShardId").asText());
|
||||
assertFalse(result.path("SequenceNumber").asText().isEmpty());
|
||||
if ("PutRecords".equals(action)) {
|
||||
assertEquals(0, response.path("FailedRecordCount").asInt());
|
||||
}
|
||||
assertEquals(1, stream.getShards().get(3).recordCount());
|
||||
assertEquals(1, stream.getShards().get(5).recordCount());
|
||||
assertTrue(stream.getShards().stream().filter(KinesisShard::isClosed)
|
||||
.allMatch(shard -> shard.recordCount() == 0));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"PutRecord", "PutRecords"})
|
||||
void writesFailWithoutAppendingWhenNoOpenDescendantExists(String action) {
|
||||
void writesFailWithoutAppendingWhenNoOpenShardCoversHash(String action) {
|
||||
createStream("test-stream");
|
||||
KinesisShard shard = service.describeStream("test-stream", REGION).getShards().getFirst();
|
||||
shard.setClosed(true);
|
||||
@@ -1010,6 +1012,94 @@ class KinesisJsonHandlerTest {
|
||||
assertEquals(FIRST_SHARD_ID, respMin.get("ShardId").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ordinaryPartitionKeysUseMd5HashRangesReportedByListShards() {
|
||||
createStream("md5-routing", TWO_SHARDS);
|
||||
ObjectNode list = MAPPER.createObjectNode().put("StreamName", "md5-routing");
|
||||
ArrayNode shards = (ArrayNode) responseEntity(handler.handle("ListShards", list, REGION)).get("Shards");
|
||||
|
||||
ObjectNode put = MAPPER.createObjectNode()
|
||||
.put("StreamName", "md5-routing")
|
||||
.put("Data", "dGVzdA==")
|
||||
.put("PartitionKey", "pk-0");
|
||||
String singleShard = responseEntity(handler.handle("PutRecord", put, REGION)).get("ShardId").asText();
|
||||
assertEquals(shardForMd5Hash(shards, "EFFC8E8920E4EE5505B7F963C5D72F6D"), singleShard);
|
||||
|
||||
ObjectNode batch = MAPPER.createObjectNode().put("StreamName", "md5-routing");
|
||||
ArrayNode records = batch.putArray("Records");
|
||||
records.addObject().put("Data", "dGVzdA==").put("PartitionKey", "a");
|
||||
records.addObject().put("Data", "dGVzdA==").put("PartitionKey", "b");
|
||||
records.addObject().put("Data", "dGVzdA==").put("PartitionKey", "é-0");
|
||||
ArrayNode results = (ArrayNode) responseEntity(handler.handle("PutRecords", batch, REGION)).get("Records");
|
||||
assertEquals(shardForMd5Hash(shards, "0CC175B9C0F1B6A831C399E269772661"),
|
||||
results.get(0).get("ShardId").asText());
|
||||
assertEquals(shardForMd5Hash(shards, "92EB5FFEE6AE2FEC3AD71C777531578F"),
|
||||
results.get(1).get("ShardId").asText());
|
||||
assertEquals(shardForMd5Hash(shards, "2F6923E2AAC1CFC8729E5C892BBE2244"),
|
||||
results.get(2).get("ShardId").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ordinaryPartitionKeyNormalizesLegacyOverlappingShardRanges() {
|
||||
createStream("legacy-md5-routing", TWO_SHARDS);
|
||||
KinesisStream stream = service.describeStream("legacy-md5-routing", REGION);
|
||||
for (KinesisShard shard : stream.getShards()) {
|
||||
shard.setHashKeyRange(new KinesisShard.HashKeyRange(MIN_HASH_KEY, MAX_HASH_KEY));
|
||||
}
|
||||
|
||||
ObjectNode put = MAPPER.createObjectNode()
|
||||
.put("StreamName", "legacy-md5-routing")
|
||||
.put("Data", "dGVzdA==")
|
||||
.put("PartitionKey", "pk-0");
|
||||
assertEquals(SECOND_SHARD_ID,
|
||||
responseEntity(handler.handle("PutRecord", put, REGION)).get("ShardId").asText());
|
||||
assertEquals(MIN_HASH_KEY, stream.getShards().getFirst().getHashKeyRange().startingHashKey());
|
||||
assertEquals(MAX_HASH_KEY, stream.getShards().get(1).getHashKeyRange().endingHashKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordsDoesNotRescanEveryShardForEachEntry() {
|
||||
KinesisStream stream = service.createStream("indexed-routing", 256, REGION);
|
||||
AtomicInteger rangeReads = new AtomicInteger();
|
||||
List<KinesisShard> counted = new ArrayList<>();
|
||||
for (KinesisShard shard : stream.getShards()) {
|
||||
counted.add(new KinesisShard(shard.getShardId(),
|
||||
shard.getHashKeyRange().startingHashKey(), shard.getHashKeyRange().endingHashKey(),
|
||||
shard.getSequenceNumberRange().startingSequenceNumber()) {
|
||||
@Override
|
||||
public HashKeyRange getHashKeyRange() {
|
||||
rangeReads.incrementAndGet();
|
||||
return super.getHashKeyRange();
|
||||
}
|
||||
});
|
||||
}
|
||||
stream.setShards(counted);
|
||||
|
||||
ObjectNode batch = MAPPER.createObjectNode().put("StreamName", "indexed-routing");
|
||||
ArrayNode records = batch.putArray("Records");
|
||||
for (int i = 0; i < 100; i++) {
|
||||
records.addObject().put("Data", "dGVzdA==").put("PartitionKey", "pk-" + i);
|
||||
}
|
||||
|
||||
ObjectNode result = responseEntity(handler.handle("PutRecords", batch, REGION));
|
||||
|
||||
assertEquals(0, result.path("FailedRecordCount").asInt());
|
||||
assertEquals(100, result.path("Records").size());
|
||||
assertTrue(rangeReads.get() < 8_000, () -> "Shard range reads: " + rangeReads.get());
|
||||
}
|
||||
|
||||
private static String shardForMd5Hash(ArrayNode shards, String md5Hex) {
|
||||
BigInteger hash = new BigInteger(md5Hex, 16);
|
||||
for (JsonNode shard : shards) {
|
||||
BigInteger start = new BigInteger(shard.path("HashKeyRange").path("StartingHashKey").asText());
|
||||
BigInteger end = new BigInteger(shard.path("HashKeyRange").path("EndingHashKey").asText());
|
||||
if (hash.compareTo(start) >= 0 && hash.compareTo(end) <= 0) {
|
||||
return shard.path("ShardId").asText();
|
||||
}
|
||||
}
|
||||
throw new AssertionError("No shard covers MD5 hash " + md5Hex);
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordAcceptsRecordAtSizeLimit() {
|
||||
createStream("test-stream");
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.floci.aws.services.kinesis.model.KinesisStream;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
@@ -53,8 +54,12 @@ class KinesisListShardsConcurrencyTest {
|
||||
// A concurrent split closes the parent and atomically appends two children, mutating the
|
||||
// live backing list the pre-fix sub-list page still points at.
|
||||
String firstShardId = stream.getShards().getFirst().getShardId();
|
||||
KinesisShard.HashKeyRange firstRange = stream.getShards().getFirst().getHashKeyRange();
|
||||
String splitPoint = new BigInteger(firstRange.startingHashKey())
|
||||
.add(new BigInteger(firstRange.endingHashKey())).add(BigInteger.ONE)
|
||||
.divide(BigInteger.TWO).toString();
|
||||
service.splitShard("reshard-stream", firstShardId,
|
||||
"170141183460469231731687303715884105728", REGION);
|
||||
splitPoint, REGION);
|
||||
|
||||
// Pre-fix: iterating/sizing the live sub-list here throws ConcurrentModificationException.
|
||||
assertDoesNotThrow(() -> {
|
||||
|
||||
@@ -451,7 +451,11 @@ class KinesisServiceTest {
|
||||
Iterator<KinesisShard> reader = stream.getShards().iterator();
|
||||
reader.next();
|
||||
|
||||
kinesisService.splitShard("my-stream", shardId, "170141183460469231731687303715884105728", REGION);
|
||||
KinesisShard.HashKeyRange firstRange = stream.getShards().getFirst().getHashKeyRange();
|
||||
String splitPoint = new BigInteger(firstRange.startingHashKey())
|
||||
.add(new BigInteger(firstRange.endingHashKey())).add(BigInteger.ONE)
|
||||
.divide(BigInteger.TWO).toString();
|
||||
kinesisService.splitShard("my-stream", shardId, splitPoint, REGION);
|
||||
|
||||
// A plain ArrayList iterator throws ConcurrentModificationException here; COWAL keeps reading
|
||||
// its fixed pre-split snapshot.
|
||||
|
||||
Reference in new issue
Block a user