mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(amazonmq): add configuration for AMQP and console host port ranges (#3315)
Signed-off-by: Matheus André <matheusandr2@gmail.com>
This commit is contained in:
1 parent
21d9e7b8f4
commit
2d5a8f705b
8 files changed
+315
-54
No files matched your search
@@ -1172,6 +1172,31 @@ public interface EmulatorConfig {
|
||||
|
||||
@WithDefault("rabbitmq:3-management")
|
||||
String defaultImage();
|
||||
|
||||
/**
|
||||
* Host port range the AMQP listener (container port 5672) is published on, one
|
||||
* port per broker. Published in both topologies: no Floci-internal proxy fronts
|
||||
* the broker, so the Docker host-port binding is the only way a client outside
|
||||
* the Docker network (e.g. on the host, with Floci itself containerized) can
|
||||
* reach it (#3240). Env: FLOCI_SERVICES_AMAZONMQ_AMQP_HOST_PORT_BASE
|
||||
*/
|
||||
@WithDefault("5672")
|
||||
int amqpHostPortBase();
|
||||
|
||||
/** Env: FLOCI_SERVICES_AMAZONMQ_AMQP_HOST_PORT_MAX */
|
||||
@WithDefault("5699")
|
||||
int amqpHostPortMax();
|
||||
|
||||
/**
|
||||
* Host port range the RabbitMQ management console (container port 15672) is
|
||||
* published on. Env: FLOCI_SERVICES_AMAZONMQ_CONSOLE_HOST_PORT_BASE
|
||||
*/
|
||||
@WithDefault("15672")
|
||||
int consoleHostPortBase();
|
||||
|
||||
/** Env: FLOCI_SERVICES_AMAZONMQ_CONSOLE_HOST_PORT_MAX */
|
||||
@WithDefault("15699")
|
||||
int consoleHostPortMax();
|
||||
}
|
||||
|
||||
interface KinesisAnalyticsServiceConfig {
|
||||
|
||||
+74
-27
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.E
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
|
||||
import io.github.hectorvent.floci.core.common.docker.PortAllocator;
|
||||
import io.github.hectorvent.floci.services.amazonmq.model.Broker;
|
||||
import io.github.hectorvent.floci.services.amazonmq.model.BrokerInstance;
|
||||
import io.github.hectorvent.floci.services.amazonmq.model.MqUser;
|
||||
@@ -27,13 +28,17 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Manages the backing RabbitMQ Docker container for an Amazon MQ broker.
|
||||
* In native (dev) mode it publishes the AMQP (5672) and management (15672)
|
||||
* ports to dynamic host ports; in Docker mode sibling containers reach the
|
||||
* broker over the docker network.
|
||||
* The AMQP (5672) and management (15672) ports are each published to a host
|
||||
* port allocated from the configured ranges, in both topologies. Unlike the
|
||||
* proxy-fronted services (RDS, ElastiCache), nothing inside Floci relays AMQP
|
||||
* traffic, so when Floci itself runs in Docker the host-port binding is the only
|
||||
* way a client on the host can reach the broker (#3240), the same reasoning as
|
||||
* {@code OpenSearchDomainManager}. Sibling containers keep reaching the broker
|
||||
* over the docker network via the endpoints reported by {@code DescribeBroker}.
|
||||
*
|
||||
* <p>Unlike {@code RedpandaManager}, RabbitMQ does not advertise a self-perceived
|
||||
* address back to clients, so no pre-allocated/advertised port is needed —
|
||||
* dynamic ports (the ElastiCache pattern) are enough.
|
||||
* address back to clients, so the host port does not need to be passed to the
|
||||
* container as a startup argument.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class RabbitMqManager {
|
||||
@@ -46,22 +51,32 @@ public class RabbitMqManager {
|
||||
private final ContainerLifecycleManager lifecycleManager;
|
||||
private final ContainerLogStreamer logStreamer;
|
||||
private final ContainerDetector containerDetector;
|
||||
private final PortAllocator portAllocator;
|
||||
private final EmulatorConfig config;
|
||||
private final RegionResolver regionResolver;
|
||||
private final Map<String, Closeable> logStreams = new ConcurrentHashMap<>();
|
||||
private final Map<String, String> containerIds = new ConcurrentHashMap<>();
|
||||
// Host ports reserved for each running broker (in-memory only, like containerIds):
|
||||
// released when the container goes away so repeated create/delete cycles do not
|
||||
// exhaust the configured ranges.
|
||||
private final Map<String, HostPorts> hostPorts = new ConcurrentHashMap<>();
|
||||
|
||||
/** The pair of host ports a broker's container is published on. */
|
||||
record HostPorts(int amqp, int console) {}
|
||||
|
||||
@Inject
|
||||
public RabbitMqManager(ContainerBuilder containerBuilder,
|
||||
ContainerLifecycleManager lifecycleManager,
|
||||
ContainerLogStreamer logStreamer,
|
||||
ContainerDetector containerDetector,
|
||||
PortAllocator portAllocator,
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.logStreamer = logStreamer;
|
||||
this.containerDetector = containerDetector;
|
||||
this.portAllocator = portAllocator;
|
||||
this.config = config;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
@@ -106,38 +121,46 @@ public class RabbitMqManager {
|
||||
specBuilder.withEnv("RABBITMQ_DEFAULT_PASS", admin.getPassword());
|
||||
}
|
||||
|
||||
if (!containerDetector.isRunningInContainer()) {
|
||||
specBuilder.withDynamicPort(AMQP_PORT).withDynamicPort(MGMT_PORT);
|
||||
} else {
|
||||
specBuilder.withExposedPort(AMQP_PORT).withExposedPort(MGMT_PORT);
|
||||
}
|
||||
// A re-provision of the same broker has just removed its old container, so any
|
||||
// reservation held for it is stale; hand the ports back before allocating anew.
|
||||
releaseHostPorts(broker.getBrokerId());
|
||||
HostPorts ports = allocateHostPorts();
|
||||
|
||||
if (ContainerStorageHelper.isNamedVolumeMode(config)) {
|
||||
ContainerStorageHelper.applyStorage(specBuilder, lifecycleManager, config,
|
||||
"amazonmq", broker.getVolumeId(), broker.getBrokerId(),
|
||||
"/var/lib/rabbitmq");
|
||||
} else {
|
||||
String hostDataPath = ContainerStorageHelper.hostResourcePath(config, "amazonmq", broker.getBrokerId())
|
||||
.toAbsolutePath().toString();
|
||||
if (!containerDetector.isRunningInContainer()) {
|
||||
ContainerStorageHelper.ensureHostDir(hostDataPath);
|
||||
}
|
||||
specBuilder.withBind(hostDataPath, "/var/lib/rabbitmq");
|
||||
}
|
||||
|
||||
ContainerSpec spec = specBuilder.build();
|
||||
// Publish both ports on the host in every topology (see class javadoc). The
|
||||
// endpoints resolved below stay topology-aware: localhost:<hostPort> natively,
|
||||
// the container's network IP when Floci runs in Docker.
|
||||
specBuilder.withPortBinding(AMQP_PORT, ports.amqp())
|
||||
.withPortBinding(MGMT_PORT, ports.console());
|
||||
|
||||
ContainerInfo info;
|
||||
try {
|
||||
if (ContainerStorageHelper.isNamedVolumeMode(config)) {
|
||||
ContainerStorageHelper.applyStorage(specBuilder, lifecycleManager, config,
|
||||
"amazonmq", broker.getVolumeId(), broker.getBrokerId(),
|
||||
"/var/lib/rabbitmq");
|
||||
} else {
|
||||
String hostDataPath = ContainerStorageHelper.hostResourcePath(config, "amazonmq", broker.getBrokerId())
|
||||
.toAbsolutePath().toString();
|
||||
if (!containerDetector.isRunningInContainer()) {
|
||||
ContainerStorageHelper.ensureHostDir(hostDataPath);
|
||||
}
|
||||
specBuilder.withBind(hostDataPath, "/var/lib/rabbitmq");
|
||||
}
|
||||
|
||||
ContainerSpec spec = specBuilder.build();
|
||||
info = lifecycleManager.createAndStart(spec);
|
||||
} catch (RuntimeException e) {
|
||||
// Roll back a partially-created container so a failed CreateBroker
|
||||
// does not leave an orphaned container behind.
|
||||
// does not leave an orphaned container behind, and hand the reserved
|
||||
// ports back so the failure does not leak them.
|
||||
lifecycleManager.removeIfExists(containerName);
|
||||
portAllocator.release(ports.amqp());
|
||||
portAllocator.release(ports.console());
|
||||
throw e;
|
||||
}
|
||||
broker.setContainerId(info.containerId());
|
||||
containerIds.put(broker.getBrokerId(), info.containerId());
|
||||
hostPorts.put(broker.getBrokerId(), ports);
|
||||
|
||||
EndpointInfo amqp = info.getEndpoint(AMQP_PORT);
|
||||
EndpointInfo mgmt = info.getEndpoint(MGMT_PORT);
|
||||
@@ -146,8 +169,9 @@ public class RabbitMqManager {
|
||||
List.of("amqp://" + amqp.host() + ":" + amqp.port()),
|
||||
amqp.host());
|
||||
broker.setBrokerInstances(new java.util.ArrayList<>(List.of(instance)));
|
||||
LOG.infov("RabbitMQ container {0} started for broker {1}: amqp={2}",
|
||||
info.containerId(), broker.getBrokerName(), instance.getEndpoints().get(0));
|
||||
LOG.infov("RabbitMQ container {0} started for broker {1}: amqp={2} (host ports amqp={3}, console={4})",
|
||||
info.containerId(), broker.getBrokerName(), instance.getEndpoints().get(0),
|
||||
String.valueOf(ports.amqp()), String.valueOf(ports.console()));
|
||||
|
||||
String shortId = info.containerId().length() >= 8
|
||||
? info.containerId().substring(0, 8)
|
||||
@@ -199,6 +223,7 @@ public class RabbitMqManager {
|
||||
|
||||
public void stopContainer(Broker broker) {
|
||||
containerIds.remove(broker.getBrokerId());
|
||||
releaseHostPorts(broker.getBrokerId());
|
||||
Closeable logHandle = logStreams.remove(broker.getBrokerId());
|
||||
String containerId = broker.getContainerId();
|
||||
if (containerId != null) {
|
||||
@@ -224,6 +249,7 @@ public class RabbitMqManager {
|
||||
}
|
||||
for (String brokerId : new ArrayList<>(containerIds.keySet())) {
|
||||
String containerId = containerIds.remove(brokerId);
|
||||
releaseHostPorts(brokerId);
|
||||
if (containerId == null) {
|
||||
continue;
|
||||
}
|
||||
@@ -232,6 +258,27 @@ public class RabbitMqManager {
|
||||
}
|
||||
}
|
||||
|
||||
private HostPorts allocateHostPorts() {
|
||||
EmulatorConfig.AmazonMqServiceConfig mq = config.services().amazonmq();
|
||||
int amqp = portAllocator.allocate(mq.amqpHostPortBase(), mq.amqpHostPortMax());
|
||||
int console;
|
||||
try {
|
||||
console = portAllocator.allocate(mq.consoleHostPortBase(), mq.consoleHostPortMax());
|
||||
} catch (RuntimeException e) {
|
||||
portAllocator.release(amqp);
|
||||
throw e;
|
||||
}
|
||||
return new HostPorts(amqp, console);
|
||||
}
|
||||
|
||||
private void releaseHostPorts(String brokerId) {
|
||||
HostPorts ports = hostPorts.remove(brokerId);
|
||||
if (ports != null) {
|
||||
portAllocator.release(ports.amqp());
|
||||
portAllocator.release(ports.console());
|
||||
}
|
||||
}
|
||||
|
||||
public void removeBrokerStorage(Broker broker) {
|
||||
ContainerStorageHelper.removeStorage(config, lifecycleManager,
|
||||
"amazonmq", broker.getVolumeId(), broker.getBrokerId());
|
||||
|
||||
Reference in new issue
Block a user