Light, fluffy, and always free - The AWS Local Emulator alternative
fix(dsql): run each transaction age check on a thread of its own (#6216)
Alessandro Lattao2ef7ef0b7a35
diff --git a/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.java b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.javaindex 01edbd27f5..d06e08c52a 100644--- a/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.java+++ b/src/main/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpoint.java@@ -39,6 +39,7 @@ import java.util.Locale;import java.util.Map;import java.util.Set;import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.ScheduledFuture;@@ -77,9 +78,13 @@ public class DsqlEndpoint {private final Map<String, Set<Socket>> connections = new ConcurrentHashMap<>();private volatile ServerSocket server;private final Clock clock;- // The sessions' age checks, each due by the clock, on one thread for the whole endpoint.+ // The sessions' age checks, timed by the clock on one thread for the whole endpoint. Each runs on a+ // thread of its own: ending a session waits for its client to take what it is sent, and for its+ // database, and a client that stops reading must not hold back the other sessions' checks.private final ScheduledExecutorService ageChecks = Executors.newSingleThreadScheduledExecutor(Thread.ofVirtual().name("dsql-age-checks").factory());+ private final ExecutorService ageCheckRuns = Executors.newThreadPerTaskExecutor(+ Thread.ofVirtual().name("dsql-age-check-", 0).factory());private final Set<ScheduledCheck> pendingChecks = ConcurrentHashMap.newKeySet();// DSQL's (measured): a transaction lives 300 seconds, and one idle past that is ended 15 later.private static final Duration TRANSACTION_AGE_LIMIT = Duration.ofSeconds(300);@@ -143,6 +148,7 @@ public class DsqlEndpoint {@PreDestroypublic synchronized void stop() {ageChecks.shutdownNow();+ ageCheckRuns.shutdownNow();pendingChecks.clear();ServerSocket listening = server;server = null;@@ -234,7 +240,7 @@ public class DsqlEndpoint {pendingChecks.add(scheduled);// toMillis drops what is below a millisecond, which would run the check just before it is due.long delay = Math.max(0, Duration.between(clock.instant(), due).toMillis() + 1);- scheduled.future = ageChecks.schedule(scheduled::run, delay, TimeUnit.MILLISECONDS);+ scheduled.future = ageChecks.schedule(() -> ageCheckRuns.execute(scheduled::run), delay, TimeUnit.MILLISECONDS);return scheduled;}diff --git a/src/test/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpointTest.java b/src/test/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpointTest.javanew file mode 100644index 0000000000..d36eb1c52e--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpointTest.java@@ -0,0 +1,62 @@+package io.github.hectorvent.floci.services.dsql.dataplane;++import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;+import io.github.hectorvent.floci.services.iam.IamService;+import io.github.hectorvent.floci.services.iam.ScpProvider;+import io.github.hectorvent.floci.testing.MutableClock;+import jakarta.enterprise.inject.Instance;+import org.junit.jupiter.api.AfterEach;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++import java.util.concurrent.CountDownLatch;+import java.util.concurrent.TimeUnit;++import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;+import static org.mockito.Mockito.mock;++class DsqlEndpointTest {++ private final MutableClock clock = new MutableClock();+ private DsqlEndpoint endpoint;++ @BeforeEach+ void createEndpoint() {+ @SuppressWarnings("unchecked")+ Instance<ScpProvider> scpProvider = mock(Instance.class);+ endpoint = new DsqlEndpoint(mock(DsqlTlsCertificates.class), mock(IamService.class),+ mock(IamPolicyEvaluator.class), scpProvider, mock(EmulatorConfig.class, RETURNS_DEEP_STUBS), clock);+ }++ @AfterEach+ void stopEndpoint() {+ endpoint.stop();+ }++ /**+ * Ending a session waits for its client to take what it is sent, so the check of a session whose+ * client stopped reading can block; another session's check due with it still runs on time.+ */+ @Test+ void aCheckThatBlocksHoldsBackNoOtherCheck() throws InterruptedException {+ CountDownLatch release = new CountDownLatch(1);+ CountDownLatch otherRan = new CountDownLatch(1);+ try {+ endpoint.schedule(clock.instant(), () -> {+ try {+ release.await();+ } catch (InterruptedException ignored) {+ // Stopping the endpoint interrupts the check, which then ends as a blocked write would.+ Thread.currentThread().interrupt();+ }+ });+ endpoint.schedule(clock.instant(), otherRan::countDown);++ assertTrue(otherRan.await(10, TimeUnit.SECONDS), "the second check must not wait for the first");+ } finally {+ release.countDown();+ }+ }+}