be/src/cloud/cloud_warm_up_manager.cpp
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_warm_up_manager.h" |
19 | | |
20 | | #include <bthread/condition_variable.h> |
21 | | #include <bthread/mutex.h> |
22 | | #include <bthread/unstable.h> |
23 | | #include <butil/time.h> |
24 | | #include <bvar/bvar.h> |
25 | | #include <bvar/reducer.h> |
26 | | |
27 | | #include <algorithm> |
28 | | #include <chrono> |
29 | | #include <cstddef> |
30 | | #include <list> |
31 | | #include <string> |
32 | | #include <tuple> |
33 | | #include <vector> |
34 | | |
35 | | #include "bthread/mutex.h" |
36 | | #include "bvar/bvar.h" |
37 | | #include "cloud/cloud_tablet.h" |
38 | | #include "cloud/cloud_tablet_mgr.h" |
39 | | #include "cloud/config.h" |
40 | | #include "common/cast_set.h" |
41 | | #include "common/check.h" |
42 | | #include "common/config.h" |
43 | | #include "common/logging.h" |
44 | | #include "cpp/sync_point.h" |
45 | | #include "io/cache/block_file_cache_downloader.h" |
46 | | #include "runtime/cluster_info.h" |
47 | | #include "runtime/exec_env.h" |
48 | | #include "service/backend_options.h" |
49 | | #include "storage/index/inverted/inverted_index_desc.h" |
50 | | #include "storage/rowset/beta_rowset.h" |
51 | | #include "storage/tablet/tablet.h" |
52 | | #include "util/brpc_client_cache.h" // BrpcClientCache |
53 | | #include "util/bvar_windowed_adder.h" |
54 | | #include "util/client_cache.h" |
55 | | #include "util/defer_op.h" |
56 | | #include "util/stack_util.h" |
57 | | #include "util/thrift_rpc_helper.h" |
58 | | #include "util/time.h" |
59 | | |
60 | | namespace doris { |
61 | | |
62 | | // Peer candidate management statistics |
63 | | bvar::Adder<uint64_t> g_peer_candidate_cache_hit("peer_candidate_cache_hit"); |
64 | | bvar::Adder<uint64_t> g_peer_candidate_cache_miss("peer_candidate_cache_miss"); |
65 | | bvar::Adder<uint64_t> g_peer_lazy_fetch_total("peer_lazy_fetch_total"); |
66 | | bvar::Adder<uint64_t> g_peer_lazy_fetch_success("peer_lazy_fetch_success"); |
67 | | bvar::Adder<uint64_t> g_peer_lazy_fetch_failed("peer_lazy_fetch_failed"); |
68 | | bvar::LatencyRecorder g_peer_lazy_fetch_latency("peer_lazy_fetch_latency"); |
69 | | bvar::Adder<uint64_t> g_peer_rpc_failure_eviction("peer_rpc_failure_eviction"); |
70 | | bvar::Adder<uint64_t> g_peer_candidate_expiry_eviction("peer_candidate_expiry_eviction"); |
71 | | bvar::Adder<uint64_t> g_peer_candidate_rotate("peer_candidate_rotate"); |
72 | | bvar::Adder<uint64_t> g_peer_tablet_cooldown_entered("peer_tablet_cooldown_entered"); |
73 | | bvar::Adder<uint64_t> g_peer_tablet_cooldown_skipped("peer_tablet_cooldown_skipped"); |
74 | | |
75 | | bvar::Adder<uint64_t> g_file_cache_event_driven_warm_up_skipped_rowset_num( |
76 | | "file_cache_event_driven_warm_up_skipped_rowset_num"); |
77 | | bvar::Adder<uint64_t> g_file_cache_event_driven_warm_up_requested_segment_size( |
78 | | "file_cache_event_driven_warm_up_requested_segment_size"); |
79 | | bvar::Adder<uint64_t> g_file_cache_event_driven_warm_up_requested_segment_num( |
80 | | "file_cache_event_driven_warm_up_requested_segment_num"); |
81 | | bvar::Adder<uint64_t> g_file_cache_event_driven_warm_up_requested_index_size( |
82 | | "file_cache_event_driven_warm_up_requested_index_size"); |
83 | | bvar::Adder<uint64_t> g_file_cache_event_driven_warm_up_requested_index_num( |
84 | | "file_cache_event_driven_warm_up_requested_index_num"); |
85 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_submitted_tablet_num( |
86 | | "file_cache_once_or_periodic_warm_up_submitted_tablet_num"); |
87 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_finished_tablet_num( |
88 | | "file_cache_once_or_periodic_warm_up_finished_tablet_num"); |
89 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_submitted_segment_size( |
90 | | "file_cache_once_or_periodic_warm_up_submitted_segment_size"); |
91 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_submitted_segment_num( |
92 | | "file_cache_once_or_periodic_warm_up_submitted_segment_num"); |
93 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_submitted_index_size( |
94 | | "file_cache_once_or_periodic_warm_up_submitted_index_size"); |
95 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_submitted_index_num( |
96 | | "file_cache_once_or_periodic_warm_up_submitted_index_num"); |
97 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_finished_segment_size( |
98 | | "file_cache_once_or_periodic_warm_up_finished_segment_size"); |
99 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_finished_segment_num( |
100 | | "file_cache_once_or_periodic_warm_up_finished_segment_num"); |
101 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_finished_index_size( |
102 | | "file_cache_once_or_periodic_warm_up_finished_index_size"); |
103 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_finished_index_num( |
104 | | "file_cache_once_or_periodic_warm_up_finished_index_num"); |
105 | | bvar::Adder<uint64_t> g_file_cache_recycle_cache_requested_segment_num( |
106 | | "file_cache_recycle_cache_requested_segment_num"); |
107 | | bvar::Adder<uint64_t> g_file_cache_recycle_cache_requested_index_num( |
108 | | "file_cache_recycle_cache_requested_index_num"); |
109 | | bvar::Status<int64_t> g_file_cache_warm_up_rowset_last_call_unix_ts( |
110 | | "file_cache_warm_up_rowset_last_call_unix_ts", 0); |
111 | | bvar::Adder<uint64_t> file_cache_warm_up_failed_task_num("file_cache_warm_up", "failed_task_num"); |
112 | | bvar::Adder<int64_t> g_balance_tablet_be_mapping_size("balance_tablet_be_mapping_size"); |
113 | | // Number of warm up jobs currently held in this BE's memory. |
114 | | // Incremented when FE dispatches a new job to this BE (SET_JOB / SET_BATCH / event SET_JOB), |
115 | | // decremented when the job is cleared (CLEAR_JOB / event CLEAR_JOB). |
116 | | bvar::Adder<int64_t> g_file_cache_warm_up_job_num("file_cache_warm_up_job_num"); |
117 | | |
118 | | bvar::LatencyRecorder g_file_cache_warm_up_rowset_wait_for_compaction_latency( |
119 | | "file_cache_warm_up_rowset_wait_for_compaction_latency"); |
120 | | |
121 | | // Per-job windowed metrics for source BE |
122 | | // bvar::Window enforces MAX_SECONDS_LIMIT = 3600, so the longest window is 1h. |
123 | | static constexpr int WINDOW_5M = 300; |
124 | | static constexpr int WINDOW_30M = 1800; |
125 | | static constexpr int WINDOW_1H = 3600; |
126 | | |
127 | | MBvarWindowedAdder g_warmup_ed_requested_segment_num("warmup_ed_requested_segment_num", {"job_id"}, |
128 | | {WINDOW_5M, WINDOW_30M, WINDOW_1H}, false); |
129 | | MBvarWindowedAdder g_warmup_ed_requested_segment_size("warmup_ed_requested_segment_size", |
130 | | {"job_id"}, |
131 | | {WINDOW_5M, WINDOW_30M, WINDOW_1H}, false); |
132 | | MBvarWindowedAdder g_warmup_ed_requested_index_num("warmup_ed_requested_index_num", {"job_id"}, |
133 | | {WINDOW_5M, WINDOW_30M, WINDOW_1H}, false); |
134 | | MBvarWindowedAdder g_warmup_ed_requested_index_size("warmup_ed_requested_index_size", {"job_id"}, |
135 | | {WINDOW_5M, WINDOW_30M, WINDOW_1H}, false); |
136 | | bvar::MultiDimension<bvar::Status<int64_t>> g_warmup_ed_last_trigger_ts({"job_id"}); |
137 | | |
138 | 56 | CloudWarmUpManager::CloudWarmUpManager(CloudStorageEngine& engine) : _engine(engine) { |
139 | 56 | auto st = ThreadPoolBuilder("CloudWarmUpManagerThreadPool") |
140 | 56 | .set_min_threads(config::warm_up_manager_thread_pool_size) |
141 | 56 | .set_max_threads(config::warm_up_manager_thread_pool_size) |
142 | 56 | .build(&_thread_pool); |
143 | 56 | DORIS_CHECK(st.ok()) << st; |
144 | 56 | _thread_pool_token = _thread_pool->new_token(ThreadPool::ExecutionMode::CONCURRENT); |
145 | 56 | DORIS_CHECK(_thread_pool_token != nullptr); |
146 | 56 | _download_thread = std::thread(&CloudWarmUpManager::handle_jobs, this); |
147 | 56 | _cleanup_thread = std::thread(&CloudWarmUpManager::run_cleanup_loop, this); |
148 | 56 | } |
149 | | |
150 | 56 | CloudWarmUpManager::~CloudWarmUpManager() { |
151 | 56 | { |
152 | | // Set _closed under both mutexes so that both threads' wait predicates see it. |
153 | 56 | std::lock_guard lock(_mtx); |
154 | 56 | std::lock_guard<std::mutex> cleanup_lock(_cleanup_mtx); |
155 | 56 | _closed = true; |
156 | 56 | } |
157 | 56 | _cond.notify_all(); |
158 | 56 | _cleanup_cond.notify_all(); |
159 | 56 | if (_download_thread.joinable()) { |
160 | 56 | _download_thread.join(); |
161 | 56 | } |
162 | 56 | if (_cleanup_thread.joinable()) { |
163 | 56 | _cleanup_thread.join(); |
164 | 56 | } |
165 | | |
166 | 56 | _thread_pool_token->shutdown(); |
167 | 56 | _thread_pool_token.reset(); |
168 | 56 | _thread_pool->shutdown(); |
169 | 56 | _thread_pool.reset(); |
170 | | |
171 | 573k | for (auto& shard : _balanced_tablets_shards) { |
172 | 573k | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
173 | 573k | shard.tablets.clear(); |
174 | 573k | } |
175 | 56 | } |
176 | | |
177 | 0 | std::unordered_map<std::string, RowsetMetaSharedPtr> snapshot_rs_metas(BaseTablet* tablet) { |
178 | 0 | std::unordered_map<std::string, RowsetMetaSharedPtr> id_to_rowset_meta_map; |
179 | 0 | auto visitor = [&id_to_rowset_meta_map](const RowsetSharedPtr& r) { |
180 | 0 | id_to_rowset_meta_map.emplace(r->rowset_meta()->rowset_id().to_string(), r->rowset_meta()); |
181 | 0 | }; |
182 | 0 | constexpr bool include_stale = false; |
183 | 0 | tablet->traverse_rowsets(visitor, include_stale); |
184 | 0 | return id_to_rowset_meta_map; |
185 | 0 | } |
186 | | |
187 | | void CloudWarmUpManager::submit_download_tasks(io::Path path, int64_t file_size, |
188 | | io::FileSystemSPtr file_system, |
189 | | int64_t expiration_time, |
190 | | std::shared_ptr<bthread::CountdownEvent> wait, |
191 | | bool is_index, std::function<void(Status)> done_cb, |
192 | 0 | int64_t tablet_id) { |
193 | 0 | VLOG_DEBUG << "submit warm up task for file: " << path << ", file_size: " << file_size |
194 | 0 | << ", expiration_time: " << expiration_time |
195 | 0 | << ", is_index: " << (is_index ? "true" : "false"); |
196 | 0 | if (file_size < 0) { |
197 | 0 | auto st = file_system->file_size(path, &file_size); |
198 | 0 | if (!st.ok()) [[unlikely]] { |
199 | 0 | LOG(WARNING) << "get file size failed: " << path; |
200 | 0 | file_cache_warm_up_failed_task_num << 1; |
201 | 0 | return; |
202 | 0 | } |
203 | 0 | } |
204 | 0 | if (is_index) { |
205 | 0 | g_file_cache_once_or_periodic_warm_up_submitted_index_num << 1; |
206 | 0 | g_file_cache_once_or_periodic_warm_up_submitted_index_size << file_size; |
207 | 0 | } else { |
208 | 0 | g_file_cache_once_or_periodic_warm_up_submitted_segment_num << 1; |
209 | 0 | g_file_cache_once_or_periodic_warm_up_submitted_segment_size << file_size; |
210 | 0 | } |
211 | |
|
212 | 0 | const int64_t chunk_size = 10 * 1024 * 1024; // 10MB |
213 | 0 | int64_t offset = 0; |
214 | 0 | int64_t remaining_size = file_size; |
215 | |
|
216 | 0 | while (remaining_size > 0) { |
217 | 0 | int64_t current_chunk_size = std::min(chunk_size, remaining_size); |
218 | 0 | wait->add_count(); |
219 | |
|
220 | 0 | _engine.file_cache_block_downloader().submit_download_task(io::DownloadFileMeta { |
221 | 0 | .path = path, |
222 | 0 | .file_size = file_size, |
223 | 0 | .offset = offset, |
224 | 0 | .download_size = current_chunk_size, |
225 | 0 | .file_system = file_system, |
226 | 0 | .ctx = {.expiration_time = expiration_time, |
227 | 0 | .is_dryrun = config::enable_reader_dryrun_when_download_file_cache, |
228 | 0 | .is_warmup = true}, |
229 | 0 | .download_done = |
230 | 0 | [=, done_cb = std::move(done_cb)](Status st) { |
231 | 0 | if (done_cb) done_cb(st); |
232 | 0 | if (!st) { |
233 | 0 | LOG_WARNING("Warm up error ").error(st); |
234 | 0 | } else if (is_index) { |
235 | 0 | g_file_cache_once_or_periodic_warm_up_finished_index_num |
236 | 0 | << (offset == 0 ? 1 : 0); |
237 | 0 | g_file_cache_once_or_periodic_warm_up_finished_index_size |
238 | 0 | << current_chunk_size; |
239 | 0 | } else { |
240 | 0 | g_file_cache_once_or_periodic_warm_up_finished_segment_num |
241 | 0 | << (offset == 0 ? 1 : 0); |
242 | 0 | g_file_cache_once_or_periodic_warm_up_finished_segment_size |
243 | 0 | << current_chunk_size; |
244 | 0 | } |
245 | 0 | wait->signal(); |
246 | 0 | }, |
247 | 0 | .tablet_id = tablet_id, |
248 | 0 | }); |
249 | |
|
250 | 0 | offset += current_chunk_size; |
251 | 0 | remaining_size -= current_chunk_size; |
252 | 0 | } |
253 | 0 | } |
254 | | |
255 | 56 | void CloudWarmUpManager::handle_jobs() { |
256 | | #ifndef BE_TEST |
257 | | constexpr int WAIT_TIME_SECONDS = 600; |
258 | | while (true) { |
259 | | std::shared_ptr<JobMeta> cur_job = nullptr; |
260 | | { |
261 | | std::unique_lock lock(_mtx); |
262 | | while (!_closed && _pending_job_metas.empty()) { |
263 | | _cond.wait(lock); |
264 | | } |
265 | | if (_closed) break; |
266 | | if (!_pending_job_metas.empty()) { |
267 | | cur_job = _pending_job_metas.front(); |
268 | | } |
269 | | } |
270 | | |
271 | | if (!cur_job) { |
272 | | LOG_WARNING("Warm up job is null"); |
273 | | continue; |
274 | | } |
275 | | |
276 | | std::shared_ptr<bthread::CountdownEvent> wait = |
277 | | std::make_shared<bthread::CountdownEvent>(0); |
278 | | |
279 | | for (int64_t tablet_id : cur_job->tablet_ids) { |
280 | | VLOG_DEBUG << "Warm up tablet " << tablet_id << " stack: " << get_stack_trace(); |
281 | | if (_cur_job_id == 0) { // The job is canceled |
282 | | break; |
283 | | } |
284 | | auto res = _engine.tablet_mgr().get_tablet(tablet_id); |
285 | | if (!res.has_value()) { |
286 | | LOG_WARNING("Warm up error ").tag("tablet_id", tablet_id).error(res.error()); |
287 | | continue; |
288 | | } |
289 | | auto tablet = res.value(); |
290 | | auto st = tablet->sync_rowsets(); |
291 | | if (!st) { |
292 | | LOG_WARNING("Warm up error ").tag("tablet_id", tablet_id).error(st); |
293 | | continue; |
294 | | } |
295 | | |
296 | | auto tablet_meta = tablet->tablet_meta(); |
297 | | auto rs_metas = snapshot_rs_metas(tablet.get()); |
298 | | for (auto& [_, rs] : rs_metas) { |
299 | | auto storage_resource = rs->remote_storage_resource(); |
300 | | if (!storage_resource) { |
301 | | LOG(WARNING) << storage_resource.error(); |
302 | | continue; |
303 | | } |
304 | | |
305 | | int64_t expiration_time = tablet_meta->ttl_seconds(); |
306 | | if (!tablet->add_rowset_warmup_state(*rs, WarmUpTriggerSource::JOB)) { |
307 | | LOG(INFO) << "found duplicate warmup task for rowset " << rs->rowset_id() |
308 | | << ", skip it"; |
309 | | continue; |
310 | | } |
311 | | for (int64_t seg_id = 0; seg_id < rs->num_segments(); seg_id++) { |
312 | | // 1st. download segment files |
313 | | // Use rs->fs() instead of storage_resource.value()->fs to support packed |
314 | | // files. PackedFileSystem wrapper in RowsetMeta::fs() handles the index_map |
315 | | // lookup and reads from the correct packed file. |
316 | | if (!config::file_cache_enable_only_warm_up_idx) { |
317 | | submit_download_tasks( |
318 | | storage_resource.value()->remote_segment_path(*rs, seg_id), |
319 | | rs->segment_file_size(cast_set<int>(seg_id)), rs->fs(), |
320 | | expiration_time, wait, false, |
321 | | [tablet, rs, seg_id](Status st) { |
322 | | VLOG_DEBUG << "warmup rowset " << rs->version() << " segment " |
323 | | << seg_id << " completed"; |
324 | | if (tablet->complete_rowset_segment_warmup( |
325 | | WarmUpTriggerSource::JOB, rs->rowset_id(), st, |
326 | | 1, 0) |
327 | | .trigger_source == WarmUpTriggerSource::JOB) { |
328 | | VLOG_DEBUG << "warmup rowset " << rs->version() |
329 | | << " completed"; |
330 | | } |
331 | | }, |
332 | | tablet_id); |
333 | | } |
334 | | |
335 | | // 2nd. download inverted index files |
336 | | int64_t file_size = -1; |
337 | | auto schema_ptr = rs->tablet_schema(); |
338 | | auto idx_version = schema_ptr->get_inverted_index_storage_format(); |
339 | | const auto& idx_file_info = rs->inverted_index_file_info(cast_set<int>(seg_id)); |
340 | | if (idx_version == InvertedIndexStorageFormatPB::V1) { |
341 | | auto&& inverted_index_info = |
342 | | rs->inverted_index_file_info(cast_set<int>(seg_id)); |
343 | | std::unordered_map<int64_t, int64_t> index_size_map; |
344 | | for (const auto& info : inverted_index_info.index_info()) { |
345 | | if (info.index_file_size() != -1) { |
346 | | index_size_map[info.index_id()] = info.index_file_size(); |
347 | | } else { |
348 | | VLOG_DEBUG << "Invalid index_file_size for segment_id " << seg_id |
349 | | << ", index_id " << info.index_id(); |
350 | | } |
351 | | } |
352 | | for (const auto& index : schema_ptr->inverted_indexes()) { |
353 | | auto idx_path = storage_resource.value()->remote_idx_v1_path( |
354 | | *rs, seg_id, index->index_id(), index->get_index_suffix()); |
355 | | if (idx_file_info.index_info_size() > 0) { |
356 | | for (const auto& idx_info : idx_file_info.index_info()) { |
357 | | if (index->index_id() == idx_info.index_id() && |
358 | | index->get_index_suffix() == idx_info.index_suffix()) { |
359 | | file_size = idx_info.index_file_size(); |
360 | | break; |
361 | | } |
362 | | } |
363 | | } |
364 | | tablet->update_rowset_warmup_state_inverted_idx_num( |
365 | | WarmUpTriggerSource::JOB, rs->rowset_id(), 1); |
366 | | submit_download_tasks( |
367 | | idx_path, file_size, rs->fs(), expiration_time, wait, true, |
368 | | [=](Status st) { |
369 | | VLOG_DEBUG << "warmup rowset " << rs->version() |
370 | | << " segment " << seg_id |
371 | | << "inverted idx:" << idx_path << " completed"; |
372 | | if (tablet->complete_rowset_segment_warmup( |
373 | | WarmUpTriggerSource::JOB, rs->rowset_id(), |
374 | | st, 0, 1) |
375 | | .trigger_source == WarmUpTriggerSource::JOB) { |
376 | | VLOG_DEBUG << "warmup rowset " << rs->version() |
377 | | << " completed"; |
378 | | } |
379 | | }, |
380 | | tablet_id); |
381 | | } |
382 | | } else { |
383 | | if (schema_ptr->has_inverted_index() || schema_ptr->has_ann_index()) { |
384 | | auto idx_path = |
385 | | storage_resource.value()->remote_idx_v2_path(*rs, seg_id); |
386 | | file_size = idx_file_info.has_index_size() ? idx_file_info.index_size() |
387 | | : -1; |
388 | | tablet->update_rowset_warmup_state_inverted_idx_num( |
389 | | WarmUpTriggerSource::JOB, rs->rowset_id(), 1); |
390 | | submit_download_tasks( |
391 | | idx_path, file_size, rs->fs(), expiration_time, wait, true, |
392 | | [=](Status st) { |
393 | | VLOG_DEBUG << "warmup rowset " << rs->version() |
394 | | << " segment " << seg_id |
395 | | << "inverted idx:" << idx_path << " completed"; |
396 | | if (tablet->complete_rowset_segment_warmup( |
397 | | WarmUpTriggerSource::JOB, rs->rowset_id(), |
398 | | st, 0, 1) |
399 | | .trigger_source == WarmUpTriggerSource::JOB) { |
400 | | VLOG_DEBUG << "warmup rowset " << rs->version() |
401 | | << " completed"; |
402 | | } |
403 | | }, |
404 | | tablet_id); |
405 | | } |
406 | | } |
407 | | } |
408 | | } |
409 | | g_file_cache_once_or_periodic_warm_up_finished_tablet_num << 1; |
410 | | } |
411 | | |
412 | | timespec time; |
413 | | time.tv_sec = UnixSeconds() + WAIT_TIME_SECONDS; |
414 | | if (wait->timed_wait(time)) { |
415 | | LOG_WARNING("Warm up {} tablets take a long time", cur_job->tablet_ids.size()); |
416 | | } |
417 | | { |
418 | | std::unique_lock lock(_mtx); |
419 | | _finish_job.push_back(cur_job); |
420 | | // _pending_job_metas may be cleared by a CLEAR_JOB request |
421 | | // so we need to check it again. |
422 | | if (!_pending_job_metas.empty()) { |
423 | | // We can not call pop_front before the job is finished, |
424 | | // because GET_CURRENT_JOB_STATE_AND_LEASE is relying on the pending job size. |
425 | | _pending_job_metas.pop_front(); |
426 | | } |
427 | | } |
428 | | } |
429 | | #endif |
430 | 56 | } |
431 | | |
432 | | JobMeta::JobMeta(const TJobMeta& meta) |
433 | 0 | : be_ip(meta.be_ip), brpc_port(meta.brpc_port), tablet_ids(meta.tablet_ids) { |
434 | 0 | switch (meta.download_type) { |
435 | 0 | case TDownloadType::BE: |
436 | 0 | download_type = DownloadType::BE; |
437 | 0 | break; |
438 | 0 | case TDownloadType::S3: |
439 | 0 | download_type = DownloadType::S3; |
440 | 0 | break; |
441 | 0 | } |
442 | 0 | } |
443 | | |
444 | 0 | Status CloudWarmUpManager::check_and_set_job_id(int64_t job_id) { |
445 | 0 | std::lock_guard lock(_mtx); |
446 | 0 | if (_cur_job_id == 0) { |
447 | 0 | _cur_job_id = job_id; |
448 | 0 | g_file_cache_warm_up_job_num << 1; |
449 | 0 | } |
450 | 0 | Status st = Status::OK(); |
451 | 0 | if (_cur_job_id != job_id) { |
452 | 0 | st = Status::InternalError("The job {} is running", _cur_job_id); |
453 | 0 | } |
454 | 0 | return st; |
455 | 0 | } |
456 | | |
457 | 0 | Status CloudWarmUpManager::check_and_set_batch_id(int64_t job_id, int64_t batch_id, bool* retry) { |
458 | 0 | std::lock_guard lock(_mtx); |
459 | 0 | Status st = Status::OK(); |
460 | 0 | if (_cur_job_id != 0 && _cur_job_id != job_id) { |
461 | 0 | st = Status::InternalError("The job {} is not current job, current job is {}", job_id, |
462 | 0 | _cur_job_id); |
463 | 0 | return st; |
464 | 0 | } |
465 | 0 | if (_cur_job_id == 0) { |
466 | 0 | _cur_job_id = job_id; |
467 | 0 | g_file_cache_warm_up_job_num << 1; |
468 | 0 | } |
469 | 0 | if (_cur_batch_id == batch_id) { |
470 | 0 | *retry = true; |
471 | 0 | return st; |
472 | 0 | } |
473 | 0 | if (_pending_job_metas.empty()) { |
474 | 0 | _cur_batch_id = batch_id; |
475 | 0 | } else { |
476 | 0 | st = Status::InternalError("The batch {} is not finish", _cur_batch_id); |
477 | 0 | } |
478 | 0 | return st; |
479 | 0 | } |
480 | | |
481 | 0 | void CloudWarmUpManager::add_job(const std::vector<TJobMeta>& job_metas) { |
482 | 0 | { |
483 | 0 | std::lock_guard lock(_mtx); |
484 | 0 | std::for_each(job_metas.begin(), job_metas.end(), [this](const TJobMeta& meta) { |
485 | 0 | _pending_job_metas.emplace_back(std::make_shared<JobMeta>(meta)); |
486 | 0 | g_file_cache_once_or_periodic_warm_up_submitted_tablet_num << meta.tablet_ids.size(); |
487 | 0 | }); |
488 | 0 | } |
489 | 0 | _cond.notify_all(); |
490 | 0 | } |
491 | | |
492 | | #ifdef BE_TEST |
493 | 0 | void CloudWarmUpManager::consumer_job() { |
494 | 0 | { |
495 | 0 | std::unique_lock lock(_mtx); |
496 | 0 | _finish_job.push_back(_pending_job_metas.front()); |
497 | 0 | _pending_job_metas.pop_front(); |
498 | 0 | } |
499 | 0 | } |
500 | | |
501 | | #endif |
502 | | |
503 | 0 | std::tuple<int64_t, int64_t, int64_t, int64_t> CloudWarmUpManager::get_current_job_state() { |
504 | 0 | std::lock_guard lock(_mtx); |
505 | 0 | return std::make_tuple(_cur_job_id, _cur_batch_id, _pending_job_metas.size(), |
506 | 0 | _finish_job.size()); |
507 | 0 | } |
508 | | |
509 | 0 | Status CloudWarmUpManager::clear_job(int64_t job_id) { |
510 | 0 | std::lock_guard lock(_mtx); |
511 | 0 | Status st = Status::OK(); |
512 | 0 | if (job_id == _cur_job_id) { |
513 | 0 | if (_cur_job_id != 0) { |
514 | 0 | g_file_cache_warm_up_job_num << -1; |
515 | 0 | } |
516 | 0 | _cur_job_id = 0; |
517 | 0 | _cur_batch_id = -1; |
518 | 0 | _pending_job_metas.clear(); |
519 | 0 | _finish_job.clear(); |
520 | 0 | } else { |
521 | 0 | st = Status::InternalError("The job {} is not current job, current job is {}", job_id, |
522 | 0 | _cur_job_id); |
523 | 0 | } |
524 | 0 | return st; |
525 | 0 | } |
526 | | |
527 | | Status CloudWarmUpManager::set_event(int64_t job_id, TWarmUpEventType::type event, bool clear, |
528 | 8 | const std::vector<int64_t>* table_ids) { |
529 | 8 | DBUG_EXECUTE_IF("CloudWarmUpManager.set_event.ignore_all", { |
530 | 8 | LOG(INFO) << "Ignore set_event request, job_id=" << job_id << ", event=" << event |
531 | 8 | << ", clear=" << clear; |
532 | 8 | return Status::OK(); |
533 | 8 | }); |
534 | 8 | std::lock_guard lock(_mtx); |
535 | 8 | Status st = Status::OK(); |
536 | 8 | if (event == TWarmUpEventType::type::LOAD) { |
537 | 7 | if (clear) { |
538 | 1 | if (_tablet_replica_cache.erase(job_id) > 0) { |
539 | 1 | g_file_cache_warm_up_job_num << -1; |
540 | 1 | } |
541 | 1 | _event_driven_filters.erase(job_id); |
542 | 1 | LOG(INFO) << "Clear event driven sync, job_id=" << job_id << ", event=" << event; |
543 | 6 | } else if (!_tablet_replica_cache.contains(job_id)) { |
544 | 5 | static_cast<void>(_tablet_replica_cache[job_id]); |
545 | 5 | g_file_cache_warm_up_job_num << 1; |
546 | 5 | if (table_ids != nullptr) { |
547 | | // table-level filter: set to the given table_id set (may be empty, |
548 | | // meaning all matched tables were deleted — warm up nothing) |
549 | 4 | _event_driven_filters[job_id] = |
550 | 4 | std::unordered_set<int64_t>(table_ids->begin(), table_ids->end()); |
551 | 4 | LOG(INFO) << "Set event driven sync with table filter, job_id=" << job_id |
552 | 4 | << ", event=" << event << ", table_ids_size=" << table_ids->size(); |
553 | 4 | } else { |
554 | | // cluster-level: no filter, warm up all tables |
555 | 1 | _event_driven_filters[job_id] = std::nullopt; |
556 | 1 | LOG(INFO) << "Set event driven sync, job_id=" << job_id << ", event=" << event; |
557 | 1 | } |
558 | 5 | } else if (table_ids != nullptr) { |
559 | | // Update table_ids for an existing job (may be empty) |
560 | 1 | _event_driven_filters[job_id] = |
561 | 1 | std::unordered_set<int64_t>(table_ids->begin(), table_ids->end()); |
562 | 1 | LOG(INFO) << "Updated table filter for event driven sync, job_id=" << job_id |
563 | 1 | << ", table_ids_size=" << table_ids->size(); |
564 | 1 | } |
565 | 7 | } else { |
566 | 1 | st = Status::InternalError("The event {} is not supported yet", event); |
567 | 1 | } |
568 | 8 | return st; |
569 | 8 | } |
570 | | |
571 | | std::vector<JobReplicaInfo> CloudWarmUpManager::get_replica_info(int64_t tablet_id, |
572 | | int64_t table_id, |
573 | | bool bypass_cache, |
574 | 4 | bool& cache_hit) { |
575 | 4 | std::vector<JobReplicaInfo> replicas; |
576 | 4 | std::vector<int64_t> cancelled_jobs; |
577 | 4 | std::lock_guard<std::mutex> lock(_mtx); |
578 | 4 | cache_hit = false; |
579 | 4 | for (auto& [job_id, cache] : _tablet_replica_cache) { |
580 | | // Check table-level filter: skip this job if table_id doesn't match |
581 | | // table_id == 0 means the caller doesn't have table context (e.g., recycle_cache), |
582 | | // so skip filtering |
583 | 4 | if (table_id != 0) { |
584 | 2 | auto filter_it = _event_driven_filters.find(job_id); |
585 | 2 | if (filter_it != _event_driven_filters.end() && filter_it->second.has_value()) { |
586 | 2 | if (filter_it->second->find(table_id) == filter_it->second->end()) { |
587 | 1 | VLOG_DEBUG << "get_replica_info: table_id=" << table_id |
588 | 0 | << " not in filter for job_id=" << job_id << ", skipping"; |
589 | 1 | continue; |
590 | 1 | } |
591 | 2 | } |
592 | 2 | } |
593 | | |
594 | 3 | if (!bypass_cache) { |
595 | 3 | auto it = cache.find(tablet_id); |
596 | 3 | if (it != cache.end()) { |
597 | | // check ttl expire |
598 | 3 | auto now = std::chrono::steady_clock::now(); |
599 | 3 | auto sec = std::chrono::duration_cast<std::chrono::seconds>(now - it->second.first); |
600 | 3 | if (sec.count() < config::warmup_tablet_replica_info_cache_ttl_sec) { |
601 | 3 | replicas.push_back(JobReplicaInfo {job_id, it->second.second}); |
602 | 3 | VLOG_DEBUG << "get_replica_info: cache hit, tablet_id=" << tablet_id |
603 | 0 | << ", job_id=" << job_id; |
604 | 3 | cache_hit = true; |
605 | 3 | continue; |
606 | 3 | } else { |
607 | 0 | VLOG_DEBUG << "get_replica_info: cache expired, tablet_id=" << tablet_id |
608 | 0 | << ", job_id=" << job_id; |
609 | 0 | cache.erase(it); |
610 | 0 | } |
611 | 3 | } |
612 | 0 | VLOG_DEBUG << "get_replica_info: cache miss, tablet_id=" << tablet_id |
613 | 0 | << ", job_id=" << job_id; |
614 | 0 | } |
615 | | |
616 | 0 | if (!cache_hit) { |
617 | | // We are trying to save one retry by refresh all the remaining caches |
618 | 0 | bypass_cache = true; |
619 | 0 | } |
620 | 0 | ClusterInfo* cluster_info = ExecEnv::GetInstance()->cluster_info(); |
621 | 0 | if (cluster_info == nullptr) { |
622 | 0 | LOG(WARNING) << "get_replica_info: have not get FE Master heartbeat yet, job_id=" |
623 | 0 | << job_id; |
624 | 0 | continue; |
625 | 0 | } |
626 | 0 | TNetworkAddress master_addr = cluster_info->master_fe_addr; |
627 | 0 | if (master_addr.hostname == "" || master_addr.port == 0) { |
628 | 0 | LOG(WARNING) << "get_replica_info: have not get FE Master heartbeat yet, job_id=" |
629 | 0 | << job_id; |
630 | 0 | continue; |
631 | 0 | } |
632 | | |
633 | 0 | TGetTabletReplicaInfosRequest request; |
634 | 0 | TGetTabletReplicaInfosResult result; |
635 | 0 | request.warm_up_job_id = job_id; |
636 | 0 | request.__isset.warm_up_job_id = true; |
637 | 0 | request.tablet_ids.emplace_back(tablet_id); |
638 | 0 | Status rpc_st = ThriftRpcHelper::rpc<FrontendServiceClient>( |
639 | 0 | master_addr.hostname, master_addr.port, |
640 | 0 | [&request, &result](FrontendServiceConnection& client) { |
641 | 0 | client->getTabletReplicaInfos(result, request); |
642 | 0 | }); |
643 | |
|
644 | 0 | if (!rpc_st.ok()) { |
645 | 0 | LOG(WARNING) << "get_replica_info: rpc failed error=" << rpc_st |
646 | 0 | << ", tablet id=" << tablet_id << ", job_id=" << job_id; |
647 | 0 | continue; |
648 | 0 | } |
649 | | |
650 | 0 | auto st = Status::create<false>(result.status); |
651 | 0 | if (!st.ok()) { |
652 | 0 | if (st.is<ErrorCode::CANCELLED>()) { |
653 | 0 | LOG(INFO) << "get_replica_info: warm up job cancelled, tablet_id=" << tablet_id |
654 | 0 | << ", job_id=" << job_id; |
655 | 0 | cancelled_jobs.push_back(job_id); |
656 | 0 | } else { |
657 | 0 | LOG(WARNING) << "get_replica_info: failed status=" << st |
658 | 0 | << ", tablet id=" << tablet_id << ", job_id=" << job_id; |
659 | 0 | } |
660 | 0 | continue; |
661 | 0 | } |
662 | 0 | VLOG_DEBUG << "get_replica_info: got " << result.tablet_replica_infos.size() |
663 | 0 | << " tablets, tablet id=" << tablet_id << ", job_id=" << job_id; |
664 | |
|
665 | 0 | for (const auto& it : result.tablet_replica_infos) { |
666 | 0 | auto tid = it.first; |
667 | 0 | VLOG_DEBUG << "get_replica_info: got " << it.second.size() |
668 | 0 | << " replica_infos, tablet id=" << tid << ", job_id=" << job_id; |
669 | 0 | for (const auto& replica : it.second) { |
670 | 0 | cache[tid] = std::make_pair(std::chrono::steady_clock::now(), replica); |
671 | 0 | replicas.push_back(JobReplicaInfo {job_id, replica}); |
672 | 0 | LOG(INFO) << "get_replica_info: cache add, tablet_id=" << tid |
673 | 0 | << ", job_id=" << job_id; |
674 | 0 | } |
675 | 0 | } |
676 | 0 | } |
677 | 4 | for (auto job_id : cancelled_jobs) { |
678 | 0 | LOG(INFO) << "get_replica_info: erasing cancelled job, job_id=" << job_id; |
679 | | // Lazy cleanup path: FE reported the warm up job as CANCELLED, so the job is |
680 | | // no longer held in memory. Keep the job-count metric and the event filter |
681 | | // consistent with the explicit CLEAR_JOB path in set_event(). |
682 | 0 | if (_tablet_replica_cache.erase(job_id) > 0) { |
683 | 0 | g_file_cache_warm_up_job_num << -1; |
684 | 0 | } |
685 | 0 | _event_driven_filters.erase(job_id); |
686 | 0 | } |
687 | 4 | VLOG_DEBUG << "get_replica_info: return " << replicas.size() |
688 | 0 | << " replicas, tablet id=" << tablet_id; |
689 | 4 | return replicas; |
690 | 4 | } |
691 | | |
692 | | void CloudWarmUpManager::warm_up_rowset(RowsetMeta& rs_meta, int64_t table_id, |
693 | 3 | int64_t sync_wait_timeout_ms) { |
694 | 3 | if (sync_wait_timeout_ms <= 0) { |
695 | 2 | auto rs_meta_pb = std::make_shared<RowsetMetaPB>(rs_meta.get_rowset_pb()); |
696 | 2 | auto st = _thread_pool_token->submit_func([this, rs_meta_pb, table_id, |
697 | 2 | sync_wait_timeout_ms]() { |
698 | 2 | RowsetMeta async_rs_meta; |
699 | 2 | bool init_succeed = async_rs_meta.init_from_pb(*rs_meta_pb); |
700 | 2 | TEST_SYNC_POINT_CALLBACK("CloudWarmUpManager::warm_up_rowset.async_init_from_pb", |
701 | 2 | &init_succeed); |
702 | 2 | if (!init_succeed) { |
703 | 1 | LOG(WARNING) << "Failed to init rowset meta when warming up rowset asynchronously"; |
704 | 1 | return; |
705 | 1 | } |
706 | 1 | _warm_up_rowset(async_rs_meta, table_id, sync_wait_timeout_ms); |
707 | 1 | }); |
708 | 2 | if (!st.ok()) { |
709 | 0 | LOG(WARNING) << "Failed to submit warm up rowset task: " << st; |
710 | 0 | file_cache_warm_up_failed_task_num << 1; |
711 | 0 | } |
712 | 2 | return; |
713 | 2 | } |
714 | | |
715 | 1 | bthread::Mutex mu; |
716 | 1 | bthread::ConditionVariable cv; |
717 | 1 | bool finished = false; |
718 | 1 | std::unique_lock<bthread::Mutex> lock(mu); |
719 | 1 | auto st = _thread_pool_token->submit_func([&, this]() { |
720 | 1 | _warm_up_rowset(rs_meta, table_id, sync_wait_timeout_ms); |
721 | 1 | std::unique_lock<bthread::Mutex> l(mu); |
722 | 1 | finished = true; |
723 | 1 | cv.notify_one(); |
724 | 1 | }); |
725 | 1 | if (!st.ok()) { |
726 | 0 | LOG(WARNING) << "Failed to submit warm up rowset task: " << st; |
727 | 0 | file_cache_warm_up_failed_task_num << 1; |
728 | 1 | } else { |
729 | 3 | while (!finished) { |
730 | 2 | TEST_SYNC_POINT_CALLBACK("CloudWarmUpManager::warm_up_rowset.before_wait", &cv); |
731 | 2 | cv.wait(lock); |
732 | 2 | } |
733 | 1 | } |
734 | 1 | } |
735 | | |
736 | | void CloudWarmUpManager::_warm_up_rowset(RowsetMeta& rs_meta, int64_t table_id, |
737 | 2 | int64_t sync_wait_timeout_ms) { |
738 | 2 | TEST_SYNC_POINT_CALLBACK("CloudWarmUpManager::_warm_up_rowset.enter", &rs_meta, |
739 | 2 | &sync_wait_timeout_ms); |
740 | 2 | bool cache_hit = false; |
741 | 2 | auto replicas = get_replica_info(rs_meta.tablet_id(), table_id, false, cache_hit); |
742 | 2 | if (replicas.empty()) { |
743 | 2 | VLOG_DEBUG << "There is no need to warmup tablet=" << rs_meta.tablet_id() |
744 | 0 | << ", skipping rowset=" << rs_meta.rowset_id().to_string(); |
745 | 2 | g_file_cache_event_driven_warm_up_skipped_rowset_num << 1; |
746 | 2 | return; |
747 | 2 | } |
748 | 0 | Status st = _do_warm_up_rowset(rs_meta, table_id, replicas, sync_wait_timeout_ms, !cache_hit); |
749 | 0 | if (cache_hit && !st.ok() && st.is<ErrorCode::TABLE_NOT_FOUND>()) { |
750 | 0 | replicas = get_replica_info(rs_meta.tablet_id(), table_id, true, cache_hit); |
751 | 0 | st = _do_warm_up_rowset(rs_meta, table_id, replicas, sync_wait_timeout_ms, true); |
752 | 0 | } |
753 | 0 | if (!st.ok()) { |
754 | 0 | LOG(WARNING) << "Failed to warm up rowset, tablet_id=" << rs_meta.tablet_id() |
755 | 0 | << ", rowset_id=" << rs_meta.rowset_id().to_string() << ", status=" << st; |
756 | 0 | } |
757 | 0 | } |
758 | | |
759 | | Status CloudWarmUpManager::_build_warm_up_rowset_result( |
760 | | const std::vector<WarmUpRowsetFailure>& failures, size_t replica_count, int64_t tablet_id, |
761 | 3 | int64_t table_id, const std::string& rowset_id) { |
762 | 3 | if (failures.empty()) { |
763 | 1 | return Status::OK(); |
764 | 1 | } |
765 | | |
766 | 2 | int code = failures.front().code; |
767 | 2 | std::string failure_msg; |
768 | 6 | for (size_t i = 0; i < failures.size(); ++i) { |
769 | 4 | if (failures[i].code == ErrorCode::TABLE_NOT_FOUND) { |
770 | 1 | code = ErrorCode::TABLE_NOT_FOUND; |
771 | 1 | } |
772 | 4 | if (i > 0) { |
773 | 2 | failure_msg.append("; "); |
774 | 2 | } |
775 | 4 | failure_msg.append(failures[i].reason); |
776 | 4 | } |
777 | | |
778 | 2 | return Status::Error(code, |
779 | 2 | "warm up rowset failed on {}/{} replicas, tablet_id={}, table_id={}, " |
780 | 2 | "rowset_id={}, failures=[{}]", |
781 | 2 | failures.size(), replica_count, tablet_id, table_id, rowset_id, |
782 | 2 | failure_msg); |
783 | 3 | } |
784 | | |
785 | | Status CloudWarmUpManager::_do_warm_up_rowset(RowsetMeta& rs_meta, int64_t table_id, |
786 | | std::vector<JobReplicaInfo>& replicas, |
787 | | int64_t sync_wait_timeout_ms, |
788 | 0 | bool skip_existence_check) { |
789 | 0 | auto tablet_id = rs_meta.tablet_id(); |
790 | 0 | int64_t now_ts = std::chrono::duration_cast<std::chrono::microseconds>( |
791 | 0 | std::chrono::system_clock::now().time_since_epoch()) |
792 | 0 | .count(); |
793 | 0 | g_file_cache_warm_up_rowset_last_call_unix_ts.set_value(now_ts); |
794 | 0 | std::vector<WarmUpRowsetFailure> failures; |
795 | 0 | auto add_failure = [&failures](const JobReplicaInfo& info, const std::string& target, |
796 | 0 | const Status& st) { |
797 | 0 | failures.push_back(WarmUpRowsetFailure { |
798 | 0 | .code = st.code(), |
799 | 0 | .reason = "job_id=" + std::to_string(info.job_id) + |
800 | 0 | ", backend_id=" + std::to_string(info.replica.backend_id) + |
801 | 0 | ", target=" + target + ", status=" + st.to_string_no_stack()}); |
802 | 0 | }; |
803 | |
|
804 | 0 | for (auto& info : replicas) { |
805 | 0 | std::string job_id_str = std::to_string(info.job_id); |
806 | 0 | std::string target = get_host_port(info.replica.host, info.replica.brpc_port); |
807 | 0 | int64_t trigger_ts_ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
808 | 0 | std::chrono::system_clock::now().time_since_epoch()) |
809 | 0 | .count(); |
810 | |
|
811 | 0 | PWarmUpRowsetRequest request; |
812 | 0 | request.add_rowset_metas()->CopyFrom(rs_meta.get_rowset_pb()); |
813 | 0 | request.set_unix_ts_us(now_ts); |
814 | 0 | request.set_sync_wait_timeout_ms(sync_wait_timeout_ms); |
815 | 0 | request.set_skip_existence_check(skip_existence_check); |
816 | 0 | request.set_job_id(info.job_id); |
817 | 0 | request.set_upstream_trigger_ts_ms(trigger_ts_ms); |
818 | | |
819 | | // send sync request |
820 | 0 | std::string host = info.replica.host; |
821 | 0 | auto dns_cache = ExecEnv::GetInstance()->dns_cache(); |
822 | 0 | if (dns_cache == nullptr) { |
823 | 0 | LOG(WARNING) << "DNS cache is not initialized, skipping hostname resolve"; |
824 | 0 | } else if (!is_valid_ip(info.replica.host)) { |
825 | 0 | Status status = dns_cache->get(info.replica.host, &host); |
826 | 0 | if (!status.ok()) { |
827 | 0 | LOG(WARNING) << "failed to get ip from host " << info.replica.host << ": " |
828 | 0 | << status.to_string(); |
829 | 0 | add_failure(info, target, status); |
830 | 0 | continue; |
831 | 0 | } |
832 | 0 | } |
833 | 0 | std::string brpc_addr = get_host_port(host, info.replica.brpc_port); |
834 | 0 | Status st = Status::OK(); |
835 | 0 | std::shared_ptr<PBackendService_Stub> brpc_stub = |
836 | 0 | ExecEnv::GetInstance()->brpc_internal_client_cache()->get_new_client_no_cache( |
837 | 0 | brpc_addr); |
838 | 0 | if (!brpc_stub) { |
839 | 0 | st = Status::RpcError("Address {} is wrong", brpc_addr); |
840 | 0 | add_failure(info, target, st); |
841 | 0 | continue; |
842 | 0 | } |
843 | | |
844 | | // update metrics |
845 | 0 | auto schema_ptr = rs_meta.tablet_schema(); |
846 | 0 | auto idx_version = schema_ptr->get_inverted_index_storage_format(); |
847 | 0 | for (int64_t segment_id = 0; segment_id < rs_meta.num_segments(); segment_id++) { |
848 | 0 | auto seg_size = rs_meta.segment_file_size(cast_set<int>(segment_id)); |
849 | |
|
850 | 0 | g_file_cache_event_driven_warm_up_requested_segment_num << 1; |
851 | 0 | g_warmup_ed_requested_segment_num.put({job_id_str}, 1); |
852 | |
|
853 | 0 | g_file_cache_event_driven_warm_up_requested_segment_size << seg_size; |
854 | 0 | g_warmup_ed_requested_segment_size.put({job_id_str}, seg_size); |
855 | |
|
856 | 0 | if (schema_ptr->has_inverted_index() || schema_ptr->has_ann_index()) { |
857 | 0 | if (idx_version == InvertedIndexStorageFormatPB::V1) { |
858 | 0 | auto&& inverted_index_info = |
859 | 0 | rs_meta.inverted_index_file_info(cast_set<int>(segment_id)); |
860 | 0 | if (inverted_index_info.index_info().empty()) { |
861 | 0 | VLOG_DEBUG << "No index info available for segment " << segment_id; |
862 | 0 | continue; |
863 | 0 | } |
864 | 0 | for (const auto& idx_info : inverted_index_info.index_info()) { |
865 | 0 | g_file_cache_event_driven_warm_up_requested_index_num << 1; |
866 | 0 | g_warmup_ed_requested_index_num.put({job_id_str}, 1); |
867 | |
|
868 | 0 | if (idx_info.index_file_size() != -1) { |
869 | 0 | g_file_cache_event_driven_warm_up_requested_index_size |
870 | 0 | << idx_info.index_file_size(); |
871 | 0 | g_warmup_ed_requested_index_size.put({job_id_str}, |
872 | 0 | idx_info.index_file_size()); |
873 | 0 | } else { |
874 | 0 | VLOG_DEBUG << "Invalid index_file_size for segment_id " << segment_id |
875 | 0 | << ", index_id " << idx_info.index_id(); |
876 | 0 | } |
877 | 0 | } |
878 | 0 | } else { // InvertedIndexStorageFormatPB::V2 |
879 | 0 | auto&& inverted_index_info = |
880 | 0 | rs_meta.inverted_index_file_info(cast_set<int>(segment_id)); |
881 | 0 | g_file_cache_event_driven_warm_up_requested_index_num << 1; |
882 | 0 | g_warmup_ed_requested_index_num.put({job_id_str}, 1); |
883 | |
|
884 | 0 | if (inverted_index_info.has_index_size()) { |
885 | 0 | g_file_cache_event_driven_warm_up_requested_index_size |
886 | 0 | << inverted_index_info.index_size(); |
887 | 0 | g_warmup_ed_requested_index_size.put({job_id_str}, |
888 | 0 | inverted_index_info.index_size()); |
889 | 0 | } else { |
890 | 0 | VLOG_DEBUG << "index_size is not set for segment " << segment_id; |
891 | 0 | } |
892 | 0 | } |
893 | 0 | } |
894 | 0 | } |
895 | | |
896 | | // Update last trigger timestamp |
897 | 0 | auto* trigger_ts = |
898 | 0 | g_warmup_ed_last_trigger_ts.get_stats(std::list<std::string> {job_id_str}); |
899 | 0 | if (trigger_ts) { |
900 | 0 | trigger_ts->set_value(trigger_ts_ms); |
901 | 0 | } |
902 | |
|
903 | 0 | brpc::Controller cntl; |
904 | 0 | if (sync_wait_timeout_ms > 0) { |
905 | 0 | cntl.set_timeout_ms(sync_wait_timeout_ms + 1000); |
906 | 0 | } |
907 | 0 | PWarmUpRowsetResponse response; |
908 | 0 | MonotonicStopWatch watch; |
909 | 0 | watch.start(); |
910 | 0 | brpc_stub->warm_up_rowset(&cntl, &request, &response, nullptr); |
911 | 0 | if (cntl.Failed()) { |
912 | 0 | LOG_WARNING("warm up rowset {} for tablet {} failed, rpc error: {}", |
913 | 0 | rs_meta.rowset_id().to_string(), tablet_id, cntl.ErrorText()); |
914 | 0 | add_failure(info, target, Status::RpcError(cntl.ErrorText())); |
915 | 0 | continue; |
916 | 0 | } |
917 | 0 | if (sync_wait_timeout_ms > 0) { |
918 | 0 | auto cost_us = watch.elapsed_time_microseconds(); |
919 | 0 | VLOG_DEBUG << "warm up rowset wait for compaction: " << cost_us << " us"; |
920 | 0 | if (cost_us / 1000 > sync_wait_timeout_ms) { |
921 | 0 | LOG_WARNING( |
922 | 0 | "Warm up rowset {} for tabelt {} wait for compaction timeout, takes {} ms", |
923 | 0 | rs_meta.rowset_id().to_string(), tablet_id, cost_us / 1000); |
924 | 0 | } |
925 | 0 | g_file_cache_warm_up_rowset_wait_for_compaction_latency << cost_us; |
926 | 0 | } |
927 | 0 | auto status = Status::create<false>(response.status()); |
928 | 0 | if (response.has_status() && !status.ok()) { |
929 | 0 | LOG(INFO) << "warm_up_rowset failed, tablet_id=" << rs_meta.tablet_id() |
930 | 0 | << ", rowset_id=" << rs_meta.rowset_id().to_string() |
931 | 0 | << ", target=" << info.replica.host << ", skip_existence_check" |
932 | 0 | << skip_existence_check << ", status=" << status; |
933 | 0 | add_failure(info, target, status); |
934 | 0 | } |
935 | 0 | } |
936 | 0 | return _build_warm_up_rowset_result(failures, replicas.size(), tablet_id, table_id, |
937 | 0 | rs_meta.rowset_id().to_string()); |
938 | 0 | } |
939 | | |
940 | | void CloudWarmUpManager::recycle_cache(int64_t tablet_id, |
941 | 0 | const std::vector<RecycledRowsets>& rowsets) { |
942 | 0 | bthread::Mutex mu; |
943 | 0 | bthread::ConditionVariable cv; |
944 | 0 | std::unique_lock<bthread::Mutex> lock(mu); |
945 | 0 | auto st = _thread_pool_token->submit_func([&, this]() { |
946 | 0 | std::unique_lock<bthread::Mutex> l(mu); |
947 | 0 | _recycle_cache(tablet_id, rowsets); |
948 | 0 | cv.notify_one(); |
949 | 0 | }); |
950 | 0 | if (!st.ok()) { |
951 | 0 | LOG(WARNING) << "Failed to submit recycle cache task, tablet_id=" << tablet_id |
952 | 0 | << ", error=" << st; |
953 | 0 | } else { |
954 | 0 | cv.wait(lock); |
955 | 0 | } |
956 | 0 | } |
957 | | |
958 | | void CloudWarmUpManager::_recycle_cache(int64_t tablet_id, |
959 | 0 | const std::vector<RecycledRowsets>& rowsets) { |
960 | 0 | LOG(INFO) << "recycle_cache: tablet_id=" << tablet_id << ", num_rowsets=" << rowsets.size(); |
961 | 0 | bool cache_hit = false; |
962 | 0 | auto replicas = get_replica_info(tablet_id, /*table_id=*/0, false, cache_hit); |
963 | 0 | if (replicas.empty()) { |
964 | 0 | return; |
965 | 0 | } |
966 | | |
967 | 0 | PRecycleCacheRequest request; |
968 | 0 | for (const auto& rowset : rowsets) { |
969 | 0 | RecycleCacheMeta* meta = request.add_cache_metas(); |
970 | 0 | meta->set_tablet_id(tablet_id); |
971 | 0 | meta->set_rowset_id(rowset.rowset_id.to_string()); |
972 | 0 | meta->set_num_segments(rowset.num_segments); |
973 | 0 | for (const auto& name : rowset.index_file_names) { |
974 | 0 | meta->add_index_file_names(name); |
975 | 0 | } |
976 | 0 | g_file_cache_recycle_cache_requested_segment_num << rowset.num_segments; |
977 | 0 | g_file_cache_recycle_cache_requested_index_num << rowset.index_file_names.size(); |
978 | 0 | } |
979 | 0 | auto dns_cache = ExecEnv::GetInstance()->dns_cache(); |
980 | 0 | for (auto& replica : replicas) { |
981 | | // send sync request |
982 | 0 | std::string host = replica.replica.host; |
983 | 0 | if (dns_cache == nullptr) { |
984 | 0 | LOG(WARNING) << "DNS cache is not initialized, skipping hostname resolve"; |
985 | 0 | } else if (!is_valid_ip(replica.replica.host)) { |
986 | 0 | Status status = dns_cache->get(replica.replica.host, &host); |
987 | 0 | if (!status.ok()) { |
988 | 0 | LOG(WARNING) << "failed to get ip from host " << replica.replica.host << ": " |
989 | 0 | << status.to_string(); |
990 | 0 | continue; |
991 | 0 | } |
992 | 0 | } |
993 | 0 | std::string brpc_addr = get_host_port(host, replica.replica.brpc_port); |
994 | 0 | Status st = Status::OK(); |
995 | 0 | std::shared_ptr<PBackendService_Stub> brpc_stub = |
996 | 0 | ExecEnv::GetInstance()->brpc_internal_client_cache()->get_new_client_no_cache( |
997 | 0 | brpc_addr); |
998 | 0 | if (!brpc_stub) { |
999 | 0 | st = Status::RpcError("Address {} is wrong", brpc_addr); |
1000 | 0 | continue; |
1001 | 0 | } |
1002 | 0 | brpc::Controller cntl; |
1003 | 0 | PRecycleCacheResponse response; |
1004 | 0 | brpc_stub->recycle_cache(&cntl, &request, &response, nullptr); |
1005 | 0 | } |
1006 | 0 | } |
1007 | | |
1008 | | // Balance warm up cache management methods implementation |
1009 | | void CloudWarmUpManager::record_balanced_tablet(int64_t tablet_id, const std::string& host, |
1010 | | int32_t brpc_port, |
1011 | 55 | const std::string& compute_group_id) { |
1012 | 55 | int64_t now_ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
1013 | 55 | std::chrono::system_clock::now().time_since_epoch()) |
1014 | 55 | .count(); |
1015 | | |
1016 | 55 | PeerCandidate candidate; |
1017 | 55 | candidate.host = host; |
1018 | 55 | candidate.brpc_port = brpc_port; |
1019 | 55 | candidate.compute_group_id = compute_group_id; |
1020 | 55 | candidate.last_access_time_ms = now_ms; |
1021 | 55 | candidate.consecutive_rpc_failures = 0; |
1022 | | |
1023 | 55 | auto& shard = get_shard(tablet_id); |
1024 | 55 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1025 | | |
1026 | 55 | auto [it, inserted] = shard.tablets.try_emplace(tablet_id); |
1027 | 55 | if (inserted) { |
1028 | | // Only increment the gauge counter on first insertion. |
1029 | 38 | g_balance_tablet_be_mapping_size << 1; |
1030 | 38 | } |
1031 | | |
1032 | 55 | auto& cands = it->second.candidates; |
1033 | | // Warmup rebalance: a tablet has at most one warm-up peer (the current rebalance source). |
1034 | | // Upsert: replace existing same-CG entry if present, otherwise prepend. |
1035 | 55 | auto same_cg_it = std::find_if(cands.begin(), cands.end(), [&](const PeerCandidate& c) { |
1036 | 19 | return c.compute_group_id == compute_group_id; |
1037 | 19 | }); |
1038 | | |
1039 | 55 | if (same_cg_it != cands.end()) { |
1040 | | // Update in-place, preserve position (already at or near front from prior insert). |
1041 | 2 | same_cg_it->host = std::move(candidate.host); |
1042 | 2 | same_cg_it->brpc_port = candidate.brpc_port; |
1043 | 2 | same_cg_it->last_access_time_ms = candidate.last_access_time_ms; |
1044 | 2 | same_cg_it->consecutive_rpc_failures = 0; |
1045 | 53 | } else { |
1046 | | // New CG entry: insert at front (warmup has highest priority). |
1047 | 53 | cands.insert(cands.begin(), std::move(candidate)); |
1048 | 53 | } |
1049 | | |
1050 | 55 | VLOG_DEBUG << "Recorded balanced warm up cache tablet: tablet_id=" << tablet_id |
1051 | 0 | << ", host=" << host << ":" << brpc_port |
1052 | 0 | << ", compute_group_id=" << compute_group_id; |
1053 | 55 | } |
1054 | | |
1055 | 3 | void CloudWarmUpManager::remove_balanced_tablet(int64_t tablet_id) { |
1056 | 3 | auto& shard = get_shard(tablet_id); |
1057 | 3 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1058 | 3 | auto it = shard.tablets.find(tablet_id); |
1059 | 3 | if (it != shard.tablets.end()) { |
1060 | 2 | shard.tablets.erase(it); |
1061 | 2 | g_balance_tablet_be_mapping_size << -1; |
1062 | 2 | VLOG_DEBUG << "Removed balanced warm up cache tablet by timer, tablet_id=" << tablet_id; |
1063 | 2 | } |
1064 | 3 | } |
1065 | | |
1066 | 0 | void CloudWarmUpManager::remove_balanced_tablets(const std::vector<int64_t>& tablet_ids) { |
1067 | | // Group tablet_ids by shard to minimize lock contention |
1068 | 0 | std::array<std::vector<int64_t>, SHARD_COUNT> shard_groups; |
1069 | 0 | for (int64_t tablet_id : tablet_ids) { |
1070 | 0 | shard_groups[get_shard_index(tablet_id)].push_back(tablet_id); |
1071 | 0 | } |
1072 | | |
1073 | | // Process each shard |
1074 | 0 | for (size_t i = 0; i < SHARD_COUNT; ++i) { |
1075 | 0 | if (shard_groups[i].empty()) continue; |
1076 | | |
1077 | 0 | auto& shard = _balanced_tablets_shards[i]; |
1078 | 0 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1079 | 0 | for (int64_t tablet_id : shard_groups[i]) { |
1080 | 0 | auto it = shard.tablets.find(tablet_id); |
1081 | 0 | if (it != shard.tablets.end()) { |
1082 | 0 | shard.tablets.erase(it); |
1083 | 0 | g_balance_tablet_be_mapping_size << -1; |
1084 | 0 | VLOG_DEBUG << "Removed balanced warm up cache tablet: tablet_id=" << tablet_id; |
1085 | 0 | } |
1086 | 0 | } |
1087 | 0 | } |
1088 | 0 | } |
1089 | | |
1090 | | // Cleanup loop: runs on a dedicated pthread, wakes up periodically to evict |
1091 | | // expired peer candidates and empty tablet entries. |
1092 | 56 | void CloudWarmUpManager::run_cleanup_loop() { |
1093 | 56 | while (true) { |
1094 | 56 | { |
1095 | 56 | std::unique_lock<std::mutex> lock(_cleanup_mtx); |
1096 | 56 | _cleanup_cond.wait_for(lock, |
1097 | 56 | std::chrono::seconds(config::peer_candidate_cleanup_interval_s), |
1098 | 76 | [this]() { return _closed; }); |
1099 | 56 | if (_closed) break; |
1100 | 56 | } |
1101 | | |
1102 | 0 | int64_t now_ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
1103 | 0 | std::chrono::system_clock::now().time_since_epoch()) |
1104 | 0 | .count(); |
1105 | 0 | int64_t expiry_ms = config::peer_candidate_expiry_s * 1000LL; |
1106 | |
|
1107 | 0 | for (auto& shard : _balanced_tablets_shards) { |
1108 | 0 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1109 | 0 | auto tablet_it = shard.tablets.begin(); |
1110 | 0 | while (tablet_it != shard.tablets.end()) { |
1111 | 0 | auto& tpc = tablet_it->second; |
1112 | | // Remove expired candidates |
1113 | 0 | auto& cands = tpc.candidates; |
1114 | 0 | size_t cands_before = cands.size(); |
1115 | 0 | cands.erase(std::remove_if(cands.begin(), cands.end(), |
1116 | 0 | [&](const PeerCandidate& c) { |
1117 | 0 | return (now_ms - c.last_access_time_ms) >= expiry_ms; |
1118 | 0 | }), |
1119 | 0 | cands.end()); |
1120 | 0 | size_t removed = cands_before - cands.size(); |
1121 | 0 | if (removed > 0) { |
1122 | 0 | g_peer_candidate_expiry_eviction << removed; |
1123 | 0 | } |
1124 | | // Remove the tablet entry if no candidates remain |
1125 | 0 | if (cands.empty()) { |
1126 | 0 | tablet_it = shard.tablets.erase(tablet_it); |
1127 | 0 | g_balance_tablet_be_mapping_size << -1; |
1128 | 0 | } else { |
1129 | 0 | ++tablet_it; |
1130 | 0 | } |
1131 | 0 | } |
1132 | 0 | } |
1133 | 0 | } |
1134 | 56 | } |
1135 | | |
1136 | | // fetch_candidates_from_fe: lazy fetch path — appends candidates to the end |
1137 | | // (lower priority than warmup-inserted ones). Uses singleflight to avoid |
1138 | | // duplicate concurrent RPCs for the same tablet. |
1139 | 1 | void CloudWarmUpManager::fetch_candidates_from_fe(int64_t tablet_id) { |
1140 | | // --- singleflight check --- |
1141 | 1 | { |
1142 | 1 | auto& shard = get_shard(tablet_id); |
1143 | 1 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1144 | 1 | auto it = shard.tablets.find(tablet_id); |
1145 | 1 | if (it != shard.tablets.end() && it->second.fetching_from_fe) { |
1146 | 0 | return; // another fetch is already in flight |
1147 | 0 | } |
1148 | | // Increment gauge when we create a genuinely new tablet entry |
1149 | 1 | if (it == shard.tablets.end()) { |
1150 | 1 | g_balance_tablet_be_mapping_size << 1; |
1151 | 1 | } |
1152 | | // Mark as fetching (creates entry if not present). |
1153 | 1 | shard.tablets[tablet_id].fetching_from_fe = true; |
1154 | 1 | } |
1155 | | |
1156 | | // Use Defer to absolutely guarantee we reset the fetching flag on return |
1157 | 1 | Defer defer_fetching_reset {[this, tablet_id]() { |
1158 | 1 | auto& shard = get_shard(tablet_id); |
1159 | 1 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1160 | 1 | auto it = shard.tablets.find(tablet_id); |
1161 | 1 | if (it != shard.tablets.end()) { |
1162 | 1 | it->second.fetching_from_fe = false; |
1163 | 1 | } |
1164 | 1 | }}; |
1165 | | |
1166 | | // --- RPC to FE (without warm_up_job_id) --- |
1167 | 1 | ClusterInfo* cluster_info = ExecEnv::GetInstance()->cluster_info(); |
1168 | 1 | if (cluster_info == nullptr) { |
1169 | 0 | LOG(WARNING) << "fetch_candidates_from_fe: have not got FE Master heartbeat yet" |
1170 | 0 | << ", tablet_id=" << tablet_id; |
1171 | 0 | return; |
1172 | 0 | } |
1173 | 1 | TNetworkAddress master_addr = cluster_info->master_fe_addr; |
1174 | 1 | if (master_addr.hostname.empty() || master_addr.port == 0) { |
1175 | 1 | LOG(WARNING) << "fetch_candidates_from_fe: FE master address unknown" |
1176 | 1 | << ", tablet_id=" << tablet_id; |
1177 | 1 | return; |
1178 | 1 | } |
1179 | | |
1180 | 0 | TGetTabletReplicaInfosRequest request; |
1181 | 0 | TGetTabletReplicaInfosResult result; |
1182 | | // No warm_up_job_id — lazy fetch path |
1183 | 0 | request.tablet_ids.emplace_back(tablet_id); |
1184 | |
|
1185 | 0 | g_peer_lazy_fetch_total << 1; |
1186 | 0 | const auto rpc_start = std::chrono::steady_clock::now(); |
1187 | 0 | Status rpc_st = ThriftRpcHelper::rpc<FrontendServiceClient>( |
1188 | 0 | master_addr.hostname, master_addr.port, |
1189 | 0 | [&request, &result](FrontendServiceConnection& client) { |
1190 | 0 | client->getTabletReplicaInfos(result, request); |
1191 | 0 | }); |
1192 | 0 | g_peer_lazy_fetch_latency << std::chrono::duration_cast<std::chrono::microseconds>( |
1193 | 0 | std::chrono::steady_clock::now() - rpc_start) |
1194 | 0 | .count(); |
1195 | |
|
1196 | 0 | if (!rpc_st.ok()) { |
1197 | 0 | LOG(WARNING) << "fetch_candidates_from_fe: rpc failed, tablet_id=" << tablet_id |
1198 | 0 | << ", error=" << rpc_st; |
1199 | 0 | g_peer_lazy_fetch_failed << 1; |
1200 | 0 | return; |
1201 | 0 | } |
1202 | | |
1203 | 0 | auto st = Status::create<false>(result.status); |
1204 | 0 | if (!st.ok()) { |
1205 | 0 | LOG(WARNING) << "fetch_candidates_from_fe: FE returned error, tablet_id=" << tablet_id |
1206 | 0 | << ", status=" << st; |
1207 | 0 | g_peer_lazy_fetch_failed << 1; |
1208 | 0 | return; |
1209 | 0 | } |
1210 | | |
1211 | 0 | int64_t now_ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
1212 | 0 | std::chrono::system_clock::now().time_since_epoch()) |
1213 | 0 | .count(); |
1214 | | |
1215 | | // Parse the results OUTSIDE the lock |
1216 | 0 | std::vector<PeerCandidate> new_candidates; |
1217 | 0 | const std::string& self_host = BackendOptions::get_localhost(); |
1218 | 0 | const int32_t self_brpc_port = config::brpc_port; |
1219 | |
|
1220 | 0 | auto it_res = result.tablet_replica_infos.find(tablet_id); |
1221 | 0 | if (it_res != result.tablet_replica_infos.end()) { |
1222 | 0 | const auto& replicas = it_res->second; |
1223 | | // Pre-allocate memory since we know the upper bound of candidates |
1224 | 0 | new_candidates.reserve(replicas.size()); |
1225 | |
|
1226 | 0 | for (const auto& replica : replicas) { |
1227 | | // Skip self: a BE must not peer-read from its own file cache |
1228 | 0 | if (replica.host == self_host && replica.brpc_port == self_brpc_port) { |
1229 | 0 | VLOG_DEBUG << "fetch_candidates_from_fe: skipping self candidate " << replica.host |
1230 | 0 | << ":" << replica.brpc_port << " for tablet_id=" << tablet_id; |
1231 | 0 | continue; |
1232 | 0 | } |
1233 | | |
1234 | 0 | PeerCandidate& candidate = new_candidates.emplace_back(); |
1235 | 0 | candidate.host = replica.host; |
1236 | 0 | candidate.brpc_port = replica.brpc_port; |
1237 | 0 | if (replica.__isset.cloud_compute_group_id) { |
1238 | 0 | candidate.compute_group_id = replica.cloud_compute_group_id; |
1239 | 0 | } |
1240 | 0 | candidate.last_access_time_ms = now_ms; |
1241 | 0 | candidate.consecutive_rpc_failures = 0; |
1242 | 0 | } |
1243 | 0 | } |
1244 | |
|
1245 | 0 | g_peer_lazy_fetch_success << 1; |
1246 | | |
1247 | | // --- Merge results back into shard --- |
1248 | | // Acquire lock only to append to the candidates vector |
1249 | 0 | { |
1250 | 0 | auto& shard = get_shard(tablet_id); |
1251 | 0 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1252 | 0 | auto it = shard.tablets.find(tablet_id); |
1253 | | // Safely check if tablet is still there |
1254 | 0 | if (it != shard.tablets.end()) { |
1255 | 0 | auto& tpc = it->second; |
1256 | 0 | tpc.candidates.insert(tpc.candidates.end(), |
1257 | 0 | std::make_move_iterator(new_candidates.begin()), |
1258 | 0 | std::make_move_iterator(new_candidates.end())); |
1259 | 0 | LOG(INFO) << "fetch_candidates_from_fe: tablet_id=" << tablet_id << " got " |
1260 | 0 | << tpc.candidates.size() << " total candidates from FE"; |
1261 | 0 | VLOG_DEBUG << "fetch_candidates_from_fe: added " << new_candidates.size() |
1262 | 0 | << " candidates for tablet_id=" << tablet_id; |
1263 | 0 | } |
1264 | 0 | } |
1265 | 0 | } |
1266 | | |
1267 | 60 | std::vector<PeerCandidate> CloudWarmUpManager::get_peer_candidates(int64_t tablet_id) { |
1268 | 60 | auto& shard = get_shard(tablet_id); |
1269 | 60 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1270 | 60 | auto it = shard.tablets.find(tablet_id); |
1271 | 60 | if (it == shard.tablets.end()) { |
1272 | 5 | g_peer_candidate_cache_miss << 1; |
1273 | 5 | return {}; |
1274 | 5 | } |
1275 | | // Update last_access_time_ms for all candidates to keep them alive |
1276 | 55 | int64_t now_ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
1277 | 55 | std::chrono::system_clock::now().time_since_epoch()) |
1278 | 55 | .count(); |
1279 | 85 | for (auto& c : it->second.candidates) { |
1280 | 85 | c.last_access_time_ms = now_ms; |
1281 | 85 | } |
1282 | 55 | auto& tpc = it->second; |
1283 | | // Cooldown check: if this tablet is in cooldown, return empty to skip peer. |
1284 | 55 | if (tpc.cooldown_until_ms > 0 && now_ms < tpc.cooldown_until_ms) { |
1285 | 2 | g_peer_tablet_cooldown_skipped << 1; |
1286 | 2 | return {}; |
1287 | 2 | } |
1288 | | // Cooldown expired — reset for next cycle. |
1289 | 53 | if (tpc.cooldown_until_ms > 0) { |
1290 | 1 | tpc.cooldown_until_ms = 0; |
1291 | 1 | tpc.consecutive_all_miss = 0; |
1292 | 1 | } |
1293 | 53 | auto result = tpc.candidates; |
1294 | 53 | if (result.empty()) { |
1295 | 0 | g_peer_candidate_cache_miss << 1; |
1296 | 53 | } else { |
1297 | 53 | g_peer_candidate_cache_hit << 1; |
1298 | | // Apply compute group affinity: if a previous read succeeded from a particular |
1299 | | // compute group, move its candidates to the front so the next read tries it first. |
1300 | | // stable_partition preserves relative order within each group. |
1301 | | // |
1302 | | // Example: |
1303 | | // Candidates: [A(CG1), B(CG2), C(CG1), D(CG3)] |
1304 | | // pref = "CG1" |
1305 | | // After stable_partition: [A(CG1), C(CG1), B(CG2), D(CG3)] |
1306 | | // (A remains before C, and B remains before D) |
1307 | 53 | if (!tpc.last_successful_compute_group_id.empty()) { |
1308 | 11 | const std::string& pref = tpc.last_successful_compute_group_id; |
1309 | 18 | std::stable_partition(result.begin(), result.end(), [&pref](const PeerCandidate& c) { |
1310 | 18 | return c.compute_group_id == pref; |
1311 | 18 | }); |
1312 | 11 | } |
1313 | 53 | } |
1314 | 53 | return result; |
1315 | 55 | } |
1316 | | |
1317 | | void CloudWarmUpManager::update_peer_candidate_on_success(int64_t tablet_id, |
1318 | 17 | const std::string& compute_group_id) { |
1319 | 17 | auto& shard = get_shard(tablet_id); |
1320 | 17 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1321 | 17 | auto it = shard.tablets.find(tablet_id); |
1322 | 17 | if (it == shard.tablets.end()) { |
1323 | 1 | return; |
1324 | 1 | } |
1325 | 16 | it->second.last_successful_compute_group_id = compute_group_id; |
1326 | 16 | it->second.consecutive_all_miss = 0; |
1327 | 16 | it->second.cooldown_until_ms = 0; |
1328 | 16 | } |
1329 | | |
1330 | | void CloudWarmUpManager::update_peer_candidate_on_rpc_failure(int64_t tablet_id, |
1331 | | const std::string& host, |
1332 | 12 | int32_t brpc_port) { |
1333 | 12 | auto& shard = get_shard(tablet_id); |
1334 | 12 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1335 | 12 | auto it = shard.tablets.find(tablet_id); |
1336 | 12 | if (it == shard.tablets.end()) { |
1337 | 1 | return; |
1338 | 1 | } |
1339 | 11 | auto& cands = it->second.candidates; |
1340 | 12 | for (auto cit = cands.begin(); cit != cands.end(); ++cit) { |
1341 | 11 | if (cit->host == host && cit->brpc_port == brpc_port) { |
1342 | 10 | ++cit->consecutive_rpc_failures; |
1343 | 10 | if (cit->consecutive_rpc_failures >= config::peer_rpc_failure_eviction_threshold) { |
1344 | 2 | LOG(INFO) << "Evicting peer candidate due to consecutive RPC failures" |
1345 | 2 | << ", tablet_id=" << tablet_id << ", host=" << host << ":" << brpc_port |
1346 | 2 | << ", failures=" << cit->consecutive_rpc_failures; |
1347 | 2 | g_peer_rpc_failure_eviction << 1; |
1348 | 2 | cands.erase(cit); |
1349 | | // If all candidates have been evicted, remove the tablet entry |
1350 | | // entirely so that the gauge stays accurate. |
1351 | 2 | if (cands.empty()) { |
1352 | 1 | shard.tablets.erase(it); |
1353 | 1 | g_balance_tablet_be_mapping_size << -1; |
1354 | 1 | } |
1355 | 2 | } |
1356 | 10 | break; |
1357 | 10 | } |
1358 | 11 | } |
1359 | 11 | } |
1360 | | |
1361 | | void CloudWarmUpManager::rotate_peer_candidate_on_cache_miss(int64_t tablet_id, |
1362 | | const std::string& host, |
1363 | 15 | int32_t brpc_port) { |
1364 | 15 | auto& shard = get_shard(tablet_id); |
1365 | 15 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1366 | 15 | auto it = shard.tablets.find(tablet_id); |
1367 | 15 | if (it == shard.tablets.end()) { |
1368 | 1 | return; |
1369 | 1 | } |
1370 | 14 | auto& cands = it->second.candidates; |
1371 | 16 | auto cit = std::find_if(cands.begin(), cands.end(), [&](const PeerCandidate& c) { |
1372 | 16 | return c.host == host && c.brpc_port == brpc_port; |
1373 | 16 | }); |
1374 | 14 | if (cit != cands.end() && std::next(cit) != cands.end()) { |
1375 | | // Move this candidate to the end so the next read tries a different one. |
1376 | | // This ensures that if the first N candidates are all cache-miss, the system |
1377 | | // gradually converges to whichever compute group actually has the data. |
1378 | | // |
1379 | | // Example: |
1380 | | // cands: [B, C, D], cit points to B (front, cache miss) |
1381 | | // std::rotate(B, C, end) → [C, D, B] |
1382 | | // Next read tries C first instead of B. |
1383 | | // |
1384 | | // Also clear affinity if the rotated candidate belongs to the currently preferred |
1385 | | // compute group. Without this, get_peer_candidates() would stable_partition that |
1386 | | // CG back to the front on the very next call — completely undoing the rotate. |
1387 | 10 | if (it->second.last_successful_compute_group_id == cit->compute_group_id) { |
1388 | 1 | it->second.last_successful_compute_group_id.clear(); |
1389 | 1 | } |
1390 | 10 | std::rotate(cit, std::next(cit), cands.end()); |
1391 | 10 | } |
1392 | | // Always count the metric when the candidate is found, even if it is the |
1393 | | // last (or only) element where rotation is a no-op. |
1394 | 14 | if (cit != cands.end()) { |
1395 | 13 | g_peer_candidate_rotate << 1; |
1396 | 13 | } |
1397 | 14 | } |
1398 | | |
1399 | 10 | bool CloudWarmUpManager::is_peer_cooldown(int64_t tablet_id) const { |
1400 | 10 | const auto& shard = get_shard(tablet_id); |
1401 | 10 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1402 | 10 | auto it = shard.tablets.find(tablet_id); |
1403 | 10 | if (it == shard.tablets.end()) { |
1404 | 2 | return false; |
1405 | 2 | } |
1406 | 8 | if (it->second.cooldown_until_ms <= 0) { |
1407 | 3 | return false; |
1408 | 3 | } |
1409 | 5 | int64_t now_ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
1410 | 5 | std::chrono::system_clock::now().time_since_epoch()) |
1411 | 5 | .count(); |
1412 | 5 | return now_ms < it->second.cooldown_until_ms; |
1413 | 8 | } |
1414 | | |
1415 | 19 | void CloudWarmUpManager::record_peer_all_miss(int64_t tablet_id) { |
1416 | 19 | auto& shard = get_shard(tablet_id); |
1417 | 19 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1418 | 19 | auto it = shard.tablets.find(tablet_id); |
1419 | 19 | if (it == shard.tablets.end()) { |
1420 | 2 | return; |
1421 | 2 | } |
1422 | 17 | auto& tpc = it->second; |
1423 | 17 | tpc.consecutive_all_miss++; |
1424 | 17 | if (tpc.consecutive_all_miss >= config::peer_all_miss_cooldown_threshold) { |
1425 | 4 | int64_t now_ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
1426 | 4 | std::chrono::system_clock::now().time_since_epoch()) |
1427 | 4 | .count(); |
1428 | 4 | tpc.cooldown_until_ms = now_ms + config::peer_all_miss_cooldown_duration_s * 1000; |
1429 | 4 | g_peer_tablet_cooldown_entered << 1; |
1430 | 4 | LOG(INFO) << "Peer read cooldown entered for tablet_id=" << tablet_id << " after " |
1431 | 4 | << tpc.consecutive_all_miss << " consecutive all-miss races" |
1432 | 4 | << ", cooldown_duration_s=" << config::peer_all_miss_cooldown_duration_s; |
1433 | 4 | } |
1434 | 17 | } |
1435 | | |
1436 | | std::optional<TabletPeerCandidates> CloudWarmUpManager::get_tablet_peer_info( |
1437 | 0 | int64_t tablet_id) const { |
1438 | 0 | const auto& shard = get_shard(tablet_id); |
1439 | 0 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1440 | 0 | auto it = shard.tablets.find(tablet_id); |
1441 | 0 | if (it == shard.tablets.end()) { |
1442 | 0 | return std::nullopt; |
1443 | 0 | } |
1444 | 0 | return it->second; // copy under lock |
1445 | 0 | } |
1446 | | |
1447 | | std::vector<std::pair<int64_t, TabletPeerCandidates>> CloudWarmUpManager::get_all_peer_info( |
1448 | 0 | int64_t limit) const { |
1449 | 0 | std::vector<std::pair<int64_t, TabletPeerCandidates>> result; |
1450 | 0 | for (size_t i = 0; i < SHARD_COUNT; ++i) { |
1451 | 0 | const auto& shard = _balanced_tablets_shards[i]; |
1452 | 0 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1453 | 0 | for (const auto& [tid, tpc] : shard.tablets) { |
1454 | 0 | result.emplace_back(tid, tpc); |
1455 | 0 | if (limit > 0 && static_cast<int64_t>(result.size()) >= limit) { |
1456 | 0 | return result; |
1457 | 0 | } |
1458 | 0 | } |
1459 | 0 | } |
1460 | 0 | return result; |
1461 | 0 | } |
1462 | | |
1463 | | void CloudWarmUpManager::set_tablet_peer_candidates(int64_t tablet_id, |
1464 | 1 | TabletPeerCandidates candidates) { |
1465 | 1 | auto& shard = get_shard(tablet_id); |
1466 | 1 | std::unique_lock<bthread::Mutex> lock(shard.mtx); |
1467 | 1 | auto [it, inserted] = shard.tablets.insert_or_assign(tablet_id, std::move(candidates)); |
1468 | 1 | if (inserted) { |
1469 | 1 | g_balance_tablet_be_mapping_size << 1; |
1470 | 1 | } |
1471 | 1 | } |
1472 | | |
1473 | | } // namespace doris |