Repository navigation
refactor: make pipeline termination protocol explicit and cancellation-safe - #25273
Conversation
…rigin#24971) Refactor pipeline cleanup paths to use explicit typed terminal events (EventEnd/EventError/EventAbort) instead of the implicit nil-batch counting convention, eliminating deadlock risks and ambiguity. Key changes: - PipelineEventType enum with typed signals (EventData/End/Error/Abort) - PipelineEdge abstraction wrapping WaitRegister with observable terminal state (Done/Aborted/Err) and idempotent send guarantees - PipelineSignalReceiver.GetNextBatch supports typed terminal events while maintaining backward compatibility with legacy nil-batch path - connector/dispatch Reset() simplified to BuildCleanupSignal + sendTerminalSignalsToLocalRegs + sp.Abort() - spool.Abort() for synchronous, idempotent resource release - compile.go uses PipelineEdge.AsWaitRegister() for compatibility Co-authored-by: Astra <[email protected]>
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
I found two substantive blockers here.
- Edge-level terminal state breaks the existing multi-sender
WaitRegistercontract.
PipelineEdge now treats End/Error/Abort as terminal for the whole edge: markTerminal() closes done, and later data sends are rejected once that terminal state is set. But the compile path still shares one WaitRegister across multiple upstream senders and relies on NilBatchCnt > 1 (newMergeScope, newMergeScopeByCN, constructDispatchLocalAndRemote).
That means the first sender that sends a typed terminal signal can seal the shared edge for everyone else. Remaining senders on the same shared merge edge will then fail SendData / TrySend, even though the receiver still expects more terminal accounting and possibly more data.
Suggested fix: do not make terminal state global to the shared edge while shared WaitRegisters still exist. Either keep terminal semantics per sender, or defer this edge-global terminal protocol until each sender has its own dedicated edge.
- Normal successful cleanup now aborts queued spool data before downstream has drained it.
On normal completion (pipelineFailed=false, err=nil), Connector.Reset / Dispatch.Reset send a typed terminal signal and then immediately call sp.Abort(). But Abort() marks the spool aborted and ReceiveBatch() will then return ErrPipelineSpoolAborted instead of delivering queued batches that were already published into the spool but not yet consumed.
So if the downstream merge still has buffered GetFromSpool signals when cleanup starts, already-produced rows are discarded during a successful shutdown.
Suggested fix: reserve sp.Abort() for failure/cancellation paths where dropping pending data is acceptable. On successful completion, keep the old drain-preserving path (or another mechanism that preserves queued spool data until receivers consume it).
|
Addressed the review comments in 551cb53.
Verified locally:
|
XuPeng-SH
left a comment
There was a problem hiding this comment.
Re-reviewing the latest head: I still would not merge this yet, but the blocker set is narrower now.
What still looks wrong:
- the remote-input cleanup path in
pkg/sql/compile/scope.gois still sending legacy nil terminals (NewPipelineSignalToDirectly(nil, err, ...)) instead of the new typedEnd/Error/Abortprotocol; - the abort-fallback timeout-budget concern is weaker now, but still not really disproven for the real production budget / multi-receiver case.
The first point is still the main blocker. On the latest head, shared receiver edges still exist with NilBatchCnt > 1, and the receiver treats legacy nil as a one-sender decrement. So a remote cleanup path that bypasses typed terminals can still leave the shared receiver waiting for the rest of the sender count until timeout instead of terminating the edge immediately.
Suggested fix:
- in
Scope.sendNotifyMessage.closeWithError, use typed cleanup terminals (NewEndSignal/NewErrorSignal/NewAbortSignal) instead ofNewPipelineSignalToDirectly(nil, err, ...); - then add a targeted remote/multi-CN cleanup regression around that path.
The second point (budget inflation) now looks more like a bounded-latency concern than a clear correctness bug, so I would treat it as secondary compared with the protocol mismatch above.
|
Addressed the latest remote cleanup review in |
|
Follow-up from the full module review in Found and fixed one additional protocol edge case:
Extra regression coverage added:
Local verification after this commit: go test -count=1 -timeout 180s ./pkg/vm/process ./pkg/container/pSpool ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch
go test -count=1 -timeout 180s ./pkg/sql/compile
go test -v -count=1 -timeout 120s -run 'TestCheckBootstrapRetriesSetBootstrapStateFailure|TestHandleBootstrapFailure' ./pkg/logservice
go test -v -count=1 -timeout 180s -run TestSpeedupAbortAllTxn ./pkg/tests/issues
go vet ./pkg/vm/process ./pkg/container/pSpool ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch
go vet ./pkg/sql/compile ./pkg/logservice ./pkg/tests/issues
make err-check
git diff --checkAll passed locally. PR checks restarted for the new head commit. |
|
Follow-up from the second full module review in Additional issue found and fixed:
Also tightened deferred spool cleanup comments in pSpool/connector/dispatch: the safety condition is that the paired Merge cleanup loop has returned. On the normal path it drains queued CI note:
Local verification after this commit: make err-check
git diff --check
CGO_CFLAGS="..." CGO_LDFLAGS="..." go test -v -count=1 -timeout 180s ./pkg/vm/process ./pkg/container/pSpool
CGO_CFLAGS="..." CGO_LDFLAGS="..." go test -v -count=1 -timeout 180s ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch
CGO_CFLAGS="..." CGO_LDFLAGS="..." go test -race -v -count=1 -timeout 240s -run 'TestPipelineEdge|TestPipelineSignalReceiver|TestPipelineSpool|TestDispatchFallbackAbortUsesSharedTimeoutBudget|TestDispatchResetFallsBackToAbortWhenEndSignalCannotBeDelivered|TestDispatchResetAbortsSpoolWhenSomeLocalRegIsFull|TestDispatchResetEndPreservesQueuedBroadcastBatchUntilDeferredCleanup' ./pkg/vm/process ./pkg/container/pSpool ./pkg/sql/colexec/dispatch
CGO_CFLAGS="..." CGO_LDFLAGS="..." DYLD_LIBRARY_PATH="..." go test -ldflags="..." -v -count=1 -timeout 180s -run 'TestRemoteNotifyCleanupUsesTypedErrorForSharedReceiver|TestRemoteNotifyCleanupUsesTypedEndForSingleSender|TestReceiveMessageFromCnServerIfConnector_ReturnsOnBlockedReceiverCancel|TestReceiveMsgAndForward_ReturnsOnBlockedReceiverCancel|TestReceiveMessageFromCnServerIfDispatch_PreservesCleanupOnOriginalRoot' ./pkg/sql/compile
CGO_CFLAGS="..." CGO_LDFLAGS="..." DYLD_LIBRARY_PATH="..." go test -ldflags="..." -v -count=1 -timeout 180s -run 'TestCheckBootstrapRetriesSetBootstrapStateFailure|TestHandleBootstrapFailure' ./pkg/logservice
CGO_CFLAGS="..." CGO_LDFLAGS="..." DYLD_LIBRARY_PATH="..." go test -ldflags="..." -v -count=1 -timeout 240s -run TestSpeedupAbortAllTxn ./pkg/tests/issues
CGO_CFLAGS="..." CGO_LDFLAGS="..." DYLD_LIBRARY_PATH="..." go vet -ldflags="..." ./pkg/sql/compile ./pkg/logservice ./pkg/tests/issues
CGO_CFLAGS="..." CGO_LDFLAGS="..." go vet ./pkg/vm/process ./pkg/container/pSpool ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch |
|
Follow-up optimization in I tightened the cleanup send budget one step further:
Added regressions:
Local verification: CGO_CFLAGS="..." CGO_LDFLAGS="..." go test -v -count=1 -timeout 180s ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch
CGO_CFLAGS="..." CGO_LDFLAGS="..." DYLD_LIBRARY_PATH="..." go test -ldflags="..." -count=1 -timeout 180s ./pkg/sql/compile
CGO_CFLAGS="..." CGO_LDFLAGS="..." go test -race -v -count=1 -timeout 240s -run 'TestConnectorResetUsesSharedTerminalSendBudget|TestConnectorResetFallsBackToAbortWhenEndSignalCannotBeDelivered|TestDispatchResetUsesSharedTerminalSendBudget|TestDispatchResetFallsBackToAbortWhenEndSignalCannotBeDelivered|TestDispatchResetAbortsSpoolWhenSomeLocalRegIsFull' ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch
CGO_CFLAGS="..." CGO_LDFLAGS="..." go vet ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch
CGO_CFLAGS="..." CGO_LDFLAGS="..." DYLD_LIBRARY_PATH="..." go vet -ldflags="..." ./pkg/sql/compile
CGO_CFLAGS="..." CGO_LDFLAGS="..." go build ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch
CGO_CFLAGS="..." CGO_LDFLAGS="..." DYLD_LIBRARY_PATH="..." go build -ldflags="..." ./pkg/sql/compile
make err-check
git diff --check |
|
Follow-up closed-loop review update New issue found and fixed in
Test robustness improvement:
Local verification passed:
|
|
Additional first-principles unhappy-path coverage in This commit is test-only and closes the remaining protocol boundary cases from the Q1/Q2/Q3 review:
Local verification passed after the commit:
|
XuPeng-SH
left a comment
There was a problem hiding this comment.
Re-reviewing the latest head: the earlier remote-terminal and shared-timeout-budget concerns look addressed now, but I still see one remaining blocker.
CleanupDeferredSpool() can still run too early on the normal End path.
Both cleanupInOrder() and cleanupSenderReceiverPipeline() invoke deferred spool cleanup from the sender pipeline’s own teardown, but that does not guarantee every downstream local receiver using the same spool has finished draining and released its last consumed slot yet.
At the spool level, ForceCleanupAfterTerminalSignal() consumes the one-shot cleanupOnce and frees the currently cached buffers. Later ReleaseCurrent() calls on non-aborted receivers still put newly released batch buffers back into the spool cache (CacheBatch(...)), but there is then no second cleanup pass left to free those newly returned buffers.
So on successful runs with a fast sender and slower downstream receiver(s), this can leak spool-owned memory after the deferred cleanup fires too early.
Suggested fix:
- delay deferred spool cleanup until downstream receiver drain is actually confirmed, or
- after deferred cleanup has fired, make late
ReleaseCurrent()free buffers directly instead of re-caching them.
The earlier shared-edge fatal / remote cleanup blockers look much better on this head; this deferred-cleanup lifecycle is the remaining issue I would fix before merge.
|
Follow-up for the latest deferred-spool-cleanup review in Root cause addressed:
Fix:
Local verification passed: CGO_CFLAGS="-I/Users/xupeng/gitlab/matrixone/cgo -I/Users/xupeng/gitlab/matrixone/thirdparties/install/include" CGO_LDFLAGS="-L/Users/xupeng/gitlab/matrixone/thirdparties/install/lib -lusearch_c" go test -v -count=1 -timeout 120s ./pkg/container/pSpool
CGO_CFLAGS="-I/Users/xupeng/gitlab/matrixone/cgo -I/Users/xupeng/gitlab/matrixone/thirdparties/install/include" CGO_LDFLAGS="-L/Users/xupeng/gitlab/matrixone/thirdparties/install/lib -lusearch_c" go test -v -count=1 -timeout 180s ./pkg/vm/process ./pkg/container/pSpool ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch
CGO_CFLAGS="-I/Users/xupeng/gitlab/matrixone/cgo -I/Users/xupeng/gitlab/matrixone/thirdparties/install/include" CGO_LDFLAGS="-L/Users/xupeng/gitlab/matrixone/thirdparties/install/lib -lusearch_c" go test -race -v -count=1 -timeout 240s -run 'TestPipelineSpoolLateReleaseAfterTerminalCleanupFreesDirectly|TestPipelineSpoolForceCleanupAfterTerminalSignalDoesNotNeedNilEndMessage|TestPipelineSpoolAbortWaitsForAllCurrentBroadcastReceivers|TestConnectorResetEndPreservesQueuedSpoolBatchUntilDeferredCleanup|TestDispatchResetEndPreservesQueuedBroadcastBatchUntilDeferredCleanup' ./pkg/container/pSpool ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch
CGO_CFLAGS="-I/Users/xupeng/gitlab/matrixone/cgo -I/Users/xupeng/gitlab/matrixone/thirdparties/install/include" CGO_LDFLAGS="-L/Users/xupeng/gitlab/matrixone/thirdparties/install/lib -lusearch_c" go vet ./pkg/container/pSpool ./pkg/vm/process ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch
CGO_CFLAGS="-I/Users/xupeng/gitlab/matrixone/cgo -I/Users/xupeng/gitlab/matrixone/thirdparties/install/include" CGO_LDFLAGS="-L/Users/xupeng/gitlab/matrixone/thirdparties/install/lib -lusearch_c" go build ./pkg/container/pSpool ./pkg/vm/process ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch
make err-check
git diff --checkNote: running the pSpool test without MO CGo flags fails locally at compile time with |
XuPeng-SH
left a comment
There was a problem hiding this comment.
Re-reviewed latest head 9a46e4511.
I do not see a remaining blocker in the current version.
What I re-checked:
WaitRegisteris now a directPipelineEdgealias, so terminal state is no longer split across two objects.- Shared-edge End/Error/Abort accounting now matches
NilBatchCnt > 1: normal End waits for all expected senders, while fatal terminals latch first cause and deliver the remaining sender count. - Connector/Dispatch normal End preserves queued spool data and defers spool cleanup until the paired Merge cleanup returns; End delivery failure falls back to typed Abort and aborts the spool.
- Remote notify cleanup and remote batch forwarding no longer bypass typed terminal / edge state.
- The latest pSpool fix closes the late
ReleaseCurrentcase after terminal cleanup by freeing directly once the final cache-wide cleanup pass has run. - The HAKeeper bootstrap follow-up avoids panicking a background ticker on transient
setBootstrapStatefailure; it now logs and retries on a later tick.
Local verification:
go test ./pkg/vm/process ./pkg/container/pSpool ./pkg/vm/pipeline -count=1 -timeout 120sgo test ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatch -count=1 -timeout 120s- targeted
go test ./pkg/sql/compilefor remote forwarding / typed cleanup / broadcast-build tests - targeted
go test ./pkg/logservice -run ^TestCheckBootstrapRetriesSetBootstrapStateFailure$ go buildandgo vetfor pSpool/process/pipeline/connector/dispatch/compile/logservice
CI is green. The branch is behind main, but I do not see a code-level reason to request changes.
What type of PR is this?
Which issue(s) this PR fixes:
issue #24971
What this PR does / why we need it:
This PR makes pipeline termination explicit and cancellation-safe.
WaitRegisterandPipelineEdgeterminal state so typed terminal signals closeDone()consistently.pSpool.Abort()release pending spool batches immediately while preserving batches already handed to receivers untilReleaseCurrent.Testing:
go test -v -count=1 -timeout 120s ./pkg/vm/process ./pkg/container/pSpoolgo test -race -count=1 -timeout 120s ./pkg/vm/process ./pkg/container/pSpoolgo test -v -count=1 -timeout 120s ./pkg/sql/colexec/connector ./pkg/sql/colexec/dispatchgo build ./pkg/container/pSpool ./pkg/vm/process ./pkg/sql/compilego vet ./pkg/container/pSpool ./pkg/vm/process ./pkg/sql/compilego test -ldflags="-extldflags '-L$(pwd)/cgo -lmo -L$(pwd)/thirdparties/install/lib -Wl,-rpath,@executable_path/lib'" -v -count=1 -timeout 120s -run 'TestCompileBuildSideForBroadcastJoin(SkipsEmptyCN|GroupsDuplicateCN)$' ./pkg/sql/compile