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.
This commit is contained in:
Alessandro Lattao authored and GitHub committed 2026-10-22 06:51:13 -04:00
1 parent c8629d1c48
commit 2ef7ef0b7a
2 files changed
+70 -2

No files matched your search

@@ -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();
}
}
}