Skip to content

perf(query): use owning forward views for sealed IPC - #905

Merged
luoxiaojian merged 5 commits into
alibaba:mainfrom
luoxiaojian:feat/forward-filter-read-view
Nov 23, 2026
Merged

luoxiaojian merged 5 commits into
alibaba:mainfrom
luoxiaojian:feat/forward-filter-read-view

Conversation

@luoxiaojian

@luoxiaojian luoxiaojian commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

This PR removes the per-candidate segment lock from single-stage forward filtering on sealed IPC segments. Opening or sealing a segment prepares immutable, zero-copy forward columns. Each query captures the published state under a shared lock and projects the columns its predicate needs.

Changes and scope

  • Prepare column metadata at open and seal. Column DDL and index reload replace the state under the segment lock. Existing views retain their Arrow buffers across replacement and close.
  • Share the same Arrow chunked arrays between read-only batch fetch and filtering. Each column retains its own chunk boundaries. Writing segments and Parquet use live, locked fetch.
  • Evaluate predicates in Arrow. Deletion checks use the prepared local-to-global document ID mapping and consult the live delete bitmap.
  • Support literal predicates with empty projections. Preparation and binding errors propagate to the query.

Performance

The following warmed-query measurements were taken at d54c599a, before moving metadata construction to open/seal. A is this revision with view eligibility disabled, retaining locked fetch and the existing deletion lookup; B is the unmodified PR. Both used the same index and build dependencies and returned identical ordered results and scores. A is a same-revision control, not an upstream-main build.

Linux x86: Xeon Platinum 8369B under KVM, GCC 11.4 Release; clients bound to distinct physical cores on one NUMA node, query pool=8. One 1M-row sealed segment; 62-dimensional HNSW/COSINE/INT8, M=16, ef_construction=200; query ef=300, topk=21, ~50% predicate selectivity, forced single-stage filtering. The deletion case has 10% tombstones without rebuilding the index.

Median end-to-end QPS improvement across five alternating-order, warmed runs:

Clients No deletions 10% deleted
1 +16.7% +14.0%
2 +15.2% +11.4%
4 +12.7% +7.8%
8 +16.9% −0.6%

At 8 clients without deletions, QPS is 546.0 → 638.6 and p99 is 15.94 → 13.66 ms. Runs vary substantially: QPS ranges are 504.2–625.5 for A and 486.1–721.8 for B. Both variants scale about 3.26× from 1 to 8 clients. With 10% tombstones, 8-client throughput is effectively unchanged. The unfiltered negative control differs by less than 1% at every client count.

An unpinned Apple M5 Pro run measured larger 8-client gains: 2.08× without deletions and 1.78× with tombstones. The benefit is platform-dependent; the Linux measurements do not establish improved parallel scaling.

Validation

On macOS arm64, the Release build at 2fa1ec3b passed 168 tests covering segment storage, column DDL, forward filtering and concurrent reads. Three regressions for concurrent projections, DDL and retirement also passed 10 repetitions each. clang-format and clang-tidy passed.

@CLAassistant

CLAassistant commented Nov 13, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@luoxiaojian
luoxiaojian force-pushed the feat/forward-filter-read-view branch from 3b51fac to f959ad9 Compare November 13, 2026 15:07
@luoxiaojian
luoxiaojian marked this pull request as ready for review November 13, 2026 15:07
@luoxiaojian
luoxiaojian requested a review from chinaux as a code owner November 16, 2026 05:05
@luoxiaojian
luoxiaojian force-pushed the feat/forward-filter-read-view branch from 5a76ab2 to b5e162d Compare November 16, 2026 08:36
@luoxiaojian luoxiaojian changed the title feat(query): use owning views for forward predicate reads perf(query): use owning forward views for sealed IPC Nov 16, 2026
@luoxiaojian
luoxiaojian force-pushed the feat/forward-filter-read-view branch from d54c599 to 22fa6bb Compare November 17, 2026 10:06
@egolearner

Copy link
Copy Markdown
Collaborator

Could we simplify this by preparing an immutable forward-read state during segment open/seal, and updating it during column DDL under the existing lifecycle synchronization? Queries would only capture the published state and compose their column projections.

With that structure, prepare_forward_read() could drop:

  • Query-time column construction and the pending list.
  • Copy-on-write catalog extension and the exclusive lock used to publish columns.
  • forward_view_generation_ and stale-publication checks, since queries would no longer build state that might be published after a concurrent DDL change.

The key requirement is to finish preparation before publishing a segment as sealed, and coordinate replacement with DDL/close. Shared ownership of Arrow buffers can remain, allowing existing views to retain their data after replacement. Query preparation may still need a short shared lock to capture the state.

This could start as an internal SealedReadState, reusing the existing persisted-column/chunk metadata, before splitting the full Writing/Sealed Segment hierarchy. The tradeoff is preparing metadata for columns that may never be queried, increasing open/seal work, but it would substantially simplify the query path and cache lifecycle.

@luoxiaojian
luoxiaojian force-pushed the feat/forward-filter-read-view branch from f9e704d to 2fa1ec3 Compare November 19, 2026 11:18
@luoxiaojian

Copy link
Copy Markdown
Collaborator Author

Could we simplify this by preparing an immutable forward-read state during segment open/seal, and updating it during column DDL under the existing lifecycle synchronization? Queries would only capture the published state and compose their column projections.

With that structure, prepare_forward_read() could drop:

  • Query-time column construction and the pending list.
  • Copy-on-write catalog extension and the exclusive lock used to publish columns.
  • forward_view_generation_ and stale-publication checks, since queries would no longer build state that might be published after a concurrent DDL change.

The key requirement is to finish preparation before publishing a segment as sealed, and coordinate replacement with DDL/close. Shared ownership of Arrow buffers can remain, allowing existing views to retain their data after replacement. Query preparation may still need a short shared lock to capture the state.

This could start as an internal SealedReadState, reusing the existing persisted-column/chunk metadata, before splitting the full Writing/Sealed Segment hierarchy. The tradeoff is preparing metadata for columns that may never be queried, increasing open/seal work, but it would substantially simplify the query path and cache lifecycle.

fixed:
What changed

  • SegmentImpl now keeps one immutable PersistedReadState (shared_ptr<const>). It replaces the five fresh_persist_chunked_array members, and filtering and read-only batch fetch share its ChunkedArrays.
  • The state is built at Open() and when sealing (remove_writing_forward_block()). add_column/alter_column/drop_column and reload_* rebuild it under exclusive seg_mtx_, and close() releases it under the same lock.
  • prepare_forward_read() copies the state pointer under a shared lock, then composes the projection outside the lock.
  • Removed: query-time column construction, the pending list, the copy-on-write catalog, the exclusive publish lock, and forward_view_generation_ with its stale-publication checks.
  • Shared ownership of Arrow buffers is unchanged. Views captured before DDL or close keep reading their own buffers (covered by DdlReleasesColumnsAndRetainsOldReaders and CloseReleasesColumnsWithoutInvalidatingReaders).

Points that differ from the suggestion

  1. Preparation is best-effort. With lazy construction, a failure only affected the query that hit it. Built eagerly, the same failure would make every filtered query on that segment fail until reopen, for example after a DDL that fails partway, or would make Open() fail on an inconsistent block. Instead:
    • if building fails, the segment keeps no state and serves forward reads through locked fetch, with a WARN log;
    • DDL republishes through a scope guard on every return path, including failures;
    • reload_* never aborts because of the state, so the new indexers are always installed;
    • a closed_ flag separates a closed segment from one without a state.
  2. Named PersistedReadState, not SealedReadState. The state also backs fetch_perf for read-only segments that still have a writing block. Those segments get no owning views, which owning_views records. Happy to rename if you prefer.
  3. Locking fixes this depends on. The state relies on seg_mtx_, so flush() now takes the lock itself (flush_unsafe() is used by callers that already hold it) and reload_* take it as well. Before, the doc_count() == 0 rollover path called the unlocked flush() while holding only write_mtx_.

Reuse immutable, zero-copy forward columns across query projections so sealed
IPC candidate filtering avoids per-candidate segment locks. Read global document
IDs through owned columns while retaining live deletion bitmap checks.

Keep writing segments and unsupported storage on live locked fetch. Invalidate
column catalogs with segment lifecycle changes and cover buffer lifetime,
independent chunk layouts, schema changes and concurrent captures.
…ilures

Preparing the sealed read state at open/seal turned several failures that
the lazy catalog used to absorb into hard errors. Make preparation an
optimization again: on failure the segment serves forward reads through
locked fetch.

- DDL: add/alter/drop_column republish the state on every return through a
  scope guard. A failure partway through no longer leaves a sealed segment
  without a state, which made every filtered query on it fail until reopen.
- Reload: reload_vector/scalar/fts_index no longer abort when the state
  cannot be prepared, so the new indexers are always installed with the
  new meta.
- Open: an inconsistent block no longer makes Segment::Open fail.
- prepare_forward_read distinguishes a closed segment (explicit closed_
  flag) from a missing state instead of inferring closure from it.
- Rollover: validate the version before sealing, destroy the new writing
  segment on failure, and make the sequence retryable. dump() no longer
  takes a defaulted virtual parameter; resume_dump() accepts a segment that
  was already dumped, whether or not its writing block was removed.
- reload_scalar_index recomputes block offsets before its early returns,
  so they always match the new meta.
- Log how long each state preparation takes.
Keep the best-effort read state from the previous commit and drop changes
that are not needed for it.

- Rollover: restore switch_to_new_segment_for_writing() and the void
  remove_writing_forward_block() from main. Publishing the read state can
  no longer fail, so sealing has no new error path, and queries cannot
  observe the order of add_segment() and sealing because the whole switch
  holds the exclusive write_mtx_. resume_dump() is removed.
- Sealing: remove the writing block, then publish, under one exclusive
  seg_mtx_. The `sealing` flag that previewed the post-seal state is gone.
- Split the read state into a const build_persisted_read_state() and
  publish_persisted_read_state(), which handles failures and logs.
- Rename PersistedReadState::sealed to owning_views; it was easy to
  confuse with SegmentImpl::sealed_ (dumped).
- Share the locked-fetch Arrow field construction between fetch_unsafe()
  and prepare_forward_read() so their schemas cannot diverge.
- Shorten the Segment interface comments.

No behavior change for queries, DDL, reload or open.
@luoxiaojian
luoxiaojian force-pushed the feat/forward-filter-read-view branch 2 times, most recently from cc3bcab to 28d11b3 Compare November 20, 2026 10:08
@luoxiaojian
luoxiaojian merged commit ea1519a into alibaba:main Nov 23, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants