From 696eeda8e083b3d75726cbb7833c44cbc7308766 Mon Sep 17 00:00:00 2001 From: mrfelek <17274927+mrfelek@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:48:40 +0100 Subject: [PATCH] fix(mq): don't write a broker deleted during its readiness check back as RUNNING (#8468) The Amazon MQ readiness poller no longer writes a deleted broker back as RUNNING. After the blocking readiness check it reads the broker again under the broker's lock and only writes RUNNING when the broker is still there and still CREATION_IN_PROGRESS. DeleteBroker reads the broker and stores it as DELETION_IN_PROGRESS under the same lock before stopping its container, and removes it under the lock afterwards, so a delete racing the poller, or a second delete of the same broker, can't store it again. The stored brokers' locks are kept apart from the name locks CreateBroker holds while its container starts, so the poller and DeleteBroker never wait on another broker's create. --- .../services/amazonmq/AmazonMqService.java | 49 ++++++++++-- .../amazonmq/AmazonMqServiceTest.java | 74 +++++++++++++++++++ 2 files changed, 117 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/floci/aws/services/amazonmq/AmazonMqService.java b/src/main/java/io/floci/aws/services/amazonmq/AmazonMqService.java index 00a0963b6f..b6099b16ad 100644 --- a/src/main/java/io/floci/aws/services/amazonmq/AmazonMqService.java +++ b/src/main/java/io/floci/aws/services/amazonmq/AmazonMqService.java @@ -51,6 +51,11 @@ public class AmazonMqService implements ResourceProvider { private final RegionResolver regionResolver; private final RabbitMqManager rabbitMqManager; private final StripedLocks brokerLocks = new StripedLocks(256); + /** + * The stored brokers' locks, apart from the name locks a create holds while its container starts, so the + * readiness poller and a DeleteBroker never wait on a create of another broker. + */ + private final StripedLocks storedBrokerLocks = new StripedLocks(256); private final ScheduledExecutorService poller = Executors.newSingleThreadScheduledExecutor(); @Inject @@ -222,13 +227,25 @@ public class AmazonMqService implements ResourceProvider { } public void deleteBroker(String brokerId) { - Broker broker = describeBroker(brokerId); - broker.setBrokerState(BrokerState.DELETION_IN_PROGRESS); + String key = brokerKey(regionResolver.getRegion(), brokerId); + Object lock = storedBrokerLock(regionResolver.getAccountId(), key); + Broker broker; + // The broker is read and marked, and later removed, under its lock, which the readiness poller holds while + // it reads the broker again and writes it back: a broker turning ready while it is deleted is not stored + // again as RUNNING, and a second delete either marks the broker before the removal or finds it gone, so it + // never stores it again after the removal. The container stops outside the lock. + synchronized (lock) { + broker = describeBroker(brokerId); + broker.setBrokerState(BrokerState.DELETION_IN_PROGRESS); + storage.put(key, broker); + } if (!config.services().amazonmq().mock()) { rabbitMqManager.stopContainer(broker); rabbitMqManager.removeBrokerStorage(broker); } - storage.delete(brokerKey(regionResolver.getRegion(), brokerId)); + synchronized (lock) { + storage.delete(key); + } LOG.infov("Deleted Amazon MQ broker {0}", brokerId); } @@ -273,9 +290,7 @@ public class AmazonMqService implements ResourceProvider { Broker broker = entry.value(); if (broker.getBrokerState() == BrokerState.CREATION_IN_PROGRESS && rabbitMqManager.isReady(broker)) { - LOG.infov("Amazon MQ broker {0} is now RUNNING", broker.getBrokerName()); - broker.setBrokerState(BrokerState.RUNNING); - storage.putForAccount(entry.accountId(), entry.key(), broker); + markRunning(entry.accountId(), entry.key()); } } } catch (Exception e) { @@ -283,6 +298,28 @@ public class AmazonMqService implements ResourceProvider { } } + /** + * Stores a broker that turned ready as RUNNING, read again under its lock first. The readiness check is a + * blocking call to the broker's container, long enough for a DeleteBroker, which marks the broker under the same + * lock, to land in the meantime, and a broker deleted or being deleted is not written back. + */ + private void markRunning(String accountId, String key) { + synchronized (storedBrokerLock(accountId, key)) { + Broker broker = storage.getForAccount(accountId, key).orElse(null); + if (broker == null || broker.getBrokerState() != BrokerState.CREATION_IN_PROGRESS) { + return; + } + LOG.infov("Amazon MQ broker {0} is now RUNNING", broker.getBrokerName()); + broker.setBrokerState(BrokerState.RUNNING); + storage.putForAccount(accountId, key, broker); + } + } + + /** The lock of the broker stored under {@code key} in {@code accountId}. */ + private Object storedBrokerLock(String accountId, String key) { + return storedBrokerLocks.lockFor(accountId + ":" + key); + } + static String brokerKey(String region, String brokerId) { return region + KEY_SEPARATOR + brokerId; } diff --git a/src/test/java/io/floci/aws/services/amazonmq/AmazonMqServiceTest.java b/src/test/java/io/floci/aws/services/amazonmq/AmazonMqServiceTest.java index 6ed552f62d..3f2628538b 100644 --- a/src/test/java/io/floci/aws/services/amazonmq/AmazonMqServiceTest.java +++ b/src/test/java/io/floci/aws/services/amazonmq/AmazonMqServiceTest.java @@ -262,6 +262,80 @@ class AmazonMqServiceTest { assertEquals(1, brokers.scanAllAccounts().size()); } + /** + * A DeleteBroker landing while the readiness poller waits on the broker's container: the poller reads the broker + * again before writing it, finds it gone, and does not store it back as RUNNING. + */ + @Test + void aBrokerDeletedWhileItsReadinessIsCheckedIsNotWrittenBack() { + RabbitMqManager readyManager = Mockito.mock(RabbitMqManager.class); + service = newService(false, readyManager); + String brokerId = startingBroker(); + when(readyManager.isReady(Mockito.any())).thenAnswer(check -> { + service.deleteBroker(brokerId); + return true; + }); + + service.pollReadiness(); + + assertEquals(List.of(), service.listBrokers()); + assertTrue(brokers.scanAllAccounts().isEmpty()); + } + + /** + * A DeleteBroker arriving while the poller reads a ready broker again waits for the poller to write it back, + * then deletes it, so the broker is not written back after the delete. + */ + @Test + void aDeleteWaitsWhileThePollerWritesAReadyBrokerBack() throws Exception { + ParkingStore parking = new ParkingStore<>(); + brokers = new AccountAwareStorageBackend<>(parking, null, ACCOUNT); + RabbitMqManager readyManager = Mockito.mock(RabbitMqManager.class); + service = newService(false, readyManager); + String brokerId = startingBroker(); + // Armed once the poller has scanned, so the read it parks is the one it makes again under the lock. + when(readyManager.isReady(Mockito.any())).thenAnswer(check -> { + parking.parkNextRead(); + return true; + }); + + assertOperationWaitsForParkedStore(parking, service::pollReadiness, () -> service.deleteBroker(brokerId)); + assertEquals(List.of(), service.listBrokers()); + } + + /** + * Two DeleteBroker calls of one broker: the second waits while the first, parked as it reads the broker, holds + * the broker's lock. It then either deletes the broker too or finds it gone, and either way the broker is not + * stored once both are done. + */ + @Test + void aSecondDeleteWaitsForTheFirstAndLeavesTheBrokerDeleted() throws Exception { + ParkingStore parking = new ParkingStore<>(); + brokers = new AccountAwareStorageBackend<>(parking, null, ACCOUNT); + service = newService(false, Mockito.mock(RabbitMqManager.class)); + String brokerId = startingBroker(); + parking.parkNextRead(); + + assertOperationWaitsForParkedStore(parking, () -> service.deleteBroker(brokerId), () -> { + try { + service.deleteBroker(brokerId); + } catch (AwsException e) { + assertEquals("NotFoundException", e.getErrorCode()); + } + }); + assertEquals(List.of(), service.listBrokers()); + assertTrue(brokers.scanAllAccounts().isEmpty()); + } + + /** A broker still being created, stored in the caller's account and Region; its id. */ + private String startingBroker() { + Broker starting = new Broker("b-starting", "arn:aws:mq:us-east-1:" + ACCOUNT + ":broker:starting:b-starting", + "starting", "RABBITMQ", "3.13", "SINGLE_INSTANCE", "mq.t3.micro"); + starting.setBrokerState(BrokerState.CREATION_IN_PROGRESS); + brokers.putForAccount(ACCOUNT, AmazonMqService.brokerKey("us-east-1", "b-starting"), starting); + return "b-starting"; + } + /** * Two creates of one broker name in an account: the second waits for the first, parked as it stores its * broker, and then finds the name taken, so only one broker has it.