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