Coverage Report

Created: 2026-09-12 04:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/index/index_file_writer.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
// CLucene is third-party code and is not clean under -Wconversion (which
21
// -Wshorten-64-to-32 belongs to). Whether its first expansion lands inside
22
// someone else's suppressed region depends on include order, so suppress it
23
// deliberately here (same pattern as inverted_index_common_impl.h).
24
#ifdef __clang__
25
#pragma clang diagnostic push
26
#pragma clang diagnostic ignored "-Wconversion"
27
#endif
28
#include <CLucene.h> // IWYU pragma: keep
29
#include <CLucene/store/IndexInput.h>
30
#ifdef __clang__
31
#pragma clang diagnostic pop
32
#endif
33
#include <gen_cpp/olap_common.pb.h>
34
#include <gen_cpp/olap_file.pb.h>
35
36
#include <optional>
37
#include <string>
38
#include <utility>
39
#include <vector>
40
41
#include "common/be_mock_util.h"
42
#include "io/fs/file_system.h"
43
#include "io/fs/file_writer.h"
44
#include "io/fs/local_file_system.h"
45
#include "storage/index/index_storage_format.h"
46
#include "storage/index/inverted/gram/gram_scheme.h"
47
#include "storage/index/inverted/inverted_index_common.h"
48
#include "storage/index/inverted/inverted_index_compound_reader.h"
49
#include "storage/index/inverted/inverted_index_searcher.h"
50
#include "storage/index/snii/format/format_constants.h"
51
#include "storage/index/snii/snii_doris_adapter.h"
52
#include "storage/index/snii/writer/snii_compound_writer.h"
53
54
namespace doris::snii::writer {
55
class MemoryReporter;
56
class SpimiTermBuffer;
57
class SniiCompoundWriter;
58
} // namespace doris::snii::writer
59
60
namespace doris {
61
class TabletIndex;
62
63
namespace segment_v2 {
64
class DorisFSDirectory;
65
namespace snii_doris {
66
class DorisSniiFileWriter;
67
} // namespace snii_doris
68
69
using InvertedIndexDirectoryMap =
70
        std::map<std::pair<int64_t, std::string>, std::shared_ptr<lucene::store::Directory>>;
71
72
class IndexFileWriter;
73
using IndexFileWriterPtr = std::unique_ptr<IndexFileWriter>;
74
75
class IndexFileWriter {
76
public:
77
    IndexFileWriter(io::FileSystemSPtr fs, std::string index_path_prefix, std::string rowset_id,
78
                    int64_t seg_id, InvertedIndexStorageFormatPB storage_format,
79
                    io::FileWriterPtr file_writer = nullptr, bool can_use_ram_dir = true,
80
                    int64_t tablet_id = -1);
81
751
    virtual ~IndexFileWriter() = default;
82
83
    MOCK_FUNCTION Result<std::shared_ptr<DorisFSDirectory>> open(const TabletIndex* index_meta);
84
    // The directory an ANN index is built into. Separate from open() because the
85
    // two formats stage ANN output in different places: V1/V2 hand faiss the same
86
    // CLucene filesystem directory every other index gets, while SNII hands it a
87
    // memory-backed staging directory whose bytes begin_close() seals into a blob
88
    // logical index. Callers only ever write through it, so the return type is
89
    // the lucene::store::Directory base -- widening open() itself would push that
90
    // base type onto the CLucene inverted writer and index_tool, which genuinely
91
    // need the DorisFSDirectory subclass.
92
    Result<std::shared_ptr<lucene::store::Directory>> open_ann_directory(
93
            const TabletIndex* index_meta);
94
    // SNII only: drops the staging directory of one ANN index whose serialization
95
    // failed. Its sub-files unlink themselves once their last owner is gone, and
96
    // the producer releases its own reference alongside this call -- without that
97
    // a failed save keeps an ANN-sized file and its descriptor on the temp
98
    // filesystem until this writer is destroyed, which for a rowset build is not
99
    // until every other segment has been written. V1/V2 keep their directory: its
100
    // files ARE the index output, and begin_close() is what removes them.
101
    void discard_ann_staging_directory(const TabletIndex* index_meta);
102
    // SNII only: drops the staging of EVERY index on this writer because the
103
    // segment they belong to is being abandoned. Layered above the per-index
104
    // discard, not a duplicate of it: that one fires the instant one ANN
105
    // serialization fails, while this one covers a segment that failed AFTER its
106
    // indexes staged successfully, when nothing else will ever seal them.
107
    void abandon_snii_staging();
108
    // Write-path facts for one SNII index flush.
109
    struct SniiAddIndexOptions {
110
        // This flush serves a stream/broker load (DataWriteType::TYPE_DIRECT):
111
        // the prx region compresses at snii_prx_zstd_level_direct_load;
112
        // compaction / schema change / ADD INDEX keep snii_prx_zstd_level.
113
        bool is_direct_load = false;
114
        // The exact gram tokenizer scheme that produced this index's dictionary.
115
        std::optional<gram::GramScheme> gram_scheme;
116
        // One byte of BM25 norms per document; empty for keyword or positionless indexes.
117
        // If nonempty, its size must equal doc_count, and postings retain frequencies for scoring.
118
        std::vector<uint8_t> encoded_norms;
119
    };
120
    Status add_snii_index(const TabletIndex* index_meta, uint32_t doc_count,
121
                          std::vector<uint32_t> null_docids,
122
                          doris::snii::writer::SpimiTermBuffer* const term_buffer,
123
                          doris::snii::format::IndexConfig index_config,
124
                          SniiAddIndexOptions options,
125
                          doris::snii::writer::MemoryReporter* const mem_reporter);
126
    // T2.2 compaction index merge fast path: begins a STREAMED SNII index
127
    // session on this compound. Unlike add_snii_index (which drains a SPIMI
128
    // term buffer), the caller pushes pre-merged, lexicographically sorted
129
    // terms through *session and seals the index with (*session)->finish().
130
    // Write parameters resolve through the SAME helper as add_snii_index
131
    // (zstd levels / dict block size), always at the COMPACTION
132
    // prx tier (a merge is never a direct load). CommonGrams T3 callers transfer
133
    // a precharged destination norm vector and a validated static metadata seed;
134
    // the streamed session late-binds semantic token_count before finish. Only ONE
135
    // session may be active per compound at a time, and begin_close() with an
136
    // unfinished session fails instead of sealing a half-fed container. The
137
    // handle is owned by this writer and valid until it is destroyed.
138
    Status add_snii_index_streamed(
139
            const TabletIndex* index_meta, uint32_t doc_count,
140
            doris::snii::writer::TrackedNullDocids null_docids,
141
            doris::snii::format::IndexConfig index_config,
142
            std::shared_ptr<doris::snii::writer::MemoryReporter> mem_reporter,
143
            doris::snii::writer::SniiStreamedIndexSession** session);
144
    // Sessions with write_norms=true must supply norms through set_encoded_norms before finish.
145
    // Compaction rebuilds them in the same pass that merges postings.
146
    Status add_snii_index_streamed(
147
            const TabletIndex* index_meta, uint32_t doc_count,
148
            doris::snii::writer::TrackedNullDocids null_docids, bool write_norms,
149
            doris::snii::format::IndexConfig index_config,
150
            std::shared_ptr<doris::snii::writer::MemoryReporter> mem_reporter,
151
            doris::snii::writer::SniiStreamedIndexSession** session);
152
    // Registers one opaque BLOB logical index (a numeric BKD, an ANN graph, ...)
153
    // on this SNII compound. Unlike add_snii_index it feeds the writer no terms:
154
    // the sub-file bytes are pulled through the BlobFileSource callbacks at
155
    // finish(), which is what lets the container -- not the producer -- decide
156
    // cold/hot placement. Registration writes no byte, so a rejected call leaves
157
    // the writer clean.
158
    Status add_snii_blob_index(const TabletIndex* index_meta,
159
                               doris::snii::format::LogicalIndexKind kind,
160
                               std::vector<doris::snii::writer::BlobFileSource> cold_files,
161
                               std::vector<doris::snii::writer::BlobFileSource> hot_files);
162
    void retain_snii_memory_reporter(
163
            std::unique_ptr<doris::snii::writer::MemoryReporter> mem_reporter);
164
    // SNII only, BUILD INDEX rewrite: copies the source container's valid
165
    // physical prefix and registers the inherited metadata groups so begin_close
166
    // re-emits them without decoding a posting. Must precede every
167
    // add_snii_index on this writer (the copied prefix owns the container
168
    // front).
169
    Status inherit_snii(const doris::snii::reader::SniiRewriteSnapshot& snapshot,
170
                        doris::snii::io::FileReader* source);
171
    Status delete_index(const TabletIndex* index_meta);
172
    Status initialize(InvertedIndexDirectoryMap& indices_dirs);
173
    Status add_into_searcher_cache();
174
    // Begin the close process. This mainly triggers the asynchronous close operation of
175
    // _idx_v2_writer by calling close(true), which starts the close process but returns
176
    // immediately without waiting for completion.
177
    Status begin_close();
178
    // Finish the close process. This waits for the close operation to complete by calling
179
    // _idx_v2_writer->close(false), which blocks until the close is fully done.
180
    Status finish_close();
181
318
    const InvertedIndexFileInfo* get_index_file_info() const {
182
318
        DCHECK(_closed) << debug_string();
183
318
        return &_file_info;
184
318
    }
185
442
    int64_t get_index_file_total_size() const {
186
442
        DCHECK(_closed) << debug_string();
187
442
        return _total_file_size;
188
442
    }
189
0
    const io::FileSystemSPtr& get_fs() const { return _fs; }
190
7.55k
    InvertedIndexStorageFormatPB get_storage_format() const { return _storage_format; }
191
385
    void set_file_writer_opts(const io::FileWriterOptions& opts) { _opts = opts; }
192
    std::vector<std::string> get_index_file_names() const;
193
    std::string debug_string() const;
194
195
    // Get internal file writer (for merge file index collection)
196
0
    io::FileWriter* get_file_writer() const { return _idx_v2_writer.get(); }
197
198
private:
199
    Status _insert_directory_into_map(int64_t index_id, const std::string& index_suffix,
200
                                      std::shared_ptr<lucene::store::Directory> dir);
201
    // SNII only: registers a memory-backed staging directory for one ANN index,
202
    // together with the metadata begin_close() needs to seal it.
203
    Result<std::shared_ptr<lucene::store::Directory>> _open_snii_ann_staging_directory(
204
            const TabletIndex* index_meta);
205
    virtual Result<std::unique_ptr<IndexSearcherBuilder>> _construct_index_searcher_builder(
206
            const DorisCompoundReader* dir);
207
    // SNII only: turns every ANN staging directory into a blob logical index in
208
    // the container. Runs once, from begin_close(), before the compound writer is
209
    // sealed. Registration copies no byte -- the staged buffers are pulled by
210
    // finish() through the blob sources.
211
    Status _seal_snii_blob_directories();
212
    // Drops the staging directories once the container owns their bytes, or once
213
    // sealing has failed and they are dead either way. Only the SNII path needs
214
    // this: the V1/V2 branch of begin_close() releases its own directories
215
    // inline.
216
    void _release_snii_blob_directories();
217
218
    // Member variables...
219
    InvertedIndexDirectoryMap _indices_dirs;
220
    // SNII only: the index metadata behind each entry of _indices_dirs. Owned a
221
    // copy rather than borrowed, because the harvest happens in begin_close(),
222
    // long after open() returned. Held by shared_ptr so this header keeps
223
    // TabletIndex incomplete -- it is included nearly everywhere.
224
    std::map<std::pair<int64_t, std::string>, std::shared_ptr<TabletIndex>> _snii_blob_dir_metas;
225
    const io::FileSystemSPtr _fs;
226
    std::string _index_path_prefix;
227
    std::string _rowset_id;
228
    int64_t _seg_id;
229
    InvertedIndexStorageFormatPB _storage_format;
230
    std::string _tmp_dir;
231
    const std::shared_ptr<io::LocalFileSystem>& _local_fs;
232
233
    // write to disk or stream
234
    io::FileWriterPtr _idx_v2_writer = nullptr;
235
    io::FileWriterOptions _opts;
236
237
    // v1: all file size
238
    // v2: file size
239
    int64_t _total_file_size = 0;
240
    InvertedIndexFileInfo _file_info;
241
242
    // only once
243
    bool _closed = false;
244
    bool _can_use_ram_dir = true;
245
246
    IndexStorageFormatPtr _index_storage_format;
247
    int64_t _tablet_id = -1;
248
    std::unique_ptr<snii_doris::DorisSniiFileWriter> _snii_file_writer;
249
    std::vector<std::shared_ptr<doris::snii::writer::MemoryReporter>> _snii_memory_reporters;
250
    std::unique_ptr<doris::snii::writer::SniiCompoundWriter> _snii_compound_writer;
251
    size_t _snii_index_count = 0;
252
253
    friend class IndexStorageFormatV1;
254
    friend class IndexStorageFormatV2;
255
    friend class IndexFileWriterTest;
256
};
257
258
} // namespace segment_v2
259
} // namespace doris