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:
TiM authored and GitHub committed 2026-10-24 08:51:07 -04:00
1 parent bb32ba06eb
commit 39f37c21ff
5 files changed
+374 -16

No files matched your search

@@ -5,6 +5,7 @@ import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.interceptor.Context;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
@@ -16,13 +17,26 @@ import software.amazon.awssdk.services.kinesis.model.CreateStreamRequest;
import software.amazon.awssdk.services.kinesis.model.DeleteStreamRequest;
import software.amazon.awssdk.services.kinesis.model.DescribeStreamSummaryRequest;
import software.amazon.awssdk.services.kinesis.model.DescribeStreamSummaryResponse;
import software.amazon.awssdk.services.kinesis.model.GetRecordsRequest;
import software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest;
import software.amazon.awssdk.services.kinesis.model.ListShardsRequest;
import software.amazon.awssdk.services.kinesis.model.ListShardsResponse;
import software.amazon.awssdk.services.kinesis.model.PutRecordRequest;
import software.amazon.awssdk.services.kinesis.model.PutRecordResponse;
import software.amazon.awssdk.services.kinesis.model.PutRecordsRequest;
import software.amazon.awssdk.services.kinesis.model.PutRecordsRequestEntry;
import software.amazon.awssdk.services.kinesis.model.PutRecordsResponse;
import software.amazon.awssdk.services.kinesis.model.Shard;
import software.amazon.awssdk.services.kinesis.model.ShardIteratorType;
import software.amazon.awssdk.services.kinesis.model.SplitShardRequest;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
@@ -42,6 +56,62 @@ class KinesisTest {
}
}
@Test
@DisplayName("PutRecord and PutRecords route to open descendants after repeated splits")
void writesUseOpenDescendantsAfterRepeatedSplits() {
try (KinesisClient client = TestFixtures.kinesisClient()) {
String streamName = TestFixtures.uniqueName("sdk-kinesis-reshard-routing");
client.createStream(CreateStreamRequest.builder().streamName(streamName).shardCount(1).build());
try {
Shard parent = client.listShards(ListShardsRequest.builder().streamName(streamName).build())
.shards().get(0);
client.putRecord(PutRecordRequest.builder().streamName(streamName).partitionKey("b")
.data(SdkBytes.fromUtf8String("before split")).build());
split(client, streamName, parent);
List<Shard> children = client.listShards(ListShardsRequest.builder().streamName(streamName).build())
.shards().stream().filter(shard -> shard.sequenceNumberRange().endingSequenceNumber() == null)
.collect(Collectors.toList());
for (Shard child : children) {
split(client, streamName, child);
}
Set<String> openIds = client.listShards(ListShardsRequest.builder().streamName(streamName).build())
.shards().stream().filter(shard -> shard.sequenceNumberRange().endingSequenceNumber() == null)
.map(Shard::shardId).collect(Collectors.toSet());
assertThat(openIds).hasSize(4);
PutRecordResponse single = client.putRecord(PutRecordRequest.builder().streamName(streamName)
.partitionKey("b").data(SdkBytes.fromUtf8String("single")).build());
assertThat(openIds).contains(single.shardId());
PutRecordsResponse batch = client.putRecords(PutRecordsRequest.builder().streamName(streamName)
.records(PutRecordsRequestEntry.builder().partitionKey("b")
.data(SdkBytes.fromUtf8String("batch ordinary")).build(),
PutRecordsRequestEntry.builder().partitionKey("b")
.explicitHashKey(parent.hashKeyRange().endingHashKey())
.data(SdkBytes.fromUtf8String("batch explicit")).build()).build());
assertThat(batch.failedRecordCount()).isZero();
assertThat(batch.records()).allSatisfy(record -> {
assertThat(openIds).contains(record.shardId());
assertThat(record.sequenceNumber()).isNotBlank();
});
String iterator = client.getShardIterator(GetShardIteratorRequest.builder().streamName(streamName)
.shardId(parent.shardId()).shardIteratorType(ShardIteratorType.TRIM_HORIZON).build())
.shardIterator();
assertThat(client.getRecords(GetRecordsRequest.builder().shardIterator(iterator).build()).records())
.singleElement().satisfies(record ->
assertThat(record.data().asUtf8String()).isEqualTo("before split"));
} finally {
client.deleteStream(DeleteStreamRequest.builder().streamName(streamName).build());
}
}
}
private static void split(KinesisClient client, String streamName, Shard shard) {
BigInteger start = new BigInteger(shard.hashKeyRange().startingHashKey());
BigInteger end = new BigInteger(shard.hashKeyRange().endingHashKey());
client.splitShard(SplitShardRequest.builder().streamName(streamName).shardToSplit(shard.shardId())
.newStartingHashKey(start.add(end).add(BigInteger.ONE).divide(BigInteger.TWO).toString()).build());
}
@Test
void awsSdkV2UsesRootCborRoute() {
AtomicReference<SdkHttpRequest> requestRef = new AtomicReference<>();