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.
This commit is contained in:
mrfelek authored and GitHub committed 2026-11-15 11:48:40 -04:00
1 parent 9bc2f16a9d
commit 696eeda8e0
2 files changed
+117 -6

No files matched your search

@@ -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;
}