mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(redshift): stay queued for backend ownership until the deadline (#6634)
* fix(redshift): stay queued for backend ownership until the deadline runWithBackendOwned waited for the fair backend lock 200 ms at a time, the length of the pump's idle read. A wait that ran out while a read was in progress left the queue, and the pump takes the lock back as soon as it releases it, so the request could miss every handoff until the 2 s deadline. Every Simple Query waits for ownership while the Spectrum interceptor inspects it. When the handoffs were missed, the query stalled for the full 2 s, and one over an external table then failed. External-table preparation on Parse and Bind failed with "Unable to prepare external query", a Simple Query S3 COPY or UNLOAD went to PostgreSQL unintercepted and failed there, and closing an abandoned Spectrum cursor kept its temp table until the session ended. The wait now spans the rest of the deadline in one attempt, as the Extended Query Execute paths already do, so the request keeps its place and takes the lock at the pump's next release. The idle check, the mid-frame backoff and the deadline itself are unchanged. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * refactor(redshift): drop the unreachable retry after a timed-out lock wait runWithBackendOwned now waits for the backend lock once, for the rest of the deadline. A timed tryLock returns false only after that whole budget has elapsed, so the deadline check that followed always held and its continue could not run. Return false straight away, with the same warning; behaviour is unchanged. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(redshift): hand the backend lock to the bridge through a constructor RedshiftBackendOwnershipTest replaced the bridge's private final backendLock by reflection, which later JDKs warn about. The public constructors now pass a new fair lock to a private constructor, and a package-private one for tests takes the lock with no services, so the test builds its bridge directly. Production bridges get the same fair lock as before. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(redshift): run the ownership control test on a short budget The control case in RedshiftBackendOwnershipTest, where the pump never releases the lock, sat out the real 2 s ownership deadline on every run. The test constructor now also takes the runWithBackendOwned budget: the control case uses 100 ms, and the handoff case keeps the production 2 s. Production bridges still get PUMP_PARK_WAIT_MS, now package-private so the test can name it. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> --------- Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
1 parent
b2c2c0c37c
commit
8b13c75fa2
2 files changed
+170
-11
No files matched your search
+133
@@ -0,0 +1,133 @@
|
||||
package io.github.hectorvent.floci.services.redshift.proxy;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class RedshiftBackendOwnershipTest {
|
||||
|
||||
// Taking ownership only sets the backend socket's read timeout, so unconnected sockets will do.
|
||||
private final Socket client = new Socket();
|
||||
private final Socket backend = new Socket();
|
||||
|
||||
@AfterEach
|
||||
void closeSockets() throws IOException {
|
||||
client.close();
|
||||
backend.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* The pump holds the backend lock for each idle read and takes it back as soon as it releases it. A
|
||||
* waiter whose timed attempt runs out while a read is in progress resumes only after the pump has
|
||||
* started its next read, so it must stay queued across that read to be handed the backend.
|
||||
*/
|
||||
@Test
|
||||
@Timeout(10)
|
||||
void retainsItsPlaceWhileAnIdlePumpFinishesReading() throws Exception {
|
||||
DelayedWaiterLock lock = new DelayedWaiterLock();
|
||||
RedshiftInterceptingBridge bridge = new RedshiftInterceptingBridge(client, backend, lock,
|
||||
RedshiftInterceptingBridge.PUMP_PARK_WAIT_MS);
|
||||
AtomicBoolean stopped = new AtomicBoolean();
|
||||
AtomicReference<Throwable> pumpFailure = new AtomicReference<>();
|
||||
Thread pump = Thread.ofVirtual().start(() -> {
|
||||
try {
|
||||
while (!stopped.get()) {
|
||||
lock.lockInterruptibly();
|
||||
try {
|
||||
lock.readStarted.release();
|
||||
// Outlasts one 200 ms lock attempt (PUMP_READ_TIMEOUT_MS), well inside the 2 s deadline.
|
||||
lock.endRead.tryAcquire(350, TimeUnit.MILLISECONDS);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException expected) {
|
||||
// The test stops the pump by interrupting it.
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (Throwable failure) {
|
||||
pumpFailure.set(failure);
|
||||
}
|
||||
});
|
||||
try {
|
||||
assertTrue(lock.readStarted.tryAcquire(1, TimeUnit.SECONDS));
|
||||
assertTrue(bridge.runWithBackendOwned(() -> true),
|
||||
"an idle pump must yield the backend before the ownership deadline");
|
||||
assertNull(pumpFailure.get());
|
||||
} finally {
|
||||
stopped.set(true);
|
||||
pump.interrupt();
|
||||
pump.join(1000);
|
||||
assertFalse(pump.isAlive());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(10)
|
||||
void doesNotRunAnExchangeWhenTheBackendRemainsOwned() throws Exception {
|
||||
ReentrantLock lock = new ReentrantLock(true);
|
||||
// A short budget is enough to show the call gives up; the handoff case keeps the production one.
|
||||
RedshiftInterceptingBridge bridge = new RedshiftInterceptingBridge(client, backend, lock, 100);
|
||||
CountDownLatch held = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
Thread pump = Thread.ofVirtual().start(() -> {
|
||||
lock.lock();
|
||||
try {
|
||||
held.countDown();
|
||||
release.await();
|
||||
} catch (InterruptedException expected) {
|
||||
// Only an interrupt from outside the test could end the wait early; keep its status.
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
});
|
||||
try {
|
||||
assertTrue(held.await(1, TimeUnit.SECONDS));
|
||||
AtomicBoolean exchanged = new AtomicBoolean();
|
||||
assertFalse(bridge.runWithBackendOwned(() -> {
|
||||
exchanged.set(true);
|
||||
return true;
|
||||
}));
|
||||
assertFalse(exchanged.get());
|
||||
} finally {
|
||||
release.countDown();
|
||||
pump.join(1000);
|
||||
assertFalse(pump.isAlive());
|
||||
}
|
||||
}
|
||||
|
||||
private static final class DelayedWaiterLock extends ReentrantLock {
|
||||
private final Semaphore readStarted = new Semaphore(0);
|
||||
private final Semaphore endRead = new Semaphore(0);
|
||||
|
||||
private DelayedWaiterLock() {
|
||||
super(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
|
||||
boolean acquired = super.tryLock(timeout, unit);
|
||||
if (!acquired) {
|
||||
// The timed-out waiter resumes only after the pump has finished this read and started the next.
|
||||
endRead.release();
|
||||
if (!readStarted.tryAcquire(1, TimeUnit.SECONDS)) {
|
||||
throw new AssertionError("pump did not resume");
|
||||
}
|
||||
}
|
||||
return acquired;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user