/root/doris/be/src/olap/tablet_meta.h
Line | Count | Source (jump to first uncovered line) |
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/olap_file.pb.h> |
22 | | #include <stdint.h> |
23 | | |
24 | | #include <atomic> |
25 | | #include <cstddef> |
26 | | #include <limits> |
27 | | #include <map> |
28 | | #include <memory> |
29 | | #include <mutex> |
30 | | #include <optional> |
31 | | #include <ostream> |
32 | | #include <roaring/roaring.hh> |
33 | | #include <shared_mutex> |
34 | | #include <string> |
35 | | #include <tuple> |
36 | | #include <unordered_map> |
37 | | #include <utility> |
38 | | #include <vector> |
39 | | |
40 | | #include "common/logging.h" |
41 | | #include "common/status.h" |
42 | | #include "gutil/stringprintf.h" |
43 | | #include "io/fs/file_system.h" |
44 | | #include "olap/binlog_config.h" |
45 | | #include "olap/lru_cache.h" |
46 | | #include "olap/olap_common.h" |
47 | | #include "olap/rowset/rowset_meta.h" |
48 | | #include "olap/tablet_schema.h" |
49 | | #include "runtime/memory/lru_cache_policy.h" |
50 | | #include "util/uid_util.h" |
51 | | |
52 | | namespace json2pb { |
53 | | struct Pb2JsonOptions; |
54 | | } // namespace json2pb |
55 | | |
56 | | namespace doris { |
57 | | class TColumn; |
58 | | |
59 | | // Lifecycle states that a Tablet can be in. Legal state transitions for a |
60 | | // Tablet object: |
61 | | // |
62 | | // NOTREADY -> RUNNING -> TOMBSTONED -> STOPPED -> SHUTDOWN |
63 | | // | | | ^^^ |
64 | | // | | +----------++| |
65 | | // | +------------------------+| |
66 | | // +-------------------------------------+ |
67 | | |
68 | | enum TabletState { |
69 | | // Tablet is under alter table, rollup, clone |
70 | | TABLET_NOTREADY, |
71 | | |
72 | | TABLET_RUNNING, |
73 | | |
74 | | // Tablet integrity has been violated, such as missing versions. |
75 | | // In this state, tablet will not accept any incoming request. |
76 | | // Report this state to FE, scheduling BE to drop tablet. |
77 | | TABLET_TOMBSTONED, |
78 | | |
79 | | // Tablet is shutting down, files in disk still remained. |
80 | | TABLET_STOPPED, |
81 | | |
82 | | // Files have been removed, tablet has been shutdown completely. |
83 | | TABLET_SHUTDOWN |
84 | | }; |
85 | | |
86 | | class DataDir; |
87 | | class TabletMeta; |
88 | | class DeleteBitmap; |
89 | | class TBinlogConfig; |
90 | | |
91 | | // Class encapsulates meta of tablet. |
92 | | // The concurrency control is handled in Tablet Class, not in this class. |
93 | | class TabletMeta { |
94 | | public: |
95 | | static TabletMetaSharedPtr create( |
96 | | const TCreateTabletReq& request, const TabletUid& tablet_uid, uint64_t shard_id, |
97 | | uint32_t next_unique_id, |
98 | | const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id); |
99 | | |
100 | | TabletMeta(); |
101 | | TabletMeta(int64_t table_id, int64_t partition_id, int64_t tablet_id, int64_t replica_id, |
102 | | int32_t schema_hash, uint64_t shard_id, const TTabletSchema& tablet_schema, |
103 | | uint32_t next_unique_id, |
104 | | const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id, |
105 | | TabletUid tablet_uid, TTabletType::type tabletType, |
106 | | TCompressionType::type compression_type, int64_t storage_policy_id = 0, |
107 | | bool enable_unique_key_merge_on_write = false, |
108 | | std::optional<TBinlogConfig> binlog_config = {}, |
109 | | std::string compaction_policy = "size_based", |
110 | | int64_t time_series_compaction_goal_size_mbytes = 1024, |
111 | | int64_t time_series_compaction_file_count_threshold = 2000, |
112 | | int64_t time_series_compaction_time_threshold_seconds = 3600, |
113 | | int64_t time_series_compaction_empty_rowsets_threshold = 5, |
114 | | TInvertedIndexStorageFormat::type inverted_index_storage_format = |
115 | | TInvertedIndexStorageFormat::V1, |
116 | | int64_t time_series_compaction_level_threshold = 1); |
117 | | // If need add a filed in TableMeta, filed init copy in copy construct function |
118 | | TabletMeta(const TabletMeta& tablet_meta); |
119 | | TabletMeta(TabletMeta&& tablet_meta) = delete; |
120 | | |
121 | | // UT |
122 | | #ifdef BE_TEST |
123 | 1 | TabletMeta(TabletSchemaSPtr tablet_schema) : _schema(tablet_schema) {} |
124 | | #endif |
125 | | |
126 | | // Function create_from_file is used to be compatible with previous tablet_meta. |
127 | | // Previous tablet_meta is a physical file in tablet dir, which is not stored in rocksdb. |
128 | | Status create_from_file(const std::string& file_path); |
129 | | Status save(const std::string& file_path); |
130 | | Status save_as_json(const string& file_path, DataDir* dir); |
131 | | static Status save(const std::string& file_path, const TabletMetaPB& tablet_meta_pb); |
132 | | static std::string construct_header_file_path(const std::string& schema_hash_path, |
133 | | int64_t tablet_id); |
134 | | Status save_meta(DataDir* data_dir); |
135 | | |
136 | | void serialize(std::string* meta_binary); |
137 | | Status deserialize(const std::string& meta_binary); |
138 | | void init_from_pb(const TabletMetaPB& tablet_meta_pb); |
139 | | // Init `RowsetMeta._fs` if rowset is local. |
140 | | void init_rs_metas_fs(const io::FileSystemSPtr& fs); |
141 | | |
142 | | void to_meta_pb(TabletMetaPB* tablet_meta_pb); |
143 | | void to_json(std::string* json_string, json2pb::Pb2JsonOptions& options); |
144 | | // Don't use. |
145 | | // TODO: memory size of TabletSchema cannot be accurately tracked. |
146 | | // In some places, temporarily use num_columns() as TabletSchema size. |
147 | | int64_t mem_size() const; |
148 | 96 | size_t tablet_columns_num() const { return _schema->num_columns(); } |
149 | | |
150 | 0 | TabletTypePB tablet_type() const { return _tablet_type; } |
151 | | TabletUid tablet_uid() const; |
152 | 28 | void set_tablet_uid(TabletUid uid) { _tablet_uid = uid; } |
153 | | int64_t table_id() const; |
154 | | int64_t partition_id() const; |
155 | | int64_t tablet_id() const; |
156 | | int64_t replica_id() const; |
157 | 0 | void set_replica_id(int64_t replica_id) { _replica_id = replica_id; } |
158 | | int32_t schema_hash() const; |
159 | | int16_t shard_id() const; |
160 | | void set_shard_id(int32_t shard_id); |
161 | | int64_t creation_time() const; |
162 | | void set_creation_time(int64_t creation_time); |
163 | | int64_t cumulative_layer_point() const; |
164 | | void set_cumulative_layer_point(int64_t new_point); |
165 | | |
166 | | size_t num_rows() const; |
167 | | // Disk space occupied by tablet, contain local and remote. |
168 | | size_t tablet_footprint() const; |
169 | | // Local disk space occupied by tablet. |
170 | | size_t tablet_local_size() const; |
171 | | // Remote disk space occupied by tablet. |
172 | | size_t tablet_remote_size() const; |
173 | | size_t version_count() const; |
174 | | size_t stale_version_count() const; |
175 | | size_t version_count_cross_with_range(const Version& range) const; |
176 | | Version max_version() const; |
177 | | |
178 | | TabletState tablet_state() const; |
179 | | void set_tablet_state(TabletState state); |
180 | | |
181 | | bool in_restore_mode() const; |
182 | | void set_in_restore_mode(bool in_restore_mode); |
183 | | |
184 | | const TabletSchemaSPtr& tablet_schema() const; |
185 | | |
186 | | TabletSchema* mutable_tablet_schema(); |
187 | | |
188 | | const std::vector<RowsetMetaSharedPtr>& all_rs_metas() const; |
189 | | std::vector<RowsetMetaSharedPtr>& all_mutable_rs_metas(); |
190 | | Status add_rs_meta(const RowsetMetaSharedPtr& rs_meta); |
191 | | void delete_rs_meta_by_version(const Version& version, |
192 | | std::vector<RowsetMetaSharedPtr>* deleted_rs_metas); |
193 | | // If same_version is true, the rowset in "to_delete" will not be added |
194 | | // to _stale_rs_meta, but to be deleted from rs_meta directly. |
195 | | void modify_rs_metas(const std::vector<RowsetMetaSharedPtr>& to_add, |
196 | | const std::vector<RowsetMetaSharedPtr>& to_delete, |
197 | | bool same_version = false); |
198 | | void revise_rs_metas(std::vector<RowsetMetaSharedPtr>&& rs_metas); |
199 | | void revise_delete_bitmap_unlocked(const DeleteBitmap& delete_bitmap); |
200 | | |
201 | | const std::vector<RowsetMetaSharedPtr>& all_stale_rs_metas() const; |
202 | | RowsetMetaSharedPtr acquire_rs_meta_by_version(const Version& version) const; |
203 | | void delete_stale_rs_meta_by_version(const Version& version); |
204 | | RowsetMetaSharedPtr acquire_stale_rs_meta_by_version(const Version& version) const; |
205 | | |
206 | | Status set_partition_id(int64_t partition_id); |
207 | | |
208 | 99 | RowsetTypePB preferred_rowset_type() const { return _preferred_rowset_type; } |
209 | | |
210 | 51 | void set_preferred_rowset_type(RowsetTypePB preferred_rowset_type) { |
211 | 51 | _preferred_rowset_type = preferred_rowset_type; |
212 | 51 | } |
213 | | |
214 | | // used for after tablet cloned to clear stale rowset |
215 | 0 | void clear_stale_rowset() { _stale_rs_metas.clear(); } |
216 | | |
217 | | bool all_beta() const; |
218 | | |
219 | 22 | int64_t storage_policy_id() const { return _storage_policy_id; } |
220 | | |
221 | 4 | void set_storage_policy_id(int64_t id) { |
222 | 4 | VLOG_NOTICE << "set tablet_id : " << _table_id << " storage policy from " |
223 | 0 | << _storage_policy_id << " to " << id; |
224 | 4 | _storage_policy_id = id; |
225 | 4 | } |
226 | | |
227 | 14 | UniqueId cooldown_meta_id() const { return _cooldown_meta_id; } |
228 | 5 | void set_cooldown_meta_id(UniqueId uid) { _cooldown_meta_id = uid; } |
229 | | |
230 | | static void init_column_from_tcolumn(uint32_t unique_id, const TColumn& tcolumn, |
231 | | ColumnPB* column); |
232 | | |
233 | 16 | DeleteBitmap& delete_bitmap() { return *_delete_bitmap; } |
234 | | |
235 | 577 | bool enable_unique_key_merge_on_write() const { return _enable_unique_key_merge_on_write; } |
236 | | |
237 | | // TODO(Drogon): thread safety |
238 | 0 | const BinlogConfig& binlog_config() const { return _binlog_config; } |
239 | 0 | void set_binlog_config(BinlogConfig binlog_config) { |
240 | 0 | _binlog_config = std::move(binlog_config); |
241 | 0 | } |
242 | | |
243 | 13 | void set_compaction_policy(std::string compaction_policy) { |
244 | 13 | _compaction_policy = compaction_policy; |
245 | 13 | } |
246 | 344 | std::string compaction_policy() const { return _compaction_policy; } |
247 | 13 | void set_time_series_compaction_goal_size_mbytes(int64_t goal_size_mbytes) { |
248 | 13 | _time_series_compaction_goal_size_mbytes = goal_size_mbytes; |
249 | 13 | } |
250 | 123 | int64_t time_series_compaction_goal_size_mbytes() const { |
251 | 123 | return _time_series_compaction_goal_size_mbytes; |
252 | 123 | } |
253 | 13 | void set_time_series_compaction_file_count_threshold(int64_t file_count_threshold) { |
254 | 13 | _time_series_compaction_file_count_threshold = file_count_threshold; |
255 | 13 | } |
256 | 121 | int64_t time_series_compaction_file_count_threshold() const { |
257 | 121 | return _time_series_compaction_file_count_threshold; |
258 | 121 | } |
259 | 13 | void set_time_series_compaction_time_threshold_seconds(int64_t time_threshold) { |
260 | 13 | _time_series_compaction_time_threshold_seconds = time_threshold; |
261 | 13 | } |
262 | 119 | int64_t time_series_compaction_time_threshold_seconds() const { |
263 | 119 | return _time_series_compaction_time_threshold_seconds; |
264 | 119 | } |
265 | 0 | void set_time_series_compaction_empty_rowsets_threshold(int64_t empty_rowsets_threshold) { |
266 | 0 | _time_series_compaction_empty_rowsets_threshold = empty_rowsets_threshold; |
267 | 0 | } |
268 | 120 | int64_t time_series_compaction_empty_rowsets_threshold() const { |
269 | 120 | return _time_series_compaction_empty_rowsets_threshold; |
270 | 120 | } |
271 | 0 | void set_time_series_compaction_level_threshold(int64_t level_threshold) { |
272 | 0 | _time_series_compaction_level_threshold = level_threshold; |
273 | 0 | } |
274 | 142 | int64_t time_series_compaction_level_threshold() const { |
275 | 142 | return _time_series_compaction_level_threshold; |
276 | 142 | } |
277 | | |
278 | 24 | int64_t avg_rs_meta_serialize_size() const { return _avg_rs_meta_serialize_size; } |
279 | | |
280 | | private: |
281 | | Status _save_meta(DataDir* data_dir); |
282 | | |
283 | | // _del_predicates is ignored to compare. |
284 | | friend bool operator==(const TabletMeta& a, const TabletMeta& b); |
285 | | friend bool operator!=(const TabletMeta& a, const TabletMeta& b); |
286 | | |
287 | | private: |
288 | | int64_t _table_id = 0; |
289 | | int64_t _partition_id = 0; |
290 | | int64_t _tablet_id = 0; |
291 | | int64_t _replica_id = 0; |
292 | | int32_t _schema_hash = 0; |
293 | | int32_t _shard_id = 0; |
294 | | int64_t _creation_time = 0; |
295 | | int64_t _cumulative_layer_point = 0; |
296 | | TabletUid _tablet_uid; |
297 | | TabletTypePB _tablet_type = TabletTypePB::TABLET_TYPE_DISK; |
298 | | |
299 | | TabletState _tablet_state = TABLET_NOTREADY; |
300 | | // the reference of _schema may use in tablet, so here need keep |
301 | | // the lifetime of tablemeta and _schema is same with tablet |
302 | | TabletSchemaSPtr _schema; |
303 | | |
304 | | std::vector<RowsetMetaSharedPtr> _rs_metas; |
305 | | // This variable _stale_rs_metas is used to record these rowsets‘ meta which are be compacted. |
306 | | // These stale rowsets meta are been removed when rowsets' pathVersion is expired, |
307 | | // this policy is judged and computed by TimestampedVersionTracker. |
308 | | std::vector<RowsetMetaSharedPtr> _stale_rs_metas; |
309 | | bool _in_restore_mode = false; |
310 | | RowsetTypePB _preferred_rowset_type = BETA_ROWSET; |
311 | | |
312 | | // meta for cooldown |
313 | | int64_t _storage_policy_id = 0; // <= 0 means no storage policy |
314 | | UniqueId _cooldown_meta_id; |
315 | | |
316 | | // For unique key data model, the feature Merge-on-Write will leverage a primary |
317 | | // key index and a delete-bitmap to mark duplicate keys as deleted in load stage, |
318 | | // which can avoid the merging cost in read stage, and accelerate the aggregation |
319 | | // query performance significantly. |
320 | | bool _enable_unique_key_merge_on_write = false; |
321 | | std::shared_ptr<DeleteBitmap> _delete_bitmap; |
322 | | |
323 | | // binlog config |
324 | | BinlogConfig _binlog_config {}; |
325 | | |
326 | | // meta for compaction |
327 | | std::string _compaction_policy; |
328 | | int64_t _time_series_compaction_goal_size_mbytes = 0; |
329 | | int64_t _time_series_compaction_file_count_threshold = 0; |
330 | | int64_t _time_series_compaction_time_threshold_seconds = 0; |
331 | | int64_t _time_series_compaction_empty_rowsets_threshold = 0; |
332 | | int64_t _time_series_compaction_level_threshold = 0; |
333 | | |
334 | | int64_t _avg_rs_meta_serialize_size = 0; |
335 | | |
336 | | mutable std::shared_mutex _meta_lock; |
337 | | }; |
338 | | |
339 | | /** |
340 | | * Wraps multiple bitmaps for recording rows (row id) that are deleted or |
341 | | * overwritten. For now, it's only used when unique key merge-on-write property |
342 | | * enabled. |
343 | | * |
344 | | * RowsetId and SegmentId are for locating segment, Version here is a single |
345 | | * uint32_t means that at which "version" of the load causes the delete or |
346 | | * overwrite. |
347 | | * |
348 | | * The start and end version of a load is the same, it's ok and straightforward |
349 | | * to use a single uint32_t. |
350 | | * |
351 | | * e.g. |
352 | | * There is a key "key1" in rowset id 1, version [1,1], segment id 1, row id 1. |
353 | | * A new load also contains "key1", the rowset id 2, version [2,2], segment id 1 |
354 | | * the delete bitmap will be `{1,1,2} -> 1`, which means the "row id 1" in |
355 | | * "rowset id 1, segment id 1" is deleted/overitten by some loads at "version 2" |
356 | | */ |
357 | | class DeleteBitmap { |
358 | | public: |
359 | | mutable std::shared_mutex lock; |
360 | | using SegmentId = uint32_t; |
361 | | using Version = uint64_t; |
362 | | using BitmapKey = std::tuple<RowsetId, SegmentId, Version>; |
363 | | std::map<BitmapKey, roaring::Roaring> delete_bitmap; // Ordered map |
364 | | constexpr static inline uint32_t INVALID_SEGMENT_ID = std::numeric_limits<uint32_t>::max() - 1; |
365 | | constexpr static inline uint32_t ROWSET_SENTINEL_MARK = |
366 | | std::numeric_limits<uint32_t>::max() - 1; |
367 | | |
368 | | // When a delete bitmap is merged into tablet's delete bitmap, the version of entries in the delete bitmap |
369 | | // will be replaced to the correspoding correct version. So before we finally merge a delete bitmap into |
370 | | // tablet's delete bitmap we can use arbitary version number in BitmapKey. Here we define some version numbers |
371 | | // for specific usage during this periods to avoid conflicts |
372 | | constexpr static inline uint64_t TEMP_VERSION_COMMON = 0; |
373 | | |
374 | | /** |
375 | | * |
376 | | * @param tablet_id the tablet which this delete bitmap associates with |
377 | | */ |
378 | | DeleteBitmap(int64_t tablet_id); |
379 | | |
380 | | /** |
381 | | * Copy c-tor for making delete bitmap snapshot on read path |
382 | | */ |
383 | | DeleteBitmap(const DeleteBitmap& r); |
384 | | DeleteBitmap& operator=(const DeleteBitmap& r); |
385 | | /** |
386 | | * Move c-tor for making delete bitmap snapshot on read path |
387 | | */ |
388 | | DeleteBitmap(DeleteBitmap&& r); |
389 | | DeleteBitmap& operator=(DeleteBitmap&& r); |
390 | | |
391 | | /** |
392 | | * Makes a snapshot of delete bitmap, read lock will be acquired in this |
393 | | * process |
394 | | */ |
395 | | DeleteBitmap snapshot() const; |
396 | | |
397 | | /** |
398 | | * Makes a snapshot of delete bitmap on given version, read lock will be |
399 | | * acquired temporary in this process |
400 | | */ |
401 | | DeleteBitmap snapshot(Version version) const; |
402 | | |
403 | | /** |
404 | | * Marks the specific row deleted |
405 | | */ |
406 | | void add(const BitmapKey& bmk, uint32_t row_id); |
407 | | |
408 | | /** |
409 | | * Clears the deletetion mark specific row |
410 | | * |
411 | | * @return non-zero if the associated delete bitmap does not exist |
412 | | */ |
413 | | int remove(const BitmapKey& bmk, uint32_t row_id); |
414 | | |
415 | | /** |
416 | | * Clears bitmaps in range [lower_key, upper_key) |
417 | | */ |
418 | | void remove(const BitmapKey& lower_key, const BitmapKey& upper_key); |
419 | | |
420 | | /** |
421 | | * Checks if the given row is marked deleted |
422 | | * |
423 | | * @return true if marked deleted |
424 | | */ |
425 | | bool contains(const BitmapKey& bmk, uint32_t row_id) const; |
426 | | |
427 | | /** |
428 | | * Checks if this delete bitmap is empty |
429 | | * |
430 | | * @return true if empty |
431 | | */ |
432 | | bool empty() const; |
433 | | |
434 | | /** |
435 | | * return the total cardinality of the Delete Bitmap |
436 | | */ |
437 | | uint64_t cardinality() const; |
438 | | |
439 | | /** |
440 | | * Sets the bitmap of specific segment, it's may be insertion or replacement |
441 | | * |
442 | | * @return 1 if the insertion took place, 0 if the assignment took place |
443 | | */ |
444 | | int set(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap); |
445 | | |
446 | | /** |
447 | | * Gets a copy of specific delete bmk |
448 | | * |
449 | | * @param segment_delete_bitmap output param |
450 | | * @return non-zero if the associated delete bitmap does not exist |
451 | | */ |
452 | | int get(const BitmapKey& bmk, roaring::Roaring* segment_delete_bitmap) const; |
453 | | |
454 | | /** |
455 | | * Gets reference to a specific delete map, DO NOT use this function on a |
456 | | * mutable DeleteBitmap object |
457 | | * @return nullptr if the given bitmap does not exist |
458 | | */ |
459 | | const roaring::Roaring* get(const BitmapKey& bmk) const; |
460 | | |
461 | | /** |
462 | | * Gets subset of delete_bitmap with given range [start, end) |
463 | | * |
464 | | * @parma start start |
465 | | * @parma end end |
466 | | * @parma subset_delete_map output param |
467 | | */ |
468 | | void subset(const BitmapKey& start, const BitmapKey& end, |
469 | | DeleteBitmap* subset_delete_map) const; |
470 | | |
471 | | /** |
472 | | * Merges the given segment delete bitmap into *this |
473 | | * |
474 | | * @param bmk |
475 | | * @param segment_delete_bitmap |
476 | | */ |
477 | | void merge(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap); |
478 | | |
479 | | /** |
480 | | * Merges the given delete bitmap into *this |
481 | | * |
482 | | * @param other |
483 | | */ |
484 | | void merge(const DeleteBitmap& other); |
485 | | |
486 | | /** |
487 | | * Checks if the given row is marked deleted in bitmap with the condition: |
488 | | * all the bitmaps that |
489 | | * RowsetId and SegmentId are the same as the given ones, |
490 | | * and Version <= the given Version |
491 | | * |
492 | | * Note: aggregation cache may be used. |
493 | | * |
494 | | * @return true if marked deleted |
495 | | */ |
496 | | bool contains_agg(const BitmapKey& bitmap, uint32_t row_id) const; |
497 | | |
498 | | bool contains_agg_without_cache(const BitmapKey& bmk, uint32_t row_id) const; |
499 | | /** |
500 | | * Gets aggregated delete_bitmap on rowset_id and version, the same effect: |
501 | | * `select sum(roaring::Roaring) where RowsetId=rowset_id and SegmentId=seg_id and Version <= version` |
502 | | * |
503 | | * @return shared_ptr to a bitmap, which may be empty |
504 | | */ |
505 | | std::shared_ptr<roaring::Roaring> get_agg(const BitmapKey& bmk) const; |
506 | | |
507 | | class AggCachePolicy : public LRUCachePolicyTrackingManual { |
508 | | public: |
509 | | AggCachePolicy(size_t capacity) |
510 | | : LRUCachePolicyTrackingManual(CachePolicy::CacheType::DELETE_BITMAP_AGG_CACHE, |
511 | | capacity, LRUCacheType::SIZE, |
512 | | config::delete_bitmap_agg_cache_stale_sweep_time_sec, |
513 | 1 | 256) {} |
514 | | }; |
515 | | |
516 | | class AggCache { |
517 | | public: |
518 | | class Value : public LRUCacheValueBase { |
519 | | public: |
520 | | roaring::Roaring bitmap; |
521 | | }; |
522 | | |
523 | 307 | AggCache(size_t size_in_bytes) { |
524 | 307 | static std::once_flag once; |
525 | 307 | std::call_once(once, [size_in_bytes] { |
526 | 1 | auto* tmp = new AggCachePolicy(size_in_bytes); |
527 | 1 | AggCache::s_repr.store(tmp, std::memory_order_release); |
528 | 1 | }); |
529 | | |
530 | 307 | while (!s_repr.load(std::memory_order_acquire)) { |
531 | 0 | } |
532 | 307 | } |
533 | | |
534 | 132 | static LRUCachePolicy* repr() { return s_repr.load(std::memory_order_acquire); } |
535 | | static std::atomic<AggCachePolicy*> s_repr; |
536 | | }; |
537 | | |
538 | | private: |
539 | | mutable std::shared_ptr<AggCache> _agg_cache; |
540 | | int64_t _tablet_id; |
541 | | }; |
542 | | |
543 | | static const std::string SEQUENCE_COL = "__DORIS_SEQUENCE_COL__"; |
544 | | |
545 | 680 | inline TabletUid TabletMeta::tablet_uid() const { |
546 | 680 | return _tablet_uid; |
547 | 680 | } |
548 | | |
549 | 119 | inline int64_t TabletMeta::table_id() const { |
550 | 119 | return _table_id; |
551 | 119 | } |
552 | | |
553 | 392 | inline int64_t TabletMeta::partition_id() const { |
554 | 392 | return _partition_id; |
555 | 392 | } |
556 | | |
557 | 3.90k | inline int64_t TabletMeta::tablet_id() const { |
558 | 3.90k | return _tablet_id; |
559 | 3.90k | } |
560 | | |
561 | 159 | inline int64_t TabletMeta::replica_id() const { |
562 | 159 | return _replica_id; |
563 | 159 | } |
564 | | |
565 | 449 | inline int32_t TabletMeta::schema_hash() const { |
566 | 449 | return _schema_hash; |
567 | 449 | } |
568 | | |
569 | 300 | inline int16_t TabletMeta::shard_id() const { |
570 | 300 | return _shard_id; |
571 | 300 | } |
572 | | |
573 | 16 | inline void TabletMeta::set_shard_id(int32_t shard_id) { |
574 | 16 | _shard_id = shard_id; |
575 | 16 | } |
576 | | |
577 | 117 | inline int64_t TabletMeta::creation_time() const { |
578 | 117 | return _creation_time; |
579 | 117 | } |
580 | | |
581 | 0 | inline void TabletMeta::set_creation_time(int64_t creation_time) { |
582 | 0 | _creation_time = creation_time; |
583 | 0 | } |
584 | | |
585 | 117 | inline int64_t TabletMeta::cumulative_layer_point() const { |
586 | 117 | return _cumulative_layer_point; |
587 | 117 | } |
588 | | |
589 | 0 | inline void TabletMeta::set_cumulative_layer_point(int64_t new_point) { |
590 | 0 | _cumulative_layer_point = new_point; |
591 | 0 | } |
592 | | |
593 | 14 | inline size_t TabletMeta::num_rows() const { |
594 | 14 | size_t num_rows = 0; |
595 | 25 | for (auto& rs : _rs_metas) { |
596 | 25 | num_rows += rs->num_rows(); |
597 | 25 | } |
598 | 14 | return num_rows; |
599 | 14 | } |
600 | | |
601 | 8 | inline size_t TabletMeta::tablet_footprint() const { |
602 | 8 | size_t total_size = 0; |
603 | 26 | for (auto& rs : _rs_metas) { |
604 | 26 | total_size += rs->data_disk_size(); |
605 | 26 | } |
606 | 8 | return total_size; |
607 | 8 | } |
608 | | |
609 | 0 | inline size_t TabletMeta::tablet_local_size() const { |
610 | 0 | size_t total_size = 0; |
611 | 0 | for (auto& rs : _rs_metas) { |
612 | 0 | if (rs->is_local()) { |
613 | 0 | total_size += rs->data_disk_size(); |
614 | 0 | } |
615 | 0 | } |
616 | 0 | return total_size; |
617 | 0 | } |
618 | | |
619 | 0 | inline size_t TabletMeta::tablet_remote_size() const { |
620 | 0 | size_t total_size = 0; |
621 | 0 | for (auto& rs : _rs_metas) { |
622 | 0 | if (!rs->is_local()) { |
623 | 0 | total_size += rs->data_disk_size(); |
624 | 0 | } |
625 | 0 | } |
626 | 0 | return total_size; |
627 | 0 | } |
628 | | |
629 | 48 | inline size_t TabletMeta::version_count() const { |
630 | 48 | return _rs_metas.size(); |
631 | 48 | } |
632 | | |
633 | 24 | inline size_t TabletMeta::stale_version_count() const { |
634 | 24 | return _rs_metas.size(); |
635 | 24 | } |
636 | | |
637 | 1.11k | inline TabletState TabletMeta::tablet_state() const { |
638 | 1.11k | return _tablet_state; |
639 | 1.11k | } |
640 | | |
641 | 25 | inline void TabletMeta::set_tablet_state(TabletState state) { |
642 | 25 | _tablet_state = state; |
643 | 25 | } |
644 | | |
645 | 117 | inline bool TabletMeta::in_restore_mode() const { |
646 | 117 | return _in_restore_mode; |
647 | 117 | } |
648 | | |
649 | 0 | inline void TabletMeta::set_in_restore_mode(bool in_restore_mode) { |
650 | 0 | _in_restore_mode = in_restore_mode; |
651 | 0 | } |
652 | | |
653 | 1.48k | inline const TabletSchemaSPtr& TabletMeta::tablet_schema() const { |
654 | 1.48k | return _schema; |
655 | 1.48k | } |
656 | | |
657 | 0 | inline TabletSchema* TabletMeta::mutable_tablet_schema() { |
658 | 0 | return _schema.get(); |
659 | 0 | } |
660 | | |
661 | 849 | inline const std::vector<RowsetMetaSharedPtr>& TabletMeta::all_rs_metas() const { |
662 | 849 | return _rs_metas; |
663 | 849 | } |
664 | | |
665 | 0 | inline std::vector<RowsetMetaSharedPtr>& TabletMeta::all_mutable_rs_metas() { |
666 | 0 | return _rs_metas; |
667 | 0 | } |
668 | | |
669 | 469 | inline const std::vector<RowsetMetaSharedPtr>& TabletMeta::all_stale_rs_metas() const { |
670 | 469 | return _stale_rs_metas; |
671 | 469 | } |
672 | | |
673 | 0 | inline bool TabletMeta::all_beta() const { |
674 | 0 | for (auto& rs : _rs_metas) { |
675 | 0 | if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) { |
676 | 0 | return false; |
677 | 0 | } |
678 | 0 | } |
679 | 0 | for (auto& rs : _stale_rs_metas) { |
680 | 0 | if (rs->rowset_type() != RowsetTypePB::BETA_ROWSET) { |
681 | 0 | return false; |
682 | 0 | } |
683 | 0 | } |
684 | 0 | return true; |
685 | 0 | } |
686 | | |
687 | | std::string tablet_state_name(TabletState state); |
688 | | |
689 | | // Only for unit test now. |
690 | | bool operator==(const TabletMeta& a, const TabletMeta& b); |
691 | | bool operator!=(const TabletMeta& a, const TabletMeta& b); |
692 | | |
693 | | } // namespace doris |