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