mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 17:33:03 -05:00
fix(elasticache): report a memcached cluster's node count, type, ARN and nodes (#5000)
* fix(elasticache): report a memcached cluster's node count, type, ARN and nodes CreateCacheCluster with Engine=memcached kept only the cluster id, so DescribeCacheClusters answered with id, status, engine, version and ConfigurationEndpoint alone. terraform's aws_elasticache_cluster reads num_cache_nodes, node_type, parameter_group_name, subnet_group_name, security_group_ids, arn and cache_nodes back from that describe, so every memcached cluster planned a diff no apply could settle. The memcached create now takes the same CreateCacheClusterRequest the redis path does and stores NumCacheNodes (default 1, below 1 refused), CacheNodeType (default cache.t4g.micro, as for redis), the parameter and subnet group names, security group ids, PreferredAvailabilityZone and an ARN. The describe emits them for memcached as it does for redis and, with ShowCacheNodeInfo, one CacheNode per node (0001..000N), each reporting the cluster endpoint since one container serves them all. A record persisted before node counts were stored reports one node. Redis output is unchanged. * fix(elasticache): cap a memcached cluster at 40 nodes A create with NumCacheNodes above 40 now fails with NodeQuotaForClusterExceeded (400), as AWS does, instead of storing the count and listing that many nodes on a ShowCacheNodeInfo describe.
This commit is contained in:
1 parent
7d9e38cf56
commit
7a1f9dd826
6 files changed
+280
-71
No files matched your search
+43
-6
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.elasticache;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerHandle;
|
||||
@@ -31,6 +32,9 @@ public class ElastiCacheMemcachedService {
|
||||
private static final String ENGINE_VERSION = "1.6.22";
|
||||
/** Memcached's well-known port, used as the endpoint port when no backing container exists. */
|
||||
private static final int BACKEND_PORT = 11211;
|
||||
/** The node type a create that names none is reported with, the same as for redis. */
|
||||
private static final String DEFAULT_CACHE_NODE_TYPE = "cache.t4g.micro";
|
||||
private static final int MAX_CACHE_NODES = 40;
|
||||
|
||||
private final StorageBackend<String, CacheCluster> clusters;
|
||||
/**
|
||||
@@ -45,14 +49,17 @@ public class ElastiCacheMemcachedService {
|
||||
private final EmulatorConfig config;
|
||||
/** Shared with {@link ElastiCacheService}: one namespace, one set of in-flight claims. */
|
||||
private final ElastiCacheProvisioningIds provisioningIds;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ConcurrentHashMap<String, Object> clusterLocks = new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
public ElastiCacheMemcachedService(ElastiCacheMemcachedContainerManager containerManager,
|
||||
StorageFactory storageFactory,
|
||||
EmulatorConfig config,
|
||||
ElastiCacheProvisioningIds provisioningIds) {
|
||||
ElastiCacheProvisioningIds provisioningIds,
|
||||
RegionResolver regionResolver) {
|
||||
this.containerManager = containerManager;
|
||||
this.regionResolver = regionResolver;
|
||||
this.config = config;
|
||||
this.provisioningIds = provisioningIds;
|
||||
this.clusters = storageFactory.create("elasticache", "elasticache-cache-clusters.json",
|
||||
@@ -63,7 +70,23 @@ public class ElastiCacheMemcachedService {
|
||||
new TypeReference<Map<String, ReplicationGroup>>() {});
|
||||
}
|
||||
|
||||
public CacheCluster createCacheCluster(String clusterId, String region) {
|
||||
/**
|
||||
* Creates a Memcached cluster from the same request a redis cluster is created from. Only
|
||||
* the members a Memcached cluster has are read; the rest are redis-only and ignored.
|
||||
*/
|
||||
public CacheCluster createCacheCluster(ElastiCacheService.CreateCacheClusterRequest request) {
|
||||
String clusterId = request.cacheClusterId();
|
||||
if (request.numCacheNodes() != null && request.numCacheNodes() < 1) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"NumCacheNodes must be at least 1.", 400);
|
||||
}
|
||||
// AWS caps a Memcached cluster at 40 nodes; without the cap a describe with
|
||||
// ShowCacheNodeInfo would list however many nodes the create asked for.
|
||||
if (request.numCacheNodes() != null && request.numCacheNodes() > MAX_CACHE_NODES) {
|
||||
throw new AwsException("NodeQuotaForClusterExceeded",
|
||||
"The request cannot be processed because it would exceed the allowed number "
|
||||
+ "of cache nodes in a single cluster.", 400);
|
||||
}
|
||||
// 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 +97,14 @@ public class ElastiCacheMemcachedService {
|
||||
"Cache cluster " + clusterId + " is already being created.", 400);
|
||||
}
|
||||
try {
|
||||
return provisionCacheCluster(clusterId, region);
|
||||
return provisionCacheCluster(request);
|
||||
} finally {
|
||||
provisioningIds.release(clusterId);
|
||||
}
|
||||
}
|
||||
|
||||
private CacheCluster provisionCacheCluster(String clusterId, String region) {
|
||||
private CacheCluster provisionCacheCluster(ElastiCacheService.CreateCacheClusterRequest request) {
|
||||
String clusterId = request.cacheClusterId();
|
||||
// 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 +120,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, region);
|
||||
ElastiCacheContainerHandle handle = containerManager.tryStart(clusterId, image, request.region());
|
||||
|
||||
String endpointHost = resolveEndpointHost(handle);
|
||||
int endpointPort = handle != null ? handle.getPort() : BACKEND_PORT;
|
||||
@@ -105,6 +129,19 @@ public class ElastiCacheMemcachedService {
|
||||
CacheCluster cluster = new CacheCluster(
|
||||
clusterId, CacheClusterStatus.AVAILABLE, ENGINE, ENGINE_VERSION,
|
||||
endpoint, Instant.now());
|
||||
// What terraform's aws_elasticache_cluster reads back: anything the request set and the
|
||||
// describe omitted would be a diff no apply can settle.
|
||||
cluster.setNumCacheNodes(request.numCacheNodes() != null ? request.numCacheNodes() : 1);
|
||||
cluster.setCacheNodeType(request.cacheNodeType() != null && !request.cacheNodeType().isBlank()
|
||||
? request.cacheNodeType() : DEFAULT_CACHE_NODE_TYPE);
|
||||
cluster.setCacheParameterGroupName(request.cacheParameterGroupName());
|
||||
cluster.setCacheSubnetGroupName(request.cacheSubnetGroupName());
|
||||
cluster.setSecurityGroupIds(request.securityGroupIds() != null
|
||||
? new ArrayList<>(request.securityGroupIds()) : null);
|
||||
cluster.setPreferredAvailabilityZone(request.preferredAvailabilityZone() != null
|
||||
&& !request.preferredAvailabilityZone().isBlank()
|
||||
? request.preferredAvailabilityZone() : regionResolver.getRegion() + "a");
|
||||
cluster.setArn(regionResolver.buildArn("elasticache", request.region(), "cluster:" + clusterId));
|
||||
if (handle != null) {
|
||||
cluster.setContainerId(handle.getContainerId());
|
||||
cluster.setContainerHost(handle.getHost());
|
||||
@@ -114,7 +151,7 @@ public class ElastiCacheMemcachedService {
|
||||
+ "is reachable. Metadata operations work; connections to the cache do not "
|
||||
+ "until a daemon appears.", clusterId);
|
||||
}
|
||||
cluster.setRegion(region);
|
||||
cluster.setRegion(request.region());
|
||||
|
||||
clusters.put(clusterId, cluster);
|
||||
LOG.infov("Memcached cluster {0} created, endpoint={1}:{2}", clusterId, endpointHost, endpointPort);
|
||||
|
||||
+45
-35
@@ -336,9 +336,7 @@ public class ElastiCacheQueryHandler {
|
||||
}
|
||||
|
||||
try {
|
||||
CacheCluster cluster = "memcached".equalsIgnoreCase(engine)
|
||||
? memcachedService.createCacheCluster(clusterId, region)
|
||||
: service.createCacheCluster(new ElastiCacheService.CreateCacheClusterRequest(
|
||||
ElastiCacheService.CreateCacheClusterRequest request = new ElastiCacheService.CreateCacheClusterRequest(
|
||||
clusterId,
|
||||
engine,
|
||||
params.getFirst("EngineVersion"),
|
||||
@@ -358,7 +356,10 @@ public class ElastiCacheQueryHandler {
|
||||
params.getFirst("IpDiscovery"),
|
||||
boolParam(params, "AtRestEncryptionEnabled"),
|
||||
region,
|
||||
parseTags(params)));
|
||||
parseTags(params));
|
||||
CacheCluster cluster = "memcached".equalsIgnoreCase(engine)
|
||||
? memcachedService.createCacheCluster(request)
|
||||
: service.createCacheCluster(request);
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateCacheCluster", AwsNamespaces.EC,
|
||||
cacheClusterXml(cluster, false))).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -826,26 +827,31 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>
|
||||
.elem("CacheClusterStatus", c.getCacheClusterStatus().wireName())
|
||||
.elem("Engine", c.getEngine())
|
||||
.elem("EngineVersion", c.getEngineVersion());
|
||||
if ("memcached".equals(c.getEngine())) {
|
||||
boolean memcached = "memcached".equals(c.getEngine());
|
||||
int numCacheNodes = c.getNumCacheNodes();
|
||||
if (memcached) {
|
||||
if (ep != null) {
|
||||
xml.start("ConfigurationEndpoint")
|
||||
.elem("Address", ep.address())
|
||||
.elem("Port", (long) ep.port())
|
||||
.end("ConfigurationEndpoint");
|
||||
}
|
||||
return xml.end("CacheCluster").build();
|
||||
// A record persisted before node counts were stored reads back 0; the cluster it
|
||||
// describes was created with AWS's default of one node.
|
||||
numCacheNodes = Math.max(1, numCacheNodes);
|
||||
xml.elem("NumCacheNodes", (long) numCacheNodes);
|
||||
} else {
|
||||
xml.elem("NumCacheNodes", (long) numCacheNodes)
|
||||
.elem("AutoMinorVersionUpgrade", true)
|
||||
.elem("AuthTokenEnabled", c.getAuthMode() == AuthMode.PASSWORD)
|
||||
.elem("TransitEncryptionEnabled", c.getAuthMode() != null && c.getAuthMode() != AuthMode.NO_AUTH)
|
||||
.elem("AtRestEncryptionEnabled", c.isAtRestEncryptionEnabled())
|
||||
.elem("SnapshotRetentionLimit", (long) c.getSnapshotRetentionLimit())
|
||||
.elem("SnapshotWindow", c.getSnapshotWindow() != null
|
||||
? c.getSnapshotWindow() : ReplicationGroupSettings.DEFAULT_SNAPSHOT_WINDOW)
|
||||
.elem("PreferredMaintenanceWindow", c.getPreferredMaintenanceWindow() != null
|
||||
? c.getPreferredMaintenanceWindow() : BackupWindows.DEFAULT_MAINTENANCE_WINDOW);
|
||||
}
|
||||
|
||||
xml.elem("NumCacheNodes", (long) c.getNumCacheNodes())
|
||||
.elem("AutoMinorVersionUpgrade", true)
|
||||
.elem("AuthTokenEnabled", c.getAuthMode() == AuthMode.PASSWORD)
|
||||
.elem("TransitEncryptionEnabled", c.getAuthMode() != null && c.getAuthMode() != AuthMode.NO_AUTH)
|
||||
.elem("AtRestEncryptionEnabled", c.isAtRestEncryptionEnabled())
|
||||
.elem("SnapshotRetentionLimit", (long) c.getSnapshotRetentionLimit())
|
||||
.elem("SnapshotWindow", c.getSnapshotWindow() != null
|
||||
? c.getSnapshotWindow() : ReplicationGroupSettings.DEFAULT_SNAPSHOT_WINDOW)
|
||||
.elem("PreferredMaintenanceWindow", c.getPreferredMaintenanceWindow() != null
|
||||
? c.getPreferredMaintenanceWindow() : BackupWindows.DEFAULT_MAINTENANCE_WINDOW);
|
||||
if (c.getCacheClusterCreateTime() != null) {
|
||||
xml.elem("CacheClusterCreateTime", c.getCacheClusterCreateTime().toString());
|
||||
}
|
||||
@@ -888,25 +894,29 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>
|
||||
// without a nil check while adopting the resource, so a node carrying only an
|
||||
// endpoint fails the refresh right after a successful create. Both are stable
|
||||
// metadata AWS returns on every available node, and both are already known here:
|
||||
// a single-node cluster's node was created with the cluster, and its parameter
|
||||
// group has the same in-sync status reported for the cluster above.
|
||||
xml.start("CacheNodes")
|
||||
.start("CacheNode")
|
||||
.elem("CacheNodeId", "0001")
|
||||
.elem("CacheNodeStatus", "available");
|
||||
if (c.getCacheClusterCreateTime() != null) {
|
||||
xml.elem("CacheNodeCreateTime", c.getCacheClusterCreateTime().toString());
|
||||
// a node was created with its cluster, and its parameter group has the same in-sync
|
||||
// status reported for the cluster above. A redis cluster has one node; a Memcached
|
||||
// cluster has NumCacheNodes, all served by its one container, so every node reports
|
||||
// the cluster's endpoint.
|
||||
xml.start("CacheNodes");
|
||||
for (int node = 1; node <= (memcached ? numCacheNodes : 1); node++) {
|
||||
xml.start("CacheNode")
|
||||
.elem("CacheNodeId", String.format("%04d", node))
|
||||
.elem("CacheNodeStatus", "available");
|
||||
if (c.getCacheClusterCreateTime() != null) {
|
||||
xml.elem("CacheNodeCreateTime", c.getCacheClusterCreateTime().toString());
|
||||
}
|
||||
xml.start("Endpoint")
|
||||
.elem("Address", ep.address())
|
||||
.elem("Port", (long) ep.port())
|
||||
.end("Endpoint")
|
||||
.elem("ParameterGroupStatus", "in-sync");
|
||||
if (c.getPreferredAvailabilityZone() != null) {
|
||||
xml.elem("CustomerAvailabilityZone", c.getPreferredAvailabilityZone());
|
||||
}
|
||||
xml.end("CacheNode");
|
||||
}
|
||||
xml.start("Endpoint")
|
||||
.elem("Address", ep.address())
|
||||
.elem("Port", (long) ep.port())
|
||||
.end("Endpoint")
|
||||
.elem("ParameterGroupStatus", "in-sync");
|
||||
if (c.getPreferredAvailabilityZone() != null) {
|
||||
xml.elem("CustomerAvailabilityZone", c.getPreferredAvailabilityZone());
|
||||
}
|
||||
xml.end("CacheNode")
|
||||
.end("CacheNodes");
|
||||
xml.end("CacheNodes");
|
||||
}
|
||||
return xml.end("CacheCluster").build();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user