chore(qwp): drain the old connection's buffered durable ack on failover reconnect#63
Closed
RaphDal wants to merge 1 commit into
Closed
chore(qwp): drain the old connection's buffered durable ack on failover reconnect#63RaphDal wants to merge 1 commit into
RaphDal wants to merge 1 commit into
Conversation
Contributor
[PR Coverage check]😍 pass : 13 / 13 (100.00%) file detail
|
Contributor
Author
|
Superseeded by questdb/questdb#7366 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The QWP store-and-forward
Sender(request_durable_ack=on+ multi-hostfailover=on) could replay frames it had already received a durable ACK for. On a failover reconnect,CursorWebSocketSendLoop.swapClient(...)closed the old connection and repositioned the replay cursor atackedFsn + 1without first draining the old connection's receive buffer. A finalSTATUS_DURABLE_ACKthat had arrived on the old wire but was not yet processed was discarded, soackedFsnstayed stale and the loop replayed frames the server had already confirmed durable.Where the failover target already holds those rows (an HA / replicated deployment) and the table has no
DEDUPkeys, this would surface as duplicate rows. The client invariant "a durably-acked frame is never re-sent" is violated.Root cause
At a role change, a demoting / handing-off primary defers its close and flushes a final covering
STATUS_DURABLE_ACKbefore it closes — precisely so a store-and-forward client won't replay already-durable work. But the client fails over on the earlier read-only (STATUS_NOT_WRITABLE) rejection, which fires before that ACK exists. By the time the server flushes the covering ACK, the I/O thread is parked inconnectLoop, so the ACK lands unread in the old socket's recv buffer.swapClientthen closes that socket and repositions atackedFsn + 1without reading it — the ACK is discarded and the loop rewinds and replays.This gap has existed since the store-and-forward client was introduced (
edbdc3a8, #17):swapClienthas never drained the old connection before repositioning. It became reachable as a duplicate once the server began flushing a covering durable ACK on a role-change close (QwpIngressUpgradeProcessor.roleChangeCloseWithUploadGrace, questdb core #7341).The fix
Before closing the old connection and computing
replayStart,swapClientnow drains the old socket's recv buffer and applies any buffered durable ACKs, advancingackedFsnfirst. The drain uses a dedicatedSwapDrainHandlerthat applies durable ACKs only and no-ops everything else:fail()/connectLoopwhile a failover is already in progress.The drain is best-effort (any exception is swallowed — the connection is going away regardless), and it is a no-op in non-durable mode. Genuinely unacked frames are untouched and still replay, exactly as before (covered by
ReconnectTest).Test
CursorWebSocketSendLoopReconnectDurableAckDrainTest— a deterministic, thread-free regression at the send-loop level (same style asCursorWebSocketSendLoopDurableAckTest): the loop is built but never started, the oldWebSocketClientcarries one covering durable ACK followed by a CLOSE (the exact shape a demoting primary leaves buffered), three OK'd-but-not-yet-durable frames sit in the pending queue, thenswapClientruns. It asserts the ACK is drained and applied (ackedFsnadvances past those frames; the cursor does not rewind) and that the trailing CLOSE does not re-enter failover.ackedFsn == -1— the ACK is dropped and the loop rewinds to FSN 0.Full run:
CursorWebSocketSendLoop*Test,ReconnectTest,CursorSendEngineTest,AckWatermarkTest— 100 tests, 0 failures.
Scope / a deeper follow-up (not in this PR)
This is the surgical fix: it closes the discard where it happens and is a safety net you want regardless. A more robust alternative — call it "wait out the deferral" — would keep the client on the demoting connection on a
NOT_WRITABLErejection (stop sending, keep draining ACKs + PING) until the final ACK + CLOSE arrive, then fail over. That honors the server's deferral contract end-to-end and is robust to the ack landing after the swap or to a reset, but it reworks theNOT_WRITABLEhandling, which is a carefully-tuned state machine (all-replica churn protection, pacing, poison-strike bounds). That belongs in a separate, maintainer-reviewed change; this PR does not touch it. The drain here composes cleanly with that follow-up if it lands later.References
core/.../qwp/client/sf/cursor/CursorWebSocketSendLoop.java—swapClient, newSwapDrainHandleredbdc3a8(feat(ilp): QWiP store-and-forward client buffer #17); reachable as a duplicate since questdb core #7341