Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(athena): keep terminal queries terminal (#4112)

Daniël van Ziel75dd057b5cc4
diff --git a/src/main/java/io/github/hectorvent/floci/services/athena/AthenaService.java b/src/main/java/io/github/hectorvent/floci/services/athena/AthenaService.java
index 3f88b10631..1fe39c2bb8 100644
--- a/src/main/java/io/github/hectorvent/floci/services/athena/AthenaService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/athena/AthenaService.java
@@ -216,9 +216,21 @@ public class AthenaService {
public void stopQueryExecution(String id) {
QueryExecution execution = getQueryExecution(id);
- execution.getStatus().setState(QueryExecutionState.CANCELLED);
- execution.getStatus().setCompletionDateTime(Instant.now());
- queryStore.put(id, execution);
+ synchronized (execution) {
+ if (isTerminal(execution.getStatus().getState())) {
+ return;
+ }
+ execution.getStatus().setState(QueryExecutionState.CANCELLED);
+ execution.getStatus().setCompletionDateTime(Instant.now());
+ queryStore.put(id, execution);
+ }
+ }
+
+ /** A terminal query is never rewritten: a late stop cannot cancel a finished query. */
+ private static boolean isTerminal(QueryExecutionState state) {
+ return state == QueryExecutionState.SUCCEEDED
+ || state == QueryExecutionState.FAILED
+ || state == QueryExecutionState.CANCELLED;
}
public WorkGroup createWorkGroup(CreateWorkGroupRequest request, String region) {
@@ -569,15 +581,25 @@ public class AthenaService {
}
private void markSucceeded(String id, QueryExecution execution) {
- execution.getStatus().setState(QueryExecutionState.SUCCEEDED);
- execution.getStatus().setCompletionDateTime(Instant.now());
- queryStore.put(id, execution);
+ synchronized (execution) {
+ if (isTerminal(execution.getStatus().getState())) {
+ return;
+ }
+ execution.getStatus().setState(QueryExecutionState.SUCCEEDED);
+ execution.getStatus().setCompletionDateTime(Instant.now());
+ queryStore.put(id, execution);
+ }
}
private void markFailed(String id, QueryExecution execution, Throwable failure) {
- execution.getStatus().setState(QueryExecutionState.FAILED);
- execution.getStatus().setStateChangeReason(failure.getMessage());
- queryStore.put(id, execution);
+ synchronized (execution) {
+ if (isTerminal(execution.getStatus().getState())) {
+ return;
+ }
+ execution.getStatus().setState(QueryExecutionState.FAILED);
+ execution.getStatus().setStateChangeReason(failure.getMessage());
+ queryStore.put(id, execution);
+ }
}
private record CreateDatabaseDdl(String name, boolean ifNotExists, String comment,
diff --git a/src/test/java/io/github/hectorvent/floci/services/athena/AthenaIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/athena/AthenaIntegrationTest.java
index ce70355874..8f2d1bacf8 100644
--- a/src/test/java/io/github/hectorvent/floci/services/athena/AthenaIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/athena/AthenaIntegrationTest.java
@@ -516,4 +516,42 @@ class AthenaIntegrationTest {
.body("QueryExecution.StatementType", equalTo("DDL"))
.body("QueryExecution.ResultConfiguration", nullValue());
}
+
+ @Test
+ @Order(16)
+ void stopQueryExecutionLeavesAFinishedQuerySucceeded() {
+ String id = given()
+ .header("X-Amz-Target", "AmazonAthena.StartQueryExecution")
+ .contentType(CONTENT_TYPE)
+ .body("""
+ {
+ "QueryString": "SELECT 1",
+ "WorkGroup": "primary"
+ }
+ """)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().path("QueryExecutionId");
+
+ given()
+ .header("X-Amz-Target", "AmazonAthena.StopQueryExecution")
+ .contentType(CONTENT_TYPE)
+ .body("{ \"QueryExecutionId\": \"" + id + "\" }")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("X-Amz-Target", "AmazonAthena.GetQueryExecution")
+ .contentType(CONTENT_TYPE)
+ .body("{ \"QueryExecutionId\": \"" + id + "\" }")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body("QueryExecution.Status.State", equalTo("SUCCEEDED"));
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/athena/AthenaServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/athena/AthenaServiceTest.java
new file mode 100644
index 0000000000..8d4ffc3e63
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/athena/AthenaServiceTest.java
@@ -0,0 +1,151 @@
+package io.github.hectorvent.floci.services.athena;
+
+import io.github.hectorvent.floci.config.EmulatorConfig;
+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;
+import io.github.hectorvent.floci.services.floci.duck.FlociDuckClient;
+import io.github.hectorvent.floci.services.glue.GlueService;
+import io.github.hectorvent.floci.services.s3.S3Service;
+import io.vertx.core.Vertx;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * A {@code StopQueryExecution} must stay terminal. The async worker used to overwrite whatever
+ * {@code stopQueryExecution} wrote, so a stopped query later reported {@code SUCCEEDED} or
+ * {@code FAILED}. These tests drive the worker with a stubbed duck client.
+ */
+class AthenaServiceTest {
+
+ private Vertx vertx;
+ private FlociDuckClient duckClient;
+ private AthenaService service;
+
+ @BeforeEach
+ void setUp() {
+ vertx = Vertx.vertx();
+ duckClient = mock(FlociDuckClient.class);
+
+ StorageFactory storageFactory = mock(StorageFactory.class);
+ doReturn(AccountAwareStorageBackend.inMemory("000000000000"))
+ .when(storageFactory).create(anyString(), anyString(), any());
+
+ GlueService glueService = mock(GlueService.class);
+ when(glueService.getTables(anyString())).thenReturn(List.of());
+ S3Service s3Service = mock(S3Service.class);
+ GlueViewDdlBuilder ddlBuilder = mock(GlueViewDdlBuilder.class);
+
+ EmulatorConfig config = mock(EmulatorConfig.class);
+ EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
+ EmulatorConfig.AthenaServiceConfig athena = mock(EmulatorConfig.AthenaServiceConfig.class);
+ when(config.services()).thenReturn(services);
+ when(services.athena()).thenReturn(athena);
+ when(athena.mock()).thenReturn(false);
+
+ service = new AthenaService(storageFactory, duckClient, glueService, s3Service,
+ config, vertx, ddlBuilder);
+ }
+
+ @AfterEach
+ void tearDown() {
+ vertx.close();
+ }
+
+ @Test
+ void stopDuringExecutionKeepsCancellationTerminal() throws Exception {
+ CountDownLatch started = new CountDownLatch(1);
+ CountDownLatch release = new CountDownLatch(1);
+ CountDownLatch finished = new CountDownLatch(1);
+ doAnswer(invocation -> {
+ started.countDown();
+ release.await(10, TimeUnit.SECONDS);
+ finished.countDown();
+ return null;
+ }).when(duckClient).execute(anyString(), any(), any());
+
+ 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());
+
+ release.countDown();
+ assertTrue(finished.await(10, TimeUnit.SECONDS), "the worker never finished");
+ assertEquals(QueryExecutionState.CANCELLED, service.getQueryExecution(id).getStatus().getState(),
+ "the worker must not overwrite a cancellation");
+ }
+
+ @Test
+ void stopDuringExecutionStaysTerminalWhenTheWorkerFails() throws Exception {
+ CountDownLatch started = new CountDownLatch(1);
+ CountDownLatch release = new CountDownLatch(1);
+ CountDownLatch finished = new CountDownLatch(1);
+ doAnswer(invocation -> {
+ started.countDown();
+ release.await(10, TimeUnit.SECONDS);
+ finished.countDown();
+ throw new IllegalStateException("duck failed");
+ }).when(duckClient).execute(anyString(), any(), any());
+
+ String id = service.startQueryExecution("SELECT 1", "primary", null, null);
+ assertTrue(started.await(10, TimeUnit.SECONDS), "the worker never started");
+
+ service.stopQueryExecution(id);
+ release.countDown();
+ assertTrue(finished.await(10, TimeUnit.SECONDS), "the worker never finished");
+ assertEquals(QueryExecutionState.CANCELLED, service.getQueryExecution(id).getStatus().getState(),
+ "a worker failure must not overwrite a cancellation");
+ }
+
+ @Test
+ void stopAfterCompletionLeavesTheExecutionSucceeded() throws Exception {
+ String id = service.startQueryExecution("SELECT 1", "primary", null, null);
+
+ awaitState(id, QueryExecutionState.SUCCEEDED);
+ service.stopQueryExecution(id);
+
+ assertEquals(QueryExecutionState.SUCCEEDED, service.getQueryExecution(id).getStatus().getState(),
+ "a late stop must not cancel a finished query");
+ }
+
+ @Test
+ void stopAfterFailureLeavesTheExecutionFailed() throws Exception {
+ doAnswer(invocation -> {
+ throw new IllegalStateException("duck failed");
+ }).when(duckClient).execute(anyString(), any(), any());
+
+ String id = service.startQueryExecution("SELECT 1", "primary", null, null);
+
+ awaitState(id, QueryExecutionState.FAILED);
+ service.stopQueryExecution(id);
+
+ assertEquals(QueryExecutionState.FAILED, service.getQueryExecution(id).getStatus().getState(),
+ "a late stop must not cancel a failed query");
+ }
+
+ 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) {
+ return;
+ }
+ Thread.sleep(10);
+ }
+ assertEquals(expected, service.getQueryExecution(id).getStatus().getState());
+ }
+}
\ No newline at end of file