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