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 <map> |
26 | | #include <memory> // unique_ptr |
27 | | #include <string> |
28 | | #include <unordered_set> |
29 | | #include <utility> |
30 | | #include <vector> |
31 | | |
32 | | #include "common/status.h" // Status |
33 | | #include "storage/index/index_file_writer.h" |
34 | | #include "storage/key/row_key_encoder.h" |
35 | | #include "storage/olap_define.h" |
36 | | #include "storage/segment/column_writer.h" |
37 | | #include "storage/segment/segment_index_file_cache_loader.h" |
38 | | #include "storage/tablet/tablet.h" |
39 | | #include "storage/tablet/tablet_schema.h" |
40 | | #include "util/faststring.h" |
41 | | #include "util/slice.h" |
42 | | |
43 | | namespace doris { |
44 | | class Block; |
45 | | class IOlapColumnDataAccessor; |
46 | | class OlapBlockDataConvertor; |
47 | | |
48 | | class DataDir; |
49 | | class MemTracker; |
50 | | class ShortKeyIndexBuilder; |
51 | | class PrimaryKeyIndexBuilder; |
52 | | class KeyCoder; |
53 | | struct RowsetWriterContext; |
54 | | |
55 | | namespace io { |
56 | | class FileWriter; |
57 | | class FileSystem; |
58 | | } // namespace io |
59 | | namespace segment_v2 { |
60 | | class IndexFileWriter; |
61 | | |
62 | | struct VerticalSegmentWriterOptions { |
63 | | uint32_t num_rows_per_block = 1024; |
64 | | bool enable_unique_key_merge_on_write = false; |
65 | | CompressionTypePB compression_type = UNKNOWN_COMPRESSION; |
66 | | |
67 | | RowsetWriterContext* rowset_ctx = nullptr; |
68 | | DataWriteType write_type = DataWriteType::TYPE_DEFAULT; |
69 | | }; |
70 | | |
71 | | class DerivedColumnGenerator; |
72 | | // Matches block_transform.h: at most one derived column (the row-store column) |
73 | | // for each flush, held as a {cid, generator} pair; null generator means none. |
74 | | using DerivedColumn = std::pair<uint32_t, std::shared_ptr<const DerivedColumnGenerator>>; |
75 | | |
76 | | struct RowsInBlock { |
77 | | const Block* block; |
78 | | size_t row_pos; |
79 | | size_t num_rows; |
80 | | }; |
81 | | |
82 | | class VerticalSegmentWriter { |
83 | | public: |
84 | | explicit VerticalSegmentWriter(io::FileWriter* file_writer, uint32_t segment_id, |
85 | | TabletSchemaSPtr tablet_schema, BaseTabletSPtr tablet, |
86 | | DataDir* data_dir, const VerticalSegmentWriterOptions& opts, |
87 | | IndexFileWriter* index_file_writer); |
88 | | ~VerticalSegmentWriter(); |
89 | | |
90 | | VerticalSegmentWriter(const VerticalSegmentWriter&) = delete; |
91 | | const VerticalSegmentWriter& operator=(const VerticalSegmentWriter&) = delete; |
92 | | |
93 | | Status init(); |
94 | | |
95 | | // Add one block to batch, memory is owned by the caller. |
96 | | // The batched blocks will be flushed in write_batch. |
97 | | // Once write_batch is called, no more blocks shoud be added. |
98 | | Status batch_block(const Block* block, size_t row_pos, size_t num_rows); |
99 | | Status write_batch(); |
100 | | |
101 | 144 | void set_derived_column(DerivedColumn derived_column) { |
102 | 144 | _derived_column = std::move(derived_column); |
103 | 144 | } |
104 | | |
105 | 0 | [[nodiscard]] std::string data_dir_path() const { |
106 | 0 | return _data_dir == nullptr ? "" : _data_dir->path(); |
107 | 0 | } |
108 | | |
109 | 144 | [[nodiscard]] uint32_t num_rows_written() const { return _num_rows_written; } |
110 | | |
111 | 0 | [[nodiscard]] uint32_t row_count() const { return _row_count; } |
112 | 144 | [[nodiscard]] uint32_t segment_id() const { return _segment_id; } |
113 | | |
114 | | Status finalize(uint64_t* segment_file_size, uint64_t* index_size, |
115 | | SegmentIndexFileCacheInfo* index_file_cache_info = nullptr); |
116 | | |
117 | | Status finalize_columns_index(uint64_t* index_size); |
118 | | Status finalize_footer(uint64_t* segment_file_size, |
119 | | SegmentIndexFileCacheInfo* index_file_cache_info = nullptr); |
120 | | |
121 | | Slice min_encoded_key(); |
122 | | Slice max_encoded_key(); |
123 | | |
124 | | void clear(); |
125 | | |
126 | 144 | Status close_inverted_index(int64_t* inverted_index_file_size) { |
127 | | // no inverted index |
128 | 144 | if (_index_file_writer == nullptr) { |
129 | 122 | *inverted_index_file_size = 0; |
130 | 122 | return Status::OK(); |
131 | 122 | } |
132 | 22 | RETURN_IF_ERROR(_index_file_writer->begin_close()); |
133 | 22 | *inverted_index_file_size = _index_file_writer->get_index_file_total_size(); |
134 | 22 | return Status::OK(); |
135 | 22 | } |
136 | | |
137 | | private: |
138 | | void _init_column_meta(ColumnMetaPB* meta, uint32_t column_id, const TabletColumn& column, |
139 | | const ColumnWriterOptions& opts); |
140 | | Status _create_column_writer(uint32_t cid, const TabletColumn& column, |
141 | | const TabletSchemaSPtr& schema); |
142 | | uint64_t _estimated_remaining_size(); |
143 | | Status _write_ordinal_index(); |
144 | | Status _write_zone_map(); |
145 | | Status _write_inverted_index(); |
146 | | Status _write_ann_index(); |
147 | | Status _write_bloom_filter_index(); |
148 | | Status _write_short_key_index(); |
149 | | Status _write_primary_key_index(); |
150 | | Status _write_footer(); |
151 | | Status _write_raw_data(const std::vector<Slice>& slices); |
152 | | void _set_min_max_key(const Slice& key); |
153 | | void _set_min_key(const Slice& key); |
154 | | void _set_max_key(const Slice& key); |
155 | | Status _append_generated_column(const DerivedColumnGenerator& generator, const Block& block, |
156 | | size_t row_pos, size_t num_rows, uint32_t cid); |
157 | | Status _generate_key_index(RowsInBlock& data, |
158 | | std::vector<IOlapColumnDataAccessor*>& key_columns, |
159 | | IOlapColumnDataAccessor* seq_column, |
160 | | std::map<uint32_t, IOlapColumnDataAccessor*>& cid_to_column); |
161 | | Status _generate_primary_key_index( |
162 | | const std::vector<IOlapColumnDataAccessor*>& primary_key_columns, |
163 | | IOlapColumnDataAccessor* seq_column, size_t num_rows, bool need_sort); |
164 | | Status _generate_short_key_index(std::vector<IOlapColumnDataAccessor*>& key_columns, |
165 | | size_t num_rows, const std::vector<size_t>& short_key_pos); |
166 | | Status _check_column_writer_disk_capacity(size_t cid); |
167 | | Status _finalize_column_writer_and_update_meta(size_t cid); |
168 | | |
169 | 2.80k | bool _is_mow() { |
170 | 2.80k | return _tablet_schema->keys_type() == UNIQUE_KEYS && _opts.enable_unique_key_merge_on_write; |
171 | 2.80k | } |
172 | 2.18k | bool _is_mow_with_cluster_key() { |
173 | 2.18k | return _is_mow() && !_tablet_schema->cluster_key_uids().empty(); |
174 | 2.18k | } |
175 | | |
176 | | private: |
177 | | uint32_t _segment_id; |
178 | | TabletSchemaSPtr _tablet_schema; |
179 | | BaseTabletSPtr _tablet; |
180 | | DataDir* _data_dir = nullptr; |
181 | | VerticalSegmentWriterOptions _opts; |
182 | | |
183 | | // Not owned. owned by RowsetWriter |
184 | | io::FileWriter* _file_writer = nullptr; |
185 | | // Not owned. owned by RowsetWriter or SegmentFlusher |
186 | | IndexFileWriter* _index_file_writer = nullptr; |
187 | | |
188 | | SegmentFooterPB _footer; |
189 | | SegmentIndexFileCacheInfo _index_file_cache_info; |
190 | | size_t _num_short_key_columns; |
191 | | |
192 | | std::unique_ptr<ShortKeyIndexBuilder> _short_key_index_builder; |
193 | | std::unique_ptr<PrimaryKeyIndexBuilder> _primary_key_index_builder; |
194 | | std::vector<std::unique_ptr<ColumnWriter>> _column_writers; |
195 | | std::unique_ptr<MemTracker> _mem_tracker; |
196 | | |
197 | | std::unique_ptr<OlapBlockDataConvertor> _olap_data_convertor; |
198 | | // used for building short key index or primary key index during vectorized write. |
199 | | // NOTE: must stay declared after _tablet_schema and _opts, the constructor |
200 | | // init list reads both through _is_mow(). |
201 | | RowKeyEncoder _key_encoder; |
202 | | size_t _short_key_row_pos = 0; |
203 | | |
204 | | // _num_rows_written means row count already written in this current column group |
205 | | uint32_t _num_rows_written = 0; |
206 | | |
207 | | // _row_count means total row count of this segment |
208 | | // In vertical compaction row count is recorded when key columns group finish |
209 | | // and _num_rows_written will be updated in value column group |
210 | | uint32_t _row_count = 0; |
211 | | |
212 | | bool _is_first_row = true; |
213 | | faststring _min_key; |
214 | | faststring _max_key; |
215 | | |
216 | | std::vector<RowsInBlock> _batched_blocks; |
217 | | |
218 | | // the derived column the transform chain hands off to this writer's bounded pump |
219 | | DerivedColumn _derived_column; |
220 | | }; |
221 | | |
222 | | } // namespace segment_v2 |
223 | | } // namespace doris |