mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(kinesis): implement UpdateShardCount API (#3200)
Adds uniform-scaling reshard support via existing splitShard/mergeShards machinery, with the documented double/half, 10000-shard, and 10-scales-per-24h limits enforced as LimitExceededException/InvalidArgumentException/ValidationException. The half-shard-count minimum rounds up for an odd current count, which is Floci's own call since AWS documents only "below half" with no stated rounding direction; the separate 10 TPS call-rate limit AWS documents on this action is not simulated, matching Floci's lack of real-time request throttling elsewhere. Both are now called out explicitly in the docs and code. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> * Fix #3064
This commit is contained in:
1 parent
43eace4e02
commit
e3766eac6f
6 files changed
+516
No files matched your search
+121
@@ -1177,6 +1177,127 @@ class KinesisJsonHandlerTest {
|
||||
assertEquals("ResourceNotFoundException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
private ObjectNode updateShardCount(String streamName, int targetShardCount, String scalingType) {
|
||||
ObjectNode req = MAPPER.createObjectNode();
|
||||
req.put("StreamName", streamName);
|
||||
req.put("TargetShardCount", targetShardCount);
|
||||
req.put("ScalingType", scalingType);
|
||||
return responseEntity(handler.handle("UpdateShardCount", req, REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountScalesUpAndReturnsExpectedShape() {
|
||||
createStream("test-stream", 2);
|
||||
|
||||
ObjectNode response = updateShardCount("test-stream", 4, "UNIFORM_SCALING");
|
||||
assertEquals("test-stream", response.get("StreamName").asText());
|
||||
assertEquals(streamArn("test-stream"), response.get("StreamARN").asText());
|
||||
assertEquals(2, response.get("CurrentShardCount").asInt());
|
||||
assertEquals(4, response.get("TargetShardCount").asInt());
|
||||
|
||||
ObjectNode summaryReq = MAPPER.createObjectNode();
|
||||
summaryReq.put("StreamName", "test-stream");
|
||||
ObjectNode summary = (ObjectNode) responseEntity(
|
||||
handler.handle("DescribeStreamSummary", summaryReq, REGION)).get("StreamDescriptionSummary");
|
||||
assertEquals(4, summary.get("OpenShardCount").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountScalesDown() {
|
||||
createStream("test-stream", 4);
|
||||
|
||||
ObjectNode response = updateShardCount("test-stream", 2, "UNIFORM_SCALING");
|
||||
assertEquals(4, response.get("CurrentShardCount").asInt());
|
||||
assertEquals(2, response.get("TargetShardCount").asInt());
|
||||
|
||||
ObjectNode summaryReq = MAPPER.createObjectNode();
|
||||
summaryReq.put("StreamName", "test-stream");
|
||||
ObjectNode summary = (ObjectNode) responseEntity(
|
||||
handler.handle("DescribeStreamSummary", summaryReq, REGION)).get("StreamDescriptionSummary");
|
||||
assertEquals(2, summary.get("OpenShardCount").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountRejectsTargetAboveDouble() {
|
||||
createStream("test-stream", 2);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> updateShardCount("test-stream", 5, "UNIFORM_SCALING"));
|
||||
assertEquals("LimitExceededException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountRejectsTargetBelowHalf() {
|
||||
createStream("test-stream", 4);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> updateShardCount("test-stream", 1, "UNIFORM_SCALING"));
|
||||
assertEquals("LimitExceededException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountRejectsNonUniformScalingType() {
|
||||
createStream("test-stream", 2);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> updateShardCount("test-stream", 4, "BOGUS"));
|
||||
assertEquals("InvalidArgumentException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountRejectsOnDemandStream() {
|
||||
ObjectNode create = MAPPER.createObjectNode();
|
||||
create.put("StreamName", "on-demand-stream");
|
||||
create.put("ShardCount", 2);
|
||||
create.putObject("StreamModeDetails").put("StreamMode", "ON_DEMAND");
|
||||
assertThat(handler.handle("CreateStream", create, REGION).getStatus(), is(200));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> updateShardCount("on-demand-stream", 4, "UNIFORM_SCALING"));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountRejectsAStreamThatIsNotActive() {
|
||||
createStream("test-stream", 2);
|
||||
service.describeStream("test-stream", REGION).setStreamStatus("UPDATING");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> updateShardCount("test-stream", 4, "UNIFORM_SCALING"));
|
||||
assertEquals("ResourceInUseException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountRejectsUnknownStream() {
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> updateShardCount("missing-stream", 2, "UNIFORM_SCALING"));
|
||||
assertEquals("ResourceNotFoundException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountLineageAfterSplitCoversWholeParentRange() {
|
||||
createStream("test-stream", 1);
|
||||
|
||||
updateShardCount("test-stream", 2, "UNIFORM_SCALING");
|
||||
|
||||
ObjectNode descReq = MAPPER.createObjectNode();
|
||||
descReq.put("StreamName", "test-stream");
|
||||
ArrayNode shards = (ArrayNode) responseEntity(
|
||||
handler.handle("DescribeStream", descReq, REGION)).get("StreamDescription").get("Shards");
|
||||
assertEquals(3, shards.size());
|
||||
|
||||
ObjectNode parent = (ObjectNode) shards.get(0);
|
||||
List<ObjectNode> children = new java.util.ArrayList<>();
|
||||
for (JsonNode s : shards) {
|
||||
if (s.has("ParentShardId") && s.get("ParentShardId").asText().equals(parent.get("ShardId").asText())) {
|
||||
children.add((ObjectNode) s);
|
||||
}
|
||||
}
|
||||
assertEquals(2, children.size());
|
||||
assertFalse(children.get(0).has("AdjacentParentShardId"));
|
||||
assertFalse(children.get(1).has("AdjacentParentShardId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createStreamAppliesTagsFromTheRequest() {
|
||||
ObjectNode create = MAPPER.createObjectNode();
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -448,6 +449,182 @@ class KinesisServiceTest {
|
||||
assertFalse(updated.getShards().get(2).isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountScalesUpToDouble() {
|
||||
kinesisService.createStream("my-stream", 2, REGION);
|
||||
|
||||
KinesisService.UpdateShardCountResult result =
|
||||
kinesisService.updateShardCount("my-stream", 4, "UNIFORM_SCALING", REGION);
|
||||
|
||||
assertEquals(2, result.currentShardCount());
|
||||
assertEquals(4, result.targetShardCount());
|
||||
assertEquals("my-stream", result.streamName());
|
||||
|
||||
KinesisStream updated = kinesisService.describeStream("my-stream", REGION);
|
||||
List<KinesisShard> openShards = updated.getShards().stream().filter(s -> !s.isClosed()).toList();
|
||||
assertEquals(4, openShards.size());
|
||||
assertEquals(2, updated.getShards().stream().filter(KinesisShard::isClosed).count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountScalesDownToHalf() {
|
||||
kinesisService.createStream("my-stream", 4, REGION);
|
||||
|
||||
KinesisService.UpdateShardCountResult result =
|
||||
kinesisService.updateShardCount("my-stream", 2, "UNIFORM_SCALING", REGION);
|
||||
|
||||
assertEquals(4, result.currentShardCount());
|
||||
assertEquals(2, result.targetShardCount());
|
||||
|
||||
KinesisStream updated = kinesisService.describeStream("my-stream", REGION);
|
||||
List<KinesisShard> openShards = updated.getShards().stream().filter(s -> !s.isClosed()).toList();
|
||||
assertEquals(2, openShards.size());
|
||||
assertEquals(4, updated.getShards().stream().filter(KinesisShard::isClosed).count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountRejectsTargetAboveDouble() {
|
||||
kinesisService.createStream("my-stream", 2, REGION);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> kinesisService.updateShardCount("my-stream", 5, "UNIFORM_SCALING", REGION));
|
||||
assertEquals("LimitExceededException", ex.getErrorCode());
|
||||
|
||||
assertEquals(2, kinesisService.describeStream("my-stream", REGION).getShards().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountRejectsTargetBelowHalf() {
|
||||
kinesisService.createStream("my-stream", 4, REGION);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> kinesisService.updateShardCount("my-stream", 1, "UNIFORM_SCALING", REGION));
|
||||
assertEquals("LimitExceededException", ex.getErrorCode());
|
||||
|
||||
assertEquals(4, kinesisService.describeStream("my-stream", REGION).getShards().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountAllowsExactlyHalfRoundedUp() {
|
||||
// current=5, half rounded up is 3: 2 is rejected, 3 is the lowest allowed target.
|
||||
kinesisService.createStream("my-stream", 5, REGION);
|
||||
|
||||
assertThrows(AwsException.class,
|
||||
() -> kinesisService.updateShardCount("my-stream", 2, "UNIFORM_SCALING", REGION));
|
||||
|
||||
KinesisService.UpdateShardCountResult result =
|
||||
kinesisService.updateShardCount("my-stream", 3, "UNIFORM_SCALING", REGION);
|
||||
assertEquals(3, result.targetShardCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountRejectsNonUniformScalingType() {
|
||||
kinesisService.createStream("my-stream", 2, REGION);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> kinesisService.updateShardCount("my-stream", 4, "BISECTION", REGION));
|
||||
assertEquals("InvalidArgumentException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountRejectsOnDemandStream() {
|
||||
kinesisService.createStream("my-stream", 2, "ON_DEMAND", REGION);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> kinesisService.updateShardCount("my-stream", 4, "UNIFORM_SCALING", REGION));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountSplitLineageRecordsParentAndCoversWholeRange() {
|
||||
kinesisService.createStream("my-stream", 2, REGION);
|
||||
KinesisStream before = kinesisService.describeStream("my-stream", REGION);
|
||||
List<String> originalOpenIds = before.getShards().stream().map(KinesisShard::getShardId).toList();
|
||||
|
||||
kinesisService.updateShardCount("my-stream", 4, "UNIFORM_SCALING", REGION);
|
||||
|
||||
KinesisStream after = kinesisService.describeStream("my-stream", REGION);
|
||||
List<KinesisShard> newOpenShards = after.getShards().stream()
|
||||
.filter(s -> !s.isClosed())
|
||||
.toList();
|
||||
assertEquals(4, newOpenShards.size());
|
||||
|
||||
for (KinesisShard child : newOpenShards) {
|
||||
assertNotNull(child.getParentShardId(), "each split child must record its parent");
|
||||
assertTrue(originalOpenIds.contains(child.getParentShardId()),
|
||||
"split child's parent must be one of the pre-scaling open shards");
|
||||
assertNull(child.getAdjacentParentShardId(), "a split child has no adjacent parent");
|
||||
}
|
||||
|
||||
for (String originalId : originalOpenIds) {
|
||||
KinesisShard closedParent = after.getShards().stream()
|
||||
.filter(s -> s.getShardId().equals(originalId))
|
||||
.findFirst().orElseThrow();
|
||||
assertTrue(closedParent.isClosed(), "original open shards must be closed after a split");
|
||||
|
||||
List<KinesisShard> children = newOpenShards.stream()
|
||||
.filter(s -> originalId.equals(s.getParentShardId()))
|
||||
.toList();
|
||||
assertEquals(2, children.size(), "each split parent must have exactly two children");
|
||||
KinesisShard first = children.get(0);
|
||||
KinesisShard second = children.get(1);
|
||||
assertEquals(closedParent.getHashKeyRange().startingHashKey(), first.getHashKeyRange().startingHashKey());
|
||||
assertEquals(closedParent.getHashKeyRange().endingHashKey(), second.getHashKeyRange().endingHashKey());
|
||||
assertEquals(
|
||||
new java.math.BigInteger(first.getHashKeyRange().endingHashKey()).add(java.math.BigInteger.ONE).toString(),
|
||||
second.getHashKeyRange().startingHashKey(),
|
||||
"children must tile the parent's range with no gap or overlap");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountMergeLineageRecordsBothParents() {
|
||||
kinesisService.createStream("my-stream", 4, REGION);
|
||||
KinesisStream before = kinesisService.describeStream("my-stream", REGION);
|
||||
List<KinesisShard> originalOpenShards = before.getShards().stream()
|
||||
.sorted(Comparator.comparing(s -> new java.math.BigInteger(s.getHashKeyRange().startingHashKey())))
|
||||
.toList();
|
||||
|
||||
kinesisService.updateShardCount("my-stream", 2, "UNIFORM_SCALING", REGION);
|
||||
|
||||
KinesisStream after = kinesisService.describeStream("my-stream", REGION);
|
||||
List<KinesisShard> newOpenShards = after.getShards().stream()
|
||||
.filter(s -> !s.isClosed())
|
||||
.toList();
|
||||
assertEquals(2, newOpenShards.size());
|
||||
|
||||
List<String> expectedParents = originalOpenShards.stream().map(KinesisShard::getShardId).toList();
|
||||
List<String> actualParentPairs = new ArrayList<>();
|
||||
for (KinesisShard mergedShard : newOpenShards) {
|
||||
assertNotNull(mergedShard.getParentShardId(), "merge child must record its first parent");
|
||||
assertNotNull(mergedShard.getAdjacentParentShardId(), "merge child must record its adjacent parent");
|
||||
actualParentPairs.add(mergedShard.getParentShardId());
|
||||
actualParentPairs.add(mergedShard.getAdjacentParentShardId());
|
||||
}
|
||||
assertEquals(new HashSet<>(expectedParents), new HashSet<>(actualParentPairs));
|
||||
|
||||
for (String originalId : expectedParents) {
|
||||
KinesisShard closedParent = after.getShards().stream()
|
||||
.filter(s -> s.getShardId().equals(originalId))
|
||||
.findFirst().orElseThrow();
|
||||
assertTrue(closedParent.isClosed(), "merged parent shards must be closed");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateShardCountRejectsMoreThanTenScalesInRollingDay() {
|
||||
kinesisService.createStream("my-stream", 128, REGION);
|
||||
String streamName = "my-stream";
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int target = 128 + (i % 2 == 0 ? 1 : -1);
|
||||
kinesisService.updateShardCount(streamName, target, "UNIFORM_SCALING", REGION);
|
||||
}
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> kinesisService.updateShardCount(streamName, 129, "UNIFORM_SCALING", REGION));
|
||||
assertEquals("LimitExceededException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void enableEnhancedMonitoring() {
|
||||
kinesisService.createStream("my-stream", 1, REGION);
|
||||
|
||||
Reference in new issue
Block a user