be/src/storage/segment/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 <butil/macros.h> |
21 | | #include <gen_cpp/olap_file.pb.h> |
22 | | #include <gen_cpp/segment_v2.pb.h> |
23 | | #include <stddef.h> |
24 | | |
25 | | #include <cstdint> |
26 | | #include <functional> |
27 | | #include <map> |
28 | | #include <memory> // unique_ptr |
29 | | #include <string> |
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/partial_update_info.h" |
37 | | #include "storage/segment/column_writer.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 | | // TODO(lingbin): Should be a conf that can be dynamically adjusted, or a member in the context |
49 | | const uint32_t MAX_SEGMENT_SIZE = static_cast<uint32_t>(OLAP_MAX_COLUMN_SEGMENT_FILE_SIZE * |
50 | | OLAP_COLUMN_FILE_SEGMENT_SIZE_SCALE); |
51 | | class DataDir; |
52 | | class MemTracker; |
53 | | class ShortKeyIndexBuilder; |
54 | | class PrimaryKeyIndexBuilder; |
55 | | class KeyCoder; |
56 | | struct RowsetWriterContext; |
57 | | |
58 | | namespace io { |
59 | | class FileWriter; |
60 | | } // namespace io |
61 | | |
62 | | namespace segment_v2 { |
63 | | |
64 | | extern const char* k_segment_magic; |
65 | | extern const uint32_t k_segment_magic_length; |
66 | | |
67 | | class VariantStatsCaculator; |
68 | | |
69 | | struct SegmentWriterOptions { |
70 | | uint32_t num_rows_per_block = 1024; |
71 | | uint32_t max_rows_per_segment = UINT32_MAX; |
72 | | bool enable_unique_key_merge_on_write = false; |
73 | | CompressionTypePB compression_type = UNKNOWN_COMPRESSION; |
74 | | |
75 | | RowsetWriterContext* rowset_ctx = nullptr; |
76 | | DataWriteType write_type = DataWriteType::TYPE_DEFAULT; |
77 | | }; |
78 | | |
79 | | using TabletSharedPtr = std::shared_ptr<Tablet>; |
80 | | |
81 | | class SegmentWriter { |
82 | | public: |
83 | | explicit SegmentWriter(io::FileWriter* file_writer, uint32_t segment_id, |
84 | | TabletSchemaSPtr tablet_schema, BaseTabletSPtr tablet, DataDir* data_dir, |
85 | | const SegmentWriterOptions& opts, IndexFileWriter* inverted_file_writer); |
86 | | virtual ~SegmentWriter(); |
87 | | |
88 | | virtual Status init(); |
89 | | |
90 | | // for vertical compaction |
91 | | virtual Status init(const std::vector<uint32_t>& col_ids, bool has_key); |
92 | | |
93 | | virtual Status append_block(const Block* block, size_t row_pos, size_t num_rows); |
94 | | |
95 | | int64_t max_row_to_add(size_t row_avg_size_in_bytes); |
96 | | |
97 | | uint64_t estimate_segment_size(); |
98 | | |
99 | 3.24k | uint32_t num_rows_written() const { return _num_rows_written; } |
100 | | |
101 | 3.29k | uint32_t row_count() const { return _row_count; } |
102 | | |
103 | | Status finalize(uint64_t* segment_file_size, uint64_t* index_size); |
104 | | |
105 | 1.23k | uint32_t get_segment_id() const { return _segment_id; } |
106 | | |
107 | | Status finalize_columns_data(); |
108 | | Status finalize_columns_index(uint64_t* index_size); |
109 | | Status finalize_footer(uint64_t* segment_file_size); |
110 | | |
111 | | void init_column_meta(ColumnMetaPB* meta, uint32_t column_id, const TabletColumn& column, |
112 | | const ColumnWriterOptions& opts); |
113 | | Slice min_encoded_key(); |
114 | | Slice max_encoded_key(); |
115 | | |
116 | 0 | bool is_unique_key() { return _tablet_schema->keys_type() == UNIQUE_KEYS; } |
117 | | |
118 | | void clear(); |
119 | | |
120 | 1.23k | Status close_inverted_index(int64_t* inverted_index_file_size) { |
121 | | // no inverted index |
122 | 1.23k | if (_index_file_writer == nullptr) { |
123 | 999 | *inverted_index_file_size = 0; |
124 | 999 | return Status::OK(); |
125 | 999 | } |
126 | 235 | RETURN_IF_ERROR(_index_file_writer->begin_close()); |
127 | 235 | *inverted_index_file_size = _index_file_writer->get_index_file_total_size(); |
128 | 235 | return Status::OK(); |
129 | 235 | } |
130 | | |
131 | 0 | uint64_t primary_keys_size() const { return _primary_keys_size; } |
132 | | |
133 | | private: |
134 | | friend class TestSegmentWriter; |
135 | | DISALLOW_COPY_AND_ASSIGN(SegmentWriter); |
136 | | Status _create_column_writer(uint32_t cid, const TabletColumn& column, |
137 | | const TabletSchemaSPtr& schema); |
138 | | Status _create_writers(const TabletSchemaSPtr& tablet_schema, |
139 | | const std::vector<uint32_t>& col_ids); |
140 | | Status _write_data(); |
141 | | Status _write_ordinal_index(); |
142 | | Status _write_zone_map(); |
143 | | Status _write_inverted_index(); |
144 | | Status _write_ann_index(); |
145 | | Status _write_bloom_filter_index(); |
146 | | Status _write_short_key_index(); |
147 | | Status _write_primary_key_index(); |
148 | | Status _write_footer(); |
149 | | Status _write_raw_data(const std::vector<Slice>& slices); |
150 | | void set_min_max_key(const Slice& key); |
151 | | void set_min_key(const Slice& key); |
152 | | void set_max_key(const Slice& key); |
153 | | Status _generate_primary_key_index( |
154 | | const std::vector<IOlapColumnDataAccessor*>& primary_key_columns, |
155 | | IOlapColumnDataAccessor* seq_column, size_t num_rows, bool need_sort); |
156 | | Status _generate_short_key_index(std::vector<IOlapColumnDataAccessor*>& key_columns, |
157 | | size_t num_rows, const std::vector<size_t>& short_key_pos); |
158 | 279k | bool _is_mow() { |
159 | 279k | return _tablet_schema->keys_type() == UNIQUE_KEYS && _opts.enable_unique_key_merge_on_write; |
160 | 279k | } |
161 | 137k | bool _is_mow_with_cluster_key() { |
162 | 137k | return _is_mow() && !_tablet_schema->cluster_key_uids().empty(); |
163 | 137k | } |
164 | | |
165 | | Status build_key_index(std::vector<IOlapColumnDataAccessor*>& key_columns, |
166 | | IOlapColumnDataAccessor* seq_column, size_t num_rows); |
167 | | |
168 | | protected: |
169 | | uint32_t _segment_id; |
170 | | TabletSchemaSPtr _tablet_schema; |
171 | | BaseTabletSPtr _tablet; |
172 | | DataDir* _data_dir = nullptr; |
173 | | SegmentWriterOptions _opts; |
174 | | |
175 | | // Not owned. owned by RowsetWriter or SegmentFlusher |
176 | | io::FileWriter* _file_writer = nullptr; |
177 | | // Not owned. owned by RowsetWriter or SegmentFlusher |
178 | | IndexFileWriter* _index_file_writer = nullptr; |
179 | | |
180 | | SegmentFooterPB _footer; |
181 | | size_t _num_short_key_columns; |
182 | | |
183 | | std::unique_ptr<ShortKeyIndexBuilder> _short_key_index_builder; |
184 | | std::unique_ptr<PrimaryKeyIndexBuilder> _primary_key_index_builder; |
185 | | std::vector<std::unique_ptr<ColumnWriter>> _column_writers; |
186 | | std::unique_ptr<MemTracker> _mem_tracker; |
187 | | |
188 | | std::unique_ptr<OlapBlockDataConvertor> _olap_data_convertor; |
189 | | // used for building short key index or primary key index during vectorized write. |
190 | | // NOTE: must stay declared after _tablet_schema and _opts, the constructor |
191 | | // init list reads both through _is_mow(). |
192 | | RowKeyEncoder _key_encoder; |
193 | | size_t _short_key_row_pos = 0; |
194 | | |
195 | | std::vector<uint32_t> _column_ids; |
196 | | bool _has_key = true; |
197 | | // _num_rows_written means row count already written in this current column group |
198 | | uint32_t _num_rows_written = 0; |
199 | | |
200 | | // _row_count means total row count of this segment |
201 | | // In vertical compaction row count is recorded when key columns group finish |
202 | | // and _num_rows_written will be updated in value column group |
203 | | uint32_t _row_count = 0; |
204 | | |
205 | | bool _is_first_row = true; |
206 | | faststring _min_key; |
207 | | faststring _max_key; |
208 | | |
209 | | // group every rowset-segment row id to speed up reader |
210 | | std::vector<std::string> _primary_keys; |
211 | | uint64_t _primary_keys_size = 0; |
212 | | // variant statistics calculator for efficient stats collection |
213 | | std::unique_ptr<VariantStatsCaculator> _variant_stats_calculator; |
214 | | }; |
215 | | |
216 | | } // namespace segment_v2 |
217 | | } // namespace doris |