airborne12 commented on code in PR #70593:
URL: https://github.com/apache/doris/pull/70593#discussion_r4763854212
##########
be/src/storage/index/inverted/snii/writer/logical_index_writer.cpp:
##########
@@ -710,6 +744,69 @@ Status
LogicalIndexWriter::build_streamed_dictionary(BlockState* state, PostingB
}
Status LogicalIndexWriter::flush_streamed_block(BlockState* state) {
+ RETURN_IF_ERROR(flush_streamed_block_bytes(state));
+ return fold_block_grams(state);
+}
+
+// Gives the summary up for this index and frees what it held. The summary is
optional: a build
+// that cannot afford it (the gram limit, or a reservation refused under a
memory cap) goes on
+// without it, and the dictionary is walked in full as before.
+void LogicalIndexWriter::abandon_gram_summary(BlockState* state) {
+ dict_gram_summary_enabled_ = false;
+ gram_summary_ = format::DictGramSummaryBuilder {};
+ gram_summary_reservation_.reset();
+ std::vector<uint32_t>().swap(state->gram_codes);
+ state->gram_set = format::DictGramSet {};
+ state->gram_codes_reservation.reset();
+}
+
+Status LogicalIndexWriter::collect_term_grams(std::string_view term,
BlockState* state) {
+ format::collect_dict_gram_codes(term, &state->gram_set,
&state->gram_codes);
Review Comment:
Confirmed and fixed in 0b9075f9c8f.
On a498b38737f the gram state was charged after it grew.
`collect_dict_gram_codes` grew the set (at the 32,769th gram a new 512 KiB
array next to the old 256 KiB one) and the codes vector before
`collect_term_grams` raised `gram_codes_reservation`, and `fold_block_grams`
let `add_block` allocate a block's new map nodes, the rehashed bucket array and
the runs before it raised `gram_summary_reservation_`. Under the hard limit of
native compaction, a shared reporter near its cap could admit the smaller
retained increase while the replacement peak was already over it. The builder's
`resident_bytes()` was also an estimate that left out the bucket array and the
runs' spare capacity, so even the retained charge was short.
The fix keeps the existing order of reserve, then allocate
(`reserve_term_hash_for_append`, `prepare_replacement`):
- `DictGramSet` allocates nothing until `reserve()` and grows only there;
`collect_dict_gram_codes` requires room for every gram of the term. Before the
set or the codes grow, the writer reserves the old storage plus the
replacement, and gives the summary up before allocating when that reservation
or the cap/64 budget refuses it.
- `DictGramSummaryBuilder` keeps its rows in a vector with an
open-addressing index instead of `std::unordered_map`, and grows the rows, the
index and each row's runs only by explicit `reserve`. `add_block_peak_bytes()`
returns the most it holds while `add_block` runs (what remains plus the old
storage of the array being replaced). `fold_block_grams` reserves that first,
gives the summary up if it is refused, and afterwards lowers the reservation to
`resident_bytes()`, which is now the exact heap the builder holds (checked
`resident <= peak`).
No format change. Tests, counting the test thread's heap through
AddressSanitizer allocator hooks:
`SniiDictGramSummary.ResidentBytesAreTheHeapTheBuilderHolds` compares
`resident_bytes()` with the heap the builder holds after every block (on
a498b38737f it fails after the first block: 117,572 bytes accounted, 129,512
held); `AddBlockStaysWithinItsPeak` checks that the heap never exceeds
`add_block_peak_bytes()` while a block goes in; `GramSetGrowsOnlyWhenReserved`
checks that the set grows only in `reserve()` and that collecting into its room
allocates nothing. All `Snii*` BE UT pass, including
`SummaryThatCannotBeReservedIsLeftOut`.
##########
be/src/storage/index/inverted/snii/reader/logical_index_reader.cpp:
##########
@@ -1226,6 +1254,38 @@ Status LogicalIndexReader::visit_prefix_terms(
return Status::OK();
}
+Status LogicalIndexReader::dict_gram_candidates(std::string_view text,
+ std::vector<uint64_t>* blocks,
bool* known,
+ bool* any) const {
+ const RegionRef& ref = section_refs().dict_gram_summary;
+ size_t length = 0;
+ RETURN_IF_ERROR(checked_size(ref.length, "dict gram summary: length out of
range", &length));
+ std::vector<uint8_t> section;
+ RETURN_IF_ERROR(reader_->read_at(ref.offset, length, §ion));
Review Comment:
Confirmed and fixed in 0b9075f9c8f.
On a498b38737f `visit_prefix_terms` read, CRC-checked and validated the
whole summary before the first block, so `*cache*` under the default limit of
50 paid for the summary even when its first block held 50 matches.
Now a walk that may end early (a bounded visitor, or a prefix range) opens
its first block before it reads the summary. When the visitor stops or the
prefix ends there, the summary is never read. When the walk goes on, it reads
the summary once and skips the blocks without the literal from the next block
on, so a bounded walk that ends in its second or third block still pays for one
summary read. A walk of the whole dictionary (an unbounded contains expansion)
still reads the summary first. `SniiIndexSource::expand_terms` passes `bounded
= max_expansions > 0`.
I did not defer the read further, for example until the walk had opened as
many bytes as the summary holds. The suffix token of `MATCH_PHRASE_EDGE` is
bounded by the same limit, and for a rare literal its matches are spread over
the whole dictionary, so every such query would read up to the summary's length
in extra blocks (about a twentieth of a text dictionary) before the summary
could help. Opening the summary once costs one read and a CRC check, about what
a few blocks cost. Keeping the validated summary on the reader across
expansions would remove the repeated work too, but it needs memory accounting
in the searcher cache and synchronization for concurrent queries, so it is left
out of this PR.
Tests:
`SniiDictGramSummaryWalk.BoundedExpansionEndingInItsFirstBlockSkipsTheSummary`
counts the summary reads as well as the blocks: with a limit of 5 the walk
opens one block and reads no summary bytes (on a498b38737f it read the summary
once). `BoundedExpansionThatGoesOnStillSkipsBlocks` checks that a bounded walk
that has to go on reads the summary once and opens at most three blocks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]