mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
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:
1 parent
c8629d1c48
commit
2ef7ef0b7a
2 files changed
+70
-2
No files matched your search
@@ -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 {
|
||||
@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;
|
||||
}
|
||||
|
||||
|
||||
+62
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user