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 | | #include "cloud/cloud_tablet_mgr.h" |
19 | | |
20 | | #include <bthread/countdown_event.h> |
21 | | |
22 | | #include <chrono> |
23 | | |
24 | | #include "cloud/cloud_cluster_info.h" |
25 | | #include "cloud/cloud_meta_mgr.h" |
26 | | #include "cloud/cloud_storage_engine.h" |
27 | | #include "cloud/cloud_tablet.h" |
28 | | #include "cloud/config.h" |
29 | | #include "common/metrics/doris_metrics.h" |
30 | | #include "common/status.h" |
31 | | #include "cpp/lru_cache.h" |
32 | | #include "cpp/sync_point.h" |
33 | | #include "runtime/memory/cache_policy.h" |
34 | | #include "util/debug_points.h" |
35 | | #include "util/stack_util.h" |
36 | | |
37 | | namespace doris { |
38 | | uint64_t g_tablet_report_inactive_duration_ms = 0; |
39 | | bvar::Adder<uint64_t> g_base_compaction_not_frozen_tablet_num( |
40 | | "base_compaction_not_frozen_tablet_num"); |
41 | | bvar::Adder<uint64_t> g_cumu_compaction_not_frozen_tablet_num( |
42 | | "cumu_compaction_not_frozen_tablet_num"); |
43 | | namespace { |
44 | | |
45 | | // port from |
46 | | // https://github.com/golang/groupcache/blob/master/singleflight/singleflight.go |
47 | | template <typename Key, typename Val> |
48 | | class SingleFlight { |
49 | | public: |
50 | 1 | SingleFlight() = default; |
51 | | |
52 | | SingleFlight(const SingleFlight&) = delete; |
53 | | void operator=(const SingleFlight&) = delete; |
54 | | |
55 | | using Loader = std::function<Val(const Key&)>; |
56 | | |
57 | | // Do executes and returns the results of the given function, making |
58 | | // sure that only one execution is in-flight for a given key at a |
59 | | // time. If a duplicate comes in, the duplicate caller waits for the |
60 | | // original to complete and receives the same results. |
61 | 12 | Val load(const Key& key, Loader loader) { |
62 | 12 | std::unique_lock lock(_call_map_mtx); |
63 | | |
64 | 12 | auto it = _call_map.find(key); |
65 | 12 | if (it != _call_map.end()) { |
66 | 1 | auto call = it->second; |
67 | 1 | lock.unlock(); |
68 | 1 | if (int ec = call->event.wait(); ec != 0) { |
69 | 0 | throw std::system_error(std::error_code(ec, std::system_category()), |
70 | 0 | "CountdownEvent wait failed"); |
71 | 0 | } |
72 | 1 | return call->val; |
73 | 1 | } |
74 | 11 | auto call = std::make_shared<Call>(); |
75 | 11 | _call_map.emplace(key, call); |
76 | 11 | lock.unlock(); |
77 | | |
78 | 11 | call->val = loader(key); |
79 | 11 | call->event.signal(); |
80 | | |
81 | 11 | lock.lock(); |
82 | 11 | _call_map.erase(key); |
83 | 11 | lock.unlock(); |
84 | | |
85 | 11 | return call->val; |
86 | 12 | } |
87 | | |
88 | | private: |
89 | | // `Call` is an in-flight or completed `load` call |
90 | | struct Call { |
91 | | bthread::CountdownEvent event; |
92 | | Val val; |
93 | | }; |
94 | | |
95 | | std::mutex _call_map_mtx; |
96 | | std::unordered_map<Key, std::shared_ptr<Call>> _call_map; |
97 | | }; |
98 | | |
99 | | // tablet_id -> load tablet function |
100 | | SingleFlight<int64_t, Result<std::shared_ptr<CloudTablet>>> s_singleflight_load_tablet; |
101 | | |
102 | | } // namespace |
103 | | |
104 | | // tablet_id -> cached tablet |
105 | | // This map owns all cached tablets. The lifetime of tablet can be longer than the LRU handle. |
106 | | // It's also used for scenarios where users want to access the tablet by `tablet_id` without changing the LRU order. |
107 | | // TODO(plat1ko): multi shard to increase concurrency |
108 | | class CloudTabletMgr::TabletMap { |
109 | | public: |
110 | 3 | void put(std::shared_ptr<CloudTablet> tablet) { |
111 | 3 | std::lock_guard lock(_mtx); |
112 | 3 | _map[tablet->tablet_id()] = std::move(tablet); |
113 | 3 | } |
114 | | |
115 | 1 | void erase(CloudTablet* tablet) { |
116 | 1 | std::lock_guard lock(_mtx); |
117 | 1 | auto it = _map.find(tablet->tablet_id()); |
118 | | // According to the implementation of `LRUCache`, `deleter` may be called after a tablet |
119 | | // with same tablet id insert into cache and `TabletMap`. So we MUST check if the tablet |
120 | | // instance to be erased is the same one in the map. |
121 | 1 | if (it != _map.end() && it->second.get() == tablet) { |
122 | 1 | _map.erase(it); |
123 | 1 | } |
124 | 1 | } |
125 | | |
126 | 0 | std::shared_ptr<CloudTablet> get(int64_t tablet_id) { |
127 | 0 | std::lock_guard lock(_mtx); |
128 | 0 | if (auto it = _map.find(tablet_id); it != _map.end()) { |
129 | 0 | return it->second; |
130 | 0 | } |
131 | 0 | return nullptr; |
132 | 0 | } |
133 | | |
134 | 5 | size_t size() { return _map.size(); } |
135 | | |
136 | 5 | void traverse(std::function<void(const std::shared_ptr<CloudTablet>&)> visitor) { |
137 | 5 | std::lock_guard lock(_mtx); |
138 | 5 | for (auto& [_, tablet] : _map) { |
139 | 5 | visitor(tablet); |
140 | 5 | } |
141 | 5 | } |
142 | | |
143 | | private: |
144 | | std::mutex _mtx; |
145 | | std::unordered_map<int64_t, std::shared_ptr<CloudTablet>> _map; |
146 | | }; |
147 | | |
148 | | // TODO(plat1ko): Prune cache |
149 | | CloudTabletMgr::CloudTabletMgr(CloudStorageEngine& engine) |
150 | 160 | : _engine(engine), |
151 | 160 | _tablet_map(std::make_unique<TabletMap>()), |
152 | 160 | _cache(std::make_unique<LRUCachePolicy>( |
153 | 160 | CachePolicy::CacheType::CLOUD_TABLET_CACHE, config::tablet_cache_capacity, |
154 | 160 | LRUCacheType::NUMBER, /*sweep time*/ 0, config::tablet_cache_shards, |
155 | 160 | /*element_count_capacity*/ 0, /*enable_prune*/ false, |
156 | 160 | /*is_lru_k*/ false)) {} |
157 | | |
158 | 160 | CloudTabletMgr::~CloudTabletMgr() = default; |
159 | | |
160 | 5 | void set_tablet_access_time_ms(CloudTablet* tablet) { |
161 | 5 | using namespace std::chrono; |
162 | 5 | int64_t now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
163 | 5 | tablet->last_access_time_ms = now; |
164 | 5 | } |
165 | | |
166 | | Result<std::shared_ptr<CloudTablet>> CloudTabletMgr::get_tablet(int64_t tablet_id, bool warmup_data, |
167 | | bool sync_delete_bitmap, |
168 | | SyncRowsetStats* sync_stats, |
169 | | bool force_use_only_cached, |
170 | 13 | bool cache_on_miss) { |
171 | 13 | DBUG_EXECUTE_IF("CloudTabletMgr::get_tablet.block", DBUG_BLOCK); |
172 | | // LRU value type. `Value`'s lifetime MUST NOT be longer than `CloudTabletMgr` |
173 | 13 | class Value : public LRUCacheValueBase { |
174 | 13 | public: |
175 | 13 | Value(const std::shared_ptr<CloudTablet>& tablet, TabletMap& tablet_map) |
176 | 13 | : tablet(tablet), tablet_map(tablet_map) {} |
177 | 13 | ~Value() override { tablet_map.erase(tablet.get()); } |
178 | | |
179 | | // FIXME(plat1ko): The ownership of tablet seems to belong to 'TabletMap', while `Value` |
180 | | // only requires a reference. |
181 | 13 | std::shared_ptr<CloudTablet> tablet; |
182 | 13 | TabletMap& tablet_map; |
183 | 13 | }; |
184 | | |
185 | 13 | VLOG_DEBUG << "get_tablet tablet_id=" << tablet_id << " stack: " << get_stack_trace(); |
186 | | |
187 | 13 | auto tablet_id_str = std::to_string(tablet_id); |
188 | 13 | CacheKey key(tablet_id_str); |
189 | 13 | auto* handle = _cache->lookup(key); |
190 | | |
191 | 13 | if (handle == nullptr) { |
192 | 12 | if (force_use_only_cached) { |
193 | 0 | LOG(INFO) << "tablet=" << tablet_id |
194 | 0 | << "does not exists in local tablet cache, because param " |
195 | 0 | "force_use_only_cached=true, " |
196 | 0 | "treat it as an error"; |
197 | 0 | return ResultError(Status::InternalError( |
198 | 0 | "tablet={} does not exists in local tablet cache, because param " |
199 | 0 | "force_use_only_cached=true, " |
200 | 0 | "treat it as an error", |
201 | 0 | tablet_id)); |
202 | 0 | } |
203 | 12 | TEST_SYNC_POINT("CloudTabletMgr::get_tablet.not_found_in_cache"); |
204 | 12 | if (sync_stats) { |
205 | 0 | ++sync_stats->tablet_meta_cache_miss; |
206 | 0 | } |
207 | | // Insert into cache and tablet_map inside SingleFlight lambda to ensure |
208 | | // only the leader caller does this. Moving these outside the lambda causes |
209 | | // a race condition: when multiple concurrent callers share the same CloudTablet* |
210 | | // from SingleFlight, each creates a competing LRU cache entry. Delayed Value |
211 | | // destructors then erase the tablet_map entry (the raw pointer safety check |
212 | | // passes since all callers share the same pointer), and the tablet permanently |
213 | | // disappears from tablet_map. Subsequent get_tablet() calls hit the LRU cache |
214 | | // directly (cache hit path) which never re-inserts into tablet_map, making the |
215 | | // tablet invisible to the compaction scheduler. |
216 | 12 | auto load_tablet = [this, &key, warmup_data, sync_delete_bitmap, sync_stats, cache_on_miss]( |
217 | 12 | int64_t tablet_id) -> Result<std::shared_ptr<CloudTablet>> { |
218 | 11 | TabletMetaSharedPtr tablet_meta; |
219 | 11 | auto start = std::chrono::steady_clock::now(); |
220 | 11 | auto st = _engine.meta_mgr().get_tablet_meta(tablet_id, &tablet_meta); |
221 | 11 | auto end = std::chrono::steady_clock::now(); |
222 | 11 | if (sync_stats) { |
223 | 0 | sync_stats->get_remote_tablet_meta_rpc_ns += |
224 | 0 | std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count(); |
225 | 0 | } |
226 | 11 | if (!st.ok()) { |
227 | 10 | LOG(WARNING) << "failed to tablet " << tablet_id << ": " << st; |
228 | 10 | return ResultError(st); |
229 | 10 | } |
230 | | |
231 | 1 | auto tablet = std::make_shared<CloudTablet>(_engine, std::move(tablet_meta)); |
232 | | // MUST sync stats to let compaction scheduler work correctly |
233 | 1 | SyncOptions options; |
234 | 1 | options.warmup_delta_data = warmup_data; |
235 | 1 | options.sync_delete_bitmap = sync_delete_bitmap; |
236 | 1 | st = _engine.meta_mgr().sync_tablet_rowsets(tablet.get(), options, sync_stats); |
237 | 1 | if (!st.ok()) { |
238 | 0 | LOG(WARNING) << "failed to sync tablet " << tablet_id << ": " << st; |
239 | 0 | return ResultError(st); |
240 | 0 | } |
241 | | |
242 | 1 | if (!cache_on_miss) { |
243 | 0 | set_tablet_access_time_ms(tablet.get()); |
244 | 0 | return tablet; |
245 | 0 | } |
246 | | |
247 | 1 | auto value = std::make_unique<Value>(tablet, *_tablet_map); |
248 | 1 | auto* insert_handle = _cache->insert(key, value.release(), 1, sizeof(CloudTablet), |
249 | 1 | CachePriority::NORMAL); |
250 | 1 | auto ret = std::shared_ptr<CloudTablet>(tablet.get(), |
251 | 1 | [this, insert_handle](CloudTablet* tablet_ptr) { |
252 | 1 | set_tablet_access_time_ms(tablet_ptr); |
253 | 1 | _cache->release(insert_handle); |
254 | 1 | }); |
255 | 1 | _tablet_map->put(std::move(tablet)); |
256 | 1 | return ret; |
257 | 1 | }; |
258 | | |
259 | 12 | auto load_result = s_singleflight_load_tablet.load(tablet_id, std::move(load_tablet)); |
260 | 12 | if (!load_result.has_value()) { |
261 | 10 | return ResultError(Status::InternalError("failed to get tablet {}, msg={}", tablet_id, |
262 | 10 | load_result.error())); |
263 | 10 | } |
264 | 2 | auto tablet = load_result.value(); |
265 | 2 | set_tablet_access_time_ms(tablet.get()); |
266 | 2 | return tablet; |
267 | 12 | } |
268 | 1 | if (sync_stats) { |
269 | 0 | ++sync_stats->tablet_meta_cache_hit; |
270 | 0 | } |
271 | 1 | CloudTablet* tablet_raw_ptr = reinterpret_cast<Value*>(_cache->value(handle))->tablet.get(); |
272 | 1 | set_tablet_access_time_ms(tablet_raw_ptr); |
273 | 1 | auto tablet = std::shared_ptr<CloudTablet>(tablet_raw_ptr, [this, handle](CloudTablet* tablet) { |
274 | 1 | set_tablet_access_time_ms(tablet); |
275 | 1 | _cache->release(handle); |
276 | 1 | }); |
277 | 1 | return tablet; |
278 | 13 | } |
279 | | |
280 | 0 | bool CloudTabletMgr::peek_tablet_meta(int64_t tablet_id, TabletMetaSharedPtr* tablet_meta) { |
281 | 0 | if (tablet_meta == nullptr) { |
282 | 0 | return false; |
283 | 0 | } |
284 | 0 | auto tablet = _tablet_map->get(tablet_id); |
285 | 0 | if (!tablet) { |
286 | 0 | return false; |
287 | 0 | } |
288 | 0 | *tablet_meta = tablet->tablet_meta(); |
289 | 0 | return true; |
290 | 0 | } |
291 | | |
292 | 0 | void CloudTabletMgr::erase_tablet(int64_t tablet_id) { |
293 | 0 | auto tablet_id_str = std::to_string(tablet_id); |
294 | 0 | CacheKey key(tablet_id_str.data(), tablet_id_str.size()); |
295 | 0 | _cache->erase(key); |
296 | 0 | } |
297 | | |
298 | 0 | void CloudTabletMgr::vacuum_stale_rowsets(const CountDownLatch& stop_latch) { |
299 | 0 | LOG_INFO("begin to vacuum stale rowsets"); |
300 | 0 | std::vector<std::shared_ptr<CloudTablet>> tablets_to_vacuum; |
301 | 0 | tablets_to_vacuum.reserve(_tablet_map->size()); |
302 | 0 | _tablet_map->traverse([&tablets_to_vacuum](auto&& t) { |
303 | 0 | if (t->has_stale_rowsets()) { |
304 | 0 | tablets_to_vacuum.push_back(t); |
305 | 0 | } |
306 | 0 | }); |
307 | 0 | int num_vacuumed = 0; |
308 | 0 | for (auto& t : tablets_to_vacuum) { |
309 | 0 | if (stop_latch.count() <= 0) { |
310 | 0 | break; |
311 | 0 | } |
312 | | |
313 | 0 | num_vacuumed += t->delete_expired_stale_rowsets(); |
314 | 0 | } |
315 | 0 | LOG_INFO("finish vacuum stale rowsets") |
316 | 0 | .tag("num_vacuumed", num_vacuumed) |
317 | 0 | .tag("num_tablets", tablets_to_vacuum.size()); |
318 | |
|
319 | 0 | { |
320 | 0 | LOG_INFO("begin to remove unused rowsets"); |
321 | 0 | std::vector<std::shared_ptr<CloudTablet>> tablets_to_remove_unused_rowsets; |
322 | 0 | tablets_to_remove_unused_rowsets.reserve(_tablet_map->size()); |
323 | 0 | _tablet_map->traverse([&tablets_to_remove_unused_rowsets](auto&& t) { |
324 | 0 | if (t->need_remove_unused_rowsets()) { |
325 | 0 | tablets_to_remove_unused_rowsets.push_back(t); |
326 | 0 | } |
327 | 0 | }); |
328 | 0 | for (auto& t : tablets_to_remove_unused_rowsets) { |
329 | 0 | t->remove_unused_rowsets(); |
330 | 0 | } |
331 | 0 | LOG_INFO("finish remove unused rowsets") |
332 | 0 | .tag("num_tablets", tablets_to_remove_unused_rowsets.size()); |
333 | 0 | if (config::enable_check_agg_and_remove_pre_rowsets_delete_bitmap) { |
334 | 0 | int64_t max_useless_rowset_count = 0; |
335 | 0 | int64_t tablet_id_with_max_useless_rowset_count = 0; |
336 | 0 | int64_t max_useless_rowset_version_count = 0; |
337 | 0 | int64_t tablet_id_with_max_useless_rowset_version_count = 0; |
338 | 0 | OlapStopWatch watch; |
339 | 0 | _tablet_map->traverse([&](auto&& tablet) { |
340 | 0 | int64_t useless_rowset_count = 0; |
341 | 0 | int64_t useless_rowset_version_count = 0; |
342 | 0 | tablet->check_agg_delete_bitmap_for_stale_rowsets(useless_rowset_count, |
343 | 0 | useless_rowset_version_count); |
344 | 0 | if (useless_rowset_count > max_useless_rowset_count) { |
345 | 0 | max_useless_rowset_count = useless_rowset_count; |
346 | 0 | tablet_id_with_max_useless_rowset_count = tablet->tablet_id(); |
347 | 0 | } |
348 | 0 | if (useless_rowset_version_count > max_useless_rowset_version_count) { |
349 | 0 | max_useless_rowset_version_count = useless_rowset_version_count; |
350 | 0 | tablet_id_with_max_useless_rowset_version_count = tablet->tablet_id(); |
351 | 0 | } |
352 | 0 | }); |
353 | 0 | g_max_rowsets_with_useless_delete_bitmap.set_value(max_useless_rowset_count); |
354 | 0 | g_max_rowsets_with_useless_delete_bitmap_version.set_value( |
355 | 0 | max_useless_rowset_version_count); |
356 | 0 | LOG(INFO) << "finish check_agg_delete_bitmap_for_stale_rowsets, cost(us)=" |
357 | 0 | << watch.get_elapse_time_us() |
358 | 0 | << ". max useless rowset count=" << max_useless_rowset_count |
359 | 0 | << ", tablet_id=" << tablet_id_with_max_useless_rowset_count |
360 | 0 | << ", max useless rowset version count=" << max_useless_rowset_version_count |
361 | 0 | << ", tablet_id=" << tablet_id_with_max_useless_rowset_version_count; |
362 | 0 | } |
363 | 0 | } |
364 | 0 | { |
365 | 0 | _tablet_map->traverse( |
366 | 0 | [](auto&& tablet) { tablet->clear_unused_visible_pending_rowsets(); }); |
367 | 0 | } |
368 | 0 | } |
369 | | |
370 | 4 | std::vector<std::weak_ptr<CloudTablet>> CloudTabletMgr::get_weak_tablets() { |
371 | 4 | std::vector<std::weak_ptr<CloudTablet>> weak_tablets; |
372 | 4 | weak_tablets.reserve(_tablet_map->size()); |
373 | 4 | _tablet_map->traverse([&weak_tablets](auto& t) { weak_tablets.push_back(t); }); |
374 | 4 | return weak_tablets; |
375 | 4 | } |
376 | | |
377 | 0 | void CloudTabletMgr::sync_tablets(const CountDownLatch& stop_latch) { |
378 | 0 | LOG_INFO("begin to sync tablets"); |
379 | 0 | int64_t last_sync_time_bound = ::time(nullptr) - config::tablet_sync_interval_s; |
380 | |
|
381 | 0 | auto weak_tablets = get_weak_tablets(); |
382 | | |
383 | | // sort by last_sync_time |
384 | 0 | static auto cmp = [](const auto& a, const auto& b) { return a.first < b.first; }; |
385 | 0 | std::multiset<std::pair<int64_t, std::weak_ptr<CloudTablet>>, decltype(cmp)> |
386 | 0 | sync_time_tablet_set(cmp); |
387 | |
|
388 | 0 | for (auto& weak_tablet : weak_tablets) { |
389 | 0 | if (auto tablet = weak_tablet.lock()) { |
390 | 0 | int64_t last_sync_time = tablet->last_sync_time_s; |
391 | 0 | if (last_sync_time <= last_sync_time_bound) { |
392 | 0 | sync_time_tablet_set.emplace(last_sync_time, weak_tablet); |
393 | 0 | } |
394 | 0 | } |
395 | 0 | } |
396 | |
|
397 | 0 | int num_sync = 0; |
398 | 0 | for (auto&& [_, weak_tablet] : sync_time_tablet_set) { |
399 | 0 | if (stop_latch.count() <= 0) { |
400 | 0 | break; |
401 | 0 | } |
402 | | |
403 | 0 | if (auto tablet = weak_tablet.lock()) { |
404 | 0 | if (tablet->last_sync_time_s > last_sync_time_bound) { |
405 | 0 | continue; |
406 | 0 | } |
407 | | |
408 | 0 | ++num_sync; |
409 | 0 | auto st = tablet->sync_meta(); |
410 | 0 | if (!st) { |
411 | 0 | LOG_WARNING("failed to sync tablet meta {}", tablet->tablet_id()).error(st); |
412 | 0 | if (st.is<ErrorCode::NOT_FOUND>()) { |
413 | 0 | continue; |
414 | 0 | } |
415 | 0 | } |
416 | 0 | SyncOptions options; |
417 | 0 | options.query_version = -1; |
418 | 0 | options.merge_schema = true; |
419 | 0 | st = tablet->sync_rowsets(options); |
420 | 0 | if (!st) { |
421 | 0 | LOG_WARNING("failed to sync tablet rowsets {}", tablet->tablet_id()).error(st); |
422 | 0 | } |
423 | 0 | } |
424 | 0 | } |
425 | 0 | LOG_INFO("finish sync tablets").tag("num_sync", num_sync); |
426 | 0 | } |
427 | | |
428 | | Status CloudTabletMgr::get_topn_tablets_to_compact( |
429 | | int n, CompactionType compaction_type, const std::function<bool(CloudTablet*)>& filter_out, |
430 | 4 | std::vector<std::shared_ptr<CloudTablet>>* tablets, int64_t* max_score) { |
431 | 4 | DCHECK(compaction_type == CompactionType::BASE_COMPACTION || |
432 | 4 | compaction_type == CompactionType::CUMULATIVE_COMPACTION); |
433 | 4 | *max_score = 0; |
434 | 4 | int64_t max_score_tablet_id = 0; |
435 | | // clang-format off |
436 | 4 | auto score = [compaction_type](CloudTablet* t) { |
437 | 4 | return compaction_type == CompactionType::BASE_COMPACTION ? t->get_cloud_base_compaction_score() |
438 | 4 | : compaction_type == CompactionType::CUMULATIVE_COMPACTION ? t->get_cloud_cumu_compaction_score() |
439 | 1 | : 0; |
440 | 4 | }; |
441 | | |
442 | 4 | using namespace std::chrono; |
443 | 4 | auto now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
444 | 4 | auto skip = [now, compaction_type](CloudTablet* t) { |
445 | 3 | auto* cloud_cluster_info = static_cast<CloudClusterInfo*>(ExecEnv::GetInstance()->cluster_info()); |
446 | | |
447 | 3 | if (config::enable_standby_passive_compaction && cloud_cluster_info->is_in_standby()) { |
448 | 0 | if (t->fetch_add_approximate_num_rowsets(0) < config::max_tablet_version_num * config::standby_compaction_version_ratio) { |
449 | 0 | return true; |
450 | 0 | } |
451 | 0 | } |
452 | | |
453 | | // Compaction read-write separation: skip tablets that should be compacted by other clusters. |
454 | | // Placed after standby check so standby invariants (version count threshold) are preserved. |
455 | 3 | if (cloud_cluster_info->should_skip_compaction(t)) { |
456 | 0 | return true; |
457 | 0 | } |
458 | | |
459 | 3 | int32_t max_version_config = t->max_version_config(); |
460 | 3 | if (compaction_type == CompactionType::BASE_COMPACTION) { |
461 | 2 | bool is_recent_failure = now - t->last_base_compaction_failure_time() < config::min_compaction_failure_interval_ms; |
462 | 2 | bool is_frozen = (now - t->last_load_time_ms > config::compaction_load_max_freeze_interval_s * 1000 |
463 | 2 | && now - t->last_base_compaction_success_time_ms < config::base_compaction_freeze_interval_s * 1000 |
464 | 2 | && t->fetch_add_approximate_num_rowsets(0) < max_version_config / 2); |
465 | 2 | g_base_compaction_not_frozen_tablet_num << !is_frozen; |
466 | 2 | return is_recent_failure || is_frozen; |
467 | 2 | } |
468 | | |
469 | | // If tablet has too many rowsets but not be compacted for a long time, compaction should be performed |
470 | | // regardless of whether there is a load job recently. |
471 | 1 | bool is_recent_failure = now - t->last_cumu_compaction_failure_time() < config::min_compaction_failure_interval_ms; |
472 | 1 | bool is_recent_no_suitable_version = now - t->last_cumu_no_suitable_version_ms < config::min_compaction_failure_interval_ms; |
473 | 1 | bool is_frozen = (now - t->last_load_time_ms > config::compaction_load_max_freeze_interval_s * 1000 |
474 | 1 | && now - t->last_cumu_compaction_success_time_ms < config::cumu_compaction_interval_s * 1000 |
475 | 1 | && t->fetch_add_approximate_num_rowsets(0) < max_version_config / 2); |
476 | 1 | g_cumu_compaction_not_frozen_tablet_num << !is_frozen; |
477 | 1 | return is_recent_failure || is_recent_no_suitable_version || is_frozen; |
478 | 3 | }; |
479 | | // We don't schedule tablets that are disabled for compaction |
480 | 4 | auto disable = [](CloudTablet* t) { return t->tablet_meta()->tablet_schema()->disable_auto_compaction(); }; |
481 | | |
482 | 4 | auto [num_filtered, num_disabled, num_skipped] = std::make_tuple(0, 0, 0); |
483 | | |
484 | 4 | auto weak_tablets = get_weak_tablets(); |
485 | 4 | std::vector<std::pair<std::shared_ptr<CloudTablet>, int64_t>> buf; |
486 | 4 | buf.reserve(n + 1); |
487 | 4 | for (auto& weak_tablet : weak_tablets) { |
488 | 4 | auto t = weak_tablet.lock(); |
489 | 4 | if (t == nullptr) { continue; } |
490 | | |
491 | 4 | int64_t s = score(t.get()); |
492 | 4 | if (s <= 0) { continue; } |
493 | 3 | if (s > *max_score) { |
494 | 3 | max_score_tablet_id = t->tablet_id(); |
495 | 3 | *max_score = s; |
496 | 3 | } |
497 | | |
498 | 3 | if (filter_out(t.get())) { ++num_filtered; continue; } |
499 | 3 | if (disable(t.get())) { ++num_disabled; continue; } |
500 | 3 | if (skip(t.get())) { ++num_skipped; continue; } |
501 | | |
502 | 2 | buf.emplace_back(std::move(t), s); |
503 | 2 | std::sort(buf.begin(), buf.end(), [](auto& a, auto& b) { return a.second > b.second; }); |
504 | 2 | if (buf.size() > n) { buf.pop_back(); } |
505 | 2 | } |
506 | | |
507 | 4 | LOG_EVERY_N(INFO, 1000) << "get_topn_compaction_score, n=" << n << " type=" << compaction_type |
508 | 1 | << " num_tablets=" << weak_tablets.size() << " num_skipped=" << num_skipped |
509 | 1 | << " num_disabled=" << num_disabled << " num_filtered=" << num_filtered |
510 | 1 | << " max_score=" << *max_score << " max_score_tablet=" << max_score_tablet_id |
511 | 1 | << " tablets=[" << [&buf] { std::stringstream ss; for (auto& i : buf) ss << i.first->tablet_id() << ":" << i.second << ","; return ss.str(); }() << "]" |
512 | 4 | ; |
513 | | // clang-format on |
514 | | |
515 | 4 | tablets->clear(); |
516 | 4 | tablets->reserve(n + 1); |
517 | 4 | for (auto& [t, _] : buf) { |
518 | 2 | tablets->emplace_back(std::move(t)); |
519 | 2 | } |
520 | | |
521 | 4 | return Status::OK(); |
522 | 4 | } |
523 | | |
524 | | void CloudTabletMgr::build_all_report_tablets_info(std::map<TTabletId, TTablet>* tablets_info, |
525 | 0 | uint64_t* tablet_num) { |
526 | 0 | DCHECK(tablets_info != nullptr); |
527 | 0 | VLOG_NOTICE << "begin to build all report cloud tablets info"; |
528 | |
|
529 | 0 | HistogramStat tablet_version_num_hist; |
530 | |
|
531 | 0 | auto handler = [&](const std::weak_ptr<CloudTablet>& tablet_wk) { |
532 | 0 | auto tablet = tablet_wk.lock(); |
533 | 0 | if (!tablet) return; |
534 | 0 | (*tablet_num)++; |
535 | 0 | TTabletInfo tablet_info; |
536 | 0 | tablet->build_tablet_report_info(&tablet_info); |
537 | 0 | using namespace std::chrono; |
538 | 0 | int64_t now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
539 | 0 | if (now - g_tablet_report_inactive_duration_ms < tablet->last_access_time_ms) { |
540 | | // the tablet is still being accessed and used in recently, so not report it |
541 | 0 | return; |
542 | 0 | } |
543 | 0 | auto& t_tablet = (*tablets_info)[tablet->tablet_id()]; |
544 | | // On the cloud, a specific BE has only one tablet replica; |
545 | | // there are no multiple replicas for a specific BE. |
546 | | // This is only to reuse the non-cloud report protocol. |
547 | 0 | tablet_version_num_hist.add(tablet_info.total_version_count); |
548 | 0 | t_tablet.tablet_infos.emplace_back(std::move(tablet_info)); |
549 | 0 | }; |
550 | |
|
551 | 0 | auto weak_tablets = get_weak_tablets(); |
552 | 0 | std::for_each(weak_tablets.begin(), weak_tablets.end(), handler); |
553 | |
|
554 | 0 | DorisMetrics::instance()->tablet_version_num_distribution->set_histogram( |
555 | 0 | tablet_version_num_hist); |
556 | 0 | LOG(INFO) << "success to build all cloud report tablets info. all_tablet_count=" << *tablet_num |
557 | 0 | << " exceed drop time limit count=" << tablets_info->size(); |
558 | 0 | } |
559 | | |
560 | 0 | void CloudTabletMgr::get_tablet_info(int64_t num_tablets, std::vector<TabletInfo>* tablets_info) { |
561 | 0 | auto weak_tablets = get_weak_tablets(); |
562 | 0 | for (auto& weak_tablet : weak_tablets) { |
563 | 0 | auto tablet = weak_tablet.lock(); |
564 | 0 | if (tablet == nullptr) { |
565 | 0 | continue; |
566 | 0 | } |
567 | 0 | if (tablets_info->size() >= num_tablets) { |
568 | 0 | return; |
569 | 0 | } |
570 | 0 | tablets_info->push_back(tablet->get_tablet_info()); |
571 | 0 | } |
572 | 0 | } |
573 | | |
574 | | void CloudTabletMgr::get_topn_tablet_delete_bitmap_score( |
575 | 0 | uint64_t* max_delete_bitmap_score, uint64_t* max_base_rowset_delete_bitmap_score) { |
576 | 0 | int64_t max_delete_bitmap_score_tablet_id = 0; |
577 | 0 | OlapStopWatch watch; |
578 | 0 | uint64_t total_delete_map_count = 0; |
579 | 0 | int64_t max_base_rowset_delete_bitmap_score_tablet_id = 0; |
580 | 0 | int n = config::check_tablet_delete_bitmap_score_top_n; |
581 | 0 | std::vector<std::pair<std::shared_ptr<CloudTablet>, int64_t>> buf; |
582 | 0 | buf.reserve(n + 1); |
583 | 0 | auto handler = [&](const std::weak_ptr<CloudTablet>& tablet_wk) { |
584 | 0 | auto t = tablet_wk.lock(); |
585 | 0 | if (!t) return; |
586 | 0 | uint64_t delete_bitmap_count = |
587 | 0 | t.get()->tablet_meta()->delete_bitmap().get_delete_bitmap_count(); |
588 | 0 | total_delete_map_count += delete_bitmap_count; |
589 | 0 | if (delete_bitmap_count > *max_delete_bitmap_score) { |
590 | 0 | max_delete_bitmap_score_tablet_id = t->tablet_id(); |
591 | 0 | *max_delete_bitmap_score = delete_bitmap_count; |
592 | 0 | } |
593 | 0 | buf.emplace_back(std::move(t), delete_bitmap_count); |
594 | 0 | std::sort(buf.begin(), buf.end(), [](auto& a, auto& b) { return a.second > b.second; }); |
595 | 0 | if (buf.size() > n) { |
596 | 0 | buf.pop_back(); |
597 | 0 | } |
598 | 0 | }; |
599 | 0 | auto weak_tablets = get_weak_tablets(); |
600 | 0 | std::for_each(weak_tablets.begin(), weak_tablets.end(), handler); |
601 | 0 | for (auto& [t, _] : buf) { |
602 | 0 | t->get_base_rowset_delete_bitmap_count(max_base_rowset_delete_bitmap_score, |
603 | 0 | &max_base_rowset_delete_bitmap_score_tablet_id); |
604 | 0 | } |
605 | 0 | std::stringstream ss; |
606 | 0 | for (auto& i : buf) { |
607 | 0 | ss << i.first->tablet_id() << ": " << i.second << ", "; |
608 | 0 | } |
609 | 0 | LOG(INFO) << "get_topn_tablet_delete_bitmap_score, n=" << n |
610 | 0 | << ", tablet size=" << weak_tablets.size() |
611 | 0 | << ", total_delete_map_count=" << total_delete_map_count |
612 | 0 | << ", cost(us)=" << watch.get_elapse_time_us() |
613 | 0 | << ", max_delete_bitmap_score=" << *max_delete_bitmap_score |
614 | 0 | << ", max_delete_bitmap_score_tablet_id=" << max_delete_bitmap_score_tablet_id |
615 | 0 | << ", max_base_rowset_delete_bitmap_score=" << *max_base_rowset_delete_bitmap_score |
616 | 0 | << ", max_base_rowset_delete_bitmap_score_tablet_id=" |
617 | 0 | << max_base_rowset_delete_bitmap_score_tablet_id << ", tablets=[" << ss.str() << "]"; |
618 | 0 | } |
619 | | |
620 | 1 | std::vector<std::shared_ptr<CloudTablet>> CloudTabletMgr::get_all_tablet() { |
621 | 1 | std::vector<std::shared_ptr<CloudTablet>> tablets; |
622 | 1 | tablets.reserve(_tablet_map->size()); |
623 | 1 | _tablet_map->traverse([&tablets](auto& t) { tablets.push_back(t); }); |
624 | 1 | return tablets; |
625 | 1 | } |
626 | | |
627 | 2 | void CloudTabletMgr::put_tablet_for_UT(std::shared_ptr<CloudTablet> tablet) { |
628 | 2 | _tablet_map->put(tablet); |
629 | 2 | } |
630 | | |
631 | | } // namespace doris |