[Bug]: Index seal can lose errors and keep an incomplete range index, so range queries miss documents

Đang mở
#808 0 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@zhourrr đang làm issue này rồi.

Từ ngày 21/10/2026.

  • #809 của @jak-pan — đang mở

Đánh giá

Issue này chưa được đánh giá.

Mô tả

Description

When a segment seals, InvertedIndexer::seal() seals each inverted column, then flushes and compacts. A column seal (InvertedColumnIndexer::seal()) builds the range index and value histogram of a range-optimized field, then writes the column's "sealed" marker. I found four defects on this path. Each one is reproduced below on 98aa126 with tests only. Three of them can make range queries (<, <=, >, >=) silently return too few documents, and two together can crash the process after a single I/O error. They share one code path, so I report them together.

1. A column seal error is logged and then lost. The loop logs a failed column seal and goes on. flush() and compact() then overwrite s, so seal() returns OK:

https://github.com/alibaba/zvec/blob/7f54d9460c4c083f98b959ceb4a1d1bbfd07a8b6/src/db/index/column/inverted_column/inverted_indexer.cc#L127-L149

InvertedIndexer::flush() in the same file returns on a column error. From code reading, SegmentImpl::dump() then finishes the seal as if it had succeeded, so nothing retries the failed column. Finding 6 of #789 mentions this from code reading. The tests below confirm it.

2. The seal marker can reach disk before the range index it describes. The indexes write without a WAL, so data becomes durable only when its column family is flushed. RocksdbContext::flush_unlocked() flushes the families in the order of cf_handles_:

https://github.com/alibaba/zvec/blob/7f54d9460c4c083f98b959ceb4a1d1bbfd07a8b6/src/db/common/rocksdb_context.cc#L339-L361

create() puts the default family, which holds the sealed markers, last. A writable open() of an existing instance lists the families in RocksDB's order, with the default family first:

https://github.com/alibaba/zvec/blob/7f54d9460c4c083f98b959ceb4a1d1bbfd07a8b6/src/db/common/rocksdb_context.cc#L176-L180

So an index that was opened in this process, not created in it, flushes the marker before the $RANGES and $CDF families. One example is the writing segment's scalar index after a restart (from code reading). A large range index is also partly flushed while the seal builds it, whenever its memtable fills. A crash after the default family's flush therefore keeps the marker and only part of the range index. After a reopen, the column is sealed, and range queries skip every term after the last surviving range:

https://github.com/alibaba/zvec/blob/7f54d9460c4c083f98b959ceb4a1d1bbfd07a8b6/src/db/index/column/inverted_column/inverted_column_indexer_search.cc#L286-L297

A later seal() skips the column because it is already sealed, so the wrong results stay.

3. reset_cf() keeps a deleted handle when the re-create fails. It deletes the old handle after DropColumnFamily() succeeds. It stores the new handle only if CreateColumnFamily() succeeds:

https://github.com/alibaba/zvec/blob/7f54d9460c4c083f98b959ceb4a1d1bbfd07a8b6/src/db/common/rocksdb_context.cc#L483-L528

If the create fails, for example because a MANIFEST sync fails, cf_handles_ keeps the deleted pointer. The next flush() or close() uses it. drop_cf() removes the handle from cf_handles_ when it deletes it. generate_statistical_indexes() starts with reset_cf(). Because of defect 1, InvertedIndexer::seal() continues into flush() after the failed reset. So one failed sync during a seal crashes the process.

4. The term scan treats a read error as the end of the terms. generate_statistical_indexes() loops while iter_term->Valid() and never checks iter_term->status():

https://github.com/alibaba/zvec/blob/7f54d9460c4c083f98b959ceb4a1d1bbfd07a8b6/src/db/index/column/inverted_column/inverted_column_indexer_write.cc#L337-L408

A read error in the middle of the term table ends the scan. The seal then builds the range index from the terms it has read, writes the marker and returns OK. If the error was transient, the following compaction succeeds, and nothing reports the error. The FTS indexer checks status() after its scan (fts_column_indexer.cc, line 769).

Steps to Reproduce

All runs use 98aa126 with test files only and no source change. main (7f54d94) has the same code in these files. The index has one INT32 field seq with InvertIndexParams(true), and seq = document id unless noted. The tests inject faults through a rocksdb::FileSystemWrapper that they set as the RocksDB env before the index opens. To reach the indexer's private context, the test file uses a test-only access override. Crash tests kill a child process with SIGKILL from a rocksdb::EventListener.

  1. Column seal error. Create an index with two fields, insert 5,000 documents, flush and close. Put a value with header byte 0xff under the term of seq = 0. Reopen the index and call InvertedIndexer::seal().
    • Expected: seal() returns the column error.
    • Actual: seal() returns OK. Column seq is not sealed, and column cyc is sealed.
  2. Marker before the range index. Insert 4,500,000 documents with seq = id * 7919 mod 4,500,000, which is a field unrelated to insertion order, then flush and close. A child process reopens the index writable and calls InvertedIndexer::seal(). Its handle order is default seq$TERMS seq$RANGES $CDF. A listener kills the child right after the default family's flush is installed. The parent then reopens the index. All RocksDB options are zvec's defaults.
    • Expected: a sealed column has its whole range index, and range queries return the exact count.
    • Actual: column seq is sealed, but only 768 of its 1,000 range entries are on disk. The test checks <, <=, > and >= at 41 values, and 82 of these 164 range queries are wrong. For example, seq >= 0 returns 3,456,000 of 4,500,000 documents. seal() then returns OK and the answers stay wrong.
    • The same happens with 200,000 documents and a 64 KB range memtable: 998 of 1,000 entries survive, and 82 of 164 queries are wrong.
    • Control: an index created in the same process flushes the default family last.
  3. Failed re-create. Create a RocksdbContext with a family r. Fail the second MANIFEST sync after arming the fault (the first one is the drop's), then call reset_cf("r").
    • Actual: reset_cf() returns an error, and cf_handles_ still holds the deleted handle. close() then crashes with SIGSEGV.
    • Through the seal: insert 5,000 documents, reopen the index and inject the same fault. InvertedIndexer::seal() logs the two errors below and then crashes with SIGSEGV in rocksdb::DBImpl::Flush(const FlushOptions&, ColumnFamilyHandle*).
  4. Read error in the term scan. Insert 100,000 documents, flush and close. This leaves one term table with 1,749,069 bytes of data blocks. Reopen the index. While InvertedColumnIndexer::seal() runs, fail every read that covers the middle byte of that table. One read failed.
    • Expected: the column seal fails and writes no marker.
    • Actual: the column seal returns OK, and only 499 of 1,000 range entries are written. 40 of 164 range queries are wrong. For example, seq > 14999 returns 34,841 documents instead of 85,000.
    • InvertedIndexer::seal() then returns OK, because its compaction reads the table without the fault. The wrong answers remain after a reopen.
    • Control without the fault: 1,000 entries, and 0 of 164 queries wrong.
Logs / Stack Trace
# 1
[ERROR inverted_column_indexer_write.cc:343] Failed to deserialize bitmap for term[...] from InvertedColumnIndexer[collection:repro|field:seq|...]
[ERROR inverted_column_indexer_write.cc:449] Failed to generate statistical indexes in InvertedColumnIndexer[collection:repro|field:seq|...]
[ERROR inverted_indexer.cc:134] Failed to seal InvertedColumnIndexer[collection:repro|field:seq|...]
InvertedIndexer::seal() -> OK

# 3, through the seal
[ERROR rocksdb_context.cc:525] Failed to create cf[seq$RANGES] in RocksDB[...], code[5], reason[IO error: injected MANIFEST sync failure]
[ERROR inverted_indexer.cc:134] Failed to seal InvertedColumnIndexer[collection:repro|field:seq|...]
stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
frame #0: rocksdb::DBImpl::Flush(rocksdb::FlushOptions const&, rocksdb::ColumnFamilyHandle*) + 64
Operating System

macOS 27 (arm64). The code paths are platform independent. Linux was not run.

Build & Runtime Environment

Apple clang, CMake Release build of 98aa126, RocksDB 8.1.1 from the submodule.

Additional Context

PR #809 fixes the four defects, with one commit and one regression test each:

  1. InvertedIndexer::seal() returns the first column error, as flush() does. It prepares every column before it marks any of them sealed, so a failed seal leaves no column read-only.
  2. The seal markers are written only after the data they describe is flushed. Flushing the default family last would order only the explicit flush. An automatic flush of a full default memtable during the seal could still go first.
  3. When the drop succeeded and the create failed, reset_cf() removes the handle from cf_handles_, as drop_cf() does.
  4. The seal fails if iter_term->status() is not OK after the scan, or if SeekToLast() finds no term.

With fixes 1 and 3, the failed MANIFEST sync makes the seal return an error instead of crashing.

SegmentImpl also ignores the result of column_indexer->seal() in two places (segment.cc lines 3254 and 3406, from code reading). The PR leaves them unchanged, because the segment meta is already updated there and returning the error needs a decision on how to roll it back.

Related: #798 and PR #803 replace the reset_cf() calls in the seal with an in-place clear, and they keep reset_cf() only as a fallback for an unreadable range family. With #803 applied, defects 1, 2 and 4 reproduce unchanged. Defect 3 is then reachable from the seal only through that fallback. The PR and #803 merge without conflicts.

Ngôn ngữ chính
C++
Star
16.3k
Fork
1k
Merge trung bình
2 ngày 10 giờ
Pull request đã merge (30 ngày)
32

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của alibaba/zvec

Tất cả issue của alibaba/zvec

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.