fix(kinesis): reject DeleteStream on a missing stream and honour EnforceConsumerDeletion (#8459)

* fix(kinesis): reject DeleteStream on a missing stream and honour EnforceConsumerDeletion
* fix(kinesis): serialize stream deletion with consumer registration
* fix(kinesis): refuse RegisterStreamConsumer for a stream that no longer exists
This commit is contained in:
Mahmoud Khalifa authored and GitHub committed 2026-11-21 18:29:05 -04:00
1 parent 3f64a4514a
commit 2f99c0ae8c
6 files changed
+130 -6

No files matched your search

@@ -213,7 +213,8 @@ public class KinesisJsonHandler {
private Response handleDeleteStream(JsonNode request, String region) {
String streamName = resolveStreamName(request);
service.deleteStream(streamName, region);
boolean enforceConsumerDeletion = request.path("EnforceConsumerDeletion").asBoolean(false);
service.deleteStream(streamName, enforceConsumerDeletion, region);
return Response.ok(objectMapper.createObjectNode()).build();
}
@@ -315,8 +315,14 @@ public class KinesisService implements ResourceProvider {
String consumerArn = streamArn + "/consumer/" + consumerName + ":" + System.currentTimeMillis();
KinesisConsumer consumer = new KinesisConsumer(consumerName, consumerArn, streamArn);
consumer.getTags().putAll(tags);
synchronized (consumerStore) {
consumerStore.put(regionKey(region, consumerArn), consumer);
// Same lock order as deleteStream (stream monitor, then consumerStore), so a registration
// cannot land between a delete's consumer sweep and its removal of the stream.
String streamKey = regionKey(region, streamNameFromTagArn(streamArn));
synchronized (lockFor(streamKey)) {
describeStreamByArn(streamArn, region);
synchronized (consumerStore) {
consumerStore.put(regionKey(region, consumerArn), consumer);
}
}
LOG.infov("Registered Kinesis consumer: {0} for stream {1}", consumerName, streamArn);
return consumer;
@@ -355,11 +361,35 @@ public class KinesisService implements ResourceProvider {
}
public void deleteStream(String streamName, String region) {
// Internal callers (CloudFormation delete) treat an already-gone stream as deleted.
deleteStream(streamName, true, true, region);
}
public void deleteStream(String streamName, boolean enforceConsumerDeletion, String region) {
deleteStream(streamName, enforceConsumerDeletion, false, region);
}
private void deleteStream(String streamName, boolean enforceConsumerDeletion, boolean missingIsDeleted,
String region) {
String storageKey = regionKey(region, streamName);
// Delete under the per-stream monitor every writer takes, so the delete is serialized with
// any in-flight append, split, merge or metadata write on this stream. See lockFor for why
// the monitor is deliberately left in the map.
synchronized (lockFor(storageKey)) {
if (missingIsDeleted && store.get(storageKey).isEmpty()) {
return;
}
KinesisStream stream = resolveStream(streamName, region);
synchronized (consumerStore) {
List<KinesisConsumer> consumers = listStreamConsumers(stream.getStreamArn(), region);
if (!consumers.isEmpty() && !enforceConsumerDeletion) {
throw new AwsException("ResourceInUseException",
"Stream " + streamName + " has registered consumers", 400);
}
for (KinesisConsumer consumer : consumers) {
consumerStore.delete(regionKey(region, consumer.getConsumerArn()));
}
}
store.delete(storageKey);
}
LOG.infov("Deleted Kinesis stream: {0}", streamName);