feat(elasticache): IncreaseReplicaCount, DecreaseReplicaCount and TestFailover (#7881)

* feat(elasticache): IncreaseReplicaCount, DecreaseReplicaCount and TestFailover
* fix(elasticache): refuse a replica count change or failover without a ReplicationGroupId
* fix(elasticache): number a group's members so a decrease keeps the primary
This commit is contained in:
jibin-jusdb authored and GitHub committed 2026-11-15 15:13:56 -04:00
1 parent edb42cebf1
commit fdbcbb68ec
9 files changed
+508 -41

No files matched your search

@@ -72,6 +72,9 @@ public class ElastiCacheQueryHandler {
case "CreateReplicationGroup" -> handleCreateReplicationGroup(params, region);
case "DescribeReplicationGroups" -> handleDescribeReplicationGroups(params);
case "ModifyReplicationGroup" -> handleModifyReplicationGroup(params);
case "IncreaseReplicaCount" -> handleReplicaCount(params, true);
case "DecreaseReplicaCount" -> handleReplicaCount(params, false);
case "TestFailover" -> handleTestFailover(params);
case "DeleteReplicationGroup" -> handleDeleteReplicationGroup(params);
case "CreateUser" -> handleCreateUser(params);
case "DescribeUsers" -> handleDescribeUsers(params);
@@ -267,6 +270,32 @@ public class ElastiCacheQueryHandler {
}
}
private Response handleReplicaCount(MultivaluedMap<String, String> params, boolean increase) {
String action = increase ? "IncreaseReplicaCount" : "DecreaseReplicaCount";
try {
if (!increase && !extractMemberList(params, "ReplicasToRemove.member.").isEmpty()) {
throw new AwsException("InvalidParameterValue",
"Floci decreases the replica count by NewReplicaCount only, not by ReplicasToRemove.", 400);
}
ReplicationGroup group = service.changeReplicaCount(params.getFirst("ReplicationGroupId"),
intParam(params, "NewReplicaCount"), boolParam(params, "ApplyImmediately"), increase);
return Response.ok(AwsQueryResponse.envelope(action, AwsNamespaces.EC, replicationGroupXml(group))).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.EC, e.getHttpStatus());
}
}
private Response handleTestFailover(MultivaluedMap<String, String> params) {
try {
ReplicationGroup group = service.testFailover(params.getFirst("ReplicationGroupId"),
params.getFirst("NodeGroupId"));
return Response.ok(AwsQueryResponse.envelope("TestFailover", AwsNamespaces.EC,
replicationGroupXml(group))).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.EC, e.getHttpStatus());
}
}
// ── Users ─────────────────────────────────────────────────────────────────
private Response handleCreateUser(MultivaluedMap<String, String> params) {
@@ -2126,14 +2126,137 @@ public class ElastiCacheService implements ResourceProvider {
}
return members;
}
for (int i = 1; i <= group.getNumCacheClusters(); i++) {
List<Integer> numbers = memberNumbers(group);
int primary = primaryMember(group, numbers);
for (int number : numbers) {
members.add(new MemberCacheCluster(group,
group.getReplicationGroupId() + "-" + String.format("%03d", i),
group.getProxyPort(), i == 1));
group.getReplicationGroupId() + "-" + String.format("%03d", number),
group.getProxyPort(), number == primary));
}
return members;
}
/** A cluster-mode-disabled group's member numbers, in order: 1 to NumCacheClusters until a count change. */
private static List<Integer> memberNumbers(ReplicationGroup group) {
if (!group.getMemberNumbers().isEmpty()) {
return group.getMemberNumbers();
}
List<Integer> numbers = new ArrayList<>();
for (int number = 1; number <= group.getNumCacheClusters(); number++) {
numbers.add(number);
}
return numbers;
}
/** The member a cluster-mode-disabled group reports as primary: the first, unless TestFailover moved it. */
private static int primaryMember(ReplicationGroup group, List<Integer> numbers) {
return numbers.contains(group.getPrimaryMemberNumber()) ? group.getPrimaryMemberNumber() : numbers.getFirst();
}
// ── Replicas and failover ─────────────────────────────────────────────────
/**
* IncreaseReplicaCount and DecreaseReplicaCount for a cluster-mode-disabled group. Its members
* share one backend, so a replica is a member cache cluster the group reports, added or removed at
* once, since {@code ApplyImmediately=False} is not supported. Up to five replicas, and at least one
* while Multi-AZ is enabled. New members take the next numbers; a decrease removes the
* highest-numbered replicas and never the primary, which AWS does not delete.
*/
public ReplicationGroup changeReplicaCount(String groupId, Integer newReplicaCount, Boolean applyImmediately,
boolean increase) {
requireGroupId(groupId);
if (applyImmediately == null) {
throw new AwsException("InvalidParameterValue", "The parameter ApplyImmediately must be provided.", 400);
}
if (!applyImmediately) {
throw new AwsException("InvalidParameterValue", "ApplyImmediately=False is not currently supported.", 400);
}
if (newReplicaCount == null) {
throw new AwsException("InvalidParameterValue", "The parameter NewReplicaCount must be provided.", 400);
}
synchronized (lockFor("rg:" + groupId)) {
ReplicationGroup group = requireFailoverCapable(groupId,
increase ? "IncreaseReplicaCount" : "DecreaseReplicaCount");
validateRange("NewReplicaCount", newReplicaCount, group.isMultiAzEnabled() ? 1 : 0, 5);
List<Integer> numbers = new ArrayList<>(memberNumbers(group));
int primary = primaryMember(group, numbers);
int current = numbers.size() - 1;
if (increase ? newReplicaCount <= current : newReplicaCount >= current) {
throw new AwsException("InvalidParameterValue", "NewReplicaCount " + newReplicaCount + " must be "
+ (increase ? "greater" : "less") + " than the " + current + " replica(s) replication group "
+ groupId + " has.", 400);
}
if (increase) {
int next = numbers.getLast();
while (numbers.size() < newReplicaCount + 1) {
numbers.add(++next);
}
} else {
for (int i = numbers.size() - 1; i >= 0 && numbers.size() > newReplicaCount + 1; i--) {
if (numbers.get(i) != primary) {
numbers.remove(i);
}
}
}
group.setMemberNumbers(numbers);
group.setNumCacheClusters(numbers.size());
group.setPrimaryMemberNumber(primary);
groups.put(groupId, group);
LOG.infov("Replication group {0} now has {1} replica(s)", groupId, String.valueOf(newReplicaCount));
return group;
}
}
/**
* TestFailover on a cluster-mode-disabled group, whose only node group is 0001: the next member
* becomes primary and the old primary a replica. The members share one backend, so the data and
* the endpoints stay as they are; the roles every API reports change.
*/
public ReplicationGroup testFailover(String groupId, String nodeGroupId) {
requireGroupId(groupId);
if (nodeGroupId == null || nodeGroupId.isBlank()) {
throw new AwsException("InvalidParameterValue", "The parameter NodeGroupId must be provided.", 400);
}
synchronized (lockFor("rg:" + groupId)) {
ReplicationGroup group = requireFailoverCapable(groupId, "TestFailover");
if (!"0001".equals(nodeGroupId)) {
throw new AwsException("NodeGroupNotFoundFault", "Node group " + nodeGroupId
+ " not found in replication group " + groupId + ".", 404);
}
List<Integer> numbers = memberNumbers(group);
if (!group.isAutomaticFailoverEnabled() || numbers.size() < 2) {
throw new AwsException("InvalidReplicationGroupState", "Replication group " + groupId
+ " needs automatic failover enabled and at least one replica to fail over.", 400);
}
int primary = primaryMember(group, numbers);
group.setPrimaryMemberNumber(numbers.get((numbers.indexOf(primary) + 1) % numbers.size()));
groups.put(groupId, group);
LOG.infov("Replication group {0} failed over to member {1}", groupId,
String.valueOf(group.getPrimaryMemberNumber()));
return group;
}
}
private static void requireGroupId(String groupId) {
if (groupId == null || groupId.isBlank()) {
throw new AwsException("InvalidParameterValue", "The parameter ReplicationGroupId must be provided.", 400);
}
}
/** The group, read under its monitor by the caller: available and cluster-mode disabled. */
private ReplicationGroup requireFailoverCapable(String groupId, String operation) {
ReplicationGroup group = getReplicationGroup(groupId);
if (group.isClusterEnabled()) {
throw new AwsException("InvalidParameterValue", "Floci supports " + operation
+ " on cluster-mode-disabled replication groups only; " + groupId + " is cluster-mode enabled.", 400);
}
if (group.getStatus() != ReplicationGroupStatus.AVAILABLE) {
throw new AwsException("InvalidReplicationGroupState",
"Replication group " + groupId + " is not available.", 400);
}
return group;
}
public ReplicationGroup modifyReplicationGroup(String groupId, List<String> userGroupIdsToAdd,
List<String> userGroupIdsToRemove) {
return modifyReplicationGroup(groupId, userGroupIdsToAdd, userGroupIdsToRemove,
@@ -43,6 +43,13 @@ public class ReplicationGroup {
private int numNodeGroups = 1;
private int replicasPerNodeGroup;
private int numCacheClusters = 1;
/**
* The numbers of a cluster-mode-disabled group's members, {@code <id>-001} upward, once a replica count
* change has added or removed one. Empty until then, while the members are 1 to {@link #numCacheClusters}.
*/
private List<Integer> memberNumbers = new ArrayList<>();
/** The number of the member a cluster-mode-disabled group reports as primary; TestFailover moves it. */
private int primaryMemberNumber = 1;
private String engine;
private String engineVersion;
private String cacheNodeType;
@@ -170,6 +177,14 @@ public class ReplicationGroup {
public int getNumCacheClusters() { return numCacheClusters; }
public void setNumCacheClusters(int numCacheClusters) { this.numCacheClusters = numCacheClusters; }
public List<Integer> getMemberNumbers() { return memberNumbers; }
public void setMemberNumbers(List<Integer> memberNumbers) {
this.memberNumbers = memberNumbers == null ? new ArrayList<>() : new ArrayList<>(memberNumbers);
}
public int getPrimaryMemberNumber() { return primaryMemberNumber; }
public void setPrimaryMemberNumber(int primaryMemberNumber) { this.primaryMemberNumber = primaryMemberNumber; }
public String getEngine() { return engine; }
public void setEngine(String engine) { this.engine = engine; }