be/src/storage/segment/vertical_segment_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 | | #include <gen_cpp/olap_file.pb.h> |
21 | | #include <gen_cpp/segment_v2.pb.h> |
22 | | |
23 | | #include <cstddef> |
24 | | #include <cstdint> |
25 | | #include <functional> |
26 | | #include <map> |
27 | | #include <memory> // unique_ptr |
28 | | #include <string> |
29 | | #include <unordered_set> |
30 | | #include <vector> |
31 | | |
32 | | #include "common/status.h" // Status |
33 | | #include "storage/index/index_file_writer.h" |
34 | | #include "storage/olap_define.h" |
35 | | #include "storage/partial_update_info.h" |
36 | | #include "storage/segment/column_writer.h" |
37 | | #include "storage/tablet/tablet.h" |
38 | | #include "storage/tablet/tablet_schema.h" |
39 | | #include "util/faststring.h" |
40 | | #include "util/slice.h" |
41 | | |
42 | | namespace doris { |
43 | | class Block; |
44 | | class IOlapColumnDataAccessor; |
45 | | class OlapBlockDataConvertor; |
46 | | |
47 | | class DataDir; |
48 | | class MemTracker; |
49 | | class ShortKeyIndexBuilder; |
50 | | class PrimaryKeyIndexBuilder; |
51 | | class KeyCoder; |
52 | | struct RowsetWriterContext; |
53 | | |
54 | | namespace io { |
55 | | class FileWriter; |
56 | | class FileSystem; |
57 | | } // namespace io |
58 | | namespace segment_v2 { |
59 | | class IndexFileWriter; |
60 | | |
61 | | struct VerticalSegmentWriterOptions { |
62 | | uint32_t num_rows_per_block = 1024; |
63 | | bool enable_unique_key_merge_on_write = false; |
64 | | CompressionTypePB compression_type = UNKNOWN_COMPRESSION; |
65 | | |
66 | | RowsetWriterContext* rowset_ctx = nullptr; |
67 | | DataWriteType write_type = DataWriteType::TYPE_DEFAULT; |
68 | | std::shared_ptr<MowContext> mow_ctx; |
69 | | }; |
70 | | |
71 | | struct RowsInBlock { |
72 | | const Block* block; |
73 | | size_t row_pos; |
74 | | size_t num_rows; |
75 | | }; |
76 | | |
77 | | class VerticalSegmentWriter { |
78 | | public: |
79 | | explicit VerticalSegmentWriter(io::FileWriter* file_writer, uint32_t segment_id, |
80 | | TabletSchemaSPtr tablet_schema, BaseTabletSPtr tablet, |
81 | | DataDir* data_dir, const VerticalSegmentWriterOptions& opts, |
82 | | IndexFileWriter* index_file_writer); |
83 | | ~VerticalSegmentWriter(); |
84 | | |
85 | | VerticalSegmentWriter(const VerticalSegmentWriter&) = delete; |
86 | | const VerticalSegmentWriter& operator=(const VerticalSegmentWriter&) = delete; |
87 | | |
88 | | Status init(); |
89 | | |
90 | | // Add one block to batch, memory is owned by the caller. |
91 | | // The batched blocks will be flushed in write_batch. |
92 | | // Once write_batch is called, no more blocks shoud be added. |
93 | | Status batch_block(const Block* block, size_t row_pos, size_t num_rows); |
94 | | Status write_batch(); |
95 | | |
96 | 0 | [[nodiscard]] std::string data_dir_path() const { |
97 | 0 | return _data_dir == nullptr ? "" : _data_dir->path(); |
98 | 0 | } |
99 | | |
100 | 12 | [[nodiscard]] uint32_t num_rows_written() const { return _num_rows_written; } |
101 | | |
102 | | // for partial update |
103 | 12 | [[nodiscard]] int64_t num_rows_updated() const { return _num_rows_updated; } |
104 | 12 | [[nodiscard]] int64_t num_rows_deleted() const { return _num_rows_deleted; } |
105 | 12 | [[nodiscard]] int64_t num_rows_new_added() const { return _num_rows_new_added; } |
106 | 12 | [[nodiscard]] int64_t num_rows_filtered() const { return _num_rows_filtered; } |
107 | 0 | [[nodiscard]] uint32_t row_count() const { return _row_count; } |
108 | 12 | [[nodiscard]] uint32_t segment_id() const { return _segment_id; } |
109 | | |
110 | | Status finalize(uint64_t* segment_file_size, uint64_t* index_size); |
111 | | |
112 | | Status finalize_columns_index(uint64_t* index_size); |
113 | | Status finalize_footer(uint64_t* segment_file_size); |
114 | | |
115 | | Slice min_encoded_key(); |
116 | | Slice max_encoded_key(); |
117 | | |
118 | | void clear(); |
119 | | |
120 | 12 | Status close_inverted_index(int64_t* inverted_index_file_size) { |
121 | | // no inverted index |
122 | 12 | if (_index_file_writer == nullptr) { |
123 | 12 | *inverted_index_file_size = 0; |
124 | 12 | return Status::OK(); |
125 | 12 | } |
126 | 0 | RETURN_IF_ERROR(_index_file_writer->begin_close()); |
127 | 0 | *inverted_index_file_size = _index_file_writer->get_index_file_total_size(); |
128 | 0 | return Status::OK(); |
129 | 0 | } |
130 | | |
131 | | private: |
132 | | void _init_column_meta(ColumnMetaPB* meta, uint32_t column_id, const TabletColumn& column); |
133 | | Status _create_column_writer(uint32_t cid, const TabletColumn& column, |
134 | | const TabletSchemaSPtr& schema); |
135 | | uint64_t _estimated_remaining_size(); |
136 | | Status _write_ordinal_index(); |
137 | | Status _write_zone_map(); |
138 | | Status _write_inverted_index(); |
139 | | Status _write_ann_index(); |
140 | | Status _write_bloom_filter_index(); |
141 | | Status _write_short_key_index(); |
142 | | Status _write_primary_key_index(); |
143 | | Status _write_footer(); |
144 | | Status _write_raw_data(const std::vector<Slice>& slices); |
145 | | void _maybe_invalid_row_cache(const std::string& key) const; |
146 | | std::string _encode_keys(const std::vector<IOlapColumnDataAccessor*>& key_columns, size_t pos); |
147 | | // used for unique-key with merge on write and segment min_max key |
148 | | std::string _full_encode_keys(const std::vector<IOlapColumnDataAccessor*>& key_columns, |
149 | | size_t pos); |
150 | | std::string _full_encode_keys(const std::vector<const KeyCoder*>& key_coders, |
151 | | const std::vector<IOlapColumnDataAccessor*>& key_columns, |
152 | | size_t pos); |
153 | | // used for unique-key with merge on write |
154 | | void _encode_seq_column(const IOlapColumnDataAccessor* seq_column, size_t pos, |
155 | | std::string* encoded_keys); |
156 | | // used for unique-key with merge on write tables with cluster keys |
157 | | void _encode_rowid(const uint32_t rowid, std::string* encoded_keys); |
158 | | void _set_min_max_key(const Slice& key); |
159 | | void _set_min_key(const Slice& key); |
160 | | void _set_max_key(const Slice& key); |
161 | | void _serialize_block_to_row_column(const Block& block); |
162 | | Status _probe_key_for_mow(std::string key, std::size_t segment_pos, bool have_input_seq_column, |
163 | | bool have_delete_sign, |
164 | | const std::vector<RowsetSharedPtr>& specified_rowsets, |
165 | | std::vector<std::unique_ptr<SegmentCacheHandle>>& segment_caches, |
166 | | bool& has_default_or_nullable, |
167 | | std::vector<bool>& use_default_or_null_flag, |
168 | | const std::function<void(const RowLocation& loc)>& found_cb, |
169 | | const std::function<Status()>& not_found_cb, |
170 | | PartialUpdateStats& stats); |
171 | | Status _partial_update_preconditions_check(size_t row_pos, bool is_flexible_update); |
172 | | Status _append_block_with_partial_content(RowsInBlock& data, Block& full_block); |
173 | | Status _append_block_with_flexible_partial_content(RowsInBlock& data, Block& full_block); |
174 | | Status _generate_encoded_default_seq_value(const TabletSchema& tablet_schema, |
175 | | const PartialUpdateInfo& info, |
176 | | std::string* encoded_value); |
177 | | Status _generate_flexible_read_plan( |
178 | | FlexibleReadPlan& read_plan, RowsInBlock& data, size_t segment_start_pos, |
179 | | bool schema_has_sequence_col, int32_t seq_map_col_unique_id, |
180 | | std::vector<BitmapValue>* skip_bitmaps, |
181 | | const std::vector<IOlapColumnDataAccessor*>& key_columns, |
182 | | IOlapColumnDataAccessor* seq_column, const signed char* delete_signs, |
183 | | const std::vector<RowsetSharedPtr>& specified_rowsets, |
184 | | std::vector<std::unique_ptr<SegmentCacheHandle>>& segment_caches, |
185 | | bool& has_default_or_nullable, std::vector<bool>& use_default_or_null_flag, |
186 | | PartialUpdateStats& stats); |
187 | | Status _generate_key_index(RowsInBlock& data, |
188 | | std::vector<IOlapColumnDataAccessor*>& key_columns, |
189 | | IOlapColumnDataAccessor* seq_column, |
190 | | std::map<uint32_t, IOlapColumnDataAccessor*>& cid_to_column); |
191 | | Status _generate_primary_key_index( |
192 | | const std::vector<const KeyCoder*>& primary_key_coders, |
193 | | const std::vector<IOlapColumnDataAccessor*>& primary_key_columns, |
194 | | IOlapColumnDataAccessor* seq_column, size_t num_rows, bool need_sort); |
195 | | Status _generate_short_key_index(std::vector<IOlapColumnDataAccessor*>& key_columns, |
196 | | size_t num_rows, const std::vector<size_t>& short_key_pos); |
197 | | Status _finalize_column_writer_and_update_meta(size_t cid); |
198 | | |
199 | | bool _is_mow(); |
200 | | bool _is_mow_with_cluster_key(); |
201 | | |
202 | | private: |
203 | | friend class ::doris::BlockAggregator; |
204 | | uint32_t _segment_id; |
205 | | TabletSchemaSPtr _tablet_schema; |
206 | | BaseTabletSPtr _tablet; |
207 | | DataDir* _data_dir = nullptr; |
208 | | VerticalSegmentWriterOptions _opts; |
209 | | |
210 | | // Not owned. owned by RowsetWriter |
211 | | io::FileWriter* _file_writer = nullptr; |
212 | | // Not owned. owned by RowsetWriter or SegmentFlusher |
213 | | IndexFileWriter* _index_file_writer = nullptr; |
214 | | |
215 | | SegmentFooterPB _footer; |
216 | | // for mow tables with cluster key, the sort key is the cluster keys not unique keys |
217 | | // for other tables, the sort key is the keys |
218 | | size_t _num_sort_key_columns; |
219 | | size_t _num_short_key_columns; |
220 | | |
221 | | std::unique_ptr<ShortKeyIndexBuilder> _short_key_index_builder; |
222 | | std::unique_ptr<PrimaryKeyIndexBuilder> _primary_key_index_builder; |
223 | | std::vector<std::unique_ptr<ColumnWriter>> _column_writers; |
224 | | std::unique_ptr<MemTracker> _mem_tracker; |
225 | | |
226 | | std::unique_ptr<OlapBlockDataConvertor> _olap_data_convertor; |
227 | | // used for building short key index or primary key index during vectorized write. |
228 | | std::vector<const KeyCoder*> _key_coders; |
229 | | // for mow table with cluster keys, this is primary keys |
230 | | std::vector<const KeyCoder*> _primary_key_coders; |
231 | | const KeyCoder* _seq_coder = nullptr; |
232 | | const KeyCoder* _rowid_coder = nullptr; |
233 | | std::vector<uint16_t> _key_index_size; |
234 | | size_t _short_key_row_pos = 0; |
235 | | |
236 | | // _num_rows_written means row count already written in this current column group |
237 | | uint32_t _num_rows_written = 0; |
238 | | |
239 | | /** for partial update stats **/ |
240 | | int64_t _num_rows_updated = 0; |
241 | | int64_t _num_rows_new_added = 0; |
242 | | int64_t _num_rows_deleted = 0; |
243 | | // number of rows filtered in strict mode partial update |
244 | | int64_t _num_rows_filtered = 0; |
245 | | |
246 | | // _row_count means total row count of this segment |
247 | | // In vertical compaction row count is recorded when key columns group finish |
248 | | // and _num_rows_written will be updated in value column group |
249 | | uint32_t _row_count = 0; |
250 | | |
251 | | bool _is_first_row = true; |
252 | | faststring _min_key; |
253 | | faststring _max_key; |
254 | | |
255 | | std::shared_ptr<MowContext> _mow_context; |
256 | | // group every rowset-segment row id to speed up reader |
257 | | std::map<RowsetId, RowsetSharedPtr> _rsid_to_rowset; |
258 | | |
259 | | std::vector<RowsInBlock> _batched_blocks; |
260 | | |
261 | | BlockAggregator _block_aggregator; |
262 | | }; |
263 | | |
264 | | } // namespace segment_v2 |
265 | | } // namespace doris |