From 2ef7ef0b7a3568e272b7d7ec67db1356d3f50d19 Mon Sep 17 00:00:00 2001 From: Alessandro Lattao Date: Thu, 22 Oct 2026 12:51:13 +0200 Subject: [PATCH] fix(dsql): run each transaction age check on a thread of its own (#6216) The endpoint's one dsql-age-checks thread both timed the sessions' age checks and ran them, and a check can block. Ending a session writes the age error under clientWrites, which the relay holds while it writes, and waits for the client to take it; stopping a statement sends the database a cancel request. One client that stopped reading mid-result could then hold back every other session's check. The thread now only times the checks, and each due check runs on a virtual thread of its own. --- .../services/dsql/dataplane/DsqlEndpoint.java | 10 ++- .../dsql/dataplane/DsqlEndpointTest.java | 62 +++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/dsql/dataplane/DsqlEndpointTest.java 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.java index 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> 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 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 { @PreDestroy public 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.java new file mode 100644 index 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 = 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(); + } + } +}