mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(application-autoscaling): paginate describe operations (#6398)
* fix(application-autoscaling): paginate describe operations * fix(application-autoscaling): harden pagination tokens
This commit is contained in:
1 parent
5a9f9cbbd6
commit
2a1d3f3cec
5 files changed
+403
-16
No files matched your search
+27
-4
@@ -5,6 +5,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.services.applicationautoscaling.model.Alarm;
|
||||
import io.github.hectorvent.floci.services.applicationautoscaling.model.PredefinedMetricSpecification;
|
||||
import io.github.hectorvent.floci.services.applicationautoscaling.model.ScalableTarget;
|
||||
@@ -86,15 +88,20 @@ public class ApplicationAutoScalingJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleDescribeScalableTargets(JsonNode request, String region) {
|
||||
List<ScalableTarget> found = service.describeScalableTargets(
|
||||
PaginatedResult<ScalableTarget> result = service.describeScalableTargets(
|
||||
text(request, "ServiceNamespace"),
|
||||
stringList(request.path("ResourceIds")),
|
||||
text(request, "ScalableDimension"),
|
||||
optionalMaxResults(request),
|
||||
text(request, "NextToken"),
|
||||
region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode array = response.putArray("ScalableTargets");
|
||||
found.forEach(t -> array.add(scalableTargetJson(t)));
|
||||
result.items().forEach(t -> array.add(scalableTargetJson(t)));
|
||||
if (result.nextToken() != null) {
|
||||
response.put("NextToken", result.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@@ -128,16 +135,21 @@ public class ApplicationAutoScalingJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleDescribeScalingPolicies(JsonNode request, String region) {
|
||||
List<ScalingPolicy> found = service.describeScalingPolicies(
|
||||
PaginatedResult<ScalingPolicy> result = service.describeScalingPolicies(
|
||||
text(request, "ServiceNamespace"),
|
||||
text(request, "ResourceId"),
|
||||
text(request, "ScalableDimension"),
|
||||
stringList(request.path("PolicyNames")),
|
||||
optionalMaxResults(request),
|
||||
text(request, "NextToken"),
|
||||
region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode array = response.putArray("ScalingPolicies");
|
||||
found.forEach(p -> array.add(scalingPolicyJson(p)));
|
||||
result.items().forEach(p -> array.add(scalingPolicyJson(p)));
|
||||
if (result.nextToken() != null) {
|
||||
response.put("NextToken", result.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@@ -369,4 +381,15 @@ public class ApplicationAutoScalingJsonHandler {
|
||||
JsonNode value = node.path(field);
|
||||
return value.isMissingNode() || value.isNull() ? null : value.asInt();
|
||||
}
|
||||
|
||||
private static Integer optionalMaxResults(JsonNode request) {
|
||||
JsonNode value = request.get("MaxResults");
|
||||
if (value == null || value.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!value.isIntegralNumber() || !value.canConvertToInt()) {
|
||||
throw new AwsException("ValidationException", "MaxResults must be an integer.", 400);
|
||||
}
|
||||
return value.intValue();
|
||||
}
|
||||
}
|
||||
+83
-1
@@ -2,8 +2,11 @@ package io.github.hectorvent.floci.services.applicationautoscaling;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
import io.github.hectorvent.floci.core.common.SignedMarkers;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.applicationautoscaling.model.Alarm;
|
||||
@@ -45,6 +48,8 @@ import java.util.UUID;
|
||||
public class ApplicationAutoScalingService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ApplicationAutoScalingService.class);
|
||||
private static final int MAX_TARGET_PAGE_SIZE = 50;
|
||||
private static final int MAX_POLICY_PAGE_SIZE = 10;
|
||||
|
||||
static final Set<String> SERVICE_NAMESPACES = Set.of(
|
||||
"ecs", "elasticmapreduce", "ec2", "appstream", "dynamodb", "rds", "sagemaker",
|
||||
@@ -85,6 +90,7 @@ public class ApplicationAutoScalingService {
|
||||
private final StorageBackend<String, ScalingActivity> activities;
|
||||
private final RegionResolver regionResolver;
|
||||
private final CloudWatchMetricsService cloudWatchMetricsService;
|
||||
private final SignedMarkers pageTokens = new SignedMarkers();
|
||||
|
||||
@Inject
|
||||
public ApplicationAutoScalingService(StorageFactory factory,
|
||||
@@ -186,6 +192,18 @@ public class ApplicationAutoScalingService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
PaginatedResult<ScalableTarget> describeScalableTargets(String serviceNamespace, List<String> resourceIds,
|
||||
String scalableDimension, Integer maxResults,
|
||||
String nextToken, String region) {
|
||||
String cursor = decodeNextToken(nextToken, "DescribeScalableTargets");
|
||||
List<ScalableTarget> matching = describeScalableTargets(serviceNamespace, resourceIds,
|
||||
scalableDimension, region);
|
||||
return signedPage(Pagination.paginate(matching, ApplicationAutoScalingService::scalableTargetCursor,
|
||||
pageSize(maxResults, MAX_TARGET_PAGE_SIZE), cursor,
|
||||
pageNamespace(resourceIds, "DescribeScalableTargets", region, serviceNamespace, scalableDimension),
|
||||
token -> invalidNextToken("DescribeScalableTargets")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deregisters a scalable target and, per AWS semantics, deletes every scaling policy
|
||||
* attached to it along with the CloudWatch alarms those policies own.
|
||||
@@ -275,10 +293,23 @@ public class ApplicationAutoScalingService {
|
||||
.filter(p -> resourceId == null || resourceId.equals(p.getResourceId()))
|
||||
.filter(p -> scalableDimension == null || scalableDimension.equals(p.getScalableDimension()))
|
||||
.filter(p -> policyNames == null || policyNames.isEmpty() || policyNames.contains(p.getPolicyName()))
|
||||
.sorted(Comparator.comparing(ScalingPolicy::getPolicyName))
|
||||
.sorted(Comparator.comparing(ApplicationAutoScalingService::scalingPolicyCursor))
|
||||
.toList();
|
||||
}
|
||||
|
||||
PaginatedResult<ScalingPolicy> describeScalingPolicies(String serviceNamespace, String resourceId,
|
||||
String scalableDimension, List<String> policyNames,
|
||||
Integer maxResults, String nextToken, String region) {
|
||||
String cursor = decodeNextToken(nextToken, "DescribeScalingPolicies");
|
||||
List<ScalingPolicy> matching = describeScalingPolicies(serviceNamespace, resourceId,
|
||||
scalableDimension, policyNames, region);
|
||||
return signedPage(Pagination.paginate(matching, ApplicationAutoScalingService::scalingPolicyCursor,
|
||||
pageSize(maxResults, MAX_POLICY_PAGE_SIZE), cursor,
|
||||
pageNamespace(policyNames, "DescribeScalingPolicies", region, serviceNamespace,
|
||||
resourceId, scalableDimension),
|
||||
token -> invalidNextToken("DescribeScalingPolicies")));
|
||||
}
|
||||
|
||||
public void deleteScalingPolicy(String policyName, String serviceNamespace, String resourceId,
|
||||
String scalableDimension, String region) {
|
||||
validateTriple(serviceNamespace, resourceId, scalableDimension);
|
||||
@@ -513,6 +544,57 @@ public class ApplicationAutoScalingService {
|
||||
return now.getEpochSecond() + (now.getNano() / 1_000_000 / 1000.0d);
|
||||
}
|
||||
|
||||
private static int pageSize(Integer maxResults, int maximum) {
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > maximum)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"MaxResults must be between 1 and " + maximum + ".", 400);
|
||||
}
|
||||
return maxResults == null ? maximum : maxResults;
|
||||
}
|
||||
|
||||
private static AwsException invalidNextToken(String operation) {
|
||||
return new AwsException("InvalidNextTokenException",
|
||||
"Invalid NextToken for " + operation + ".", 400);
|
||||
}
|
||||
|
||||
private String decodeNextToken(String nextToken, String operation) {
|
||||
if (nextToken == null) {
|
||||
return null;
|
||||
}
|
||||
String cursor = pageTokens.cursorOf(nextToken);
|
||||
if (cursor == null) {
|
||||
throw invalidNextToken(operation);
|
||||
}
|
||||
return cursor;
|
||||
}
|
||||
|
||||
private <T> PaginatedResult<T> signedPage(PaginatedResult<T> page) {
|
||||
return new PaginatedResult<>(page.items(),
|
||||
page.nextToken() == null ? null : pageTokens.issue(page.nextToken()));
|
||||
}
|
||||
|
||||
private static String scalableTargetCursor(ScalableTarget target) {
|
||||
return target.getResourceId() + "\u0000" + target.getScalableDimension();
|
||||
}
|
||||
|
||||
private static String scalingPolicyCursor(ScalingPolicy policy) {
|
||||
return policy.getPolicyName() + "\u0000" + policy.getResourceId()
|
||||
+ "\u0000" + policy.getScalableDimension();
|
||||
}
|
||||
|
||||
private static String pageNamespace(List<String> filters, String... fields) {
|
||||
StringBuilder namespace = new StringBuilder();
|
||||
for (String field : fields) {
|
||||
namespace.append(field == null ? "-1:" : field.length() + ":" + field);
|
||||
}
|
||||
List<String> normalized = filters == null ? List.of() : filters.stream().distinct().sorted().toList();
|
||||
namespace.append(normalized.size()).append(':');
|
||||
for (String filter : normalized) {
|
||||
namespace.append(filter.length()).append(':').append(filter);
|
||||
}
|
||||
return namespace.toString();
|
||||
}
|
||||
|
||||
private static String targetKey(String region, String serviceNamespace, String resourceId,
|
||||
String scalableDimension) {
|
||||
return region + "::" + serviceNamespace + "::" + scalableDimension + "::" + resourceId;
|
||||
|
||||
Reference in new issue
Block a user