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.31k | 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.14k | explicit FileWriterCreatorT(T* t) : _t(t) {}_ZN5doris18FileWriterCreatorTINS_20BaseBetaRowsetWriterEEC2EPS1_ Line | Count | Source | 67 | 1.14k | 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 | 1.96k | FileType file_type = FileType::SEGMENT_FILE) override { |
71 | 1.96k | return _t->create_file_writer(segment_id, file_writer, file_type); |
72 | 1.96k | } _ZN5doris18FileWriterCreatorTINS_20BaseBetaRowsetWriterEE6createEjRSt10unique_ptrINS_2io10FileWriterESt14default_deleteIS5_EENS_8FileTypeE Line | Count | Source | 70 | 1.96k | FileType file_type = FileType::SEGMENT_FILE) override { | 71 | 1.96k | return _t->create_file_writer(segment_id, file_writer, file_type); | 72 | 1.96k | } |
Unexecuted instantiation: _ZN5doris18FileWriterCreatorTINS_18BetaRowsetWriterV2EE6createEjRSt10unique_ptrINS_2io10FileWriterESt14default_deleteIS5_EENS_8FileTypeE |
73 | | |
74 | 276 | Status create(uint32_t segment_id, IndexFileWriterPtr* file_writer) override { |
75 | 276 | return _t->create_index_file_writer(segment_id, file_writer); |
76 | 276 | } _ZN5doris18FileWriterCreatorTINS_20BaseBetaRowsetWriterEE6createEjPSt10unique_ptrINS_10segment_v215IndexFileWriterESt14default_deleteIS5_EE Line | Count | Source | 74 | 276 | Status create(uint32_t segment_id, IndexFileWriterPtr* file_writer) override { | 75 | 276 | return _t->create_index_file_writer(segment_id, file_writer); | 76 | 276 | } |
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.31k | 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.14k | explicit SegmentCollectorT(T* t) : _t(t) {}_ZN5doris17SegmentCollectorTINS_20BaseBetaRowsetWriterEEC2EPS1_ Line | Count | Source | 93 | 1.14k | explicit SegmentCollectorT(T* t) : _t(t) {} |
_ZN5doris17SegmentCollectorTINS_18BetaRowsetWriterV2EEC2EPS1_ Line | Count | Source | 93 | 1 | explicit SegmentCollectorT(T* t) : _t(t) {} |
|
94 | | |
95 | 1.96k | Status add(uint32_t segment_id, SegmentStatistics& segstat) override { |
96 | 1.96k | return _t->add_segment(segment_id, segstat); |
97 | 1.96k | } _ZN5doris17SegmentCollectorTINS_20BaseBetaRowsetWriterEE3addEjRNS_17SegmentStatisticsE Line | Count | Source | 95 | 1.96k | Status add(uint32_t segment_id, SegmentStatistics& segstat) override { | 96 | 1.96k | return _t->add_segment(segment_id, segstat); | 97 | 1.96k | } |
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 | 15 | int64_t num_rows_written() const { return _num_rows_written; } |
121 | | |
122 | | // for partial update |
123 | 0 | int64_t num_rows_updated() const { return _num_rows_updated; } |
124 | 0 | int64_t num_rows_deleted() const { return _num_rows_deleted; } |
125 | 0 | int64_t num_rows_new_added() const { return _num_rows_new_added; } |
126 | 0 | 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.48k | Status add_rows(const Block* block, size_t row_offset, size_t input_row_num) { |
138 | 2.48k | return _flusher->_add_rows(_writer, block, row_offset, input_row_num); |
139 | 2.48k | } |
140 | | |
141 | | Status flush(); |
142 | | |
143 | | int64_t max_row_to_add(size_t row_avg_size_in_bytes); |
144 | | |
145 | | private: |
146 | | Writer(SegmentFlusher* flusher, std::unique_ptr<segment_v2::SegmentWriter>& segment_writer); |
147 | | |
148 | | SegmentFlusher* _flusher = nullptr; |
149 | | std::unique_ptr<segment_v2::SegmentWriter> _writer; |
150 | | }; |
151 | | |
152 | | Status create_writer(std::unique_ptr<SegmentFlusher::Writer>& writer, uint32_t segment_id); |
153 | | |
154 | | private: |
155 | | Status _add_rows(std::unique_ptr<segment_v2::SegmentWriter>& segment_writer, const Block* block, |
156 | | size_t row_offset, size_t row_num); |
157 | | Status _add_rows(std::unique_ptr<segment_v2::VerticalSegmentWriter>& segment_writer, |
158 | | const Block* block, size_t row_offset, size_t row_num); |
159 | | Status _create_segment_writer(std::unique_ptr<segment_v2::SegmentWriter>& writer, |
160 | | int32_t segment_id, bool no_compression = false); |
161 | | Status _create_segment_writer(std::unique_ptr<segment_v2::VerticalSegmentWriter>& writer, |
162 | | int32_t segment_id, bool no_compression = false); |
163 | | Status _flush_segment_writer(std::unique_ptr<segment_v2::SegmentWriter>& writer, |
164 | | int64_t* flush_size = nullptr); |
165 | | Status _flush_segment_writer(std::unique_ptr<segment_v2::VerticalSegmentWriter>& writer, |
166 | | int64_t* flush_size = nullptr); |
167 | | void _record_segment_index_file_cache_preload( |
168 | | uint32_t segment_id, const segment_v2::SegmentIndexFileCacheInfo& info); |
169 | | Status _preload_segment_indexes_to_file_cache(); |
170 | | |
171 | | private: |
172 | | RowsetWriterContext& _context; |
173 | | SegmentFileCollection& _seg_files; |
174 | | InvertedIndexFileCollection& _idx_files; |
175 | | |
176 | | // written rows by add_block/add_row |
177 | | std::atomic<int64_t> _num_rows_written = 0; |
178 | | std::atomic<int64_t> _num_rows_updated = 0; |
179 | | std::atomic<int64_t> _num_rows_new_added = 0; |
180 | | std::atomic<int64_t> _num_rows_deleted = 0; |
181 | | std::atomic<int64_t> _num_rows_filtered = 0; |
182 | | std::mutex _segment_index_file_cache_preloads_lock; |
183 | | std::vector<segment_v2::SegmentIndexFileCachePreloadTask> _segment_index_file_cache_preloads; |
184 | | }; |
185 | | |
186 | | class SegmentCreator { |
187 | | public: |
188 | | SegmentCreator(RowsetWriterContext& context, SegmentFileCollection& seg_files, |
189 | | InvertedIndexFileCollection& idx_files); |
190 | | |
191 | 1.15k | ~SegmentCreator() = default; |
192 | | |
193 | 0 | void set_segment_start_id(uint32_t start_id) { _next_segment_id = start_id; } |
194 | | |
195 | | Status add_block(const Block* block); |
196 | | |
197 | | Status flush(); |
198 | | |
199 | 1.97k | int32_t allocate_segment_id() { return _next_segment_id.fetch_add(1); } |
200 | | |
201 | | // Return the next segment id to be allocated without advancing internal state. |
202 | 0 | int32_t get_allocated_segment_id() const { return _next_segment_id.load(); } |
203 | | |
204 | 12 | int32_t next_segment_id() const { return _next_segment_id.load(); } |
205 | | |
206 | 15 | int64_t num_rows_written() const { return _segment_flusher.num_rows_written(); } |
207 | | |
208 | | // for partial update |
209 | 0 | int64_t num_rows_updated() const { return _segment_flusher.num_rows_updated(); } |
210 | 0 | int64_t num_rows_deleted() const { return _segment_flusher.num_rows_deleted(); } |
211 | 0 | int64_t num_rows_new_added() const { return _segment_flusher.num_rows_new_added(); } |
212 | 0 | int64_t num_rows_filtered() const { return _segment_flusher.num_rows_filtered(); } |
213 | | |
214 | | // Flush a block into a single segment, with pre-allocated segment_id. |
215 | | // Return the file size flushed to disk in "flush_size" |
216 | | // This method is thread-safe. |
217 | | Status flush_single_block(const Block* block, int32_t segment_id, |
218 | | int64_t* flush_size = nullptr); |
219 | | |
220 | | // Flush a block into a single segment, without pre-allocated segment_id. |
221 | | // This method is thread-safe. |
222 | 3 | Status flush_single_block(const Block* block) { |
223 | 3 | return flush_single_block(block, allocate_segment_id()); |
224 | 3 | } |
225 | | |
226 | | Status close(); |
227 | | |
228 | | private: |
229 | | std::atomic<int32_t> _next_segment_id = 0; |
230 | | SegmentFlusher _segment_flusher; |
231 | | std::unique_ptr<SegmentFlusher::Writer> _flush_writer; |
232 | | }; |
233 | | |
234 | | } // namespace doris |