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