/root/doris/be/src/olap/base_tablet.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/olap_common.pb.h> |
21 | | |
22 | | #include <memory> |
23 | | #include <mutex> |
24 | | #include <shared_mutex> |
25 | | #include <string> |
26 | | |
27 | | #include "common/status.h" |
28 | | #include "olap/iterators.h" |
29 | | #include "olap/olap_common.h" |
30 | | #include "olap/partial_update_info.h" |
31 | | #include "olap/rowset/segment_v2/segment.h" |
32 | | #include "olap/tablet_fwd.h" |
33 | | #include "olap/tablet_meta.h" |
34 | | #include "olap/tablet_schema.h" |
35 | | #include "olap/version_graph.h" |
36 | | #include "util/metrics.h" |
37 | | |
38 | | namespace doris { |
39 | | struct RowSetSplits; |
40 | | struct RowsetWriterContext; |
41 | | class RowsetWriter; |
42 | | class CalcDeleteBitmapToken; |
43 | | class SegmentCacheHandle; |
44 | | class RowIdConversion; |
45 | | struct PartialUpdateInfo; |
46 | | class FixedReadPlan; |
47 | | |
48 | | struct TabletWithVersion { |
49 | | BaseTabletSPtr tablet; |
50 | | int64_t version; |
51 | | }; |
52 | | |
53 | | struct CaptureRsReaderOptions { |
54 | | // Used by local mode only. |
55 | | // If true, allows skipping missing versions during rowset capture. |
56 | | // This can be useful when some versions are temporarily unavailable. |
57 | | bool skip_missing_version {false}; |
58 | | |
59 | | // ======== only take effect in cloud mode ======== |
60 | | |
61 | | // Enable preference for cached/warmed-up rowsets when building version paths. |
62 | | // When enabled, the capture process will prioritize already cached rowsets |
63 | | // to avoid cold data reads and improve query performance. |
64 | | bool enable_prefer_cached_rowset {false}; |
65 | | |
66 | | // Query freshness tolerance in milliseconds. |
67 | | // Defines the time window for considering data as "fresh enough". |
68 | | // Rowsets that became visible within this time range can be skipped if not warmed up, |
69 | | // but older rowsets (before current_time - query_freshness_tolerance_ms) that are |
70 | | // not warmed up will trigger fallback to normal capture. |
71 | | // Set to -1 to disable freshness tolerance checking. |
72 | | int64_t query_freshness_tolerance_ms {-1}; |
73 | | }; |
74 | | |
75 | | enum class CompactionStage { NOT_SCHEDULED, PENDING, EXECUTING }; |
76 | | |
77 | | // Base class for all tablet classes |
78 | | class BaseTablet : public std::enable_shared_from_this<BaseTablet> { |
79 | | public: |
80 | | explicit BaseTablet(TabletMetaSharedPtr tablet_meta); |
81 | | virtual ~BaseTablet(); |
82 | | BaseTablet(const BaseTablet&) = delete; |
83 | | BaseTablet& operator=(const BaseTablet&) = delete; |
84 | | |
85 | 9.41k | TabletState tablet_state() const { return _tablet_meta->tablet_state(); } |
86 | | Status set_tablet_state(TabletState state); |
87 | 14 | int64_t table_id() const { return _tablet_meta->table_id(); } |
88 | 0 | size_t row_size() const { return _tablet_meta->tablet_schema()->row_size(); } |
89 | 28 | int64_t index_id() const { return _tablet_meta->index_id(); } |
90 | 977 | int64_t partition_id() const { return _tablet_meta->partition_id(); } |
91 | 18.3k | int64_t tablet_id() const { return _tablet_meta->tablet_id(); } |
92 | 1.51k | int32_t schema_hash() const { return _tablet_meta->schema_hash(); } |
93 | 0 | CompressKind compress_kind() const { return _tablet_meta->tablet_schema()->compress_kind(); } |
94 | 1.27k | KeysType keys_type() const { return _tablet_meta->tablet_schema()->keys_type(); } |
95 | 128 | size_t num_key_columns() const { return _tablet_meta->tablet_schema()->num_key_columns(); } |
96 | 337 | int64_t ttl_seconds() const { return _tablet_meta->ttl_seconds(); } |
97 | | // currently used by schema change, inverted index building, and cooldown |
98 | 18 | std::timed_mutex& get_schema_change_lock() { return _schema_change_lock; } |
99 | 1.21k | bool enable_unique_key_merge_on_write() const { |
100 | 1.21k | #ifdef BE_TEST |
101 | 1.21k | if (_tablet_meta == nullptr) { |
102 | 0 | return false; |
103 | 0 | } |
104 | 1.21k | #endif |
105 | 1.21k | return _tablet_meta->enable_unique_key_merge_on_write(); |
106 | 1.21k | } |
107 | | |
108 | | // Property encapsulated in TabletMeta |
109 | 2.87k | const TabletMetaSharedPtr& tablet_meta() { return _tablet_meta; } |
110 | | |
111 | | int32_t max_version_config(); |
112 | | |
113 | | // FIXME(plat1ko): It is not appropriate to expose this lock |
114 | 319 | std::shared_mutex& get_header_lock() { return _meta_lock; } |
115 | | |
116 | | void update_max_version_schema(const TabletSchemaSPtr& tablet_schema); |
117 | | |
118 | 11.6k | TabletSchemaSPtr tablet_schema() const { |
119 | 11.6k | std::shared_lock rlock(_meta_lock); |
120 | 11.6k | return _max_version_schema; |
121 | 11.6k | } |
122 | | |
123 | 0 | void set_alter_failed(bool alter_failed) { _alter_failed = alter_failed; } |
124 | 0 | bool is_alter_failed() { return _alter_failed; } |
125 | | |
126 | | virtual std::string tablet_path() const = 0; |
127 | | |
128 | | virtual bool exceed_version_limit(int32_t limit) = 0; |
129 | | |
130 | | virtual Result<std::unique_ptr<RowsetWriter>> create_rowset_writer(RowsetWriterContext& context, |
131 | | bool vertical) = 0; |
132 | | |
133 | | virtual Status capture_consistent_rowsets_unlocked( |
134 | | const Version& spec_version, std::vector<RowsetSharedPtr>* rowsets) const = 0; |
135 | | |
136 | | virtual Status capture_rs_readers(const Version& spec_version, |
137 | | std::vector<RowSetSplits>* rs_splits, |
138 | | const CaptureRsReaderOptions& opts) = 0; |
139 | | |
140 | | virtual size_t tablet_footprint() = 0; |
141 | | |
142 | | // this method just return the compaction sum on each rowset |
143 | | // note(tsy): we should unify the compaction score calculation finally |
144 | | uint32_t get_real_compaction_score() const; |
145 | | |
146 | | // MUST hold shared meta lock |
147 | | Status capture_rs_readers_unlocked(const Versions& version_path, |
148 | | std::vector<RowSetSplits>* rs_splits) const; |
149 | | |
150 | | // _rs_version_map and _stale_rs_version_map should be protected by _meta_lock |
151 | | // The caller must call hold _meta_lock when call this three function. |
152 | | RowsetSharedPtr get_rowset_by_version(const Version& version, bool find_is_stale = false) const; |
153 | | RowsetSharedPtr get_stale_rowset_by_version(const Version& version) const; |
154 | | RowsetSharedPtr get_rowset_with_max_version() const; |
155 | | |
156 | | Status get_all_rs_id(int64_t max_version, RowsetIdUnorderedSet* rowset_ids) const; |
157 | | Status get_all_rs_id_unlocked(int64_t max_version, RowsetIdUnorderedSet* rowset_ids) const; |
158 | | |
159 | | // Get the missed versions until the spec_version. |
160 | | Versions get_missed_versions(int64_t spec_version) const; |
161 | | Versions get_missed_versions_unlocked(int64_t spec_version) const; |
162 | | |
163 | | void generate_tablet_meta_copy(TabletMeta& new_tablet_meta, bool cloud_get_rowset_meta) const; |
164 | | void generate_tablet_meta_copy_unlocked(TabletMeta& new_tablet_meta, |
165 | | bool cloud_get_rowset_meta) const; |
166 | | |
167 | 36 | virtual int64_t max_version_unlocked() const { return _tablet_meta->max_version().second; } |
168 | | |
169 | | static TabletSchemaSPtr tablet_schema_with_merged_max_schema_version( |
170 | | const std::vector<RowsetMetaSharedPtr>& rowset_metas); |
171 | | |
172 | | //////////////////////////////////////////////////////////////////////////// |
173 | | // begin MoW functions |
174 | | //////////////////////////////////////////////////////////////////////////// |
175 | | std::vector<RowsetSharedPtr> get_rowset_by_ids( |
176 | | const RowsetIdUnorderedSet* specified_rowset_ids); |
177 | | |
178 | | // Lookup a row with TupleDescriptor and fill Block |
179 | | Status lookup_row_data(const Slice& encoded_key, const RowLocation& row_location, |
180 | | RowsetSharedPtr rowset, OlapReaderStatistics& stats, std::string& values, |
181 | | bool write_to_cache = false); |
182 | | // Lookup the row location of `encoded_key`, the function sets `row_location` on success. |
183 | | // NOTE: the method only works in unique key model with primary key index, you will got a |
184 | | // not supported error in other data model. |
185 | | Status lookup_row_key(const Slice& encoded_key, TabletSchema* latest_schema, bool with_seq_col, |
186 | | const std::vector<RowsetSharedPtr>& specified_rowsets, |
187 | | RowLocation* row_location, int64_t version, |
188 | | std::vector<std::unique_ptr<SegmentCacheHandle>>& segment_caches, |
189 | | RowsetSharedPtr* rowset = nullptr, bool with_rowid = true, |
190 | | std::string* encoded_seq_value = nullptr, |
191 | | OlapReaderStatistics* stats = nullptr, |
192 | | DeleteBitmapPtr tablet_delete_bitmap = nullptr); |
193 | | |
194 | | // calc delete bitmap when flush memtable, use a fake version to calc |
195 | | // For example, cur max version is 5, and we use version 6 to calc but |
196 | | // finally this rowset publish version with 8, we should make up data |
197 | | // for rowset 6-7. Also, if a compaction happens between commit_txn and |
198 | | // publish_txn, we should remove compaction input rowsets' delete_bitmap |
199 | | // and build newly generated rowset's delete_bitmap |
200 | | static Status calc_delete_bitmap( |
201 | | const BaseTabletSPtr& tablet, RowsetSharedPtr rowset, |
202 | | const std::vector<segment_v2::SegmentSharedPtr>& segments, |
203 | | const std::vector<RowsetSharedPtr>& specified_rowsets, DeleteBitmapPtr delete_bitmap, |
204 | | int64_t version, CalcDeleteBitmapToken* token, RowsetWriter* rowset_writer = nullptr, |
205 | | DeleteBitmapPtr tablet_delete_bitmap = nullptr, |
206 | | std::function<void(segment_v2::SegmentSharedPtr, Status)> callback = |
207 | 1 | [](segment_v2::SegmentSharedPtr, Status) {}); |
208 | | |
209 | | Status calc_segment_delete_bitmap(RowsetSharedPtr rowset, |
210 | | const segment_v2::SegmentSharedPtr& seg, |
211 | | const std::vector<RowsetSharedPtr>& specified_rowsets, |
212 | | DeleteBitmapPtr delete_bitmap, int64_t end_version, |
213 | | RowsetWriter* rowset_writer, |
214 | | DeleteBitmapPtr tablet_delete_bitmap = nullptr); |
215 | | |
216 | | Status calc_delete_bitmap_between_segments( |
217 | | TabletSchemaSPtr schema, RowsetId rowset_id, |
218 | | const std::vector<segment_v2::SegmentSharedPtr>& segments, |
219 | | DeleteBitmapPtr delete_bitmap); |
220 | | |
221 | | static Status commit_phase_update_delete_bitmap( |
222 | | const BaseTabletSPtr& tablet, const RowsetSharedPtr& rowset, |
223 | | RowsetIdUnorderedSet& pre_rowset_ids, DeleteBitmapPtr delete_bitmap, |
224 | | const std::vector<segment_v2::SegmentSharedPtr>& segments, int64_t txn_id, |
225 | | CalcDeleteBitmapToken* token, RowsetWriter* rowset_writer = nullptr); |
226 | | |
227 | | static void add_sentinel_mark_to_delete_bitmap(DeleteBitmap* delete_bitmap, |
228 | | const RowsetIdUnorderedSet& rowsetids); |
229 | | |
230 | | Status check_delete_bitmap_correctness(DeleteBitmapPtr delete_bitmap, int64_t max_version, |
231 | | int64_t txn_id, const RowsetIdUnorderedSet& rowset_ids, |
232 | | std::vector<RowsetSharedPtr>* rowsets = nullptr); |
233 | | |
234 | | static const signed char* get_delete_sign_column_data(const vectorized::Block& block, |
235 | | size_t rows_at_least = 0); |
236 | | |
237 | | static Status generate_default_value_block(const TabletSchema& schema, |
238 | | const std::vector<uint32_t>& cids, |
239 | | const std::vector<std::string>& default_values, |
240 | | const vectorized::Block& ref_block, |
241 | | vectorized::Block& default_value_block); |
242 | | |
243 | | static Status generate_new_block_for_partial_update( |
244 | | TabletSchemaSPtr rowset_schema, const PartialUpdateInfo* partial_update_info, |
245 | | const FixedReadPlan& read_plan_ori, const FixedReadPlan& read_plan_update, |
246 | | const std::map<RowsetId, RowsetSharedPtr>& rsid_to_rowset, |
247 | | vectorized::Block* output_block); |
248 | | |
249 | | static Status generate_new_block_for_flexible_partial_update( |
250 | | TabletSchemaSPtr rowset_schema, const PartialUpdateInfo* partial_update_info, |
251 | | std::set<uint32_t>& rids_be_overwritten, const FixedReadPlan& read_plan_ori, |
252 | | const FixedReadPlan& read_plan_update, |
253 | | const std::map<RowsetId, RowsetSharedPtr>& rsid_to_rowset, |
254 | | vectorized::Block* output_block); |
255 | | |
256 | | // We use the TabletSchema from the caller because the TabletSchema in the rowset'meta |
257 | | // may be outdated due to schema change. Also note that the the cids should indicate the indexes |
258 | | // of the columns in the TabletSchema passed in. |
259 | | static Status fetch_value_through_row_column(RowsetSharedPtr input_rowset, |
260 | | const TabletSchema& tablet_schema, uint32_t segid, |
261 | | const std::vector<uint32_t>& rowids, |
262 | | const std::vector<uint32_t>& cids, |
263 | | vectorized::Block& block); |
264 | | |
265 | | static Status fetch_value_by_rowids(RowsetSharedPtr input_rowset, uint32_t segid, |
266 | | const std::vector<uint32_t>& rowids, |
267 | | const TabletColumn& tablet_column, |
268 | | vectorized::MutableColumnPtr& dst); |
269 | | |
270 | | virtual Result<std::unique_ptr<RowsetWriter>> create_transient_rowset_writer( |
271 | | const Rowset& rowset, std::shared_ptr<PartialUpdateInfo> partial_update_info, |
272 | | int64_t txn_expiration = 0) = 0; |
273 | | |
274 | | static Status update_delete_bitmap(const BaseTabletSPtr& self, TabletTxnInfo* txn_info, |
275 | | int64_t txn_id, int64_t txn_expiration = 0, |
276 | | DeleteBitmapPtr tablet_delete_bitmap = nullptr); |
277 | | virtual Status save_delete_bitmap(const TabletTxnInfo* txn_info, int64_t txn_id, |
278 | | DeleteBitmapPtr delete_bitmap, RowsetWriter* rowset_writer, |
279 | | const RowsetIdUnorderedSet& cur_rowset_ids, |
280 | | int64_t lock_id = -1, int64_t next_visible_version = -1) = 0; |
281 | | virtual CalcDeleteBitmapExecutor* calc_delete_bitmap_executor() = 0; |
282 | | |
283 | | void calc_compaction_output_rowset_delete_bitmap( |
284 | | const std::vector<RowsetSharedPtr>& input_rowsets, |
285 | | const RowIdConversion& rowid_conversion, uint64_t start_version, uint64_t end_version, |
286 | | std::set<RowLocation>* missed_rows, |
287 | | std::map<RowsetSharedPtr, std::list<std::pair<RowLocation, RowLocation>>>* location_map, |
288 | | const DeleteBitmap& input_delete_bitmap, DeleteBitmap* output_rowset_delete_bitmap); |
289 | | |
290 | | Status check_rowid_conversion( |
291 | | RowsetSharedPtr dst_rowset, |
292 | | const std::map<RowsetSharedPtr, std::list<std::pair<RowLocation, RowLocation>>>& |
293 | | location_map); |
294 | | |
295 | | static Status update_delete_bitmap_without_lock( |
296 | | const BaseTabletSPtr& self, const RowsetSharedPtr& rowset, |
297 | | const std::vector<RowsetSharedPtr>* specified_base_rowsets = nullptr); |
298 | | |
299 | | using DeleteBitmapKeyRanges = |
300 | | std::vector<std::tuple<DeleteBitmap::BitmapKey, DeleteBitmap::BitmapKey>>; |
301 | | void agg_delete_bitmap_for_stale_rowsets( |
302 | | Version version, DeleteBitmapKeyRanges& remove_delete_bitmap_key_ranges); |
303 | | void check_agg_delete_bitmap_for_stale_rowsets(int64_t& useless_rowset_count, |
304 | | int64_t& useless_rowset_version_count); |
305 | | //////////////////////////////////////////////////////////////////////////// |
306 | | // end MoW functions |
307 | | //////////////////////////////////////////////////////////////////////////// |
308 | | |
309 | | RowsetSharedPtr get_rowset(const RowsetId& rowset_id); |
310 | | |
311 | | std::vector<RowsetSharedPtr> get_snapshot_rowset(bool include_stale_rowset = false) const; |
312 | | |
313 | | virtual void clear_cache() = 0; |
314 | | |
315 | | // Find the first consecutive empty rowsets. output->size() >= limit |
316 | | void calc_consecutive_empty_rowsets(std::vector<RowsetSharedPtr>* empty_rowsets, |
317 | | const std::vector<RowsetSharedPtr>& candidate_rowsets, |
318 | | int64_t limit); |
319 | | |
320 | | void traverse_rowsets(std::function<void(const RowsetSharedPtr&)> visitor, |
321 | 11 | bool include_stale = false) { |
322 | 11 | std::shared_lock rlock(_meta_lock); |
323 | 11 | traverse_rowsets_unlocked(visitor, include_stale); |
324 | 11 | } |
325 | | |
326 | | void traverse_rowsets_unlocked(std::function<void(const RowsetSharedPtr&)> visitor, |
327 | 16 | bool include_stale = false) { |
328 | 85 | for (auto& [v, rs] : _rs_version_map) { |
329 | 85 | visitor(rs); |
330 | 85 | } |
331 | 16 | if (!include_stale) return; |
332 | 81 | for (auto& [v, rs] : _stale_rs_version_map) { |
333 | 81 | visitor(rs); |
334 | 81 | } |
335 | 15 | } |
336 | | |
337 | | Status calc_file_crc(uint32_t* crc_value, int64_t start_version, int64_t end_version, |
338 | | uint32_t* rowset_count, int64_t* file_count); |
339 | | |
340 | | Status show_nested_index_file(std::string* json_meta); |
341 | | |
342 | 12.4k | TabletUid tablet_uid() const { return _tablet_meta->tablet_uid(); } |
343 | 561 | TabletInfo get_tablet_info() const { return TabletInfo(tablet_id(), tablet_uid()); } |
344 | | |
345 | | void get_base_rowset_delete_bitmap_count( |
346 | | uint64_t* max_base_rowset_delete_bitmap_score, |
347 | | int64_t* max_base_rowset_delete_bitmap_score_tablet_id); |
348 | | |
349 | 3 | virtual Status check_delete_bitmap_cache(int64_t txn_id, DeleteBitmap* expected_delete_bitmap) { |
350 | 3 | return Status::OK(); |
351 | 3 | } |
352 | | |
353 | | void prefill_dbm_agg_cache(const RowsetSharedPtr& rowset, int64_t version); |
354 | | void prefill_dbm_agg_cache_after_compaction(const RowsetSharedPtr& output_rowset); |
355 | | |
356 | | protected: |
357 | | // Find the missed versions until the spec_version. |
358 | | // |
359 | | // for example: |
360 | | // [0-4][5-5][8-8][9-9][14-14] |
361 | | // for cloud, if spec_version = 12, it will return [6-7],[10-12] |
362 | | // for local, if spec_version = 12, it will return [6, 6], [7, 7], [10, 10], [11, 11], [12, 12] |
363 | | virtual Versions calc_missed_versions(int64_t spec_version, |
364 | | Versions existing_versions) const = 0; |
365 | | |
366 | | void _print_missed_versions(const Versions& missed_versions) const; |
367 | | bool _reconstruct_version_tracker_if_necessary(); |
368 | | |
369 | | static void _rowset_ids_difference(const RowsetIdUnorderedSet& cur, |
370 | | const RowsetIdUnorderedSet& pre, |
371 | | RowsetIdUnorderedSet* to_add, RowsetIdUnorderedSet* to_del); |
372 | | |
373 | | Status _capture_consistent_rowsets_unlocked(const std::vector<Version>& version_path, |
374 | | std::vector<RowsetSharedPtr>* rowsets) const; |
375 | | |
376 | | Status sort_block(vectorized::Block& in_block, vectorized::Block& output_block); |
377 | | |
378 | | mutable std::shared_mutex _meta_lock; |
379 | | TimestampedVersionTracker _timestamped_version_tracker; |
380 | | // After version 0.13, all newly created rowsets are saved in _rs_version_map. |
381 | | // And if rowset being compacted, the old rowsets will be saved in _stale_rs_version_map; |
382 | | std::unordered_map<Version, RowsetSharedPtr, HashOfVersion> _rs_version_map; |
383 | | // This variable _stale_rs_version_map is used to record these rowsets which are be compacted. |
384 | | // These _stale rowsets are been removed when rowsets' pathVersion is expired, |
385 | | // this policy is judged and computed by TimestampedVersionTracker. |
386 | | std::unordered_map<Version, RowsetSharedPtr, HashOfVersion> _stale_rs_version_map; |
387 | | const TabletMetaSharedPtr _tablet_meta; |
388 | | TabletSchemaSPtr _max_version_schema; |
389 | | |
390 | | // `_alter_failed` is used to indicate whether the tablet failed to perform a schema change |
391 | | std::atomic<bool> _alter_failed = false; |
392 | | |
393 | | // metrics of this tablet |
394 | | std::shared_ptr<MetricEntity> _metric_entity; |
395 | | |
396 | | protected: |
397 | | std::timed_mutex _schema_change_lock; |
398 | | |
399 | | public: |
400 | | IntCounter* query_scan_bytes = nullptr; |
401 | | IntCounter* query_scan_rows = nullptr; |
402 | | IntCounter* query_scan_count = nullptr; |
403 | | IntCounter* flush_bytes = nullptr; |
404 | | IntCounter* flush_finish_count = nullptr; |
405 | | std::atomic<int64_t> published_count = 0; |
406 | | std::atomic<int64_t> read_block_count = 0; |
407 | | std::atomic<int64_t> write_count = 0; |
408 | | std::atomic<int64_t> compaction_count = 0; |
409 | | |
410 | | CompactionStage compaction_stage = CompactionStage::NOT_SCHEDULED; |
411 | | std::mutex sample_info_lock; |
412 | | std::vector<CompactionSampleInfo> sample_infos; |
413 | | Status last_compaction_status = Status::OK(); |
414 | | }; |
415 | | |
416 | | } /* namespace doris */ |