Coverage Report

Created: 2026-08-18 18:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/rowset/segment_creator.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
#include <gen_cpp/internal_service.pb.h>
21
#include <gen_cpp/olap_file.pb.h>
22
23
#include <memory>
24
#include <mutex>
25
#include <utility>
26
#include <vector>
27
28
#include "common/status.h"
29
#include "core/block/block.h"
30
#include "io/fs/file_reader_writer_fwd.h"
31
#include "storage/index/index_file_writer.h"
32
#include "storage/rowset/rowset_writer_context.h"
33
#include "storage/segment/segment_index_file_cache_loader.h"
34
#include "storage/tablet/tablet_fwd.h"
35
36
namespace doris {
37
class Block;
38
39
namespace segment_v2 {
40
class SegmentWriter;
41
class VerticalSegmentWriter;
42
class DerivedColumnGenerator;
43
// Matches block_transform.h: at most one derived column (the row-store column)
44
// for each flush, held as a {cid, generator} pair; null generator means none.
45
using DerivedColumn = std::pair<uint32_t, std::shared_ptr<const DerivedColumnGenerator>>;
46
} // namespace segment_v2
47
48
struct SegmentStatistics;
49
class BetaRowsetWriter;
50
class SegmentFileCollection;
51
class InvertedIndexFileCollection;
52
53
class FileWriterCreator {
54
public:
55
1.38k
    virtual ~FileWriterCreator() = default;
56
57
    virtual Status create(uint32_t segment_id, io::FileWriterPtr& file_writer,
58
                          FileType file_type = FileType::SEGMENT_FILE) = 0;
59
60
    virtual Status create(uint32_t segment_id, IndexFileWriterPtr* file_writer) = 0;
61
};
62
63
template <class T>
64
    requires std::is_base_of_v<RowsetWriter, T>
65
class FileWriterCreatorT : public FileWriterCreator {
66
public:
67
1.21k
    explicit FileWriterCreatorT(T* t) : _t(t) {}
_ZN5doris18FileWriterCreatorTINS_20BaseBetaRowsetWriterEEC2EPS1_
Line
Count
Source
67
1.21k
    explicit FileWriterCreatorT(T* t) : _t(t) {}
_ZN5doris18FileWriterCreatorTINS_18BetaRowsetWriterV2EEC2EPS1_
Line
Count
Source
67
1
    explicit FileWriterCreatorT(T* t) : _t(t) {}
68
69
    Status create(uint32_t segment_id, io::FileWriterPtr& file_writer,
70
2.03k
                  FileType file_type = FileType::SEGMENT_FILE) override {
71
2.03k
        return _t->create_file_writer(segment_id, file_writer, file_type);
72
2.03k
    }
_ZN5doris18FileWriterCreatorTINS_20BaseBetaRowsetWriterEE6createEjRSt10unique_ptrINS_2io10FileWriterESt14default_deleteIS5_EENS_8FileTypeE
Line
Count
Source
70
2.03k
                  FileType file_type = FileType::SEGMENT_FILE) override {
71
2.03k
        return _t->create_file_writer(segment_id, file_writer, file_type);
72
2.03k
    }
Unexecuted instantiation: _ZN5doris18FileWriterCreatorTINS_18BetaRowsetWriterV2EE6createEjRSt10unique_ptrINS_2io10FileWriterESt14default_deleteIS5_EENS_8FileTypeE
73
74
294
    Status create(uint32_t segment_id, IndexFileWriterPtr* file_writer) override {
75
294
        return _t->create_index_file_writer(segment_id, file_writer);
76
294
    }
_ZN5doris18FileWriterCreatorTINS_20BaseBetaRowsetWriterEE6createEjPSt10unique_ptrINS_10segment_v215IndexFileWriterESt14default_deleteIS5_EE
Line
Count
Source
74
294
    Status create(uint32_t segment_id, IndexFileWriterPtr* file_writer) override {
75
294
        return _t->create_index_file_writer(segment_id, file_writer);
76
294
    }
Unexecuted instantiation: _ZN5doris18FileWriterCreatorTINS_18BetaRowsetWriterV2EE6createEjPSt10unique_ptrINS_10segment_v215IndexFileWriterESt14default_deleteIS5_EE
77
78
private:
79
    T* _t = nullptr;
80
};
81
82
class SegmentCollector {
83
public:
84
1.38k
    virtual ~SegmentCollector() = default;
85
86
    virtual Status add(uint32_t segment_id, SegmentStatistics& segstat) = 0;
87
};
88
89
template <class T>
90
    requires std::is_base_of_v<RowsetWriter, T>
91
class SegmentCollectorT : public SegmentCollector {
92
public:
93
1.21k
    explicit SegmentCollectorT(T* t) : _t(t) {}
_ZN5doris17SegmentCollectorTINS_20BaseBetaRowsetWriterEEC2EPS1_
Line
Count
Source
93
1.21k
    explicit SegmentCollectorT(T* t) : _t(t) {}
_ZN5doris17SegmentCollectorTINS_18BetaRowsetWriterV2EEC2EPS1_
Line
Count
Source
93
1
    explicit SegmentCollectorT(T* t) : _t(t) {}
94
95
2.03k
    Status add(uint32_t segment_id, SegmentStatistics& segstat) override {
96
2.03k
        return _t->add_segment(segment_id, segstat);
97
2.03k
    }
_ZN5doris17SegmentCollectorTINS_20BaseBetaRowsetWriterEE3addEjRNS_17SegmentStatisticsE
Line
Count
Source
95
2.03k
    Status add(uint32_t segment_id, SegmentStatistics& segstat) override {
96
2.03k
        return _t->add_segment(segment_id, segstat);
97
2.03k
    }
Unexecuted instantiation: _ZN5doris17SegmentCollectorTINS_18BetaRowsetWriterV2EE3addEjRNS_17SegmentStatisticsE
98
99
private:
100
    T* _t = nullptr;
101
};
102
103
class SegmentFlusher {
104
public:
105
    SegmentFlusher(RowsetWriterContext& context, SegmentFileCollection& seg_files,
106
                   InvertedIndexFileCollection& idx_files);
107
108
    ~SegmentFlusher();
109
110
    // Runs the block transform chain on `block` and hands back the derived (row-store)
111
    // column for the caller to feed into its writer.
112
    Status transform_block(Block* block, int32_t segment_id,
113
                           segment_v2::DerivedColumn* derived_column);
114
115
    // Return the file size flushed to disk in "flush_size"
116
    // This method is thread-safe.
117
    Status flush_single_block(const Block* block, int32_t segment_id,
118
                              int64_t* flush_size = nullptr);
119
120
17
    int64_t num_rows_written() const { return _num_rows_written; }
121
122
    // for partial update
123
1
    int64_t num_rows_updated() const { return _num_rows_updated; }
124
1
    int64_t num_rows_deleted() const { return _num_rows_deleted; }
125
1
    int64_t num_rows_new_added() const { return _num_rows_new_added; }
126
1
    int64_t num_rows_filtered() const { return _num_rows_filtered; }
127
128
    Status close();
129
130
public:
131
    class Writer {
132
        friend class SegmentFlusher;
133
134
    public:
135
        ~Writer();
136
137
2.54k
        Status add_rows(const Block* block, size_t row_offset, size_t input_row_num) {
138
2.54k
            RETURN_IF_ERROR(_flusher->_add_rows(_writer, block, row_offset, input_row_num));
139
2.54k
            _flusher->_num_rows_written += input_row_num;
140
2.54k
            return Status::OK();
141
2.54k
        }
142
143
        Status flush();
144
145
        int64_t max_row_to_add(size_t row_avg_size_in_bytes);
146
147
    private:
148
        Writer(SegmentFlusher* flusher, std::unique_ptr<segment_v2::SegmentWriter>& segment_writer);
149
150
        SegmentFlusher* _flusher = nullptr;
151
        std::unique_ptr<segment_v2::SegmentWriter> _writer;
152
    };
153
154
    Status create_writer(std::unique_ptr<SegmentFlusher::Writer>& writer, uint32_t segment_id);
155
156
private:
157
    Status _add_rows(std::unique_ptr<segment_v2::SegmentWriter>& segment_writer, const Block* block,
158
                     size_t row_offset, size_t row_num);
159
    Status _add_rows(std::unique_ptr<segment_v2::VerticalSegmentWriter>& segment_writer,
160
                     const Block* block, size_t row_offset, size_t row_num);
161
    Status _create_segment_writer(std::unique_ptr<segment_v2::SegmentWriter>& writer,
162
                                  int32_t segment_id, bool no_compression = false);
163
    Status _create_segment_writer(std::unique_ptr<segment_v2::VerticalSegmentWriter>& writer,
164
                                  int32_t segment_id, bool no_compression = false);
165
    Status _flush_segment_writer(std::unique_ptr<segment_v2::SegmentWriter>& writer,
166
                                 int64_t* flush_size = nullptr);
167
    Status _flush_segment_writer(std::unique_ptr<segment_v2::VerticalSegmentWriter>& writer,
168
                                 int64_t* flush_size = nullptr);
169
    void _record_segment_index_file_cache_preload(
170
            uint32_t segment_id, const segment_v2::SegmentIndexFileCacheInfo& info);
171
    Status _preload_segment_indexes_to_file_cache();
172
173
private:
174
    RowsetWriterContext& _context;
175
    SegmentFileCollection& _seg_files;
176
    InvertedIndexFileCollection& _idx_files;
177
178
    // written rows by add_block/add_row
179
    std::atomic<int64_t> _num_rows_written = 0;
180
    std::atomic<int64_t> _num_rows_updated = 0;
181
    std::atomic<int64_t> _num_rows_new_added = 0;
182
    std::atomic<int64_t> _num_rows_deleted = 0;
183
    std::atomic<int64_t> _num_rows_filtered = 0;
184
    std::mutex _segment_index_file_cache_preloads_lock;
185
    std::vector<segment_v2::SegmentIndexFileCachePreloadTask> _segment_index_file_cache_preloads;
186
};
187
188
class SegmentCreator {
189
public:
190
    SegmentCreator(RowsetWriterContext& context, SegmentFileCollection& seg_files,
191
                   InvertedIndexFileCollection& idx_files);
192
193
1.22k
    ~SegmentCreator() = default;
194
195
0
    void set_segment_start_id(uint32_t start_id) { _next_segment_id = start_id; }
196
197
    Status add_block(const Block* block);
198
199
    Status flush();
200
201
2.03k
    int32_t allocate_segment_id() { return _next_segment_id.fetch_add(1); }
202
203
    // Return the next segment id to be allocated without advancing internal state.
204
0
    int32_t get_allocated_segment_id() const { return _next_segment_id.load(); }
205
206
19
    int32_t next_segment_id() const { return _next_segment_id.load(); }
207
208
17
    int64_t num_rows_written() const { return _segment_flusher.num_rows_written(); }
209
210
    // for partial update
211
1
    int64_t num_rows_updated() const { return _segment_flusher.num_rows_updated(); }
212
1
    int64_t num_rows_deleted() const { return _segment_flusher.num_rows_deleted(); }
213
1
    int64_t num_rows_new_added() const { return _segment_flusher.num_rows_new_added(); }
214
1
    int64_t num_rows_filtered() const { return _segment_flusher.num_rows_filtered(); }
215
216
    // Flush a block into a single segment, with pre-allocated segment_id.
217
    // Return the file size flushed to disk in "flush_size"
218
    // This method is thread-safe.
219
    Status flush_single_block(const Block* block, int32_t segment_id,
220
                              int64_t* flush_size = nullptr);
221
222
    // Flush a block into a single segment, without pre-allocated segment_id.
223
    // This method is thread-safe.
224
3
    Status flush_single_block(const Block* block) {
225
3
        return flush_single_block(block, allocate_segment_id());
226
3
    }
227
228
    Status close();
229
230
private:
231
    std::atomic<int32_t> _next_segment_id = 0;
232
    SegmentFlusher _segment_flusher;
233
    std::unique_ptr<SegmentFlusher::Writer> _flush_writer;
234
};
235
236
} // namespace doris