mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(athena): paginate query execution listings (#6816)
This commit is contained in:
1 parent
a83fa9c9e4
commit
119b7e9580
5 files changed
+203
-3
No files matched your search
@@ -41,6 +41,15 @@ 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`.
|
||||
|
||||
`ListQueryExecutions` returns at most `MaxResults` IDs from the requested
|
||||
`WorkGroup`, defaulting to `primary`. Floci uses a bounded page of 50 when the
|
||||
page size is omitted, null, or zero; the API reference permits zero but does not
|
||||
specify its behavior or a default. Positive page sizes must not exceed 50.
|
||||
IDs are ordered deterministically, and the final page omits `NextToken`.
|
||||
Continuation tokens are scoped to the action, account, region, and workgroup;
|
||||
invalid sizes, malformed tokens, and tokens outside 1 to 1024 characters 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.
|
||||
|
||||
@@ -480,6 +480,12 @@
|
||||
<version>${aws.sdk.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>athena</artifactId>
|
||||
<version>${aws.sdk.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>ec2</artifactId>
|
||||
|
||||
@@ -63,9 +63,20 @@ public class AthenaJsonHandler {
|
||||
yield Response.ok(Map.of("ResultSet", results)).build();
|
||||
}
|
||||
case "ListQueryExecutions" -> {
|
||||
yield Response.ok(Map.of("QueryExecutionIds",
|
||||
athenaService.listQueryExecutions().stream()
|
||||
.map(QueryExecution::getQueryExecutionId).toList())).build();
|
||||
Integer maxResults = Pagination.parseMaxResults(request, "MaxResults", "InvalidRequestException");
|
||||
String nextToken = Pagination.parseNextToken(request, "NextToken", "InvalidRequestException");
|
||||
JsonNode workGroup = request.get("WorkGroup");
|
||||
if (workGroup != null && !workGroup.isNull() && !workGroup.isTextual()) {
|
||||
throw new AwsException("InvalidRequestException", "WorkGroup must be a string.", 400);
|
||||
}
|
||||
PaginatedResult<QueryExecution> page = athenaService.listQueryExecutionsPage(
|
||||
region, text(request, "WorkGroup"), maxResults, nextToken);
|
||||
Map<String, Object> response = new LinkedHashMap<>();
|
||||
response.put("QueryExecutionIds", page.items().stream().map(QueryExecution::getQueryExecutionId).toList());
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
yield Response.ok(response).build();
|
||||
}
|
||||
case "StopQueryExecution" -> {
|
||||
athenaService.stopQueryExecution(request.get("QueryExecutionId").asText());
|
||||
|
||||
@@ -221,6 +221,26 @@ public class AthenaService {
|
||||
return queryStore.scan(k -> true);
|
||||
}
|
||||
|
||||
public PaginatedResult<QueryExecution> listQueryExecutionsPage(String region, String workGroup,
|
||||
Integer maxResults, String nextToken) {
|
||||
if (maxResults != null && (maxResults < 0 || maxResults > 50)) {
|
||||
throw new AwsException("InvalidRequestException", "MaxResults must be between 0 and 50.", 400);
|
||||
}
|
||||
if (nextToken != null && (nextToken.isEmpty() || nextToken.length() > 1024)) {
|
||||
throw new AwsException("InvalidRequestException", "NextToken must contain 1 to 1024 characters.", 400);
|
||||
}
|
||||
String group = workGroup == null ? DEFAULT_WORKGROUP : workGroup;
|
||||
validateWorkGroupName(group);
|
||||
getWorkGroup(group, region);
|
||||
List<QueryExecution> executions = listQueryExecutions().stream()
|
||||
.filter(execution -> group.equals(Objects.requireNonNullElse(execution.getWorkGroup(), DEFAULT_WORKGROUP)))
|
||||
.toList();
|
||||
int limit = maxResults == null || maxResults == 0 ? 50 : maxResults;
|
||||
return Pagination.paginate(executions, QueryExecution::getQueryExecutionId, limit, nextToken,
|
||||
"ListQueryExecutions:" + regionResolver.getAccountId() + ":" + region + ":" + group,
|
||||
token -> new AwsException("InvalidRequestException", "Invalid NextToken.", 400));
|
||||
}
|
||||
|
||||
public void stopQueryExecution(String id) {
|
||||
QueryExecution execution = getQueryExecution(id);
|
||||
synchronized (execution) {
|
||||
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
package io.github.hectorvent.floci.services.athena;
|
||||
|
||||
import io.github.hectorvent.floci.testing.AwsTestClients;
|
||||
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.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.athena.AthenaClient;
|
||||
import software.amazon.awssdk.services.athena.model.InvalidRequestException;
|
||||
import software.amazon.awssdk.services.athena.model.ListQueryExecutionsResponse;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class AthenaQueryPaginationIntegrationTest {
|
||||
private static final String REGION = "eu-west-2";
|
||||
private static final AtomicLong ACCOUNTS = new AtomicLong(741680600000L);
|
||||
private String account;
|
||||
private AthenaClient client;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void openClient() {
|
||||
account = Long.toString(ACCOUNTS.incrementAndGet());
|
||||
client = AwsTestClients.create(AthenaClient.builder(), account, Region.of(REGION));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void closeClient() {
|
||||
client.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void sdkPagesAndPaginatorReturnOnlyTheRequestedWorkGroupWithoutDuplicates() {
|
||||
List<String> ids = createQueries(3, "primary");
|
||||
client.createWorkGroup(request -> request.name("other"));
|
||||
createQueries(1, "other");
|
||||
List<String> seen = new ArrayList<>();
|
||||
String token = null;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
String current = token;
|
||||
ListQueryExecutionsResponse page = client.listQueryExecutions(request -> request
|
||||
.maxResults(1).nextToken(current));
|
||||
assertEquals(1, page.queryExecutionIds().size());
|
||||
seen.addAll(page.queryExecutionIds());
|
||||
token = page.nextToken();
|
||||
if (i < 2) {
|
||||
assertNotNull(token);
|
||||
}
|
||||
}
|
||||
assertNull(token);
|
||||
assertEquals(ids.stream().sorted().toList(), seen);
|
||||
assertEquals(seen, client.listQueryExecutionsPaginator(request -> request.maxResults(1))
|
||||
.stream().flatMap(page -> page.queryExecutionIds().stream()).toList());
|
||||
assertEquals(1, client.listQueryExecutions(request -> request.workGroup("other")).queryExecutionIds().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void omittedZeroAndMaximumSizesUseABoundedPage() {
|
||||
createQueries(51, "primary");
|
||||
for (Integer size : new Integer[]{null, 0, 50}) {
|
||||
ListQueryExecutionsResponse first = client.listQueryExecutions(request -> request.maxResults(size));
|
||||
assertEquals(50, first.queryExecutionIds().size());
|
||||
assertNotNull(first.nextToken());
|
||||
ListQueryExecutionsResponse last = client.listQueryExecutions(request -> request
|
||||
.nextToken(first.nextToken()).maxResults(50));
|
||||
assertEquals(1, last.queryExecutionIds().size());
|
||||
assertNull(last.nextToken());
|
||||
assertFalse(first.queryExecutionIds().contains(last.queryExecutionIds().getFirst()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void tokensCannotCrossWorkGroupAccountOrRegion() {
|
||||
createQueries(2, "primary");
|
||||
client.createWorkGroup(request -> request.name("other"));
|
||||
createQueries(2, "other");
|
||||
String token = client.listQueryExecutions(request -> request.maxResults(1)).nextToken();
|
||||
assertNotNull(token);
|
||||
assertThrows(InvalidRequestException.class, () -> client.listQueryExecutions(request -> request
|
||||
.workGroup("other").maxResults(1).nextToken(token)));
|
||||
try (AthenaClient other = AwsTestClients.create(AthenaClient.builder(), "741680699999", Region.of(REGION));
|
||||
AthenaClient region = AwsTestClients.create(AthenaClient.builder(), account, Region.US_EAST_1)) {
|
||||
assertTrue(other.listQueryExecutions().queryExecutionIds().isEmpty());
|
||||
assertThrows(InvalidRequestException.class, () -> other.listQueryExecutions(request -> request.nextToken(token)));
|
||||
assertThrows(InvalidRequestException.class, () -> region.listQueryExecutions(request -> request.nextToken(token)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyListingOmitsContinuationTokenAndNullFieldsUseDefaults() {
|
||||
ListQueryExecutionsResponse page = client.listQueryExecutions();
|
||||
assertTrue(page.queryExecutionIds().isEmpty());
|
||||
assertNull(page.nextToken());
|
||||
given().contentType("application/x-amz-json-1.1")
|
||||
.header("Authorization", authorization())
|
||||
.header("X-Amz-Target", "AmazonAthena.ListQueryExecutions")
|
||||
.body("{\"MaxResults\":null,\"NextToken\":null,\"WorkGroup\":null}")
|
||||
.post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"{\"MaxResults\":-1}", "{\"MaxResults\":51}",
|
||||
"{\"MaxResults\":4294967297}", "{\"MaxResults\":1.5}", "{\"MaxResults\":\"1\"}",
|
||||
"{\"MaxResults\":true}", "{\"MaxResults\":{}}", "{\"MaxResults\":[]}",
|
||||
"{\"NextToken\":\"\"}", "{\"NextToken\":\"%%%\"}", "{\"NextToken\":1}",
|
||||
"{\"NextToken\":true}", "{\"NextToken\":{}}", "{\"NextToken\":[]}",
|
||||
"{\"WorkGroup\":\"\"}", "{\"WorkGroup\":\"wrong:name\"}", "{\"WorkGroup\":1}",
|
||||
"{\"WorkGroup\":\"missing\"}"})
|
||||
void invalidPaginationInputsReturnAwsErrors(String body) {
|
||||
invalid(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void oversizedTokenIsRejected() {
|
||||
invalid("{\"NextToken\":\"" + "A".repeat(1025) + "\"}");
|
||||
}
|
||||
|
||||
private List<String> createQueries(int count, String workGroup) {
|
||||
List<String> ids = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
ids.add(client.startQueryExecution(request -> request.queryString("SELECT 1").workGroup(workGroup))
|
||||
.queryExecutionId());
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
private void invalid(String body) {
|
||||
given().contentType("application/x-amz-json-1.1")
|
||||
.header("Authorization", authorization())
|
||||
.header("X-Amz-Target", "AmazonAthena.ListQueryExecutions")
|
||||
.body(body).post("/").then().statusCode(400)
|
||||
.body("__type", equalTo("InvalidRequestException"));
|
||||
}
|
||||
|
||||
private String authorization() {
|
||||
return "AWS4-HMAC-SHA256 Credential=" + account + "/20261028/" + REGION + "/athena/aws4_request";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user