fix: send ElastiCache, Memcached, DocDB and Amazon MQ container logs to the resource's region (#5098)

* fix: send ElastiCache, Memcached, DocDB and Amazon MQ container logs to the resource's region

The container managers attached the CloudWatch Logs stream, and labelled
the container, with the default region, so a cluster or broker created in
another region logged to the default one. The ElastiCache, Memcached and
DocDB managers now take the resource's region: the request's on create, the
one in the resource's ARN on a startup restore. Amazon MQ reads its broker's
ARN. A Memcached cluster records no ARN or region, so a restored one still
logs to the default region.

* fix(elasticache): keep a Memcached cluster's log region across a restart

A Memcached cluster records no ARN, so a restore had no region to give its
container and logged to the default region, although the cluster logged to
its own region until the restart. The cluster record now keeps the region it
was created in (persisted only, not in any response), and the restore uses
it; a record from before this has none and keeps the default. The handler
test now checks the request region is passed on.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-05 00:37:45 -04:00
1 parent 0e89ab962a
commit 539bf15fdd
16 files changed
+174 -133

No files matched your search

@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.amazonmq.container;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
@@ -101,7 +102,7 @@ public class RabbitMqManager {
.withLogRotation()
.withLabels(ContainerStorageHelper.resourceIdentityLabels(
"amazonmq", broker.getBrokerId(), regionResolver.getAccountId(),
regionResolver.getDefaultRegion()));
brokerRegion(broker)));
// Seed the broker's admin user. RabbitMQ's built-in `guest` user is
// loopback-only, so it cannot authenticate over the mapped host port; a user
@@ -177,7 +178,7 @@ public class RabbitMqManager {
: info.containerId();
String logGroup = "/aws/amazonmq/broker/" + broker.getBrokerId();
String logStream = logStreamer.generateLogStreamName(shortId);
String region = regionResolver.getDefaultRegion();
String region = brokerRegion(broker);
Closeable logHandle = logStreamer.attach(
info.containerId(), logGroup, logStream, region, "amazonmq:" + broker.getBrokerId());
if (logHandle != null) {
@@ -185,6 +186,11 @@ public class RabbitMqManager {
}
}
/** The region in the broker's ARN; a broker recorded without one counts as the default region's. */
private String brokerRegion(Broker broker) {
return AwsArnUtils.regionOrDefault(broker.getBrokerArn(), regionResolver.getDefaultRegion());
}
/**
* Ready once the RabbitMQ management UI answers on its port. The management
* plugin starts after the broker core, so a 200 here implies AMQP is also up.
@@ -240,7 +240,8 @@ public class DocDbService {
// A cluster record is metadata: its identifier, ARN and tags need no Docker, so the
// cluster is created and reaches 'available' even when no daemon is reachable. Only
// connecting to the database needs the container.
DocDbContainerHandle handle = containerManager.tryStart(id, image, masterUsername, masterPassword);
DocDbContainerHandle handle = containerManager.tryStart(id, image, masterUsername, masterPassword,
region);
if (handle != null) {
cluster.setEndpoint(handle.getHost());
cluster.setReaderEndpoint(handle.getHost());
@@ -66,9 +66,10 @@ public class DocDbContainerManager {
*
* @return the container handle, or {@code null} when no Docker daemon is reachable
*/
public DocDbContainerHandle tryStart(String clusterId, String image, String masterUsername, String masterPassword) {
public DocDbContainerHandle tryStart(String clusterId, String image, String masterUsername, String masterPassword,
String region) {
try {
DocDbContainerHandle handle = start(clusterId, image, masterUsername, masterPassword);
DocDbContainerHandle handle = start(clusterId, image, masterUsername, masterPassword, region);
dockerUnavailableLogged = false;
return handle;
} catch (RuntimeException e) {
@@ -100,7 +101,8 @@ public class DocDbContainerManager {
}
}
public DocDbContainerHandle start(String clusterId, String image, String masterUsername, String masterPassword) {
public DocDbContainerHandle start(String clusterId, String image, String masterUsername, String masterPassword,
String region) {
LOG.infov("Starting DocumentDB container for cluster: {0}", clusterId);
String containerName = ContainerStorageHelper.resourceName(config, "docdb", null, clusterId);
@@ -117,7 +119,7 @@ public class DocDbContainerManager {
.withLogRotation()
.withEnv(envVars)
.withLabels(ContainerStorageHelper.resourceIdentityLabels(
"docdb", clusterId, regionResolver.getAccountId(), regionResolver.getDefaultRegion()));
"docdb", clusterId, regionResolver.getAccountId(), region));
if (!containerDetector.isRunningInContainer()) {
specBuilder.withDynamicPort(MONGO_PORT);
@@ -141,7 +143,6 @@ public class DocDbContainerManager {
: info.containerId();
String logGroup = "/aws/docdb/cluster/" + clusterId + "/audit";
String logStream = logStreamer.generateLogStreamName(shortId);
String region = regionResolver.getDefaultRegion();
Closeable logHandle = logStreamer.attach(
info.containerId(), logGroup, logStream, region, "docdb:" + clusterId);
@@ -63,7 +63,7 @@ public class ElastiCacheMemcachedService {
new TypeReference<Map<String, ReplicationGroup>>() {});
}
public CacheCluster createCacheCluster(String clusterId) {
public CacheCluster createCacheCluster(String clusterId, String region) {
// Claimed before the store checks rather than after, because no create here or in
// ElastiCacheService persists its record until its container has started: a store check
// that passes is no promise the id is still free by the time this one writes. The claim
@@ -74,13 +74,13 @@ public class ElastiCacheMemcachedService {
"Cache cluster " + clusterId + " is already being created.", 400);
}
try {
return provisionCacheCluster(clusterId);
return provisionCacheCluster(clusterId, region);
} finally {
provisioningIds.release(clusterId);
}
}
private CacheCluster provisionCacheCluster(String clusterId) {
private CacheCluster provisionCacheCluster(String clusterId, String region) {
// Every store that answers DescribeCacheClusters, not just this one: two records sharing
// an id would have one describe report it twice, each with a different engine.
if (clusters.get(clusterId).isPresent()
@@ -96,7 +96,7 @@ public class ElastiCacheMemcachedService {
// A cache cluster record is metadata: its id and endpoint are derived from configuration,
// so the cluster is created and reaches 'available' even when no Docker daemon is
// reachable. Only connecting to the cache needs the container.
ElastiCacheContainerHandle handle = containerManager.tryStart(clusterId, image);
ElastiCacheContainerHandle handle = containerManager.tryStart(clusterId, image, region);
String endpointHost = resolveEndpointHost(handle);
int endpointPort = handle != null ? handle.getPort() : BACKEND_PORT;
@@ -114,6 +114,7 @@ public class ElastiCacheMemcachedService {
+ "is reachable. Metadata operations work; connections to the cache do not "
+ "until a daemon appears.", clusterId);
}
cluster.setRegion(region);
clusters.put(clusterId, cluster);
LOG.infov("Memcached cluster {0} created, endpoint={1}:{2}", clusterId, endpointHost, endpointPort);
@@ -165,7 +166,9 @@ public class ElastiCacheMemcachedService {
String clusterId = cluster.getCacheClusterId();
String image = config.services().elasticache().defaultMemcachedImage();
try {
ElastiCacheContainerHandle handle = containerManager.tryStart(clusterId, image);
// The region the cluster was created in; a record from before it was kept has none and logs
// to the default region.
ElastiCacheContainerHandle handle = containerManager.tryStart(clusterId, image, cluster.getRegion());
synchronized (lockFor(clusterId)) {
if (restoreTargetLost(clusterId)) {
abandonRestoredContainer(clusterId, handle);
@@ -337,7 +337,7 @@ public class ElastiCacheQueryHandler {
try {
CacheCluster cluster = "memcached".equalsIgnoreCase(engine)
? memcachedService.createCacheCluster(clusterId)
? memcachedService.createCacheCluster(clusterId, region)
: service.createCacheCluster(new ElastiCacheService.CreateCacheClusterRequest(
clusterId,
engine,
@@ -273,7 +273,7 @@ public class ElastiCacheService implements ResourceProvider {
// derived from configuration and need no Docker, so the group is created and reaches
// 'available' even when no daemon is reachable. Only connecting to the cache needs
// the container.
handle = containerManager.tryStart(groupId, image);
handle = containerManager.tryStart(groupId, image, request.region());
String endpointHost = resolveEndpointHost();
Endpoint endpoint = new Endpoint(endpointHost, proxyPort);
@@ -359,7 +359,7 @@ public class ElastiCacheService implements ResourceProvider {
inFlightMemberId = node.getMemberClusterId();
ElastiCacheContainerHandle handle = containerManager.start(
node.getMemberClusterId(), image,
clusterNodeFlags(endpointHost, announceIp, node.getProxyPort()));
clusterNodeFlags(endpointHost, announceIp, node.getProxyPort()), request.region());
inFlightMemberId = null;
handles.add(handle);
node.setContainerId(handle.getContainerId());
@@ -694,11 +694,16 @@ public class ElastiCacheService implements ResourceProvider {
* monitor and re-reads the record, because a delete taken while the container started has
* already removed it.
*/
/** The region in a restored resource's ARN; one recorded without an ARN counts as the default region's. */
private String regionOf(String arn) {
return AwsArnUtils.regionOrDefault(arn, regionResolver.getDefaultRegion());
}
private void restoreCacheCluster(CacheCluster cluster) {
String clusterId = cluster.getCacheClusterId();
String image = config.services().elasticache().defaultImage();
try {
ElastiCacheContainerHandle handle = containerManager.tryStart(clusterId, image);
ElastiCacheContainerHandle handle = containerManager.tryStart(clusterId, image, regionOf(cluster.getArn()));
synchronized (lockFor("cc:" + clusterId)) {
if (cacheClusterRestoreTargetLost(clusterId)) {
abandonRestoredCacheClusterContainer(clusterId, handle);
@@ -827,7 +832,7 @@ public class ElastiCacheService implements ResourceProvider {
String groupId = group.getReplicationGroupId();
String image = config.services().elasticache().defaultImage();
try {
ElastiCacheContainerHandle handle = containerManager.tryStart(groupId, image);
ElastiCacheContainerHandle handle = containerManager.tryStart(groupId, image, regionOf(group.getArn()));
synchronized (lockFor("rg:" + groupId)) {
if (restoreTargetLost(groupId)) {
abandonRestoredContainer(groupId, handle);
@@ -963,7 +968,7 @@ public class ElastiCacheService implements ResourceProvider {
inFlightMemberId = node.getMemberClusterId();
ElastiCacheContainerHandle handle = containerManager.start(
node.getMemberClusterId(), image,
clusterNodeFlags(endpointHost, announceIp, node.getProxyPort()));
clusterNodeFlags(endpointHost, announceIp, node.getProxyPort()), regionOf(group.getArn()));
inFlightMemberId = null;
handles.add(handle);
node.setContainerId(handle.getContainerId());
@@ -1249,7 +1254,7 @@ public class ElastiCacheService implements ResourceProvider {
// As for a replication group, the record is metadata: it reaches 'available' even
// when no Docker daemon is reachable, and only connecting to the cache needs the
// container.
handle = containerManager.tryStart(clusterId, image);
handle = containerManager.tryStart(clusterId, image, request.region());
CacheCluster cluster = new CacheCluster(clusterId, CacheClusterStatus.AVAILABLE, engine,
request.engineVersion() != null && !request.engineVersion().isBlank()
@@ -79,9 +79,9 @@ public class ElastiCacheContainerManager {
* @return the container handle, or {@code null} when no Docker daemon is reachable and no
* container was created
*/
public ElastiCacheContainerHandle tryStart(String groupId, String image) {
public ElastiCacheContainerHandle tryStart(String groupId, String image, String region) {
try {
ElastiCacheContainerHandle handle = start(groupId, image);
ElastiCacheContainerHandle handle = start(groupId, image, region);
dockerUnavailableLogged = false;
return handle;
} catch (RuntimeException e) {
@@ -118,8 +118,8 @@ public class ElastiCacheContainerManager {
}
}
public ElastiCacheContainerHandle start(String groupId, String image) {
return start(groupId, image, List.of());
public ElastiCacheContainerHandle start(String groupId, String image, String region) {
return start(groupId, image, List.of(), region);
}
/**
@@ -127,7 +127,8 @@ public class ElastiCacheContainerManager {
* to the Valkey server command line (via the image's {@code VALKEY_EXTRA_FLAGS} hook).
* Cluster-mode nodes use this to pass {@code --cluster-enabled} and announce settings.
*/
public ElastiCacheContainerHandle start(String groupId, String image, List<String> extraServerFlags) {
public ElastiCacheContainerHandle start(String groupId, String image, List<String> extraServerFlags,
String region) {
LOG.infov("Starting ElastiCache backend container for group: {0}", groupId);
String containerName = containerName(groupId);
@@ -149,7 +150,7 @@ public class ElastiCacheContainerManager {
.withDockerNetwork(config.services().elasticache().dockerNetwork())
.withLogRotation()
.withLabels(ContainerStorageHelper.resourceIdentityLabels(
"elasticache", groupId, regionResolver.getAccountId(), regionResolver.getDefaultRegion()));
"elasticache", groupId, regionResolver.getAccountId(), region));
if (!containerDetector.isRunningInContainer()) {
specBuilder.withDynamicPort(BACKEND_PORT);
@@ -182,7 +183,6 @@ public class ElastiCacheContainerManager {
: info.containerId();
String logGroup = "/aws/elasticache/cluster/" + groupId + "/engine-log";
String logStream = logStreamer.generateLogStreamName(shortId);
String region = regionResolver.getDefaultRegion();
Closeable logHandle = logStreamer.attach(
info.containerId(), logGroup, logStream, region, "elasticache:" + groupId);
@@ -76,9 +76,9 @@ public class ElastiCacheMemcachedContainerManager {
* @return the container handle, or {@code null} when no Docker daemon is reachable and no
* container was created
*/
public ElastiCacheContainerHandle tryStart(String clusterId, String image) {
public ElastiCacheContainerHandle tryStart(String clusterId, String image, String region) {
try {
ElastiCacheContainerHandle handle = start(clusterId, image);
ElastiCacheContainerHandle handle = start(clusterId, image, region);
dockerUnavailableLogged = false;
return handle;
} catch (RuntimeException e) {
@@ -115,7 +115,9 @@ public class ElastiCacheMemcachedContainerManager {
}
}
public ElastiCacheContainerHandle start(String clusterId, String image) {
/** {@code region} is null for a cluster recorded without one, which then counts as the default region's. */
public ElastiCacheContainerHandle start(String clusterId, String image, String region) {
String effectiveRegion = region != null ? region : regionResolver.getDefaultRegion();
LOG.infov("Starting Memcached container for cluster: {0}", clusterId);
String containerName = ContainerStorageHelper.resourceName(config, "memcached", null, clusterId);
@@ -126,7 +128,7 @@ public class ElastiCacheMemcachedContainerManager {
.withDockerNetwork(config.services().elasticache().dockerNetwork())
.withLogRotation()
.withLabels(ContainerStorageHelper.resourceIdentityLabels(
"elasticache", clusterId, regionResolver.getAccountId(), regionResolver.getDefaultRegion()));
"elasticache", clusterId, regionResolver.getAccountId(), effectiveRegion));
if (!containerDetector.isRunningInContainer()) {
specBuilder.withDynamicPort(BACKEND_PORT);
@@ -149,10 +151,9 @@ public class ElastiCacheMemcachedContainerManager {
: info.containerId();
String logGroup = "/aws/elasticache/cluster/" + clusterId + "/engine-log";
String logStream = logStreamer.generateLogStreamName(shortId);
String region = regionResolver.getDefaultRegion();
Closeable logHandle = logStreamer.attach(
info.containerId(), logGroup, logStream, region, "elasticache-memcached:" + clusterId);
info.containerId(), logGroup, logStream, effectiveRegion, "elasticache-memcached:" + clusterId);
handle.setLogStream(logHandle);
waitForBackendReady(clusterId, endpoint.host(), endpoint.port());
@@ -20,6 +20,8 @@ public class CacheCluster {
private String cacheNodeType;
private int numCacheNodes;
private String arn;
// Persisted only: a Memcached cluster carries no ARN, so its region is kept here for a restart.
private String region;
private String cacheParameterGroupName;
private String cacheSubnetGroupName;
private AuthMode authMode;
@@ -79,6 +81,9 @@ public class CacheCluster {
public String getArn() { return arn; }
public void setArn(String arn) { this.arn = arn; }
public String getRegion() { return region; }
public void setRegion(String region) { this.region = region; }
public String getCacheParameterGroupName() { return cacheParameterGroupName; }
public void setCacheParameterGroupName(String cacheParameterGroupName) { this.cacheParameterGroupName = cacheParameterGroupName; }