Skip to content

feat(qwp): send batches of any size, with strict transactions and rollback - #127

Open
bluestreak01 wants to merge 11 commits into
mainfrom
vi_qwp_large_txn
Open

bluestreak01 wants to merge 11 commits into
mainfrom
vi_qwp_large_txn

Conversation

@bluestreak01

@bluestreak01 bluestreak01 commented Nov 11, 2026 •

Copy link
Copy Markdown
Member
  • Default WebSocket senders no longer reject batches of wide rows with BatchTooLargeForCapException.
  • New transaction=strict commits only on flush(), at any size.
  • On WebSocket, auto_flush=off and auto_flush_rows=off;auto_flush_interval=off both select strict mode.
  • Strict transactions never commit in parts; rows past a limit fail with TransactionTooLargeException.
  • In a pool, strict transactions beyond one segment get sf_max_total_bytes / sender_pool_max each.
  • transaction=on transactions larger than sf_max_total_bytes commit in parts instead of hanging 30 s.
  • New Sender.rollback() discards the open transaction, including rows already sent, on any server.
  • reset() in transactional mode now discards rows already sent, except transaction=on on older servers.
  • On servers without rollback support, discarding rows already sent costs one reconnect.
  • Behaviour change: an unset auto_flush_bytes now flushes at 90% of the frame size.
  • Behaviour change: with row and interval triggers off, close() discards unflushed rows instead of committing.
  • Behaviour change: with both triggers off, the sender refuses rows past qwp.max.uncommitted.rows per table.
  • Behaviour change: transaction=off with row and interval triggers both off fails at build.

The byte trigger now sizes each batch from the frame the connection can carry: the server's advertised X-QWP-Max-Batch-Size, capped by the store-and-forward segment, or the server's 2 MiB default when nothing is advertised. An explicit auto_flush_bytes=off keeps the old behaviour: no byte trigger.

The builder picks the transaction mode from the auto-flush triggers, not from how they were turned off, so both spellings build the same sender, as they already do on HTTP. A sender with no row or interval trigger and no positive auto_flush_bytes commits only on flush(): it is strict unless transaction=on keeps it non-strict. Closing it, or returning its pooled lease, without flush() discards the open transaction and logs a warning with the row count.

Strict mode ships rows as deferred frames like transaction=on, but refuses a row that would take a table past the server's advertised X-QWP-Max-Uncommitted-Rows (1M when not advertised), or the transaction past the client buffer, which must hold it until the server commits it; the refused row is never part of the transaction, and the error names the setting to raise. A table's count covers the rows sent under every name that differs from its own only in letter case, as the server counts them together. An auto-flush that finds no room keeps its rows pending in the room reserved for the commit, so the next row is the one that fails. The senders of a pool share sf_max_total_bytes and may all hold strict transactions at once, so a transaction whose frames span more than one segment counts only on its sender's share: the buffer divided by sender_pool_max, less with sf_dir the segment created ahead of the next rotation. Sized against the whole buffer, two leases could fill it together and each commit would wait for room only the other frees. A transaction within one segment keeps the whole buffer, as it needs no more than the two segments every sender always gets. close() without flush() discards the transaction, and a pooled lease discards it on return.

A transaction=on sender now tracks how its open transaction fills the store-and-forward buffer, as strict mode does. Before a flush that would not fit beside the frames already sent, it commits them with a bare commit frame and logs a warning, as the server commits at qwp.max.uncommitted.rows, and rollback() warns that those parts stay; every deferred flush keeps room for that frame, so it never waits for the buffer to drain.

rollback() hands the QWP rollback control frame to store-and-forward without waiting for the server, as flush() does, so an outage never stalls or fails it, even before the first connection. The frame goes out behind the transaction's frames and ahead of anything sent later. The producer cannot know which server the frame will reach, so the I/O loop decides per connection: it sends the frame only to a server that confirmed X-QWP-Rollback: enabled, and on a connection to any other server it waits for the acks of earlier commits, then closes the connection, which makes the server discard the rolled-back frames, and reconnects. reset() and the return of a strict pooled lease discard the same way, so the pool keeps its sender. reset() of a transaction=on transaction keeps the rows already sent, as before, only after a handshake showed a server without rollback support.

Tandem: questdb/questdb#8343

The WebSocket sender's default byte trigger now sizes every batch to 90%
of the frame the connection can carry: the smaller of the server's
advertised X-QWP-Max-Batch-Size and the store-and-forward segment
payload, or the server's default cap when nothing is advertised. Rows
wider than about 2 KiB no longer fail with BatchTooLargeForCapException,
in streaming and in transactional mode. auto_flush_bytes=off and a
sender with every auto-flush trigger off keep the old behaviour. New
symbols count toward the trigger, since their strings ride in the
frame's dictionary section.

Add strict transactional mode: transaction=strict, or auto_flush=off on
WebSocket. Rows go out as deferred frames sized for the connection and
only flush() commits, so a transaction can be any size, but it never
commits in parts: a row that would take a table to the server's
qwp.max.uncommitted.rows (advertised in X-QWP-Max-Uncommitted-Rows, or
the 1M default) and a transaction that outgrows sf_max_total_bytes are
refused with TransactionTooLargeException before anything reaches the
server, leaving the transaction open. close() without flush() discards
a strict transaction, and a pooled strict lease discards it on return.

Add Sender.rollback() and the QWP rollback control frame. The sender
asks for it with X-QWP-Request-Rollback and sends the frame only to a
server that confirmed X-QWP-Rollback: enabled; rollback() waits for
the ack, so a reconnect can never replay the rolled-back frames. reset()
in transactional mode now rolls back frames already sent when the
server supports it. Without server support rollback() aborts the
transaction, and a strict reset() does too; a non-strict reset() keeps
committing those frames on the next flush, as before.
Cover a strict transaction whose batch splits per table within the cap,
and a strict pooled lease returned with only unsent rows, which keeps
its connection and sends no rollback frame.
reset(), rollback() and returning a strict pooled lease discard the open
transaction by publishing the rollback frame. During a server outage
they wait up to 30 s for its ack, then throw and leave the sender
aborted: every later row, flush, drain and rollback throws, even after
the server comes back and acks the replayed rollback frame.
Store-and-forward promises that an outage never fails a producer call.

The new tests take the server down after a transaction's first frames
went out, discard the transaction, commit the next one and bring the
server back on the same endpoint. They fail until discarding stops
waiting for the server.

TxnServer now models the server per connection: wire sequences restart
on every connection, and it records which frames a commit frame
commits, so a test can assert that discarded rows never commit. A
connection that did not confirm rollback reads the rollback frame as an
empty deferred frame, as a server without rollback support does.
TestWebSocketServer records which connections confirmed rollback.
reset(), rollback() and returning a strict pooled lease waited up to
30 s for the rollback frame's ack, then latched the transaction as
aborted: every later row, flush, drain and rollback threw, even after
the server came back and acked the replayed rollback frame, and the
error blamed missing server rollback support. A transient outage broke
the producer for good, which store-and-forward must never do.

The wait stood in for a guarantee the producer cannot see: a reconnect
that reaches a server without rollback support, such as an older node
after a failover, must not replay the rolled-back frames there, as
that server reads the rollback frame as an empty deferred frame and
commits the frames with the next commit.

The I/O loop knows which server each connection reached, so the
guarantee moves there. The store-and-forward log already orders the
rollback frame ahead of every later frame on every connection. On a
connection whose server did not confirm X-QWP-Rollback the loop never
sends a rollback frame: once the server acked every commit-bearing
frame sent before it, the loop acknowledges the frame itself and
recycles the connection. Closing the connection discards the
rolled-back frames server-side, as every server does with what a
closed connection leaves uncommitted, and the next connection starts
past the frame, the way the orphaned-tail retirement already works.
Replays after a restart and background drains run through the same
loop, so they get the same guard. rollbackOpenTransaction() now only
publishes the frame; the timeout, its test setter and the abort on
timeout are gone. Sender.rollback() no longer promises to block, and
Sender.reset() documents the transactional discard.

On a connection that confirmed rollback, trySendBatch adds one cached
boolean check per frame and no extra writes.

CursorWebSocketSendLoopRollbackTest drives the loop against a model of
the server's commits that drops what a closed connection had in
flight: the rollback frame never reaches a server without rollback
support, the loop waits for commit acks (durable ones in durable-ack
mode) before recycling, only the exact rollback frame is held back,
consecutive rollbacks, a recovered log, the background drainer, a
per-connection capability, and a fuzz test over random transactions
and mixed servers (questdb.fuzz.rollback.iterations). A sender-level
test fails over to a server without rollback support.

testRollbackAckTimeoutAbortsTransaction asserted the abort this commit
removes; testUnacknowledgedRollbackDoesNotStallTheSender replaces it.
Tests that read the server's frames right after reset(), rollback() or
a lease close now wait with drain() or for the rollback frame, since
those calls no longer block, and assert what the server committed.
auto_flush=off made a WebSocket sender a strict transaction of any
size, while auto_flush_rows=off with auto_flush_interval=off, the same
trigger state, built a non-transactional sender with no byte trigger:
each flush went out as one batch, which failed past about 2 MiB per
table, and close() committed. The builder keyed the mode on the
auto_flush=off spelling, and recomputeFrameBudget() turned the byte
trigger off for exactly that non-transactional sender. HTTP already
treats both spellings alike.

The builder now reads the trigger state. A sender with no auto-flush
trigger (no row or interval trigger, no positive auto_flush_bytes) is
in strict transactional mode unless transaction= picks the mode.
transaction=on keeps it non-strict, so close() commits; auto_flush=off
used to override it to strict. transaction=off is rejected for every
spelling, as nothing but flush() could commit and a non-transactional
flush must fit one frame. auto_flush=off with a positive
auto_flush_bytes is rejected, as auto_flush=off with auto_flush_rows=N
already was. auto_flush_bytes=off keeps its contract: no frame sizing,
so each batch must fit one frame per table. recomputeFrameBudget() no
longer special-cases the manual-flush sender, which the builder can no
longer build.

Behaviour change: a sender, or a pool, configured with
auto_flush_rows=off and auto_flush_interval=off now discards the rows
it did not flush when it closes or returns a lease, like HTTP and
auto_flush=off, where it used to commit them. close() and lease return
now log a WARN with the row count when they discard an uncommitted
strict transaction, so a caller that relied on close() to commit sees
it. A lease returned without server rollback support now aborts its
transaction before the pool retires the connection, so the retiring
close() does not report the same rows again.

QwpLargeTransactionTest runs every spelling of "no trigger" (both
config strings and both builder forms) through the same strict
transaction of any size and the close() discard, and covers
transaction=on committing on close, the rejections, auto_flush_bytes
as the only trigger, auto_flush_bytes=off, one trigger left, the WARN
on close and on lease return with and without server rollback, and an
aborted lease retiring quietly. It replaces
testManualFlushKeepsOneCommitPerFlush, which pinned the old split.
LineSenderBuilderWebSocketTest and QuestDBBuilderTest cover the
builder and the pool config paths.
A strict transaction refused a row inconsistently. At the server's
qwp.max.uncommitted.rows the row that hit the limit stayed out of the
transaction, but at the client's sf_max_total_bytes the row whose
auto-flush found no room stayed in it, although at() threw all the
same. A caller could not tell whether flush() would commit that row.

The auto-flush that finds no room now keeps its rows pending and
returns, since the room kept for the commit holds them, and the next
row throws. A row whose at() throws TransactionTooLargeException is
never part of the transaction, at either limit. flush() still throws
when the client buffer cannot hold it, and sends nothing.

The errors and the exception's javadoc name the setting to raise
instead of suggesting code that commits part of the transaction. A row
refused at the client buffer reports the size the transaction would
have reached, as a refused flush does, rather than the bytes already
sent, which read 0 when nothing had been sent yet.
A transaction=on transaction holds its frames in the store-and-forward
buffer until the server commits it, and the server acks none of them
before that, so the buffer cannot trim them. The row whose frame no
longer fit blocked for sf_append_deadline_millis (30 s by default) and
then threw, and close() then committed the frames already sent and
dropped the rest, under an error that did not say so.

Before a flush that does not fit beside the open transaction's frames,
the sender now commits those frames with a bare commit frame and goes
on, as the server already does at qwp.max.uncommitted.rows. Every
deferred flush leaves room for that commit frame, so it never waits for
the buffer to drain. The sender logs a warning on the first part of
each transaction it commits this way, and rollback() and reset() warn
that the parts already committed stay. Strict mode still refuses such
transactions.

The client-buffer simulation strict mode uses now tracks transaction=on
transactions too. A single flush larger than the whole buffer still
blocks, as before; only auto_flush_bytes=off builds one.
A strict transaction whose first auto-flush did not fit in the client
buffer sends nothing: flushPendingRows() keeps the rows pending and
latches txnBufferFull, so the next row is refused. reset() discarded
those rows but acted on the transaction only when it had sent frames,
so the latch outlived the transaction it refused. Every later row then
failed with "transaction too large for the client buffer" and a stale
size, flush() had nothing to send, and only rollback() or a new sender
recovered. With the server's default frame cap and 4 MiB segments this
happens whenever sf_max_total_bytes holds fewer than four segments.

reset() now ends a transaction that sent no frames the way rollback()
and discardTransactionForReuse() already do: clearTransactionState()
forgets it, the latch included. A transaction that sent frames is
rolled back or aborted as before.

QwpLargeTransactionTest covers reset() and rollback() after a refused
first auto-flush, on servers with and without rollback support, a
refusal after frames were sent, and a non-transactional sender's
deferred frame, which reset() still leaves to the next commit.
rollback(), reset() of a strict transaction and the return of a strict
pooled lease decided on the producer thread, from the last handshake,
whether the server could roll back the frames already sent. Before the
first handshake the sender cannot know, yet it assumed the server could
not. With initial_connect_retry=async or lazy_connect=true and the
server down at start:

- rollback() threw "the server does not support QWP rollback" and
  aborted the transaction: every later row, flush() and rollback()
  threw, even once a server that supports rollback was up.
- reset() of a strict transaction aborted it silently, with the same
  result. A sender with no auto-flush trigger is strict, so this also
  hit manual-flush senders once their byte trigger had sent a frame.
- reset() of a transaction=on transaction warned that the server lacks
  rollback and kept the rows already sent, so the next flush committed
  them on a server that supports rollback.
- Returning a strict lease retired the pooled sender, and closing it
  dropped the rows earlier leases had committed into its memory buffer
  that no server had received yet. Against a server without rollback
  support the same happened during any outage.

Against a server without rollback support the abort also stuck: the
sender stayed unusable until closed, and a pooled transaction=on lease
threw the abort again from close().

The producer no longer decides. These calls always publish the rollback
frame, and CursorWebSocketSendLoop decides per connection, as it already
did for a reconnect that reached an older node: it sends the frame to a
server that confirmed X-QWP-Rollback, and on any other connection it
waits for the acks of earlier commits, then closes the connection, which
makes the server roll the frames back, and reconnects. The transaction
abort state and its error are gone. reset() of a transaction=on
transaction keeps the rows already sent, as before rollback existed,
only when the last handshake showed a server without rollback support.

On a server that supports rollback nothing changes on the wire. On one
that does not, discarding a transaction whose frames went out costs one
reconnect, and reset() with nothing sent sends nothing.

QwpLargeTransactionTest drives each case through Sender and QuestDB
against the server model: discards before the first connection, with
servers with and without rollback; strict reset() over every spelling
of no auto-flush trigger; rollback() on a server without rollback; pool
leases; and the two lost-commit scenarios. Five tests that pinned the
abort now pin the discard.
The senders of a pool share one sf_max_total_bytes budget, and each
can hold a strict transaction until the server commits it, yet every
sender sized its strict transactions against the whole budget. Two
leases whose transactions each fit the budget, but not together, both
sent frames until they filled it. Each commit then waited for room
that only the other commit could free, threw "cursor SF append failed"
after sf_append_deadline_millis, and neither transaction committed.

SegmentBudget now records how many senders may charge it at once, and
SenderPool declares its maximum size. hasTransactionRoom() holds a
strict transaction whose frames span segments to its sender's share,
CursorSendEngine.shareSegments(): the capacity divided by the sharers,
less in disk mode the segment the manager creates ahead of the next
rotation. The shares add up to the budget, so the transactions of all
pooled senders fit at once, and a sender refuses the row past its
share right away with a TransactionTooLargeException that names the
share and the settings that enlarge it.

Everything else keeps its sizing:
- A transaction whose frames fit one segment is exempt: with its
  commit it needs at most the two segments the budget grants every
  sender past its cap, and a share smaller than its room for the
  commit would refuse batches that fit one frame.
- A budget no other sender shares, a sender built on its own or a pool
  of one, keeps the whole buffer, without the disk-mode deduction.
- A transaction the whole buffer cannot hold is refused for that.
- transaction=on keeps sizing against the whole buffer, so it does not
  split transactions that a sender of its own commits whole.

The cost: a lone lease gets its share, not the whole buffer. With the
defaults, 128 MiB and sender_pool_max=4, a strict transaction that
spans segments counts on 32 MiB.

QwpLargeTransactionTest covers two leases sequentially and on two
threads, at and past their share, the two-segment floor under a share
of less than a segment, the one-segment exemption, disk mode, a pool
of one in memory and on disk, a refused commit and its growth past the
whole buffer, a returned lease, and transaction=on in a pool.
SegmentBudgetTest and SenderPoolSharedBudgetTest cover the sharer
count and the pool's declaration.
The server counts the rows of names that differ only in letter case as
one table's. The engine resolves a table name by folding each UTF-16
char with Character.toLowerCase(), and QwpTudCache keys its writers the
same way, so "t" and "T" share one writer and one count against
qwp.max.uncommitted.rows. A strict transaction counted the rows of each
spelling apart, in that spelling's own table buffer, so it accepted 999
rows of "t" and 999 of "T" under a limit of 1000. The server then
committed the table on its own at 1000 rows and the transaction landed
in parts: flush() committed it as two WAL transactions, and rollback(),
reset(), close() without flush() and returning a pooled lease kept the
part the server had committed.

Each name keeps its own buffer, so rows still go out under the name
they were written under, and nothing changes on the wire. What changes
is that newTableBuffer() links the buffers of names that differ only in
letter case into the spellings of one table, through a map keyed by the
name with each char folded by Character.toLowerCase(), the fold
Chars.toLowerCase() already implements. That lookup runs once per new
name, never per row. checkTransactionAcceptsRow() adds up the rows of
every spelling, those sent in deferred frames and those still buffered,
from the counts the buffers already keep, so no new counter has to stay
in step with them. A table almost always has one spelling, so the check
does the same work as before and allocates nothing.

The fold is the engine's, char by char: "ti" and its spelling with a
dotted capital I are one table, as that letter folds to an ASCII i,
while the case of a supplementary letter, a surrogate pair, stays, and a
sharp s is no double s. String.toLowerCase() would differ on both
counts, and an ASCII-only fold would split names with non-ASCII
letters.

When the table has several spellings, the refusal says that their rows
count together.

QwpLargeTransactionTest covers the limit over three spellings of a
table, over rows still pending in both spellings, after rollback(),
reset() and a pooled lease's return, with the engine's fold of
non-ASCII letters, and the row path allocating nothing with two
alternating spellings. QwpWebSocketSenderTest covers which buffers
become spellings of one table.
@mtopolnik

Copy link
Copy Markdown
Contributor

[PR Coverage check]

😍 pass : 401 / 420 (95.48%)

file detail

path covered line new line coverage
🔵 io/questdb/client/Sender.java 25 29 86.21%
🔵 io/questdb/client/cutlass/qwp/client/QwpWebSocketSender.java 278 292 95.21%
🔵 io/questdb/client/cutlass/http/client/WebSocketClient.java 24 25 96.00%
🔵 io/questdb/client/impl/PooledSender.java 6 6 100.00%
🔵 io/questdb/client/cutlass/qwp/client/QwpWebSocketEncoder.java 10 10 100.00%
🔵 io/questdb/client/cutlass/qwp/client/sf/cursor/CursorSendEngine.java 8 8 100.00%
🔵 io/questdb/client/cutlass/qwp/client/TransactionTooLargeException.java 2 2 100.00%
🔵 io/questdb/client/cutlass/qwp/client/sf/cursor/SegmentManager.java 1 1 100.00%
🔵 io/questdb/client/cutlass/qwp/client/sf/cursor/SegmentBudget.java 7 7 100.00%
🔵 io/questdb/client/impl/SenderPool.java 1 1 100.00%
🔵 io/questdb/client/cutlass/qwp/client/sf/cursor/CursorWebSocketSendLoop.java 27 27 100.00%
🔵 io/questdb/client/cutlass/qwp/protocol/QwpTableBuffer.java 12 12 100.00%

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request QWP tandem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants