mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(athena): answer a query execution only in the Region it was started in (#9453)
Athena kept query executions by their id alone, so GetQueryExecution, GetQueryResults and StopQueryExecution sent to one Region answered another Region's execution, and ListQueryExecutions listed every Region's executions of the workgroup. AWS keeps query executions per account and Region. Each is now answered and listed only in the Region recorded for it when it started; one stored before Floci recorded it is answered in every Region, as it was. An id Athena holds none of in the Region gets AWS's answer, InvalidRequestException "QueryExecution <id> was not found", in place of "Query execution not found: <id>".
This commit is contained in:
1 parent
c843dcbdfc
commit
4018e8d0b5
7 files changed
+139
-30
No files matched your search
@@ -97,6 +97,13 @@ Continuation tokens are scoped to the action, account, region, and workgroup;
|
||||
invalid sizes, malformed tokens, and tokens outside 1 to 1024 characters return
|
||||
`InvalidRequestException`.
|
||||
|
||||
Query executions are kept per account and region, as AWS keeps them. A query
|
||||
execution is listed and answered only in the region it was started in:
|
||||
`GetQueryExecution`, `GetQueryResults` and `StopQueryExecution` naming one
|
||||
started in another region, or one Athena holds none of, return
|
||||
`InvalidRequestException` with `QueryExecution <id> was not found`. An execution
|
||||
stored before Floci recorded each execution's region is answered in every region.
|
||||
|
||||
A workgroup reports what its request gave and AWS's defaults for the rest. With
|
||||
no `ResultConfiguration`, it reports none (`primary` reports an empty one, as on
|
||||
AWS); a query then names its own output location, and Floci writes results to
|
||||
|
||||
@@ -100,12 +100,12 @@ public class AthenaJsonHandler {
|
||||
}
|
||||
case "GetQueryExecution" -> {
|
||||
String id = request.get("QueryExecutionId").asText();
|
||||
QueryExecution execution = athenaService.getQueryExecution(id);
|
||||
QueryExecution execution = athenaService.getQueryExecution(region, id);
|
||||
yield Response.ok(Map.of("QueryExecution", execution)).build();
|
||||
}
|
||||
case "GetQueryResults" -> {
|
||||
String id = request.get("QueryExecutionId").asText();
|
||||
ResultSet results = athenaService.getQueryResults(id);
|
||||
ResultSet results = athenaService.getQueryResults(region, id);
|
||||
yield Response.ok(Map.of("ResultSet", results)).build();
|
||||
}
|
||||
case "ListQueryExecutions" -> {
|
||||
@@ -125,7 +125,7 @@ public class AthenaJsonHandler {
|
||||
yield Response.ok(response).build();
|
||||
}
|
||||
case "StopQueryExecution" -> {
|
||||
athenaService.stopQueryExecution(request.get("QueryExecutionId").asText());
|
||||
athenaService.stopQueryExecution(region, request.get("QueryExecutionId").asText());
|
||||
yield Response.ok(Map.of()).build();
|
||||
}
|
||||
case "GetWorkGroup" -> {
|
||||
|
||||
@@ -113,7 +113,8 @@ public class AthenaService {
|
||||
private final StorageBackend<String, DataCatalog> dataCatalogStore;
|
||||
private final StorageBackend<String, List<String>> resultColumnTypeStore;
|
||||
// The Region each query execution was started in, by its id: the execution's own record is the API's shape, which
|
||||
// carries none, and IAM checks a request naming the execution on its workgroup in that Region alone.
|
||||
// carries none. Athena answers the execution in that Region alone, and IAM checks a request naming it on its
|
||||
// workgroup there.
|
||||
private final StorageBackend<String, String> queryRegionStore;
|
||||
private final FlociDuckClient duckClient;
|
||||
private final GlueService glueService;
|
||||
@@ -381,10 +382,21 @@ public class AthenaService {
|
||||
return id == null ? Optional.empty() : queryRegionStore.get(id);
|
||||
}
|
||||
|
||||
public QueryExecution getQueryExecution(String id) {
|
||||
/**
|
||||
* The query execution of the id, as Athena answers a request sent to the Region: one started there, or one stored
|
||||
* before Athena recorded each execution's Region, which it answers in every Region as it did then. AWS keeps query
|
||||
* executions per account and Region, so one started in another Region is not found.
|
||||
*/
|
||||
public QueryExecution getQueryExecution(String region, String id) {
|
||||
return queryStore.get(id)
|
||||
.filter(execution -> answeredIn(id, region))
|
||||
.orElseThrow(() -> new AwsException("InvalidRequestException",
|
||||
"Query execution not found: " + id, 400));
|
||||
"QueryExecution " + id + " was not found", 400));
|
||||
}
|
||||
|
||||
/** Whether Athena answers the query execution of the id in the Region: started there, or in one it did not record. */
|
||||
private boolean answeredIn(String id, String region) {
|
||||
return queryRegionStore.get(id).map(startedIn -> startedIn.equals(region)).orElse(true);
|
||||
}
|
||||
|
||||
public List<QueryExecution> listQueryExecutions() {
|
||||
@@ -404,6 +416,7 @@ public class AthenaService {
|
||||
getWorkGroup(group, region);
|
||||
List<QueryExecution> executions = listQueryExecutions().stream()
|
||||
.filter(execution -> group.equals(Objects.requireNonNullElse(execution.getWorkGroup(), DEFAULT_WORKGROUP)))
|
||||
.filter(execution -> answeredIn(execution.getQueryExecutionId(), region))
|
||||
.toList();
|
||||
int limit = maxResults == null || maxResults == 0 ? 50 : maxResults;
|
||||
return Pagination.paginate(executions, QueryExecution::getQueryExecutionId, limit, nextToken,
|
||||
@@ -411,8 +424,8 @@ public class AthenaService {
|
||||
token -> new AwsException("InvalidRequestException", "Invalid NextToken.", 400));
|
||||
}
|
||||
|
||||
public void stopQueryExecution(String id) {
|
||||
QueryExecution execution = getQueryExecution(id);
|
||||
public void stopQueryExecution(String region, String id) {
|
||||
QueryExecution execution = getQueryExecution(region, id);
|
||||
synchronized (execution) {
|
||||
if (isTerminal(execution.getStatus().getState())) {
|
||||
return;
|
||||
@@ -732,8 +745,8 @@ public class AthenaService {
|
||||
return tableMetadata(catalog, database, glueService.getTable(database, tableName));
|
||||
}
|
||||
|
||||
public ResultSet getQueryResults(String id) {
|
||||
QueryExecution execution = getQueryExecution(id);
|
||||
public ResultSet getQueryResults(String region, String id) {
|
||||
QueryExecution execution = getQueryExecution(region, id);
|
||||
|
||||
if (execution.getStatus().getState() != QueryExecutionState.SUCCEEDED) {
|
||||
throw new AwsException("InvalidRequestException", "Query has not succeeded yet", 400);
|
||||
|
||||
@@ -296,7 +296,7 @@ class AthenaIcebergSidecarIntegrationTest {
|
||||
String id = RequestScopes.callAs(ACCOUNT, REGION, () -> athena.startQueryExecution(sql, "primary", context,
|
||||
new ResultConfiguration("s3://" + bucket + "/results/"), parameters));
|
||||
return await().atMost(Duration.ofSeconds(60)).pollInterval(Duration.ofMillis(100))
|
||||
.until(() -> RequestScopes.callAs(ACCOUNT, REGION, () -> athena.getQueryExecution(id)),
|
||||
.until(() -> RequestScopes.callAs(ACCOUNT, REGION, () -> athena.getQueryExecution(REGION, id)),
|
||||
found -> found.getStatus().getState() != QueryExecutionState.RUNNING
|
||||
&& found.getStatus().getState() != QueryExecutionState.QUEUED);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.junit.jupiter.api.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static io.floci.aws.testing.EnforcementFixtures.auth;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
@@ -102,7 +103,8 @@ class AthenaIntegrationTest {
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidRequestException"));
|
||||
.body("__type", equalTo("InvalidRequestException"))
|
||||
.body("message", equalTo("QueryExecution nonexistent-id was not found"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -618,4 +620,52 @@ class AthenaIntegrationTest {
|
||||
.body("message", equalTo("Only one sql statement is allowed. Got: SELECT 1; -- done"))
|
||||
.body("AthenaErrorCode", equalTo("MALFORMED_QUERY"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(20)
|
||||
void aQueryExecutionIsAnsweredOnlyInTheRegionItWasStartedIn() {
|
||||
String id = given()
|
||||
.header("X-Amz-Target", "AmazonAthena.StartQueryExecution")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{ \"QueryString\": \"SELECT 1\" }")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("QueryExecutionId");
|
||||
String elsewhere = auth("test", "eu-west-1", "athena");
|
||||
|
||||
given()
|
||||
.header("Authorization", elsewhere)
|
||||
.header("X-Amz-Target", "AmazonAthena.GetQueryExecution")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{ \"QueryExecutionId\": \"" + id + "\" }")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidRequestException"))
|
||||
.body("message", equalTo("QueryExecution " + id + " was not found"));
|
||||
|
||||
given()
|
||||
.header("Authorization", elsewhere)
|
||||
.header("X-Amz-Target", "AmazonAthena.ListQueryExecutions")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("QueryExecutionIds", not(hasItem(id)));
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "AmazonAthena.GetQueryExecution")
|
||||
.contentType(CONTENT_TYPE)
|
||||
.body("{ \"QueryExecutionId\": \"" + id + "\" }")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("QueryExecution.QueryExecutionId", equalTo(id));
|
||||
}
|
||||
}
|
||||
@@ -85,7 +85,7 @@ class AthenaQueryScopeTest {
|
||||
handedToDuck.get(10, TimeUnit.SECONDS));
|
||||
await().atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofMillis(50)).untilAsserted(() ->
|
||||
assertEquals(QueryExecutionState.SUCCEEDED, RequestScopes.callAs(account, REGION,
|
||||
() -> athena.getQueryExecution(id).getStatus().getState())));
|
||||
() -> athena.getQueryExecution(REGION, id).getStatus().getState())));
|
||||
} finally {
|
||||
RequestScopes.runAs(account, REGION, () -> glueService.deleteDatabase(database));
|
||||
}
|
||||
|
||||
@@ -50,6 +50,8 @@ import static org.mockito.Mockito.when;
|
||||
*/
|
||||
class AthenaServiceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
private Vertx vertx;
|
||||
private FlociDuckClient duckClient;
|
||||
private GlueService glueService;
|
||||
@@ -111,6 +113,43 @@ class AthenaServiceTest {
|
||||
assertTrue(service.findQueryExecutionRegion(id).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aQueryExecutionIsAnsweredOnlyInTheRegionItWasStartedIn() {
|
||||
when(regionResolver.getRegion()).thenReturn("eu-west-1");
|
||||
String id = service.startQueryExecution("SELECT 1", "primary", null, null);
|
||||
|
||||
assertEquals(id, service.getQueryExecution("eu-west-1", id).getQueryExecutionId());
|
||||
String notFound = "QueryExecution " + id + " was not found";
|
||||
AwsException read = assertThrows(AwsException.class, () -> service.getQueryExecution("us-east-1", id));
|
||||
assertEquals("InvalidRequestException", read.getErrorCode());
|
||||
assertEquals(notFound, read.getMessage());
|
||||
assertEquals(notFound, assertThrows(AwsException.class,
|
||||
() -> service.getQueryResults("us-east-1", id)).getMessage());
|
||||
assertEquals(notFound, assertThrows(AwsException.class,
|
||||
() -> service.stopQueryExecution("us-east-1", id)).getMessage());
|
||||
assertTrue(service.listQueryExecutionsPage("eu-west-1", "primary", null, null).items().stream()
|
||||
.anyMatch(execution -> id.equals(execution.getQueryExecutionId())));
|
||||
assertTrue(service.listQueryExecutionsPage("us-east-1", "primary", null, null).items().stream()
|
||||
.noneMatch(execution -> id.equals(execution.getQueryExecutionId())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aQueryExecutionAthenaHoldsNoneOfIsNotFound() {
|
||||
AwsException missing = assertThrows(AwsException.class,
|
||||
() -> service.getQueryExecution(REGION, "not-a-real-id"));
|
||||
assertEquals("InvalidRequestException", missing.getErrorCode());
|
||||
assertEquals("QueryExecution not-a-real-id was not found", missing.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aQueryExecutionStoredBeforeItsRegionWasRecordedIsAnsweredInEveryRegion() {
|
||||
when(regionResolver.getRegion()).thenReturn(null);
|
||||
String id = service.startQueryExecution("SELECT 1", "primary", null, null);
|
||||
|
||||
assertEquals(id, service.getQueryExecution("us-east-1", id).getQueryExecutionId());
|
||||
assertEquals(id, service.getQueryExecution("eu-west-1", id).getQueryExecutionId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void storedTagsReadTheWorkgroupOrTheDataCatalogTheArnNamesInItsAccountAndRegion() {
|
||||
CreateWorkGroupRequest request = new CreateWorkGroupRequest();
|
||||
@@ -145,12 +184,12 @@ class AthenaServiceTest {
|
||||
String id = service.startQueryExecution("SELECT 1", "primary", null, null);
|
||||
assertTrue(started.await(10, TimeUnit.SECONDS), "the worker never started");
|
||||
|
||||
service.stopQueryExecution(id);
|
||||
assertEquals(QueryExecutionState.CANCELLED, service.getQueryExecution(id).getStatus().getState());
|
||||
service.stopQueryExecution(REGION, id);
|
||||
assertEquals(QueryExecutionState.CANCELLED, service.getQueryExecution(REGION, id).getStatus().getState());
|
||||
|
||||
release.countDown();
|
||||
assertTrue(finished.await(10, TimeUnit.SECONDS), "the worker never finished");
|
||||
assertEquals(QueryExecutionState.CANCELLED, service.getQueryExecution(id).getStatus().getState(),
|
||||
assertEquals(QueryExecutionState.CANCELLED, service.getQueryExecution(REGION, id).getStatus().getState(),
|
||||
"the worker must not overwrite a cancellation");
|
||||
}
|
||||
|
||||
@@ -169,10 +208,10 @@ class AthenaServiceTest {
|
||||
String id = service.startQueryExecution("SELECT 1", "primary", null, null);
|
||||
assertTrue(started.await(10, TimeUnit.SECONDS), "the worker never started");
|
||||
|
||||
service.stopQueryExecution(id);
|
||||
service.stopQueryExecution(REGION, id);
|
||||
release.countDown();
|
||||
assertTrue(finished.await(10, TimeUnit.SECONDS), "the worker never finished");
|
||||
assertEquals(QueryExecutionState.CANCELLED, service.getQueryExecution(id).getStatus().getState(),
|
||||
assertEquals(QueryExecutionState.CANCELLED, service.getQueryExecution(REGION, id).getStatus().getState(),
|
||||
"a worker failure must not overwrite a cancellation");
|
||||
}
|
||||
|
||||
@@ -181,9 +220,9 @@ class AthenaServiceTest {
|
||||
String id = service.startQueryExecution("SELECT 1", "primary", null, null);
|
||||
|
||||
awaitState(id, QueryExecutionState.SUCCEEDED);
|
||||
service.stopQueryExecution(id);
|
||||
service.stopQueryExecution(REGION, id);
|
||||
|
||||
assertEquals(QueryExecutionState.SUCCEEDED, service.getQueryExecution(id).getStatus().getState(),
|
||||
assertEquals(QueryExecutionState.SUCCEEDED, service.getQueryExecution(REGION, id).getStatus().getState(),
|
||||
"a late stop must not cancel a finished query");
|
||||
}
|
||||
|
||||
@@ -196,9 +235,9 @@ class AthenaServiceTest {
|
||||
String id = service.startQueryExecution("SELECT 1", "primary", null, null);
|
||||
|
||||
awaitState(id, QueryExecutionState.FAILED);
|
||||
service.stopQueryExecution(id);
|
||||
service.stopQueryExecution(REGION, id);
|
||||
|
||||
assertEquals(QueryExecutionState.FAILED, service.getQueryExecution(id).getStatus().getState(),
|
||||
assertEquals(QueryExecutionState.FAILED, service.getQueryExecution(REGION, id).getStatus().getState(),
|
||||
"a late stop must not cancel a failed query");
|
||||
}
|
||||
|
||||
@@ -307,7 +346,7 @@ class AthenaServiceTest {
|
||||
new ResultConfiguration("s3://athena-results/"));
|
||||
awaitState(id, QueryExecutionState.SUCCEEDED);
|
||||
|
||||
assertEquals("SELECT 1 AS a -- done", service.getQueryExecution(id).getQuery());
|
||||
assertEquals("SELECT 1 AS a -- done", service.getQueryExecution(REGION, id).getQuery());
|
||||
verify(duckClient).execute(eq("SELECT 1 AS a -- done\n"), any(), any(), any());
|
||||
}
|
||||
|
||||
@@ -317,7 +356,7 @@ class AthenaServiceTest {
|
||||
new ResultConfiguration("s3://athena-results/"), List.of("'eu'", "10"));
|
||||
awaitState(id, QueryExecutionState.SUCCEEDED);
|
||||
|
||||
assertEquals("SELECT ? AS region, ? AS n", service.getQueryExecution(id).getQuery());
|
||||
assertEquals("SELECT ? AS region, ? AS n", service.getQueryExecution(REGION, id).getQuery());
|
||||
verify(duckClient).execute(eq("SELECT 'eu' AS region, 10 AS n\n"), any(), any(), any());
|
||||
}
|
||||
|
||||
@@ -327,7 +366,7 @@ class AthenaServiceTest {
|
||||
awaitState(id, QueryExecutionState.FAILED);
|
||||
|
||||
assertEquals("INVALID_PARAMETER_USAGE: line 1:1: Incorrect number of parameters: expected 2 but found 1",
|
||||
service.getQueryExecution(id).getStatus().getStateChangeReason());
|
||||
service.getQueryExecution(REGION, id).getStatus().getStateChangeReason());
|
||||
verifyNoInteractions(duckClient);
|
||||
}
|
||||
|
||||
@@ -353,7 +392,7 @@ class AthenaServiceTest {
|
||||
String unfiltered = service.startQueryExecution(query, "primary", context("analytics"),
|
||||
new ResultConfiguration("s3://athena-results/"), List.of("tenant || ''"));
|
||||
awaitState(unfiltered, QueryExecutionState.FAILED);
|
||||
assertTrue(service.getQueryExecution(unfiltered).getStatus().getStateChangeReason()
|
||||
assertTrue(service.getQueryExecution(REGION, unfiltered).getStatus().getStateChangeReason()
|
||||
.startsWith("CONSTRAINT_VIOLATION: For the injected projected partition column tenant"));
|
||||
verifyNoInteractions(duckClient);
|
||||
|
||||
@@ -397,23 +436,23 @@ class AthenaServiceTest {
|
||||
String id = service.startQueryExecution(query, "primary", null,
|
||||
new ResultConfiguration("s3://athena-results/"));
|
||||
awaitState(id, QueryExecutionState.SUCCEEDED);
|
||||
String outputLocation = service.getQueryExecution(id).getResultConfiguration().getOutputLocation();
|
||||
String outputLocation = service.getQueryExecution(REGION, id).getResultConfiguration().getOutputLocation();
|
||||
String key = outputLocation.substring("s3://athena-results/".length());
|
||||
S3Object result = new S3Object("athena-results", key, csv.getBytes(StandardCharsets.UTF_8), "text/csv");
|
||||
when(s3Service.listObjects(eq("athena-results"), anyString(), any(), anyInt())).thenReturn(List.of(result));
|
||||
when(s3Service.getObject("athena-results", key)).thenReturn(result);
|
||||
|
||||
return service.getQueryResults(id);
|
||||
return service.getQueryResults(REGION, id);
|
||||
}
|
||||
|
||||
private void awaitState(String id, QueryExecutionState expected) throws InterruptedException {
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
|
||||
while (System.nanoTime() < deadline) {
|
||||
if (service.getQueryExecution(id).getStatus().getState() == expected) {
|
||||
if (service.getQueryExecution(REGION, id).getStatus().getState() == expected) {
|
||||
return;
|
||||
}
|
||||
Thread.sleep(10);
|
||||
}
|
||||
assertEquals(expected, service.getQueryExecution(id).getStatus().getState());
|
||||
assertEquals(expected, service.getQueryExecution(REGION, id).getStatus().getState());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user