mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(elasticache): honor the requested Port on CreateReplicationGroup (#3276)
* fix(elasticache): honor the requested Port on CreateReplicationGroup
Port is a modeled optional input on CreateReplicationGroup ("the port number
on which each member of the replication group accepts connections"), but
allocateProxyPort took no argument and always handed out the next free port
from the base of the range. A caller that pinned a port read back a different
one, which Terraform reports as a replacement-forcing change and therefore as
permanent drift.
allocateProxyPort now takes the requested port and honors it when it is free
and inside the proxy range, matching NeptuneService.allocateProxyPort. Floci
multiplexes every group's proxy onto one host, so two groups genuinely cannot
share a port; when the requested one is taken or out of range this logs why
and falls back to the next free port rather than failing the create. In
cluster mode only the first node can take the requested port, because the
group reports its port from that node.
The compatibility fixtures pinned 6379, the bottom of the range and the port
floci would have allocated anyway, so they asserted nothing and passed by
coincidence. They now pin 6395, which fails without this fix.
* fix(elasticache): refuse a requested Port that cannot be honored
Honoring Port left one path that quietly ignored it: a requested port that was
already held by another replication group, or outside the range this emulator
serves, fell back to the next free one. The describe then reported the allocated
port, so a caller who pinned one still read back a different value and still saw
the replacement-forcing drift that honoring Port exists to remove. The narrower
case reproduced the bug the change was written to fix.
The fallback could only ever hit a caller who did ask for a port, since one who
does not care passes no Port at all and never reaches that branch, so it served
nobody. An explicit port is now either honored or refused with
InvalidParameterValue, the sender fault CreateReplicationGroup declares, naming
the port and the reason so the caller can pick another. Unpinned creates still
scan the range exactly as before, and the restore path for persisted groups is
unaffected because it asks for no particular port.
The two tests that pinned the old fallback are inverted rather than dropped, so
the new contract is held just as firmly, and one more proves an unpinned create
still falls back when the base port is taken.
NeptuneService.allocateProxyPort carries the same substitution and RdsService
ignores a requested port entirely; both are left for their own changes.
Raised by the review on this PR.
This commit is contained in:
1 parent
d322d48bd5
commit
67198c7064
6 files changed
+173
-7
No files matched your search
@@ -287,7 +287,11 @@ resource "aws_elasticache_replication_group" "compat" {
|
||||
engine = "redis"
|
||||
node_type = "cache.t3.micro"
|
||||
num_cache_clusters = 1
|
||||
port = 6379
|
||||
# Deliberately not 6379: that is the bottom of floci's ElastiCache proxy range and the
|
||||
# port it would hand out anyway, so pinning it asserts nothing. A non-default port is
|
||||
# what actually exercises CreateReplicationGroup's Port input, and a mismatch shows up
|
||||
# as permanent drift because Terraform treats the port as replacement-forcing.
|
||||
port = 6395
|
||||
}
|
||||
|
||||
# ── Firehose Delivery Stream ────────────────────────────────────────────────
|
||||
|
||||
@@ -287,7 +287,11 @@ resource "aws_elasticache_replication_group" "compat" {
|
||||
engine = "redis"
|
||||
node_type = "cache.t3.micro"
|
||||
num_cache_clusters = 1
|
||||
port = 6379
|
||||
# Deliberately not 6379: that is the bottom of floci's ElastiCache proxy range and the
|
||||
# port it would hand out anyway, so pinning it asserts nothing. A non-default port is
|
||||
# what actually exercises CreateReplicationGroup's Port input, and a mismatch shows up
|
||||
# as permanent drift because Terraform treats the port as replacement-forcing.
|
||||
port = 6395
|
||||
}
|
||||
|
||||
# -- Firehose Delivery Stream --------------------------------------------------
|
||||
|
||||
+1
@@ -125,6 +125,7 @@ public class ElastiCacheQueryHandler {
|
||||
intParam(params, "NumCacheClusters"),
|
||||
boolParam(params, "AutomaticFailoverEnabled"),
|
||||
boolParam(params, "MultiAZEnabled"),
|
||||
intParam(params, "Port"),
|
||||
replicationGroupSettings(params),
|
||||
parseTags(params)));
|
||||
String result = replicationGroupXml(group);
|
||||
|
||||
+44
-3
@@ -126,6 +126,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
Integer numCacheClusters,
|
||||
Boolean automaticFailoverEnabled,
|
||||
Boolean multiAzEnabled,
|
||||
Integer port,
|
||||
ReplicationGroupSettings settings,
|
||||
Map<String, String> tags) {
|
||||
}
|
||||
@@ -142,7 +143,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
Map<String, String> tags) {
|
||||
return createReplicationGroup(new CreateReplicationGroupRequest(groupId, description,
|
||||
authMode, authToken, region, null, null, null, null, null, null,
|
||||
null, null, null, null, null, settings, tags));
|
||||
null, null, null, null, null, null, settings, tags));
|
||||
}
|
||||
|
||||
public ReplicationGroup createReplicationGroup(CreateReplicationGroupRequest request) {
|
||||
@@ -221,7 +222,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
ReplicationGroupSettings resolvedSettings) {
|
||||
String groupId = request.replicationGroupId();
|
||||
AuthMode authMode = request.authMode();
|
||||
int proxyPort = allocateProxyPort();
|
||||
int proxyPort = allocateProxyPort(request.port());
|
||||
String image = config.services().elasticache().defaultImage();
|
||||
|
||||
LOG.infov("Creating replication group {0} with authMode={1} on proxy port {2}",
|
||||
@@ -304,8 +305,11 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
int[] slots = ValkeyClusterFormation.slotRange(shard, numNodeGroups);
|
||||
for (int member = 0; member <= replicasPerNodeGroup; member++) {
|
||||
String memberId = groupId + "-" + nodeGroupId + "-" + String.format("%03d", member + 1);
|
||||
// The group's Port is reported from the first node's proxy port, so only that
|
||||
// node can honor a requested port; the rest take whatever is free.
|
||||
Integer requestedPort = nodes.isEmpty() ? request.port() : null;
|
||||
nodes.add(new ClusterNode(memberId, nodeGroupId, member == 0,
|
||||
allocateProxyPort(), slots[0] + "-" + slots[1]));
|
||||
allocateProxyPort(requestedPort), slots[0] + "-" + slots[1]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -917,8 +921,45 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
}
|
||||
|
||||
private int allocateProxyPort() {
|
||||
return allocateProxyPort(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Honors the request's {@code Port} when it is free and inside the proxy range. AWS models
|
||||
* Port as an optional input on CreateReplicationGroup ("the port number on which each member
|
||||
* of the replication group accepts connections"), so a caller that pins one and reads back a
|
||||
* different value sees permanent drift: Terraform treats the port as replacement-forcing.
|
||||
*
|
||||
* <p>An explicit port is therefore either honored or refused, never quietly changed.
|
||||
* Substituting one reproduces the very drift honoring it was meant to remove, and the
|
||||
* substitution could only ever hit a caller who did ask for a port: one who does not care
|
||||
* passes null and never reaches that branch. Floci multiplexes every group's proxy onto one
|
||||
* host, so two groups genuinely cannot share a port, and a caller who pinned an unavailable
|
||||
* one needs to know rather than discover it as drift later.
|
||||
*
|
||||
* <p>Only an unpinned create falls back through the range below.
|
||||
* {@code NeptuneService.allocateProxyPort} still substitutes on this path and carries the
|
||||
* same flaw.
|
||||
*/
|
||||
private int allocateProxyPort(Integer requested) {
|
||||
int base = config.services().elasticache().proxyBasePort();
|
||||
int max = config.services().elasticache().proxyMaxPort();
|
||||
if (requested != null) {
|
||||
if (requested < base || requested > max) {
|
||||
LOG.infov("Rejecting ElastiCache port {0}: outside the proxy range {1}-{2}",
|
||||
String.valueOf(requested), String.valueOf(base), String.valueOf(max));
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Port " + requested + " is outside the port range this emulator serves ("
|
||||
+ base + "-" + max + ").", 400);
|
||||
}
|
||||
if (!usedPorts.add(requested)) {
|
||||
LOG.infov("Rejecting ElastiCache port {0}: already used by another replication group",
|
||||
String.valueOf(requested));
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Port " + requested + " is already in use by another replication group.", 400);
|
||||
}
|
||||
return requested;
|
||||
}
|
||||
for (int port = base; port <= max; port++) {
|
||||
if (usedPorts.add(port)) {
|
||||
return port;
|
||||
|
||||
+37
@@ -19,6 +19,7 @@ import io.github.hectorvent.floci.services.elasticache.model.Endpoint;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -238,6 +239,42 @@ class ElastiCacheQueryHandlerTest {
|
||||
assertEquals(Map.of("Name", "g1"), request.tags());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createReplicationGroup_passesTheRequestedPortToService() {
|
||||
// Port is a modeled optional input on CreateReplicationGroup; the handler dropped it,
|
||||
// so a caller that pinned one always read back whatever port floci allocated.
|
||||
when(service.createReplicationGroup(any(ElastiCacheService.CreateReplicationGroupRequest.class)))
|
||||
.thenReturn(group("g1"));
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("ReplicationGroupId", "g1");
|
||||
p.add("ReplicationGroupDescription", "d");
|
||||
p.add("Port", "6395");
|
||||
|
||||
assertEquals(200, handler.handle("CreateReplicationGroup", p, "us-east-1").getStatus());
|
||||
|
||||
ArgumentCaptor<ElastiCacheService.CreateReplicationGroupRequest> captor =
|
||||
ArgumentCaptor.forClass(ElastiCacheService.CreateReplicationGroupRequest.class);
|
||||
verify(service).createReplicationGroup(captor.capture());
|
||||
assertEquals(6395, captor.getValue().port());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createReplicationGroup_leavesPortNullWhenAbsent() {
|
||||
when(service.createReplicationGroup(any(ElastiCacheService.CreateReplicationGroupRequest.class)))
|
||||
.thenReturn(group("g1"));
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("ReplicationGroupId", "g1");
|
||||
p.add("ReplicationGroupDescription", "d");
|
||||
|
||||
assertEquals(200, handler.handle("CreateReplicationGroup", p, "us-east-1").getStatus());
|
||||
|
||||
ArgumentCaptor<ElastiCacheService.CreateReplicationGroupRequest> captor =
|
||||
ArgumentCaptor.forClass(ElastiCacheService.CreateReplicationGroupRequest.class);
|
||||
verify(service).createReplicationGroup(captor.capture());
|
||||
assertNull(captor.getValue().port(),
|
||||
"An absent Port must stay null so the service allocates as before");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createReplicationGroup_readsTheEncryptionFlagAsAwsDoes() {
|
||||
when(service.createReplicationGroup(any(ElastiCacheService.CreateReplicationGroupRequest.class)))
|
||||
|
||||
+81
-2
@@ -226,7 +226,86 @@ class ElastiCacheServiceTest {
|
||||
return new ElastiCacheService.CreateReplicationGroupRequest(groupId, "test",
|
||||
AuthMode.NO_AUTH, null, "us-east-1", "valkey", "8.2", "cache.t4g.micro",
|
||||
"default.valkey8.cluster.on", null, null, numNodeGroups, replicasPerNodeGroup,
|
||||
null, true, null, ReplicationGroupSettings.defaults(), Map.of());
|
||||
null, true, null, null, ReplicationGroupSettings.defaults(), Map.of());
|
||||
}
|
||||
|
||||
private static ElastiCacheService.CreateReplicationGroupRequest singleNodeRequest(
|
||||
String groupId, Integer port) {
|
||||
return new ElastiCacheService.CreateReplicationGroupRequest(groupId, "test",
|
||||
AuthMode.NO_AUTH, null, "us-east-1", null, null, null, null, null, null,
|
||||
null, null, null, null, null, port, ReplicationGroupSettings.defaults(), Map.of());
|
||||
}
|
||||
|
||||
// botocore models Port as an optional input on CreateReplicationGroup ("the port number on
|
||||
// which each member of the replication group accepts connections"). Ignoring it made every
|
||||
// caller that pins a port read back a different one, which Terraform treats as
|
||||
// replacement-forcing and reports as permanent drift.
|
||||
@Test
|
||||
void requestedPortIsHonoredWhenFreeAndInRange() {
|
||||
ReplicationGroup group = service.createReplicationGroup(singleNodeRequest("grp", 16390));
|
||||
|
||||
assertEquals(16390, group.getProxyPort(),
|
||||
"A free, in-range requested Port must be the port the group reports");
|
||||
}
|
||||
|
||||
@Test
|
||||
void unpinnedCreateStillAllocatesFromTheBasePort() {
|
||||
ReplicationGroup group = service.createReplicationGroup(singleNodeRequest("grp", null));
|
||||
|
||||
assertEquals(16379, group.getProxyPort(),
|
||||
"A create with no Port keeps the previous behavior of taking the base port");
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestedPortAlreadyInUseIsRejected() {
|
||||
// floci multiplexes every group's proxy onto one host, so two groups cannot share a port.
|
||||
// Substituting a different one would hand back the drift honoring Port exists to remove,
|
||||
// and it could only ever hit a caller who did pin a port.
|
||||
service.createReplicationGroup(singleNodeRequest("grp1", 16390));
|
||||
|
||||
AwsException thrown = assertThrows(AwsException.class,
|
||||
() -> service.createReplicationGroup(singleNodeRequest("grp2", 16390)));
|
||||
|
||||
assertEquals("InvalidParameterValue", thrown.getErrorCode());
|
||||
assertTrue(thrown.getMessage().contains("16390"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestedPortOutsideTheProxyRangeIsRejected() {
|
||||
AwsException thrown = assertThrows(AwsException.class,
|
||||
() -> service.createReplicationGroup(singleNodeRequest("grp", 9999)));
|
||||
|
||||
assertEquals("InvalidParameterValue", thrown.getErrorCode());
|
||||
assertTrue(thrown.getMessage().contains("9999"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnpinnedCreateStillFallsBackWhenTheBasePortIsTaken() {
|
||||
// The fallback survives for callers that named no port: only an explicit one is refused.
|
||||
service.createReplicationGroup(singleNodeRequest("grp1", 16379));
|
||||
|
||||
ReplicationGroup second = service.createReplicationGroup(singleNodeRequest("grp2", null));
|
||||
|
||||
assertEquals(16380, second.getProxyPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusterModeHonorsTheRequestedPortOnTheFirstNode() {
|
||||
// The group's Port is reported from the first node's proxy port, so that is the only
|
||||
// node whose port a caller can pin; the remaining members take whatever is free.
|
||||
stubPerNodeContainers();
|
||||
|
||||
ReplicationGroup group = service.createReplicationGroup(
|
||||
new ElastiCacheService.CreateReplicationGroupRequest("grp", "test",
|
||||
AuthMode.NO_AUTH, null, "us-east-1", "valkey", "8.2", "cache.t4g.micro",
|
||||
"default.valkey8.cluster.on", null, null, 2, 1,
|
||||
null, true, null, 16390, ReplicationGroupSettings.defaults(), Map.of()));
|
||||
|
||||
assertEquals(16390, group.getConfigurationEndpoint().port());
|
||||
assertEquals(16390, group.getClusterNodes().getFirst().getProxyPort());
|
||||
assertEquals(4, group.getClusterNodes().stream()
|
||||
.map(ClusterNode::getProxyPort).distinct().count(),
|
||||
"Each node must still own its own proxy port");
|
||||
}
|
||||
|
||||
private void stubPerNodeContainers() {
|
||||
@@ -687,7 +766,7 @@ class ElastiCacheServiceTest {
|
||||
return new ElastiCacheService.CreateReplicationGroupRequest(groupId, "test",
|
||||
AuthMode.NO_AUTH, null, "us-east-1", null, null, null,
|
||||
parameterGroupName, null, null, null, null,
|
||||
null, null, null, ReplicationGroupSettings.defaults(), Map.of());
|
||||
null, null, null, null, ReplicationGroupSettings.defaults(), Map.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user