Skip to content

[4.2-dev] Reduce tombstone-heavy COUNT reads and prefer cache-local execution - #33307

Merged
XuPeng-SH merged 27 commits into
matrixorigin:4.2-devfrom
ck89119:issue-33274-count-cache-4.2-dev
Nov 27, 2026
Merged

XuPeng-SH merged 27 commits into
matrixorigin:4.2-devfrom
ck89119:issue-33274-count-cache-4.2-dev

Conversation

@ck89119

@ck89119 ck89119 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Related to #33274. Backports #33293 to 4.2-dev.

What this PR does / why we need it:

When a heavy, unfiltered COUNT falls back to a per-block scan, sealed blocks can be counted from their metadata and the distinct deleted rows visible to the transaction. This avoids loading ordinary columns just to count surviving rows. The existing reader handles the other ranges.

A cold CN can run the scan on an eligible CN that has more of the sampled tombstone objects in its whole-file disk cache. Light queries skip peer discovery for cache probes. Sampling, node discovery and probing share a 100 ms budget. An inconclusive probe keeps the existing placement. Plans that need coordinator state, such as FOUND_ROWS() or a named-lock function, also keep their original placement.

The backport uses the 4.2 snapshot admission path, FIFO cache index and scheduler. The optional RPC uses the same wire identifiers as main. The original eleven commits retain their cherry-pick references. This PR is independent of the write-path and asynchronous-cache changes in #33295.

Design revision 1

This 4.2-dev backport addresses #33274. Both optimizations must return the row count that an ordinary scan would see in the same transaction. Implementation at 20c80f4171

Where the scan runs. The coordinator considers moving an otherwise local, single-table scalar count only when the transaction workspace is read-only and the plan can run remotely. Queries with uncommitted writes or coordinator-only expressions keep their original placement. The coordinator samples committed tombstone objects visible at the transaction snapshot. A selected peer restores the shipped transaction snapshot, waits for its local logtail, opens the relation and owns the complete scan. Final projection and output remain on the coordinator. Scan placement · Snapshot restoration

How the rows add up. For an unfiltered, ungrouped count, the existing fast path applies up to 5,000,000 estimated committed tombstone rows. Above that threshold, a singleton scope that owns the matching scan and aggregate can count sealed blocks from metadata. For each supported block, DistTAE reads the exact header row count and subtracts distinct, visible deleted offsets using the ordinary data source and the statement’s transaction offset. Memory, appendable and other ineligible ranges stay with the reader. Only after all sealed-block reads succeed does the scope replace the input ranges and give their subtotal to its MergeGroup, which adds the remaining reader’s count once. An unsupported relation retains its existing execution path. A read error or cancellation discards the unfinished subtotal. Sealed-block count · Aggregate handoff

What a cache probe owns. After ordinary snapshot admission, sampling, cache lookups and peer discovery share a 100 ms deadline. For a heavy count the coordinator compares at most 16 sampled object IDs against the whole-file disk-cache index on the local CN and up to three eligible peers. A unique best score above the local score wins. A tie for best, a missing reply, a busy or unsupported peer, or an expired probe budget keeps the original placement. Query cancellation propagates. Each query client allows one outstanding write per peer address for these probes. Before enqueue, the client recycles a rejected request and releases any slot it acquired. After enqueue, the writer owns both until the write completes, fails or is skipped. The caller can stop waiting as soon as its context expires. Selection · Request ownership

What happens if the selected CN fails. Once the scheduler selects a peer, the ordinary remote pipeline owns execution. Connection, send, execution and receive failures use its cleanup and error path. A peer failure does not trigger a local rerun or reuse a partial count. Only the existing statement retry policy can start a fresh execution attempt. The optional internal RPC uses the same method and request/response field numbers as main (47, 49 and 50). Cache eviction after a probe can cost extra reads, but transaction visibility still determines the result. Remote execution · Retry policy

Acceptance. The mixed-range SQL regression checks two sealed rows plus three workspace rows against an ordinary scan. The blocked-writer regression cancels one probe and verifies that 16 further probes to the same peer leave the queue bounded. The two-CN measurements below check placement and object-store reads on the same data. SQL regression · Blocked-writer regression

Local design review accepted revision 1 for this backport.

Local verification on 4.2-dev

Two CNs used separate local caches and the same MinIO bucket. The fixture had 6,291,456 source rows and 6,285,165 visible rows after updates and deletion. The table reports median COUNT latency across three runs per case. Both CNs restarted for each measurement with A's warm disk cache restored and B's cache empty. The billion-row nightly workload was not run.

SQL entry point 4.2-dev baseline With backport
Cold CN B 4,987.0 ms 584.6 ms
Warm CN A 1,118.4 ms 1,008.2 ms

For cold ingress, shared object-store reads across both CNs fell from 79.2 MiB to 3.7 KiB. Scan metrics confirmed that B placed the eligible COUNT on A.

  • Validated HAVING with FOUND_ROWS and IS_FREE_LOCK, two light table counts, and read-your-writes against an ordinary scan. Both scans returned 6,285,166 inside the transaction. Rolling it back restored 6,285,165.
  • Focused unit tests passed using the repository CGo wrapper and Go 1.26.4. The SQL regression passed all 21 checks with mo-tester. Unit and instrumented SQL tests covered 412 of 488 changed executable Go lines (84.4%) and 350 of 409 changed Go statements (85.6%).
  • Ran incremental go vet and golangci-lint on the changed packages.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify

mergify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: c6fd4d86aaf49d8789bfa176c5daeaed81386a63

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: c6fd4d86aaf49d8789bfa176c5daeaed81386a63

Assessment: approve

The backport reuses the existing snapshot, tombstone, cache and query-service owners for sealed-block counting and optional cache-aware placement. The prior probe-lifetime, accounting, integration-test and design-record concerns are resolved. No actionable defects were found.

Model: gpt-6.1-terra / max

Session: 01a21d47-1533-7fb1-84e0-377223542f45 · slot 0 · generation 25

Validation:

  • Twelve focused tests passed across the query client, storage engine, compiler, file service, object I/O and CN service: focused-tests.log and checks/1795795843178899065-3.json.

  • The public mixed-range SQL test passed on the current head, matching an ordinary scan with two sealed rows and three reader rows: public-starcount.log and checks/1795795908729381415-3.json.

  • The blocked-writer probe ownership test passed 100 race-detector repetitions: probe-race.log and checks/1795796077156315325-3.json.

  • Three accounting export and clone tests passed: accounting-clone-tests.log and checks/1795796149471695378-3.json.

  • Isolation preflight, native-input provenance verification and git diff --check passed. The final evidence and discussion audit passed after correcting its test selector: review-audit-retry.log and checks/1795796210240107892-3.json.

  • The frozen CI snapshot reports successful x86 unit-test and multi-CN BVT jobs; case-level selection was not available.

Limitations:

  • The author’s two-CN performance measurements and the original billion-row workload were not independently repeated.

  • The added distributed SQL case was not independently run with mo-tester; the focused public SQL integration test used one CN.

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants