mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(kinesis): stop routing records to closed shards (#6414)
* fix(kinesis): stop routing records to closed shards Repeated resharding can leave a selected shard and both of its direct children closed. `selectShard` currently falls back to that closed parent, so a successful write can append after the shard's ending sequence number. - Follow both parent links through closed intermediate shards to an open descendant, with deterministic breadth-first order and visited shard IDs. - Reject an unreachable open destination before appending. Keep the producer lock, ordinary-key hash selection, ExplicitHashKey routing, and actual returned ShardId. - Cover repeated splits, split/merge lineage, malformed graphs, handler success/failure results, and Java SDK writes after resharding. * test(kinesis): protect breadth-first descendant preference Add a mixed-depth split topology so routing to the still-open direct child is distinguished from depth-first traversal through its closed sibling. * test(kinesis): cover both mixed-depth shard layouts Parameterize the split child so the direct-child preference regression rejects depth-first traversal under either child ordering. * fix(kinesis): preserve the open-shard routing fast path Delay the lineage snapshot until the selected shard is closed, avoiding a shard-list copy on ordinary open-shard writes. Name expected descendant destinations in routing fixtures and cover an unrelated open shard preceding the reachable child.
This commit is contained in:
5 files changed
+374
-16
No files matched your search
+195
@@ -0,0 +1,195 @@
|
||||
package io.github.hectorvent.floci.services.kinesis;
|
||||
|
||||
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 org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class KinesisClosedShardRoutingServiceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String STREAM = "routing";
|
||||
private static final byte[] DATA = {1};
|
||||
|
||||
private KinesisService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new KinesisService(new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new RegionResolver(REGION, "000000000000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordFollowsAnOpenSplitChild() {
|
||||
KinesisStream stream = service.createStream(STREAM, 1, REGION);
|
||||
service.putRecord(STREAM, DATA, "c", REGION);
|
||||
split(stream.getShards().getFirst());
|
||||
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, "c", stream.getShards().get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordTraversesClosedSplitChildren() {
|
||||
KinesisStream stream = splitTwoGenerations();
|
||||
|
||||
// "b" hashes to 98, selecting the original parent at index zero modulo seven.
|
||||
assertOpenDestinationAndUnchangedClosedRecords(stream, "b", stream.getShards().get(3));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {1, 2})
|
||||
void putRecordPrefersAnOpenDirectChildOverADeeperDescendant(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));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"d", "e"})
|
||||
void putRecordTraversesEitherMergeParentThenAClosedChild(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));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordTraversesSplitThenMergeThenSplit() {
|
||||
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));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordUsesAReachableChildInsteadOfAnEarlierUnrelatedOpenShard() {
|
||||
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));
|
||||
|
||||
assertEquals(1, stream.getShards().get(0).recordCount());
|
||||
assertEquals(0, stream.getShards().get(1).recordCount());
|
||||
assertEquals(1, stream.getShards().get(2).recordCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordRejectsAClosedShardWithoutDescendants() {
|
||||
KinesisStream stream = service.createStream(STREAM, 1, REGION);
|
||||
stream.getShards().getFirst().setClosed(true);
|
||||
|
||||
assertNoDestination(stream, "b");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordDoesNotFallBackToAnUnrelatedOpenShard() {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
stream.getShards().getFirst().setClosed(true);
|
||||
|
||||
assertNoDestination(stream, "b");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRecordRejectsACycleWithoutHangingOrAppending() {
|
||||
KinesisStream stream = service.createStream(STREAM, 2, REGION);
|
||||
KinesisShard first = stream.getShards().get(0);
|
||||
KinesisShard second = stream.getShards().get(1);
|
||||
first.setClosed(true);
|
||||
second.setClosed(true);
|
||||
first.setParentShardId(second.getShardId());
|
||||
second.setAdjacentParentShardId(first.getShardId());
|
||||
|
||||
assertTimeoutPreemptively(Duration.ofSeconds(2), () -> assertNoDestination(stream, "b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitHashKeysKeepRoutingToOpenRangeBoundariesAfterRepeatedSplits() {
|
||||
KinesisStream stream = splitTwoGenerations();
|
||||
List<KinesisShard> openShards = stream.getShards().stream().filter(shard -> !shard.isClosed()).toList();
|
||||
|
||||
for (KinesisShard shard : openShards) {
|
||||
for (String hash : List.of(shard.getHashKeyRange().startingHashKey(),
|
||||
shard.getHashKeyRange().endingHashKey())) {
|
||||
KinesisService.PutRecordResult result = service.putRecordWithShardId(STREAM, DATA, "b", hash, REGION);
|
||||
assertEquals(shard.getShardId(), result.shardId());
|
||||
}
|
||||
}
|
||||
assertTrue(stream.getShards().stream().filter(KinesisShard::isClosed)
|
||||
.allMatch(shard -> shard.recordCount() == 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitAccountProducerAlsoTraversesClosedDescendants() {
|
||||
KinesisStream stream = splitTwoGenerations();
|
||||
|
||||
String sequence = service.putRecordForAccount("000000000000", STREAM, DATA, "b", REGION);
|
||||
|
||||
assertTrue(stream.getShards().stream().filter(KinesisShard::isClosed)
|
||||
.allMatch(shard -> shard.recordCount() == 0));
|
||||
assertTrue(stream.getShards().stream().filter(shard -> !shard.isClosed())
|
||||
.flatMap(shard -> shard.getRecords().stream())
|
||||
.anyMatch(record -> sequence.equals(record.getSequenceNumber())));
|
||||
}
|
||||
|
||||
private KinesisStream splitTwoGenerations() {
|
||||
KinesisStream stream = service.createStream(STREAM, 1, REGION);
|
||||
split(stream.getShards().getFirst());
|
||||
List<KinesisShard> children = stream.getShards().stream().filter(shard -> !shard.isClosed()).toList();
|
||||
children.forEach(this::split);
|
||||
return stream;
|
||||
}
|
||||
|
||||
private void split(KinesisShard shard) {
|
||||
BigInteger start = new BigInteger(shard.getHashKeyRange().startingHashKey());
|
||||
BigInteger end = new BigInteger(shard.getHashKeyRange().endingHashKey());
|
||||
service.splitShard(STREAM, shard.getShardId(), start.add(end).add(BigInteger.ONE)
|
||||
.divide(BigInteger.TWO).toString(), REGION);
|
||||
}
|
||||
|
||||
private void assertOpenDestinationAndUnchangedClosedRecords(KinesisStream stream, String partitionKey,
|
||||
KinesisShard expectedDestination) {
|
||||
Map<String, Integer> closedCounts = stream.getShards().stream().filter(KinesisShard::isClosed)
|
||||
.collect(Collectors.toMap(KinesisShard::getShardId, KinesisShard::recordCount));
|
||||
KinesisService.PutRecordResult result = service.putRecordWithShardId(STREAM, DATA, partitionKey, REGION);
|
||||
KinesisShard destination = stream.getShards().stream()
|
||||
.filter(shard -> result.shardId().equals(shard.getShardId())).findFirst().orElseThrow();
|
||||
|
||||
assertFalse(destination.isClosed());
|
||||
assertEquals(expectedDestination.getShardId(), result.shardId());
|
||||
assertEquals(result.sequenceNumber(), destination.getRecords().getLast().getSequenceNumber());
|
||||
stream.getShards().stream().filter(KinesisShard::isClosed)
|
||||
.forEach(shard -> assertEquals(closedCounts.get(shard.getShardId()).intValue(), shard.recordCount()));
|
||||
}
|
||||
|
||||
private void assertNoDestination(KinesisStream stream, String partitionKey) {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.putRecordWithShardId(STREAM, DATA, partitionKey, REGION));
|
||||
assertEquals("InvalidArgumentException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertTrue(stream.getShards().stream().allMatch(shard -> shard.recordCount() == 0));
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,8 @@ 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;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Clock;
|
||||
@@ -84,6 +86,58 @@ class KinesisJsonHandlerTest {
|
||||
return (ObjectNode) response.getEntity();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"PutRecord", "PutRecords"})
|
||||
void writesReportAnOpenDescendantAfterRepeatedSplits(String action) {
|
||||
createStream("test-stream");
|
||||
KinesisStream stream = service.describeStream("test-stream", REGION);
|
||||
service.splitShard("test-stream", FIRST_SHARD_ID,
|
||||
"170141183460469231731687303715884105728", REGION);
|
||||
service.splitShard("test-stream", stream.getShards().get(1).getShardId(),
|
||||
"85070591730234615865843651857942052864", REGION);
|
||||
service.splitShard("test-stream", stream.getShards().get(2).getShardId(),
|
||||
"255211775190703847597530955573826158592", REGION);
|
||||
|
||||
ObjectNode request = MAPPER.createObjectNode().put("StreamName", "test-stream");
|
||||
ObjectNode record = "PutRecords".equals(action) ? request.putArray("Records").addObject() : request;
|
||||
record.put("PartitionKey", "b").put("Data", "AQ==");
|
||||
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());
|
||||
assertFalse(result.path("SequenceNumber").asText().isEmpty());
|
||||
if ("PutRecords".equals(action)) {
|
||||
assertEquals(0, response.path("FailedRecordCount").asInt());
|
||||
}
|
||||
assertEquals(1, stream.getShards().get(3).recordCount());
|
||||
assertTrue(stream.getShards().stream().filter(KinesisShard::isClosed)
|
||||
.allMatch(shard -> shard.recordCount() == 0));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"PutRecord", "PutRecords"})
|
||||
void writesFailWithoutAppendingWhenNoOpenDescendantExists(String action) {
|
||||
createStream("test-stream");
|
||||
KinesisShard shard = service.describeStream("test-stream", REGION).getShards().getFirst();
|
||||
shard.setClosed(true);
|
||||
ObjectNode request = MAPPER.createObjectNode().put("StreamName", "test-stream");
|
||||
ObjectNode record = "PutRecords".equals(action) ? request.putArray("Records").addObject() : request;
|
||||
record.put("PartitionKey", "b").put("Data", "AQ==");
|
||||
|
||||
if ("PutRecord".equals(action)) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> handler.handle(action, request, REGION));
|
||||
assertEquals("InvalidArgumentException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
} else {
|
||||
ObjectNode response = responseEntity(handler.handle(action, request, REGION));
|
||||
assertEquals(1, response.path("FailedRecordCount").asInt());
|
||||
assertEquals("InternalFailure", response.path("Records").get(0).path("ErrorCode").asText());
|
||||
assertFalse(response.path("Records").get(0).has("SequenceNumber"));
|
||||
assertFalse(response.path("Records").get(0).has("ShardId"));
|
||||
}
|
||||
assertEquals(0, shard.recordCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeStreamByName() {
|
||||
createStream("test-stream");
|
||||
|
||||
Reference in new issue
Block a user