perf(apigateway): avoid repeated root resource scans (#3404)

Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
XuJianandxujiantop-crypto authored and GitHub committed 2026-09-11 23:29:06 -05:00
1 parent a7902b97f3
commit 0a45d2e131
4 files changed
+137 -20

No files matched your search

@@ -1933,7 +1933,11 @@ public class ApiGatewayController {
// as aws_api_gateway_rest_api.root_resource_id, which is how the first resource
// under "/" gets its parent, so leaving it out breaks the conventional way of
// building a REST API.
service.findRootResourceId(region, api.getId()).ifPresent(id -> node.put("rootResourceId", id));
if (api.getRootResourceId() != null) {
node.put("rootResourceId", api.getRootResourceId());
} else {
service.findRootResourceId(region, api.getId()).ifPresent(id -> node.put("rootResourceId", id));
}
// Neither member is settable on a REST API here: createRestApi ignores both and
// updateRestApi patches only /name and /description, so the emulated value is always
@@ -260,12 +260,13 @@ public class ApiGatewayService {
api.setEndpointConfiguration(endpointConfiguration);
apiStore.put(apiKey(region, api.getId()), api);
// Create root resource "/"
ApiGatewayResource root = new ApiGatewayResource();
root.setId(shortId(8));
root.setPath("/");
api.setRootResourceId(root.getId());
apiStore.put(apiKey(region, api.getId()), api);
resourceStore.put(resourceKey(region, api.getId(), root.getId()), root);
LOG.infov("Created REST API: {0} ({1}) in {2}", name, api.getId(), region);
@@ -315,25 +316,18 @@ public class ApiGatewayService {
return resourceStore.scan(k -> k.startsWith(prefix));
}
/**
* The id of the resource at "/", or empty if there is no API to read it from.
* <p>
* ListRestApis renders a snapshot of the APIs and resolves this per API afterwards, so an
* API deleted in between is already gone by the time its root is looked up. That must cost
* the caller the one member rather than failing the whole listing, which is why a missing
* API is empty here instead of a not-found. Every other failure still propagates.
*/
/** The root resource id, with a resource-store fallback for data persisted before it was stored on the API. */
public Optional<String> findRootResourceId(String region, String apiId) {
List<ApiGatewayResource> resources;
try {
resources = getResources(region, apiId);
} catch (AwsException e) {
if ("NotFoundException".equals(e.getErrorCode())) {
return Optional.empty();
}
throw e;
Optional<RestApi> api = apiStore.get(apiKey(region, apiId));
if (api.isEmpty()) {
return Optional.empty();
}
return resources.stream()
if (api.get().getRootResourceId() != null) {
return Optional.of(api.get().getRootResourceId());
}
String prefix = region + "::" + apiId + "::";
return resourceStore.scan(k -> k.startsWith(prefix)).stream()
.filter(r -> "/".equals(r.getPath()))
.map(ApiGatewayResource::getId)
.findFirst();
@@ -15,6 +15,7 @@ public class RestApi {
private String name;
private String description;
private long createdDate;
private String rootResourceId;
private Map<String, String> tags = new HashMap<>();
private EndpointConfiguration endpointConfiguration;
@@ -51,6 +52,14 @@ public class RestApi {
this.createdDate = createdDate;
}
public String getRootResourceId() {
return rootResourceId;
}
public void setRootResourceId(String rootResourceId) {
this.rootResourceId = rootResourceId;
}
public Map<String, String> getTags() {
return tags;
}