mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(athena): paginate data catalog summaries (#6679)
This commit is contained in:
1 parent
3867b5feaa
commit
13289b0aef
5 files changed
+233
-5
No files matched your search
@@ -33,6 +33,14 @@ Floci emulates Amazon Athena with **real SQL execution** powered by a [floci-duc
|
||||
| `DeleteWorkGroup` | Deletes a workgroup |
|
||||
<!-- floci:actions:end -->
|
||||
|
||||
`ListDataCatalogs` accepts `MaxResults` from 2 to 50. Floci uses 50 when it is
|
||||
omitted, since the API reference does not document a default. The built-in
|
||||
`AwsDataCatalog` stays first, followed by registered catalogs in name order.
|
||||
Pass the opaque `NextToken` to continue; final pages omit it. Tokens are scoped
|
||||
to the action, caller account, and request region and remain resumable after a
|
||||
cursor catalog is deleted. Malformed tokens, tokens outside the 1-to-1024
|
||||
character range, and invalid page sizes return `InvalidRequestException`.
|
||||
|
||||
## How it works
|
||||
|
||||
1. **Lazy sidecar start**: On the first `StartQueryExecution` call, Floci checks for a local `floci/floci-duck:latest` image and starts the container. Subsequent queries reuse the running container.
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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.services.athena.model.CreateWorkGroupRequest;
|
||||
import io.github.hectorvent.floci.services.athena.model.QueryExecution;
|
||||
import io.github.hectorvent.floci.services.athena.model.QueryExecutionContext;
|
||||
@@ -99,8 +100,17 @@ public class AthenaJsonHandler {
|
||||
athenaService.updateWorkGroup(updateRequest, region);
|
||||
yield Response.ok(Map.of()).build();
|
||||
}
|
||||
case "ListDataCatalogs" ->
|
||||
Response.ok(Map.of("DataCatalogsSummary", athenaService.listDataCatalogs(region))).build();
|
||||
case "ListDataCatalogs" -> {
|
||||
Integer maxResults = Pagination.parseMaxResults(request, "MaxResults", "InvalidRequestException");
|
||||
String nextToken = Pagination.parseNextToken(request, "NextToken", "InvalidRequestException");
|
||||
PaginatedResult<Map<String, Object>> page = athenaService.listDataCatalogsPage(region, maxResults, nextToken);
|
||||
Map<String, Object> response = new LinkedHashMap<>();
|
||||
response.put("DataCatalogsSummary", page.items());
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
yield Response.ok(response).build();
|
||||
}
|
||||
case "GetDataCatalog" -> {
|
||||
String name = request.has("Name") ? request.get("Name").asText() : AthenaService.DEFAULT_CATALOG;
|
||||
yield Response.ok(Map.of("DataCatalog", athenaService.getDataCatalog(region, name))).build();
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.CsvParser;
|
||||
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.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.athena.model.CreateWorkGroupConfigurationRequest;
|
||||
@@ -98,6 +99,7 @@ public class AthenaService {
|
||||
private final EmulatorConfig config;
|
||||
private final Vertx vertx;
|
||||
private final GlueViewDdlBuilder ddlBuilder;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public AthenaService(StorageFactory storageFactory,
|
||||
@@ -106,7 +108,8 @@ public class AthenaService {
|
||||
S3Service s3Service,
|
||||
EmulatorConfig config,
|
||||
Vertx vertx,
|
||||
GlueViewDdlBuilder ddlBuilder) {
|
||||
GlueViewDdlBuilder ddlBuilder,
|
||||
RegionResolver regionResolver) {
|
||||
this.queryStore = storageFactory.create("athena", "queries.json",
|
||||
new TypeReference<>() {});
|
||||
this.workGroupStore = storageFactory.create("athena", "workgroups.json",
|
||||
@@ -119,6 +122,7 @@ public class AthenaService {
|
||||
this.config = config;
|
||||
this.vertx = vertx;
|
||||
this.ddlBuilder = ddlBuilder;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
public String startQueryExecution(String query,
|
||||
@@ -393,6 +397,21 @@ public class AthenaService {
|
||||
return summaries;
|
||||
}
|
||||
|
||||
public PaginatedResult<Map<String, Object>> listDataCatalogsPage(String region, Integer maxResults, String nextToken) {
|
||||
int limit = maxResults == null ? 50 : maxResults;
|
||||
if (limit < 2 || limit > 50) {
|
||||
throw new AwsException("InvalidRequestException", "MaxResults must be between 2 and 50.", 400);
|
||||
}
|
||||
if (nextToken != null && (nextToken.isEmpty() || nextToken.length() > 1024)) {
|
||||
throw new AwsException("InvalidRequestException", "NextToken must contain 1 to 1024 characters.", 400);
|
||||
}
|
||||
return Pagination.paginate(listDataCatalogs(region), catalog -> {
|
||||
String name = (String) catalog.get("CatalogName");
|
||||
return DEFAULT_CATALOG.equals(name) ? "0" : "1" + name;
|
||||
}, limit, nextToken, "ListDataCatalogs:" + regionResolver.getAccountId() + ":" + region,
|
||||
token -> new AwsException("InvalidRequestException", "Invalid NextToken.", 400));
|
||||
}
|
||||
|
||||
public Map<String, Object> getDataCatalog(String region, String name) {
|
||||
String resolved = name == null || name.isBlank() ? DEFAULT_CATALOG : name;
|
||||
if (DEFAULT_CATALOG.equals(resolved)) {
|
||||
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
package io.github.hectorvent.floci.services.athena;
|
||||
|
||||
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.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class AthenaDataCatalogPaginationIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "741666100001";
|
||||
private static final String REGION = "eu-central-2";
|
||||
private final List<String> created = new ArrayList<>();
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanUp() {
|
||||
for (String name : created) {
|
||||
delete(name);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestedPagesKeepTheBuiltInFirstAndResumeWithoutDuplicates() {
|
||||
create("zzz-last");
|
||||
create("000-before-the-built-in");
|
||||
create("middle");
|
||||
Map<String, Object> first = list(REGION, ACCOUNT, "{\"MaxResults\":2}", 200);
|
||||
assertEquals(List.of(AthenaService.DEFAULT_CATALOG, "000-before-the-built-in"), names(first));
|
||||
String token = (String) first.get("NextToken");
|
||||
assertNotNull(token);
|
||||
Map<String, Object> last = list(REGION, ACCOUNT, tokenRequest(token), 200);
|
||||
assertEquals(List.of("middle", "zzz-last"), names(last));
|
||||
assertFalse(last.containsKey("NextToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sharedPagerTokenResumesTheCatalogListing() {
|
||||
create("catalog-a");
|
||||
create("catalog-b");
|
||||
PaginatedResult<String> page = Pagination.paginate(
|
||||
List.of(AthenaService.DEFAULT_CATALOG, "catalog-a", "catalog-b"),
|
||||
name -> AthenaService.DEFAULT_CATALOG.equals(name) ? "0" : "1" + name,
|
||||
2, null, "ListDataCatalogs:" + ACCOUNT + ":" + REGION,
|
||||
token -> new AwsException("InvalidRequestException", "Invalid NextToken", 400));
|
||||
assertNotNull(page.nextToken());
|
||||
assertEquals(page.nextToken(), list(REGION, ACCOUNT, "{\"MaxResults\":2}", 200).get("NextToken"));
|
||||
Map<String, Object> last = list(REGION, ACCOUNT, tokenRequest(page.nextToken()), 200);
|
||||
assertEquals(List.of("catalog-b"), names(last));
|
||||
assertFalse(last.containsKey("NextToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void omittedNullAndMaximumPageSizesUseTheBoundedDefault() {
|
||||
for (int i = 0; i < 50; i++) {
|
||||
create("catalog-" + i);
|
||||
}
|
||||
for (String body : List.of("{}", "{\"MaxResults\":null}", "{\"MaxResults\":50}")) {
|
||||
Map<String, Object> first = list(REGION, ACCOUNT, body, 200);
|
||||
assertEquals(50, names(first).size());
|
||||
String token = (String) first.get("NextToken");
|
||||
assertNotNull(token);
|
||||
Map<String, Object> last = list(REGION, ACCOUNT, tokenRequest(token), 200);
|
||||
assertEquals(1, names(last).size());
|
||||
assertFalse(last.containsKey("NextToken"));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {-1, 0, 1, 51})
|
||||
void outOfRangePageSizesAreRejected(int size) {
|
||||
invalid("{\"MaxResults\":" + size + "}");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"4294967297", "2.5", "\"2\"", "{}", "[]", "true"})
|
||||
void malformedPageSizesAreRejectedWithoutCoercion(String size) {
|
||||
invalid("{\"MaxResults\":" + size + "}");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"\"\"", "\"%%%%\"", "\"AA\"", "\"2AA\"", "1", "true", "{}", "[]"})
|
||||
void malformedTokensAreRejected(String token) {
|
||||
invalid("{\"NextToken\":" + token + "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void tokensLongerThanTheApiLimitAreRejected() {
|
||||
invalid(tokenRequest("A".repeat(1025)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tokensCannotCrossAccountOrRegion() {
|
||||
create("catalog-a");
|
||||
create("catalog-b");
|
||||
String token = (String) list(REGION, ACCOUNT, "{\"MaxResults\":2}", 200).get("NextToken");
|
||||
assertNotNull(token);
|
||||
assertEquals(List.of(AthenaService.DEFAULT_CATALOG), names(list(REGION, "741666100002", "{}", 200)));
|
||||
for (Map<String, Object> error : List.of(
|
||||
list(REGION, "741666100002", tokenRequest(token), 400),
|
||||
list("ap-south-2", ACCOUNT, tokenRequest(token), 400))) {
|
||||
assertEquals("InvalidRequestException", error.get("__type"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void maximumSupportedNameCursorIsBoundedAndResumesAfterDeletion() {
|
||||
String prefix = "a".repeat(123);
|
||||
String firstName = prefix + "aaaa";
|
||||
String secondName = prefix + "aaab";
|
||||
assertEquals(127, firstName.length());
|
||||
create(firstName);
|
||||
create(secondName);
|
||||
Map<String, Object> first = list(REGION, ACCOUNT, "{\"MaxResults\":2}", 200);
|
||||
assertEquals(List.of(AthenaService.DEFAULT_CATALOG, firstName), names(first));
|
||||
String token = (String) first.get("NextToken");
|
||||
assertNotNull(token);
|
||||
assertTrue(token.length() <= 1024);
|
||||
Map<String, Object> last = list(REGION, ACCOUNT, tokenRequest(token), 200);
|
||||
assertEquals(List.of(secondName), names(last));
|
||||
assertFalse(last.containsKey("NextToken"));
|
||||
delete(firstName);
|
||||
created.remove(firstName);
|
||||
assertEquals(List.of(secondName), names(list(REGION, ACCOUNT, tokenRequest(token), 200)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void builtInOnlyFinalPageOmitsTheToken() {
|
||||
for (String body : List.of("{}", "{\"NextToken\":null}")) {
|
||||
Map<String, Object> response = list(REGION, ACCOUNT, body, 200);
|
||||
assertEquals(List.of(AthenaService.DEFAULT_CATALOG), names(response));
|
||||
assertFalse(response.containsKey("NextToken"));
|
||||
}
|
||||
}
|
||||
|
||||
private void create(String name) {
|
||||
call("CreateDataCatalog", REGION, ACCOUNT, "{\"Name\":\"" + name + "\",\"Type\":\"LAMBDA\"}", 200);
|
||||
created.add(name);
|
||||
}
|
||||
|
||||
private void delete(String name) {
|
||||
call("DeleteDataCatalog", REGION, ACCOUNT, "{\"Name\":\"" + name + "\"}", 200);
|
||||
}
|
||||
|
||||
private void invalid(String body) {
|
||||
assertEquals("InvalidRequestException", list(REGION, ACCOUNT, body, 400).get("__type"));
|
||||
}
|
||||
|
||||
private List<String> names(Map<String, Object> response) {
|
||||
return ((List<?>) response.get("DataCatalogsSummary")).stream()
|
||||
.map(item -> (String) ((Map<?, ?>) item).get("CatalogName")).toList();
|
||||
}
|
||||
|
||||
private Map<String, Object> list(String region, String account, String body, int status) {
|
||||
return call("ListDataCatalogs", region, account, body, status);
|
||||
}
|
||||
|
||||
private Map<String, Object> call(String action, String region, String account, String body, int status) {
|
||||
return given().contentType("application/x-amz-json-1.1")
|
||||
.header("X-Amz-Target", "AmazonAthena." + action)
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=" + account + "/20260101/" + region + "/athena/aws4_request")
|
||||
.body(body.getBytes(StandardCharsets.UTF_8)).when().post("/").then().statusCode(status)
|
||||
.extract().jsonPath().getMap("");
|
||||
}
|
||||
|
||||
private String tokenRequest(String token) {
|
||||
return "{\"NextToken\":\"" + token + "\"}";
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.athena;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.athena.model.QueryExecutionState;
|
||||
@@ -58,7 +59,7 @@ class AthenaServiceTest {
|
||||
when(athena.mock()).thenReturn(false);
|
||||
|
||||
service = new AthenaService(storageFactory, duckClient, glueService, s3Service,
|
||||
config, vertx, ddlBuilder);
|
||||
config, vertx, ddlBuilder, mock(RegionResolver.class));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
@@ -148,4 +149,4 @@ class AthenaServiceTest {
|
||||
}
|
||||
assertEquals(expected, service.getQueryExecution(id).getStatus().getState());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user