be/src/storage/tablet/tablet_meta.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/AgentService_types.h> |
21 | | #include <gen_cpp/FrontendService_types.h> |
22 | | #include <gen_cpp/olap_file.pb.h> |
23 | | #include <stdint.h> |
24 | | |
25 | | #include <atomic> |
26 | | #include <cstddef> |
27 | | #include <limits> |
28 | | #include <map> |
29 | | #include <memory> |
30 | | #include <mutex> |
31 | | #include <optional> |
32 | | #include <ostream> |
33 | | #include <roaring/roaring.hh> |
34 | | #include <shared_mutex> |
35 | | #include <string> |
36 | | #include <tuple> |
37 | | #include <unordered_map> |
38 | | #include <utility> |
39 | | #include <vector> |
40 | | |
41 | | #include "common/logging.h" |
42 | | #include "common/status.h" |
43 | | #include "io/fs/file_system.h" |
44 | | #include "runtime/memory/lru_cache_policy.h" |
45 | | #include "storage/binlog_config.h" |
46 | | #include "storage/metadata_adder.h" |
47 | | #include "storage/olap_common.h" |
48 | | #include "storage/rowset/rowset_meta.h" |
49 | | #include "storage/tablet/tablet_schema.h" |
50 | | #include "util/lru_cache.h" |
51 | | #include "util/uid_util.h" |
52 | | |
53 | | namespace json2pb { |
54 | | struct Pb2JsonOptions; |
55 | | } // namespace json2pb |
56 | | |
57 | | namespace doris { |
58 | | class TColumn; |
59 | | |
60 | | // Lifecycle states that a Tablet can be in. Legal state transitions for a |
61 | | // Tablet object: |
62 | | // |
63 | | // NOTREADY -> RUNNING -> TOMBSTONED -> STOPPED -> SHUTDOWN |
64 | | // | | | ^^^ |
65 | | // | | +----------++| |
66 | | // | +------------------------+| |
67 | | // +-------------------------------------+ |
68 | | |
69 | | enum TabletState { |
70 | | // Tablet is under alter table, rollup, clone |
71 | | TABLET_NOTREADY, |
72 | | |
73 | | TABLET_RUNNING, |
74 | | |
75 | | // Tablet integrity has been violated, such as missing versions. |
76 | | // In this state, tablet will not accept any incoming request. |
77 | | // Report this state to FE, scheduling BE to drop tablet. |
78 | | TABLET_TOMBSTONED, |
79 | | |
80 | | // Tablet is shutting down, files in disk still remained. |
81 | | TABLET_STOPPED, |
82 | | |
83 | | // Files have been removed, tablet has been shutdown completely. |
84 | | TABLET_SHUTDOWN |
85 | | }; |
86 | | |
87 | | class DataDir; |
88 | | class TabletMeta; |
89 | | class DeleteBitmap; |
90 | | class TBinlogConfig; |
91 | | |
92 | | // Class encapsulates meta of tablet. |
93 | | // The concurrency control is handled in Tablet Class, not in this class. |
94 | | class TabletMeta : public MetadataAdder<TabletMeta> { |
95 | | public: |
96 | | static TabletMetaSharedPtr create( |
97 | | const TCreateTabletReq& request, const TabletUid& tablet_uid, uint64_t shard_id, |
98 | | uint32_t next_unique_id, |
99 | | const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id); |
100 | | |
101 | | TabletMeta(); |
102 | | ~TabletMeta() override; |
103 | | TabletMeta(int64_t table_id, int64_t partition_id, int64_t tablet_id, int64_t replica_id, |
104 | | int32_t schema_hash, int32_t shard_id, const TTabletSchema& tablet_schema, |
105 | | uint32_t next_unique_id, |
106 | | const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id, |
107 | | TabletUid tablet_uid, TTabletType::type tabletType, |
108 | | TCompressionType::type compression_type, int64_t storage_policy_id = 0, |
109 | | bool enable_unique_key_merge_on_write = false, |
110 | | std::optional<TBinlogConfig> binlog_config = {}, |
111 | | std::string compaction_policy = "size_based", |
112 | | int64_t time_series_compaction_goal_size_mbytes = 1024, |
113 | | int64_t time_series_compaction_file_count_threshold = 1000, |
114 | | int64_t time_series_compaction_time_threshold_seconds = 3600, |
115 | | int64_t time_series_compaction_empty_rowsets_threshold = 5, |
116 | | int64_t time_series_compaction_level_threshold = 1, |
117 | | TInvertedIndexFileStorageFormat::type inverted_index_file_storage_format = |
118 | | TInvertedIndexFileStorageFormat::V2, |
119 | | TEncryptionAlgorithm::type tde_algorithm = TEncryptionAlgorithm::PLAINTEXT, |
120 | | TStorageFormat::type storage_format = TStorageFormat::V2, |
121 | | int32_t vertical_compaction_num_columns_per_group = 5, |
122 | | const TTabletSchema* row_binlog_schema = nullptr); |
123 | | // If need add a filed in TableMeta, filed init copy in copy construct function |
124 | | TabletMeta(const TabletMeta& tablet_meta); |
125 | | TabletMeta(TabletMeta&& tablet_meta) = delete; |
126 | | |
127 | | // UT |
128 | | #ifdef BE_TEST |
129 | | TabletMeta(TabletSchemaSPtr tablet_schema) : _schema(tablet_schema) {} |
130 | | #endif |
131 | | |
132 | | // Function create_from_file is used to be compatible with previous tablet_meta. |
133 | | // Previous tablet_meta is a physical file in tablet dir, which is not stored in rocksdb. |
134 | | Status create_from_file(const std::string& file_path); |
135 | | // Used to create tablet meta from memory buffer. |
136 | | Status create_from_buffer(const uint8_t* buffer, size_t buffer_size); |
137 | | static Status load_from_file(const std::string& file_path, TabletMetaPB* tablet_meta_pb); |
138 | | Status save(const std::string& file_path); |
139 | | Status save_as_json(const std::string& file_path); |
140 | | static Status save(const std::string& file_path, const TabletMetaPB& tablet_meta_pb); |
141 | | static std::string construct_header_file_path(const std::string& schema_hash_path, |
142 | | int64_t tablet_id); |
143 | | Status save_meta(DataDir* data_dir); |
144 | | |
145 | | void serialize(std::string* meta_binary); |
146 | | Status deserialize(std::string_view meta_binary); |
147 | | void init_from_pb(const TabletMetaPB& tablet_meta_pb); |
148 | | |
149 | | void to_meta_pb(TabletMetaPB* tablet_meta_pb, bool cloud_get_rowset_meta); |
150 | | void to_json(std::string* json_string, json2pb::Pb2JsonOptions& options); |
151 | 302k | size_t tablet_columns_num() const { return _schema->num_columns(); } |
152 | | |
153 | 0 | TabletTypePB tablet_type() const { return _tablet_type; } |
154 | | TabletUid tablet_uid() const; |
155 | 207 | void set_tablet_uid(TabletUid uid) { _tablet_uid = uid; } |
156 | | int64_t table_id() const; |
157 | | int64_t index_id() const; |
158 | | int64_t partition_id() const; |
159 | | int64_t tablet_id() const; |
160 | | int64_t replica_id() const; |
161 | 0 | void set_replica_id(int64_t replica_id) { _replica_id = replica_id; } |
162 | | int32_t schema_hash() const; |
163 | | int32_t shard_id() const; |
164 | | void set_shard_id(int32_t shard_id); |
165 | | int64_t creation_time() const; |
166 | | void set_creation_time(int64_t creation_time); |
167 | | int64_t cumulative_layer_point() const; |
168 | | void set_cumulative_layer_point(int64_t new_point); |
169 | | |
170 | | size_t num_rows() const; |
171 | | // Disk space occupied by tablet, contain local and remote. |
172 | | size_t tablet_footprint() const; |
173 | | // Local disk space occupied by tablet. |
174 | | size_t tablet_local_size() const; |
175 | | // Remote disk space occupied by tablet. |
176 | | size_t tablet_remote_size() const; |
177 | | size_t binlog_size() const; |
178 | | |
179 | | size_t tablet_local_index_size() const; |
180 | | size_t tablet_local_segment_size() const; |
181 | | size_t tablet_remote_index_size() const; |
182 | | size_t tablet_remote_segment_size() const; |
183 | | |
184 | | size_t version_count() const; |
185 | | size_t stale_version_count() const; |
186 | | size_t binlog_file_num() const; |
187 | | size_t version_count_cross_with_range(const Version& range) const; |
188 | | Version max_version() const; |
189 | | |
190 | | TabletState tablet_state() const; |
191 | | void set_tablet_state(TabletState state); |
192 | | |
193 | | bool in_restore_mode() const; |
194 | | void set_in_restore_mode(bool in_restore_mode); |
195 | | |
196 | | const TabletSchemaSPtr& tablet_schema() const; |
197 | | |
198 | | TabletSchema* mutable_tablet_schema(); |
199 | | |
200 | | const RowsetMetaMapContainer& all_rs_metas() const; |
201 | | RowsetMetaMapContainer& all_mutable_rs_metas(); |
202 | | Status add_rs_meta(const RowsetMetaSharedPtr& rs_meta); |
203 | | void delete_rs_meta_by_version(const Version& version, |
204 | | std::vector<RowsetMetaSharedPtr>* deleted_rs_metas); |
205 | | // If same_version is true, the rowset in "to_delete" will not be added |
206 | | // to _stale_rs_meta, but to be deleted from rs_meta directly. |
207 | | void modify_rs_metas(const std::vector<RowsetMetaSharedPtr>& to_add, |
208 | | const std::vector<RowsetMetaSharedPtr>& to_delete, |
209 | | bool same_version = false); |
210 | | void revise_rs_metas(std::vector<RowsetMetaSharedPtr>&& rs_metas); |
211 | | void revise_delete_bitmap_unlocked(const DeleteBitmap& delete_bitmap); |
212 | | |
213 | | const RowsetMetaMapContainer& all_stale_rs_metas() const; |
214 | | RowsetMetaSharedPtr acquire_rs_meta_by_version(const Version& version) const; |
215 | | void delete_stale_rs_meta_by_version(const Version& version); |
216 | | RowsetMetaSharedPtr acquire_stale_rs_meta_by_version(const Version& version) const; |
217 | | |
218 | | Status set_partition_id(int64_t partition_id); |
219 | | |
220 | 24.7k | RowsetTypePB preferred_rowset_type() const { return _preferred_rowset_type; } |
221 | | |
222 | 6.64k | void set_preferred_rowset_type(RowsetTypePB preferred_rowset_type) { |
223 | 6.64k | _preferred_rowset_type = preferred_rowset_type; |
224 | 6.64k | } |
225 | | |
226 | | // used for after tablet cloned to clear stale rowset |
227 | | void clear_stale_rowset(); |
228 | | |
229 | | void clear_rowsets(); |
230 | | |
231 | | // MUST hold EXCLUSIVE `_meta_lock` in belonged Tablet |
232 | | // `to_add` MUST NOT have overlapped version with `_rs_metas` in tablet meta. |
233 | | void add_rowsets_unchecked(const std::vector<RowsetSharedPtr>& to_add); |
234 | | |
235 | | bool all_beta() const; |
236 | | |
237 | 2.07M | int64_t storage_policy_id() const { return _storage_policy_id; } |
238 | | |
239 | 4 | void set_storage_policy_id(int64_t id) { |
240 | 4 | VLOG_NOTICE << "set tablet_id : " << _table_id << " storage policy from " |
241 | 4 | << _storage_policy_id << " to " << id; |
242 | 4 | _storage_policy_id = id; |
243 | 4 | } |
244 | | |
245 | 3.12M | UniqueId cooldown_meta_id() const { return _cooldown_meta_id; } |
246 | 5 | void set_cooldown_meta_id(UniqueId uid) { _cooldown_meta_id = uid; } |
247 | | |
248 | | static void init_column_from_tcolumn(uint32_t unique_id, const TColumn& tcolumn, |
249 | | ColumnPB* column); |
250 | | |
251 | | struct SchemaCreateOptions { |
252 | | const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id; |
253 | | TCompressionType::type compression_type; |
254 | | TInvertedIndexFileStorageFormat::type inverted_index_file_storage_format; |
255 | | uint32_t next_unique_id; |
256 | | }; |
257 | | |
258 | | static void init_schema_from_thrift(const TTabletSchema& tablet_schema, |
259 | | const SchemaCreateOptions& schema_create_options, |
260 | | TabletSchemaPB* tablet_schema_pb); |
261 | | |
262 | 3.15M | DeleteBitmapPtr delete_bitmap_ptr() { return _delete_bitmap; } |
263 | 814k | DeleteBitmap& delete_bitmap() { return *_delete_bitmap; } |
264 | | |
265 | 0 | DeleteBitmapPtr binlog_delvec_ptr() { return _binlog_delvec; } |
266 | 4.09k | DeleteBitmap& binlog_delvec() { return *_binlog_delvec; } |
267 | | |
268 | | void remove_rowset_delete_bitmap(const RowsetId& rowset_id, const Version& version); |
269 | | |
270 | 6.33M | bool enable_unique_key_merge_on_write() const { return _enable_unique_key_merge_on_write; } |
271 | | #ifdef BE_TEST |
272 | | void set_enable_unique_key_merge_on_write(bool value) { |
273 | | _enable_unique_key_merge_on_write = value; |
274 | | } |
275 | | #endif |
276 | | // TODO(Drogon): thread safety |
277 | 1.25M | const BinlogConfig& binlog_config() const { return _binlog_config; } |
278 | 0 | void set_binlog_config(BinlogConfig binlog_config) { |
279 | 0 | _binlog_config = std::move(binlog_config); |
280 | 0 | } |
281 | | |
282 | 2 | const TabletSchemaSPtr& row_binlog_schema() const { return _row_binlog_schema; } |
283 | 2 | int32_t row_binlog_schema_hash() const { return _row_binlog_schema_hash; } |
284 | | const RowsetMetaMapContainer& all_row_binlog_rs_metas() const; |
285 | | Status add_row_binlog_rs_meta(const RowsetMetaSharedPtr& rs_meta); |
286 | | |
287 | 13 | void set_compaction_policy(std::string compaction_policy) { |
288 | 13 | _compaction_policy = compaction_policy; |
289 | 13 | } |
290 | 349M | std::string compaction_policy() const { return _compaction_policy; } |
291 | 12 | void set_time_series_compaction_goal_size_mbytes(int64_t goal_size_mbytes) { |
292 | 12 | _time_series_compaction_goal_size_mbytes = goal_size_mbytes; |
293 | 12 | } |
294 | 78.9k | int64_t time_series_compaction_goal_size_mbytes() const { |
295 | 78.9k | return _time_series_compaction_goal_size_mbytes; |
296 | 78.9k | } |
297 | 12 | void set_time_series_compaction_file_count_threshold(int64_t file_count_threshold) { |
298 | 12 | _time_series_compaction_file_count_threshold = file_count_threshold; |
299 | 12 | } |
300 | 78.9k | int64_t time_series_compaction_file_count_threshold() const { |
301 | 78.9k | return _time_series_compaction_file_count_threshold; |
302 | 78.9k | } |
303 | 13 | void set_time_series_compaction_time_threshold_seconds(int64_t time_threshold) { |
304 | 13 | _time_series_compaction_time_threshold_seconds = time_threshold; |
305 | 13 | } |
306 | 78.9k | int64_t time_series_compaction_time_threshold_seconds() const { |
307 | 78.9k | return _time_series_compaction_time_threshold_seconds; |
308 | 78.9k | } |
309 | 0 | void set_time_series_compaction_empty_rowsets_threshold(int64_t empty_rowsets_threshold) { |
310 | 0 | _time_series_compaction_empty_rowsets_threshold = empty_rowsets_threshold; |
311 | 0 | } |
312 | 78.9k | int64_t time_series_compaction_empty_rowsets_threshold() const { |
313 | 78.9k | return _time_series_compaction_empty_rowsets_threshold; |
314 | 78.9k | } |
315 | 0 | void set_time_series_compaction_level_threshold(int64_t level_threshold) { |
316 | 0 | _time_series_compaction_level_threshold = level_threshold; |
317 | 0 | } |
318 | 79.0k | int64_t time_series_compaction_level_threshold() const { |
319 | 79.0k | return _time_series_compaction_level_threshold; |
320 | 79.0k | } |
321 | | |
322 | 0 | void set_vertical_compaction_num_columns_per_group(int32_t num) { |
323 | 0 | _vertical_compaction_num_columns_per_group = num; |
324 | 0 | } |
325 | 86.4k | int32_t vertical_compaction_num_columns_per_group() const { |
326 | 86.4k | return _vertical_compaction_num_columns_per_group; |
327 | 86.4k | } |
328 | | |
329 | 1.62M | int64_t ttl_seconds() const { |
330 | 1.62M | std::shared_lock rlock(_meta_lock); |
331 | 1.62M | return _ttl_seconds; |
332 | 1.62M | } |
333 | | |
334 | | void set_ttl_seconds(int64_t ttl_seconds) { |
335 | | std::lock_guard wlock(_meta_lock); |
336 | | _ttl_seconds = ttl_seconds; |
337 | | } |
338 | | |
339 | 16.2k | int64_t avg_rs_meta_serialize_size() const { return _avg_rs_meta_serialize_size; } |
340 | | |
341 | 444k | EncryptionAlgorithmPB encryption_algorithm() const { return _encryption_algorithm; } |
342 | | |
343 | | private: |
344 | | Status _save_meta(DataDir* data_dir); |
345 | | void _check_mow_rowset_cache_version_size(size_t rowset_cache_version_size); |
346 | | |
347 | | // _del_predicates is ignored to compare. |
348 | | friend bool operator==(const TabletMeta& a, const TabletMeta& b); |
349 | | friend bool operator!=(const TabletMeta& a, const TabletMeta& b); |
350 | | |
351 | | private: |
352 | | int64_t _table_id = 0; |
353 | | int64_t _index_id = 0; |
354 | | int64_t _partition_id = 0; |
355 | | int64_t _tablet_id = 0; |
356 | | int64_t _replica_id = 0; |
357 | | int32_t _schema_hash = 0; |
358 | | int32_t _shard_id = 0; |
359 | | int64_t _creation_time = 0; |
360 | | int64_t _cumulative_layer_point = 0; |
361 | | TabletUid _tablet_uid; |
362 | | TabletTypePB _tablet_type = TabletTypePB::TABLET_TYPE_DISK; |
363 | | |
364 | | TabletState _tablet_state = TABLET_NOTREADY; |
365 | | // the reference of _schema may use in tablet, so here need keep |
366 | | // the lifetime of tablemeta and _schema is same with tablet |
367 | | TabletSchemaSPtr _schema; |
368 | | Cache::Handle* _handle = nullptr; |
369 | | |
370 | | RowsetMetaMapContainer _rs_metas; |
371 | | // This variable _stale_rs_metas is used to record these rowsets‘ meta which are be compacted. |
372 | | // These stale rowsets meta are been removed when rowsets' pathVersion is expired, |
373 | | // this policy is judged and computed by TimestampedVersionTracker. |
374 | | RowsetMetaMapContainer _stale_rs_metas; |
375 | | bool _in_restore_mode = false; |
376 | | RowsetTypePB _preferred_rowset_type = BETA_ROWSET; |
377 | | |
378 | | // meta for cooldown |
379 | | int64_t _storage_policy_id = 0; // <= 0 means no storage policy |
380 | | UniqueId _cooldown_meta_id; |
381 | | |
382 | | // For unique key data model, the feature Merge-on-Write will leverage a primary |
383 | | // key index and a delete-bitmap to mark duplicate keys as deleted in load stage, |
384 | | // which can avoid the merging cost in read stage, and accelerate the aggregation |
385 | | // query performance significantly. |
386 | | bool _enable_unique_key_merge_on_write = false; |
387 | | std::shared_ptr<DeleteBitmap> _delete_bitmap; |
388 | | std::shared_ptr<DeleteBitmap> _binlog_delvec; |
389 | | |
390 | | int32_t _row_binlog_schema_hash = 0; |
391 | | TabletSchemaSPtr _row_binlog_schema; |
392 | | RowsetMetaMapContainer _row_binlog_rs_metas; |
393 | | |
394 | | // binlog config |
395 | | BinlogConfig _binlog_config {}; |
396 | | |
397 | | // meta for compaction |
398 | | std::string _compaction_policy; |
399 | | int64_t _time_series_compaction_goal_size_mbytes = 0; |
400 | | int64_t _time_series_compaction_file_count_threshold = 0; |
401 | | int64_t _time_series_compaction_time_threshold_seconds = 0; |
402 | | int64_t _time_series_compaction_empty_rowsets_threshold = 0; |
403 | | int64_t _time_series_compaction_level_threshold = 0; |
404 | | int32_t _vertical_compaction_num_columns_per_group = 5; |
405 | | |
406 | | int64_t _avg_rs_meta_serialize_size = 0; |
407 | | |
408 | | // cloud |
409 | | int64_t _ttl_seconds = 0; |
410 | | |
411 | | EncryptionAlgorithmPB _encryption_algorithm = PLAINTEXT; |
412 | | |
413 | | // Persisted storage format for this tablet (e.g. V2, V3). Used to derive |
414 | | // schema-level defaults such as external ColumnMeta usage. |
415 | | TStorageFormat::type _storage_format = TStorageFormat::V2; |
416 | | |
417 | | mutable std::shared_mutex _meta_lock; |
418 | | }; |
419 | | |
420 | | class DeleteBitmapAggCache : public LRUCachePolicy { |
421 | | public: |
422 | | DeleteBitmapAggCache(size_t capacity); |
423 | | |
424 | | static DeleteBitmapAggCache* instance(); |
425 | | |
426 | | static DeleteBitmapAggCache* create_instance(size_t capacity); |
427 | | |
428 | | DeleteBitmap snapshot(int64_t tablet_id); |
429 | | |
430 | | class Value : public LRUCacheValueBase { |
431 | | public: |
432 | | roaring::Roaring bitmap; |
433 | | }; |
434 | | }; |
435 | | |
436 | | /** |
437 | | * Wraps multiple bitmaps for recording rows (row id) that are deleted or |
438 | | * overwritten. For now, it's only used when unique key merge-on-write property |
439 | | * enabled. |
440 | | * |
441 | | * RowsetId and SegmentId are for locating segment, Version here is a single |
442 | | * uint32_t means that at which "version" of the load causes the delete or |
443 | | * overwrite. |
444 | | * |
445 | | * The start and end version of a load is the same, it's ok and straightforward |
446 | | * to use a single uint32_t. |
447 | | * |
448 | | * e.g. |
449 | | * There is a key "key1" in rowset id 1, version [1,1], segment id 1, row id 1. |
450 | | * A new load also contains "key1", the rowset id 2, version [2,2], segment id 1 |
451 | | * the delete bitmap will be `{1,1,2} -> 1`, which means the "row id 1" in |
452 | | * "rowset id 1, segment id 1" is deleted/overitten by some loads at "version 2" |
453 | | */ |
454 | | class DeleteBitmap { |
455 | | public: |
456 | | mutable std::shared_mutex lock; |
457 | | using SegmentId = uint32_t; |
458 | | using Version = uint64_t; |
459 | | using BitmapKey = std::tuple<RowsetId, SegmentId, Version>; |
460 | | std::map<BitmapKey, roaring::Roaring> delete_bitmap; // Ordered map |
461 | | constexpr static inline uint32_t INVALID_SEGMENT_ID = std::numeric_limits<uint32_t>::max() - 1; |
462 | | constexpr static inline uint32_t ROWSET_SENTINEL_MARK = |
463 | | std::numeric_limits<uint32_t>::max() - 1; |
464 | | |
465 | | // When a delete bitmap is merged into tablet's delete bitmap, the version of entries in the delete bitmap |
466 | | // will be replaced to the correspoding correct version. So before we finally merge a delete bitmap into |
467 | | // tablet's delete bitmap we can use arbitary version number in BitmapKey. Here we define some version numbers |
468 | | // for specific usage during this periods to avoid conflicts |
469 | | constexpr static inline uint64_t TEMP_VERSION_COMMON = 0; |
470 | | |
471 | | /** |
472 | | * |
473 | | * @param tablet_id the tablet which this delete bitmap associates with |
474 | | */ |
475 | | DeleteBitmap(int64_t tablet_id); |
476 | | |
477 | | /** |
478 | | * Copy c-tor for making delete bitmap snapshot on read path |
479 | | */ |
480 | | DeleteBitmap(const DeleteBitmap& r); |
481 | | DeleteBitmap& operator=(const DeleteBitmap& r); |
482 | | /** |
483 | | * Move c-tor for making delete bitmap snapshot on read path |
484 | | */ |
485 | | DeleteBitmap(DeleteBitmap&& r) noexcept; |
486 | | DeleteBitmap& operator=(DeleteBitmap&& r) noexcept; |
487 | | |
488 | | static DeleteBitmap from_pb(const DeleteBitmapPB& pb, int64_t tablet_id); |
489 | | |
490 | | DeleteBitmapPB to_pb(); |
491 | | |
492 | | /** |
493 | | * Makes a snapshot of delete bitmap, read lock will be acquired in this |
494 | | * process |
495 | | */ |
496 | | DeleteBitmap snapshot() const; |
497 | | |
498 | | /** |
499 | | * Makes a snapshot of delete bitmap on given version, read lock will be |
500 | | * acquired temporary in this process |
501 | | */ |
502 | | DeleteBitmap snapshot(Version version) const; |
503 | | |
504 | | /** |
505 | | * Marks the specific row deleted |
506 | | */ |
507 | | void add(const BitmapKey& bmk, uint32_t row_id); |
508 | | |
509 | | /** |
510 | | * Clears the deletetion mark specific row |
511 | | * |
512 | | * @return non-zero if the associated delete bitmap does not exist |
513 | | */ |
514 | | int remove(const BitmapKey& bmk, uint32_t row_id); |
515 | | |
516 | | /** |
517 | | * Clears bitmaps in range [lower_key, upper_key) |
518 | | */ |
519 | | void remove(const BitmapKey& lower_key, const BitmapKey& upper_key); |
520 | | void remove(const std::vector<std::tuple<BitmapKey, BitmapKey>>& key_ranges); |
521 | | |
522 | | /** |
523 | | * Checks if the given row is marked deleted |
524 | | * |
525 | | * @return true if marked deleted |
526 | | */ |
527 | | bool contains(const BitmapKey& bmk, uint32_t row_id) const; |
528 | | |
529 | | /** |
530 | | * Checks if this delete bitmap is empty |
531 | | * |
532 | | * @return true if empty |
533 | | */ |
534 | | bool empty() const; |
535 | | |
536 | | /** |
537 | | * return the total cardinality of the Delete Bitmap |
538 | | */ |
539 | | uint64_t cardinality() const; |
540 | | |
541 | | /** |
542 | | * return the total size of the Delete Bitmap(after serialized) |
543 | | */ |
544 | | |
545 | | uint64_t get_size() const; |
546 | | |
547 | | /** |
548 | | * Sets the bitmap of specific segment, it's may be insertion or replacement |
549 | | * |
550 | | * @return 1 if the insertion took place, 0 if the assignment took place |
551 | | */ |
552 | | int set(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap); |
553 | | |
554 | | /** |
555 | | * Gets a copy of specific delete bmk |
556 | | * |
557 | | * @param segment_delete_bitmap output param |
558 | | * @return non-zero if the associated delete bitmap does not exist |
559 | | */ |
560 | | int get(const BitmapKey& bmk, roaring::Roaring* segment_delete_bitmap) const; |
561 | | |
562 | | /** |
563 | | * Gets reference to a specific delete map, DO NOT use this function on a |
564 | | * mutable DeleteBitmap object |
565 | | * @return nullptr if the given bitmap does not exist |
566 | | */ |
567 | | const roaring::Roaring* get(const BitmapKey& bmk) const; |
568 | | |
569 | | /** |
570 | | * Gets subset of delete_bitmap with given range [start, end) |
571 | | * |
572 | | * @parma start start |
573 | | * @parma end end |
574 | | * @parma subset_delete_map output param |
575 | | */ |
576 | | void subset(const BitmapKey& start, const BitmapKey& end, |
577 | | DeleteBitmap* subset_delete_map) const; |
578 | | void subset(std::vector<std::pair<RowsetId, int64_t>>& rowset_ids, int64_t start_version, |
579 | | int64_t end_version, DeleteBitmap* subset_delete_map) const; |
580 | | |
581 | | /** |
582 | | * Gets subset of delete_bitmap of the input rowsets |
583 | | * with given version range [start_version, end_version] and agg to end_version, |
584 | | * then merge to subset_delete_map |
585 | | */ |
586 | | void subset_and_agg(std::vector<std::pair<RowsetId, int64_t>>& rowset_ids, |
587 | | int64_t start_version, int64_t end_version, |
588 | | DeleteBitmap* subset_delete_map) const; |
589 | | |
590 | | /** |
591 | | * Gets count of delete_bitmap with given range [start, end) |
592 | | * |
593 | | * @parma start start |
594 | | * @parma end end |
595 | | */ |
596 | | size_t get_count_with_range(const BitmapKey& start, const BitmapKey& end) const; |
597 | | |
598 | | /** |
599 | | * Merges the given segment delete bitmap into *this |
600 | | * |
601 | | * @param bmk |
602 | | * @param segment_delete_bitmap |
603 | | */ |
604 | | void merge(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap); |
605 | | |
606 | | /** |
607 | | * Merges the given delete bitmap into *this |
608 | | * |
609 | | * @param other |
610 | | */ |
611 | | void merge(const DeleteBitmap& other); |
612 | | |
613 | | /** |
614 | | * Checks if the given row is marked deleted in bitmap with the condition: |
615 | | * all the bitmaps that |
616 | | * RowsetId and SegmentId are the same as the given ones, |
617 | | * and Version <= the given Version |
618 | | * |
619 | | * Note: aggregation cache may be used. |
620 | | * |
621 | | * @return true if marked deleted |
622 | | */ |
623 | | bool contains_agg(const BitmapKey& bitmap, uint32_t row_id) const; |
624 | | |
625 | | bool contains_agg_with_cache_if_eligible(const BitmapKey& bmk, uint32_t row_id) const; |
626 | | /** |
627 | | * Gets aggregated delete_bitmap on rowset_id and version, the same effect: |
628 | | * `select sum(roaring::Roaring) where RowsetId=rowset_id and SegmentId=seg_id and Version <= version` |
629 | | * |
630 | | * @return shared_ptr to a bitmap, which may be empty |
631 | | */ |
632 | | std::shared_ptr<roaring::Roaring> get_agg(const BitmapKey& bmk) const; |
633 | | std::shared_ptr<roaring::Roaring> get_agg_without_cache(const BitmapKey& bmk, |
634 | | const int64_t start_version = 0) const; |
635 | | |
636 | | void remove_sentinel_marks(); |
637 | | |
638 | | uint64_t get_delete_bitmap_count(); |
639 | | |
640 | | void traverse_rowset_and_version( |
641 | | const std::function<int(const RowsetId& rowsetId, int64_t version)>& func) const; |
642 | | |
643 | | bool has_calculated_for_multi_segments(const RowsetId& rowset_id) const; |
644 | | |
645 | | // return the size of the map |
646 | | size_t remove_rowset_cache_version(const RowsetId& rowset_id); |
647 | | |
648 | | void clear_rowset_cache_version(); |
649 | | |
650 | | std::set<std::string> get_rowset_cache_version(); |
651 | | |
652 | | DeleteBitmap agg_cache_snapshot(); |
653 | | |
654 | | void set_tablet_id(int64_t tablet_id); |
655 | | |
656 | | /** |
657 | | * Calculate diffset with given `key_set`. All entries with keys contained in this delete bitmap but not |
658 | | * in given key_set will be added to the output delete bitmap. |
659 | | * |
660 | | * @return Deletebitmap containning all entries in diffset |
661 | | */ |
662 | | DeleteBitmap diffset(const std::set<BitmapKey>& key_set) const; |
663 | | |
664 | | private: |
665 | | DeleteBitmap::Version _get_rowset_cache_version(const BitmapKey& bmk) const; |
666 | | |
667 | | int64_t _tablet_id; |
668 | | mutable std::shared_mutex _rowset_cache_version_lock; |
669 | | mutable std::map<RowsetId, std::map<SegmentId, Version>> _rowset_cache_version; |
670 | | }; |
671 | | |
672 | 2.39M | inline TabletUid TabletMeta::tablet_uid() const { |
673 | 2.39M | return _tablet_uid; |
674 | 2.39M | } |
675 | | |
676 | 1.96M | inline int64_t TabletMeta::table_id() const { |
677 | 1.96M | return _table_id; |
678 | 1.96M | } |
679 | | |
680 | 2.18M | inline int64_t TabletMeta::index_id() const { |
681 | 2.18M | return _index_id; |
682 | 2.18M | } |
683 | | |
684 | 5.64M | inline int64_t TabletMeta::partition_id() const { |
685 | 5.64M | return _partition_id; |
686 | 5.64M | } |
687 | | |
688 | 1.13G | inline int64_t TabletMeta::tablet_id() const { |
689 | 1.13G | return _tablet_id; |
690 | 1.13G | } |
691 | | |
692 | 1.38M | inline int64_t TabletMeta::replica_id() const { |
693 | 1.38M | return _replica_id; |
694 | 1.38M | } |
695 | | |
696 | 1.86M | inline int32_t TabletMeta::schema_hash() const { |
697 | 1.86M | return _schema_hash; |
698 | 1.86M | } |
699 | | |
700 | 318k | inline int32_t TabletMeta::shard_id() const { |
701 | 318k | return _shard_id; |
702 | 318k | } |
703 | | |
704 | 189 | inline void TabletMeta::set_shard_id(int32_t shard_id) { |
705 | 189 | _shard_id = shard_id; |
706 | 189 | } |
707 | | |
708 | 191k | inline int64_t TabletMeta::creation_time() const { |
709 | 191k | return _creation_time; |
710 | 191k | } |
711 | | |
712 | | inline void TabletMeta::set_creation_time(int64_t creation_time) { |
713 | | _creation_time = creation_time; |
714 | | } |
715 | | |
716 | 25.4k | inline int64_t TabletMeta::cumulative_layer_point() const { |
717 | 25.4k | return _cumulative_layer_point; |
718 | 25.4k | } |
719 | | |
720 | 0 | inline void TabletMeta::set_cumulative_layer_point(int64_t new_point) { |
721 | 0 | _cumulative_layer_point = new_point; |
722 | 0 | } |
723 | | |
724 | 1.21M | inline size_t TabletMeta::num_rows() const { |
725 | 1.21M | size_t num_rows = 0; |
726 | 2.09M | for (const auto& [_, rs] : _rs_metas) { |
727 | 2.09M | num_rows += rs->num_rows(); |
728 | 2.09M | } |
729 | 1.21M | return num_rows; |
730 | 1.21M | } |
731 | | |
732 | 24.5k | inline size_t TabletMeta::tablet_footprint() const { |
733 | 24.5k | size_t total_size = 0; |
734 | 61.6k | for (const auto& [_, rs] : _rs_metas) { |
735 | 61.6k | total_size += rs->total_disk_size(); |
736 | 61.6k | } |
737 | 24.5k | return total_size; |
738 | 24.5k | } |
739 | | |
740 | 4.38M | inline size_t TabletMeta::tablet_local_size() const { |
741 | 4.38M | size_t total_size = 0; |
742 | 7.59M | for (const auto& [_, rs] : _rs_metas) { |
743 | 7.59M | if (rs->is_local()) { |
744 | 7.56M | total_size += rs->total_disk_size(); |
745 | 7.56M | } |
746 | 7.59M | } |
747 | | // if we need to split data and binlog or not |
748 | 4.38M | total_size += binlog_size(); |
749 | 4.38M | return total_size; |
750 | 4.38M | } |
751 | | |
752 | 4.38M | inline size_t TabletMeta::tablet_remote_size() const { |
753 | 4.38M | size_t total_size = 0; |
754 | 7.59M | for (const auto& [_, rs] : _rs_metas) { |
755 | 7.59M | if (!rs->is_local()) { |
756 | 30.9k | total_size += rs->total_disk_size(); |
757 | 30.9k | } |
758 | 7.59M | } |
759 | 4.38M | return total_size; |
760 | 4.38M | } |
761 | | |
762 | 4.38M | inline size_t TabletMeta::binlog_size() const { |
763 | 4.38M | size_t total_size = 0; |
764 | 4.38M | for (auto& [_, rs] : _row_binlog_rs_metas) { |
765 | 0 | if (rs->is_local()) { |
766 | 0 | total_size += rs->data_disk_size(); |
767 | 0 | } |
768 | 0 | } |
769 | 4.38M | return total_size; |
770 | 4.38M | } |
771 | | |
772 | 1.21M | inline size_t TabletMeta::tablet_local_index_size() const { |
773 | 1.21M | size_t total_size = 0; |
774 | 2.09M | for (const auto& [_, rs] : _rs_metas) { |
775 | 2.09M | if (rs->is_local()) { |
776 | 2.09M | total_size += rs->index_disk_size(); |
777 | 2.09M | } |
778 | 2.09M | } |
779 | 1.21M | return total_size; |
780 | 1.21M | } |
781 | | |
782 | 1.21M | inline size_t TabletMeta::tablet_local_segment_size() const { |
783 | 1.21M | size_t total_size = 0; |
784 | 2.09M | for (const auto& [_, rs] : _rs_metas) { |
785 | 2.09M | if (rs->is_local()) { |
786 | 2.09M | total_size += rs->data_disk_size(); |
787 | 2.09M | } |
788 | 2.09M | } |
789 | 1.21M | return total_size; |
790 | 1.21M | } |
791 | | |
792 | 1.21M | inline size_t TabletMeta::tablet_remote_index_size() const { |
793 | 1.21M | size_t total_size = 0; |
794 | 2.09M | for (const auto& [_, rs] : _rs_metas) { |
795 | 2.09M | if (!rs->is_local()) { |
796 | 16 | total_size += rs->index_disk_size(); |
797 | 16 | } |
798 | 2.09M | } |
799 | 1.21M | return total_size; |
800 | 1.21M | } |
801 | | |
802 | 1.21M | inline size_t TabletMeta::tablet_remote_segment_size() const { |
803 | 1.21M | size_t total_size = 0; |
804 | 2.09M | for (const auto& [_, rs] : _rs_metas) { |
805 | 2.09M | if (!rs->is_local()) { |
806 | 16 | total_size += rs->data_disk_size(); |
807 | 16 | } |
808 | 2.09M | } |
809 | 1.21M | return total_size; |
810 | 1.21M | } |
811 | | |
812 | 9.26M | inline size_t TabletMeta::version_count() const { |
813 | 9.26M | return _rs_metas.size(); |
814 | 9.26M | } |
815 | | |
816 | 16.2k | inline size_t TabletMeta::stale_version_count() const { |
817 | 16.2k | return _rs_metas.size(); |
818 | 16.2k | } |
819 | | |
820 | 0 | inline size_t TabletMeta::binlog_file_num() const { |
821 | 0 | return _row_binlog_rs_metas.size(); |
822 | 0 | } |
823 | | |
824 | 317M | inline TabletState TabletMeta::tablet_state() const { |
825 | 317M | return _tablet_state; |
826 | 317M | } |
827 | | |
828 | 15.1k | inline void TabletMeta::set_tablet_state(TabletState state) { |
829 | 15.1k | _tablet_state = state; |
830 | 15.1k | } |
831 | | |
832 | 25.5k | inline bool TabletMeta::in_restore_mode() const { |
833 | 25.5k | return _in_restore_mode; |
834 | 25.5k | } |
835 | | |
836 | 0 | inline void TabletMeta::set_in_restore_mode(bool in_restore_mode) { |
837 | 0 | _in_restore_mode = in_restore_mode; |
838 | 0 | } |
839 | | |
840 | 684M | inline const TabletSchemaSPtr& TabletMeta::tablet_schema() const { |
841 | 684M | return _schema; |
842 | 684M | } |
843 | | |
844 | 5 | inline TabletSchema* TabletMeta::mutable_tablet_schema() { |
845 | 5 | return _schema.get(); |
846 | 5 | } |
847 | | |
848 | 11.1M | inline const RowsetMetaMapContainer& TabletMeta::all_rs_metas() const { |
849 | 11.1M | return _rs_metas; |
850 | 11.1M | } |
851 | | |
852 | 279k | inline RowsetMetaMapContainer& TabletMeta::all_mutable_rs_metas() { |
853 | 279k | return _rs_metas; |
854 | 279k | } |
855 | | |
856 | 698k | inline const RowsetMetaMapContainer& TabletMeta::all_stale_rs_metas() const { |
857 | 698k | return _stale_rs_metas; |
858 | 698k | } |
859 | | |
860 | 285k | inline const RowsetMetaMapContainer& TabletMeta::all_row_binlog_rs_metas() const { |
861 | 285k | return _row_binlog_rs_metas; |
862 | 285k | } |
863 | | |
864 | 0 | inline bool TabletMeta::all_beta() const { |
865 | 0 | for (const auto& [_, rs] : _rs_metas) { |
866 | 0 | if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) { |
867 | 0 | return false; |
868 | 0 | } |
869 | 0 | } |
870 | 0 | for (const auto& [_, rs] : _stale_rs_metas) { |
871 | 0 | if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) { |
872 | 0 | return false; |
873 | 0 | } |
874 | 0 | } |
875 | 0 | for (const auto& [_, rs] : _row_binlog_rs_metas) { |
876 | 0 | if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) { |
877 | 0 | return false; |
878 | 0 | } |
879 | 0 | } |
880 | 0 | return true; |
881 | 0 | } |
882 | | |
883 | | std::string tablet_state_name(TabletState state); |
884 | | |
885 | | // Only for unit test now. |
886 | | bool operator==(const TabletMeta& a, const TabletMeta& b); |
887 | | bool operator!=(const TabletMeta& a, const TabletMeta& b); |
888 | | |
889 | | } // namespace doris |