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