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 <cstddef> |
29 | | #include <list> |
30 | | #include <string> |
31 | | #include <tuple> |
32 | | #include <vector> |
33 | | |
34 | | #include "bvar/bvar.h" |
35 | | #include "cloud/cloud_tablet.h" |
36 | | #include "cloud/cloud_tablet_mgr.h" |
37 | | #include "cloud/config.h" |
38 | | #include "common/cast_set.h" |
39 | | #include "common/config.h" |
40 | | #include "common/logging.h" |
41 | | #include "cpp/sync_point.h" |
42 | | #include "io/cache/block_file_cache_downloader.h" |
43 | | #include "runtime/exec_env.h" |
44 | | #include "storage/index/inverted/inverted_index_desc.h" |
45 | | #include "storage/rowset/beta_rowset.h" |
46 | | #include "storage/tablet/tablet.h" |
47 | | #include "util/brpc_client_cache.h" // BrpcClientCache |
48 | | #include "util/bvar_windowed_adder.h" |
49 | | #include "util/client_cache.h" |
50 | | #include "util/defer_op.h" |
51 | | #include "util/stack_util.h" |
52 | | #include "util/thrift_rpc_helper.h" |
53 | | #include "util/time.h" |
54 | | |
55 | | namespace doris { |
56 | | |
57 | | bvar::Adder<uint64_t> g_file_cache_event_driven_warm_up_skipped_rowset_num( |
58 | | "file_cache_event_driven_warm_up_skipped_rowset_num"); |
59 | | bvar::Adder<uint64_t> g_file_cache_event_driven_warm_up_requested_segment_size( |
60 | | "file_cache_event_driven_warm_up_requested_segment_size"); |
61 | | bvar::Adder<uint64_t> g_file_cache_event_driven_warm_up_requested_segment_num( |
62 | | "file_cache_event_driven_warm_up_requested_segment_num"); |
63 | | bvar::Adder<uint64_t> g_file_cache_event_driven_warm_up_requested_index_size( |
64 | | "file_cache_event_driven_warm_up_requested_index_size"); |
65 | | bvar::Adder<uint64_t> g_file_cache_event_driven_warm_up_requested_index_num( |
66 | | "file_cache_event_driven_warm_up_requested_index_num"); |
67 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_submitted_tablet_num( |
68 | | "file_cache_once_or_periodic_warm_up_submitted_tablet_num"); |
69 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_finished_tablet_num( |
70 | | "file_cache_once_or_periodic_warm_up_finished_tablet_num"); |
71 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_submitted_segment_size( |
72 | | "file_cache_once_or_periodic_warm_up_submitted_segment_size"); |
73 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_submitted_segment_num( |
74 | | "file_cache_once_or_periodic_warm_up_submitted_segment_num"); |
75 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_submitted_index_size( |
76 | | "file_cache_once_or_periodic_warm_up_submitted_index_size"); |
77 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_submitted_index_num( |
78 | | "file_cache_once_or_periodic_warm_up_submitted_index_num"); |
79 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_finished_segment_size( |
80 | | "file_cache_once_or_periodic_warm_up_finished_segment_size"); |
81 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_finished_segment_num( |
82 | | "file_cache_once_or_periodic_warm_up_finished_segment_num"); |
83 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_finished_index_size( |
84 | | "file_cache_once_or_periodic_warm_up_finished_index_size"); |
85 | | bvar::Adder<uint64_t> g_file_cache_once_or_periodic_warm_up_finished_index_num( |
86 | | "file_cache_once_or_periodic_warm_up_finished_index_num"); |
87 | | bvar::Adder<uint64_t> g_file_cache_recycle_cache_requested_segment_num( |
88 | | "file_cache_recycle_cache_requested_segment_num"); |
89 | | bvar::Adder<uint64_t> g_file_cache_recycle_cache_requested_index_num( |
90 | | "file_cache_recycle_cache_requested_index_num"); |
91 | | bvar::Status<int64_t> g_file_cache_warm_up_rowset_last_call_unix_ts( |
92 | | "file_cache_warm_up_rowset_last_call_unix_ts", 0); |
93 | | bvar::Adder<uint64_t> file_cache_warm_up_failed_task_num("file_cache_warm_up", "failed_task_num"); |
94 | | bvar::Adder<uint64_t> g_balance_tablet_be_mapping_size("balance_tablet_be_mapping_size"); |
95 | | |
96 | | bvar::LatencyRecorder g_file_cache_warm_up_rowset_wait_for_compaction_latency( |
97 | | "file_cache_warm_up_rowset_wait_for_compaction_latency"); |
98 | | |
99 | | // Per-job windowed metrics for source BE |
100 | | // bvar::Window enforces MAX_SECONDS_LIMIT = 3600, so the longest window is 1h. |
101 | | static constexpr int WINDOW_5M = 300; |
102 | | static constexpr int WINDOW_30M = 1800; |
103 | | static constexpr int WINDOW_1H = 3600; |
104 | | |
105 | | MBvarWindowedAdder g_warmup_ed_requested_segment_num("warmup_ed_requested_segment_num", {"job_id"}, |
106 | | {WINDOW_5M, WINDOW_30M, WINDOW_1H}, false); |
107 | | MBvarWindowedAdder g_warmup_ed_requested_segment_size("warmup_ed_requested_segment_size", |
108 | | {"job_id"}, |
109 | | {WINDOW_5M, WINDOW_30M, WINDOW_1H}, false); |
110 | | MBvarWindowedAdder g_warmup_ed_requested_index_num("warmup_ed_requested_index_num", {"job_id"}, |
111 | | {WINDOW_5M, WINDOW_30M, WINDOW_1H}, false); |
112 | | MBvarWindowedAdder g_warmup_ed_requested_index_size("warmup_ed_requested_index_size", {"job_id"}, |
113 | | {WINDOW_5M, WINDOW_30M, WINDOW_1H}, false); |
114 | | bvar::MultiDimension<bvar::Status<int64_t>> g_warmup_ed_last_trigger_ts({"job_id"}); |
115 | | |
116 | 12 | CloudWarmUpManager::CloudWarmUpManager(CloudStorageEngine& engine) : _engine(engine) { |
117 | 12 | _download_thread = std::thread(&CloudWarmUpManager::handle_jobs, this); |
118 | 12 | static_cast<void>(ThreadPoolBuilder("CloudWarmUpManagerThreadPool") |
119 | 12 | .set_min_threads(1) |
120 | 12 | .set_max_threads(config::warm_up_manager_thread_pool_size) |
121 | 12 | .build(&_thread_pool)); |
122 | 12 | _thread_pool_token = _thread_pool->new_token(ThreadPool::ExecutionMode::CONCURRENT); |
123 | 12 | } |
124 | | |
125 | 11 | CloudWarmUpManager::~CloudWarmUpManager() { |
126 | 11 | { |
127 | 11 | std::lock_guard lock(_mtx); |
128 | 11 | _closed = true; |
129 | 11 | } |
130 | 11 | _cond.notify_all(); |
131 | 11 | if (_download_thread.joinable()) { |
132 | 11 | _download_thread.join(); |
133 | 11 | } |
134 | | |
135 | 112k | for (auto& shard : _balanced_tablets_shards) { |
136 | 112k | std::lock_guard<std::mutex> lock(shard.mtx); |
137 | 112k | shard.tablets.clear(); |
138 | 112k | } |
139 | 11 | } |
140 | | |
141 | 0 | std::unordered_map<std::string, RowsetMetaSharedPtr> snapshot_rs_metas(BaseTablet* tablet) { |
142 | 0 | std::unordered_map<std::string, RowsetMetaSharedPtr> id_to_rowset_meta_map; |
143 | 0 | auto visitor = [&id_to_rowset_meta_map](const RowsetSharedPtr& r) { |
144 | 0 | id_to_rowset_meta_map.emplace(r->rowset_meta()->rowset_id().to_string(), r->rowset_meta()); |
145 | 0 | }; |
146 | 0 | constexpr bool include_stale = false; |
147 | 0 | tablet->traverse_rowsets(visitor, include_stale); |
148 | 0 | return id_to_rowset_meta_map; |
149 | 0 | } |
150 | | |
151 | | void CloudWarmUpManager::submit_download_tasks(io::Path path, int64_t file_size, |
152 | | io::FileSystemSPtr file_system, |
153 | | int64_t expiration_time, |
154 | | std::shared_ptr<bthread::CountdownEvent> wait, |
155 | | bool is_index, std::function<void(Status)> done_cb, |
156 | 0 | int64_t tablet_id) { |
157 | 0 | VLOG_DEBUG << "submit warm up task for file: " << path << ", file_size: " << file_size |
158 | 0 | << ", expiration_time: " << expiration_time |
159 | 0 | << ", is_index: " << (is_index ? "true" : "false"); |
160 | 0 | if (file_size < 0) { |
161 | 0 | auto st = file_system->file_size(path, &file_size); |
162 | 0 | if (!st.ok()) [[unlikely]] { |
163 | 0 | LOG(WARNING) << "get file size failed: " << path; |
164 | 0 | file_cache_warm_up_failed_task_num << 1; |
165 | 0 | return; |
166 | 0 | } |
167 | 0 | } |
168 | 0 | if (is_index) { |
169 | 0 | g_file_cache_once_or_periodic_warm_up_submitted_index_num << 1; |
170 | 0 | g_file_cache_once_or_periodic_warm_up_submitted_index_size << file_size; |
171 | 0 | } else { |
172 | 0 | g_file_cache_once_or_periodic_warm_up_submitted_segment_num << 1; |
173 | 0 | g_file_cache_once_or_periodic_warm_up_submitted_segment_size << file_size; |
174 | 0 | } |
175 | |
|
176 | 0 | const int64_t chunk_size = 10 * 1024 * 1024; // 10MB |
177 | 0 | int64_t offset = 0; |
178 | 0 | int64_t remaining_size = file_size; |
179 | |
|
180 | 0 | while (remaining_size > 0) { |
181 | 0 | int64_t current_chunk_size = std::min(chunk_size, remaining_size); |
182 | 0 | wait->add_count(); |
183 | |
|
184 | 0 | _engine.file_cache_block_downloader().submit_download_task(io::DownloadFileMeta { |
185 | 0 | .path = path, |
186 | 0 | .file_size = file_size, |
187 | 0 | .offset = offset, |
188 | 0 | .download_size = current_chunk_size, |
189 | 0 | .file_system = file_system, |
190 | 0 | .ctx = {.expiration_time = expiration_time, |
191 | 0 | .is_dryrun = config::enable_reader_dryrun_when_download_file_cache, |
192 | 0 | .is_warmup = true}, |
193 | 0 | .download_done = |
194 | 0 | [=, done_cb = std::move(done_cb)](Status st) { |
195 | 0 | if (done_cb) done_cb(st); |
196 | 0 | if (!st) { |
197 | 0 | LOG_WARNING("Warm up error ").error(st); |
198 | 0 | } else if (is_index) { |
199 | 0 | g_file_cache_once_or_periodic_warm_up_finished_index_num |
200 | 0 | << (offset == 0 ? 1 : 0); |
201 | 0 | g_file_cache_once_or_periodic_warm_up_finished_index_size |
202 | 0 | << current_chunk_size; |
203 | 0 | } else { |
204 | 0 | g_file_cache_once_or_periodic_warm_up_finished_segment_num |
205 | 0 | << (offset == 0 ? 1 : 0); |
206 | 0 | g_file_cache_once_or_periodic_warm_up_finished_segment_size |
207 | 0 | << current_chunk_size; |
208 | 0 | } |
209 | 0 | wait->signal(); |
210 | 0 | }, |
211 | 0 | .tablet_id = tablet_id, |
212 | 0 | }); |
213 | |
|
214 | 0 | offset += current_chunk_size; |
215 | 0 | remaining_size -= current_chunk_size; |
216 | 0 | } |
217 | 0 | } |
218 | | |
219 | 1 | void CloudWarmUpManager::handle_jobs() { |
220 | 1 | #ifndef BE_TEST |
221 | 1 | constexpr int WAIT_TIME_SECONDS = 600; |
222 | 2 | while (true) { |
223 | 1 | std::shared_ptr<JobMeta> cur_job = nullptr; |
224 | 1 | { |
225 | 1 | std::unique_lock lock(_mtx); |
226 | 2 | while (!_closed && _pending_job_metas.empty()) { |
227 | 1 | _cond.wait(lock); |
228 | 1 | } |
229 | 1 | if (_closed) break; |
230 | 1 | if (!_pending_job_metas.empty()) { |
231 | 0 | cur_job = _pending_job_metas.front(); |
232 | 0 | } |
233 | 1 | } |
234 | | |
235 | 1 | if (!cur_job) { |
236 | 0 | LOG_WARNING("Warm up job is null"); |
237 | 0 | continue; |
238 | 0 | } |
239 | | |
240 | 1 | std::shared_ptr<bthread::CountdownEvent> wait = |
241 | 1 | std::make_shared<bthread::CountdownEvent>(0); |
242 | | |
243 | 1 | for (int64_t tablet_id : cur_job->tablet_ids) { |
244 | 0 | VLOG_DEBUG << "Warm up tablet " << tablet_id << " stack: " << get_stack_trace(); |
245 | 0 | if (_cur_job_id == 0) { // The job is canceled |
246 | 0 | break; |
247 | 0 | } |
248 | 0 | auto res = _engine.tablet_mgr().get_tablet(tablet_id); |
249 | 0 | if (!res.has_value()) { |
250 | 0 | LOG_WARNING("Warm up error ").tag("tablet_id", tablet_id).error(res.error()); |
251 | 0 | continue; |
252 | 0 | } |
253 | 0 | auto tablet = res.value(); |
254 | 0 | auto st = tablet->sync_rowsets(); |
255 | 0 | if (!st) { |
256 | 0 | LOG_WARNING("Warm up error ").tag("tablet_id", tablet_id).error(st); |
257 | 0 | continue; |
258 | 0 | } |
259 | | |
260 | 0 | auto tablet_meta = tablet->tablet_meta(); |
261 | 0 | auto rs_metas = snapshot_rs_metas(tablet.get()); |
262 | 0 | for (auto& [_, rs] : rs_metas) { |
263 | 0 | auto storage_resource = rs->remote_storage_resource(); |
264 | 0 | if (!storage_resource) { |
265 | 0 | LOG(WARNING) << storage_resource.error(); |
266 | 0 | continue; |
267 | 0 | } |
268 | | |
269 | 0 | int64_t expiration_time = tablet_meta->ttl_seconds(); |
270 | 0 | if (!tablet->add_rowset_warmup_state(*rs, WarmUpTriggerSource::JOB)) { |
271 | 0 | LOG(INFO) << "found duplicate warmup task for rowset " << rs->rowset_id() |
272 | 0 | << ", skip it"; |
273 | 0 | continue; |
274 | 0 | } |
275 | 0 | for (int64_t seg_id = 0; seg_id < rs->num_segments(); seg_id++) { |
276 | | // 1st. download segment files |
277 | | // Use rs->fs() instead of storage_resource.value()->fs to support packed |
278 | | // files. PackedFileSystem wrapper in RowsetMeta::fs() handles the index_map |
279 | | // lookup and reads from the correct packed file. |
280 | 0 | if (!config::file_cache_enable_only_warm_up_idx) { |
281 | 0 | submit_download_tasks( |
282 | 0 | storage_resource.value()->remote_segment_path(*rs, seg_id), |
283 | 0 | rs->segment_file_size(cast_set<int>(seg_id)), rs->fs(), |
284 | 0 | expiration_time, wait, false, |
285 | 0 | [tablet, rs, seg_id](Status st) { |
286 | 0 | VLOG_DEBUG << "warmup rowset " << rs->version() << " segment " |
287 | 0 | << seg_id << " completed"; |
288 | 0 | if (tablet->complete_rowset_segment_warmup( |
289 | 0 | WarmUpTriggerSource::JOB, rs->rowset_id(), st, |
290 | 0 | 1, 0) |
291 | 0 | .trigger_source == WarmUpTriggerSource::JOB) { |
292 | 0 | VLOG_DEBUG << "warmup rowset " << rs->version() |
293 | 0 | << " completed"; |
294 | 0 | } |
295 | 0 | }, |
296 | 0 | tablet_id); |
297 | 0 | } |
298 | | |
299 | | // 2nd. download inverted index files |
300 | 0 | int64_t file_size = -1; |
301 | 0 | auto schema_ptr = rs->tablet_schema(); |
302 | 0 | auto idx_version = schema_ptr->get_inverted_index_storage_format(); |
303 | 0 | const auto& idx_file_info = rs->inverted_index_file_info(cast_set<int>(seg_id)); |
304 | 0 | if (idx_version == InvertedIndexStorageFormatPB::V1) { |
305 | 0 | auto&& inverted_index_info = |
306 | 0 | rs->inverted_index_file_info(cast_set<int>(seg_id)); |
307 | 0 | std::unordered_map<int64_t, int64_t> index_size_map; |
308 | 0 | for (const auto& info : inverted_index_info.index_info()) { |
309 | 0 | if (info.index_file_size() != -1) { |
310 | 0 | index_size_map[info.index_id()] = info.index_file_size(); |
311 | 0 | } else { |
312 | 0 | VLOG_DEBUG << "Invalid index_file_size for segment_id " << seg_id |
313 | 0 | << ", index_id " << info.index_id(); |
314 | 0 | } |
315 | 0 | } |
316 | 0 | for (const auto& index : schema_ptr->inverted_indexes()) { |
317 | 0 | auto idx_path = storage_resource.value()->remote_idx_v1_path( |
318 | 0 | *rs, seg_id, index->index_id(), index->get_index_suffix()); |
319 | 0 | if (idx_file_info.index_info_size() > 0) { |
320 | 0 | for (const auto& idx_info : idx_file_info.index_info()) { |
321 | 0 | if (index->index_id() == idx_info.index_id() && |
322 | 0 | index->get_index_suffix() == idx_info.index_suffix()) { |
323 | 0 | file_size = idx_info.index_file_size(); |
324 | 0 | break; |
325 | 0 | } |
326 | 0 | } |
327 | 0 | } |
328 | 0 | tablet->update_rowset_warmup_state_inverted_idx_num( |
329 | 0 | WarmUpTriggerSource::JOB, rs->rowset_id(), 1); |
330 | 0 | submit_download_tasks( |
331 | 0 | idx_path, file_size, rs->fs(), expiration_time, wait, true, |
332 | 0 | [=](Status st) { |
333 | 0 | VLOG_DEBUG << "warmup rowset " << rs->version() |
334 | 0 | << " segment " << seg_id |
335 | 0 | << "inverted idx:" << idx_path << " completed"; |
336 | 0 | if (tablet->complete_rowset_segment_warmup( |
337 | 0 | WarmUpTriggerSource::JOB, rs->rowset_id(), |
338 | 0 | st, 0, 1) |
339 | 0 | .trigger_source == WarmUpTriggerSource::JOB) { |
340 | 0 | VLOG_DEBUG << "warmup rowset " << rs->version() |
341 | 0 | << " completed"; |
342 | 0 | } |
343 | 0 | }, |
344 | 0 | tablet_id); |
345 | 0 | } |
346 | 0 | } else { |
347 | 0 | if (schema_ptr->has_inverted_index() || schema_ptr->has_ann_index()) { |
348 | 0 | auto idx_path = |
349 | 0 | storage_resource.value()->remote_idx_v2_path(*rs, seg_id); |
350 | 0 | file_size = idx_file_info.has_index_size() ? idx_file_info.index_size() |
351 | 0 | : -1; |
352 | 0 | tablet->update_rowset_warmup_state_inverted_idx_num( |
353 | 0 | WarmUpTriggerSource::JOB, rs->rowset_id(), 1); |
354 | 0 | submit_download_tasks( |
355 | 0 | idx_path, file_size, rs->fs(), expiration_time, wait, true, |
356 | 0 | [=](Status st) { |
357 | 0 | VLOG_DEBUG << "warmup rowset " << rs->version() |
358 | 0 | << " segment " << seg_id |
359 | 0 | << "inverted idx:" << idx_path << " completed"; |
360 | 0 | if (tablet->complete_rowset_segment_warmup( |
361 | 0 | WarmUpTriggerSource::JOB, rs->rowset_id(), |
362 | 0 | st, 0, 1) |
363 | 0 | .trigger_source == WarmUpTriggerSource::JOB) { |
364 | 0 | VLOG_DEBUG << "warmup rowset " << rs->version() |
365 | 0 | << " completed"; |
366 | 0 | } |
367 | 0 | }, |
368 | 0 | tablet_id); |
369 | 0 | } |
370 | 0 | } |
371 | 0 | } |
372 | 0 | } |
373 | 0 | g_file_cache_once_or_periodic_warm_up_finished_tablet_num << 1; |
374 | 0 | } |
375 | | |
376 | 1 | timespec time; |
377 | 1 | time.tv_sec = UnixSeconds() + WAIT_TIME_SECONDS; |
378 | 1 | if (wait->timed_wait(time)) { |
379 | 0 | LOG_WARNING("Warm up {} tablets take a long time", cur_job->tablet_ids.size()); |
380 | 0 | } |
381 | 1 | { |
382 | 1 | std::unique_lock lock(_mtx); |
383 | 1 | _finish_job.push_back(cur_job); |
384 | | // _pending_job_metas may be cleared by a CLEAR_JOB request |
385 | | // so we need to check it again. |
386 | 1 | if (!_pending_job_metas.empty()) { |
387 | | // We can not call pop_front before the job is finished, |
388 | | // because GET_CURRENT_JOB_STATE_AND_LEASE is relying on the pending job size. |
389 | 0 | _pending_job_metas.pop_front(); |
390 | 0 | } |
391 | 1 | } |
392 | 1 | } |
393 | 1 | #endif |
394 | 1 | } |
395 | | |
396 | | JobMeta::JobMeta(const TJobMeta& meta) |
397 | 0 | : be_ip(meta.be_ip), brpc_port(meta.brpc_port), tablet_ids(meta.tablet_ids) { |
398 | 0 | switch (meta.download_type) { |
399 | 0 | case TDownloadType::BE: |
400 | 0 | download_type = DownloadType::BE; |
401 | 0 | break; |
402 | 0 | case TDownloadType::S3: |
403 | 0 | download_type = DownloadType::S3; |
404 | 0 | break; |
405 | 0 | } |
406 | 0 | } |
407 | | |
408 | 0 | Status CloudWarmUpManager::check_and_set_job_id(int64_t job_id) { |
409 | 0 | std::lock_guard lock(_mtx); |
410 | 0 | if (_cur_job_id == 0) { |
411 | 0 | _cur_job_id = job_id; |
412 | 0 | } |
413 | 0 | Status st = Status::OK(); |
414 | 0 | if (_cur_job_id != job_id) { |
415 | 0 | st = Status::InternalError("The job {} is running", _cur_job_id); |
416 | 0 | } |
417 | 0 | return st; |
418 | 0 | } |
419 | | |
420 | 0 | Status CloudWarmUpManager::check_and_set_batch_id(int64_t job_id, int64_t batch_id, bool* retry) { |
421 | 0 | std::lock_guard lock(_mtx); |
422 | 0 | Status st = Status::OK(); |
423 | 0 | if (_cur_job_id != 0 && _cur_job_id != job_id) { |
424 | 0 | st = Status::InternalError("The job {} is not current job, current job is {}", job_id, |
425 | 0 | _cur_job_id); |
426 | 0 | return st; |
427 | 0 | } |
428 | 0 | if (_cur_job_id == 0) { |
429 | 0 | _cur_job_id = job_id; |
430 | 0 | } |
431 | 0 | if (_cur_batch_id == batch_id) { |
432 | 0 | *retry = true; |
433 | 0 | return st; |
434 | 0 | } |
435 | 0 | if (_pending_job_metas.empty()) { |
436 | 0 | _cur_batch_id = batch_id; |
437 | 0 | } else { |
438 | 0 | st = Status::InternalError("The batch {} is not finish", _cur_batch_id); |
439 | 0 | } |
440 | 0 | return st; |
441 | 0 | } |
442 | | |
443 | 0 | void CloudWarmUpManager::add_job(const std::vector<TJobMeta>& job_metas) { |
444 | 0 | { |
445 | 0 | std::lock_guard lock(_mtx); |
446 | 0 | std::for_each(job_metas.begin(), job_metas.end(), [this](const TJobMeta& meta) { |
447 | 0 | _pending_job_metas.emplace_back(std::make_shared<JobMeta>(meta)); |
448 | 0 | g_file_cache_once_or_periodic_warm_up_submitted_tablet_num << meta.tablet_ids.size(); |
449 | 0 | }); |
450 | 0 | } |
451 | 0 | _cond.notify_all(); |
452 | 0 | } |
453 | | |
454 | | #ifdef BE_TEST |
455 | | void CloudWarmUpManager::consumer_job() { |
456 | | { |
457 | | std::unique_lock lock(_mtx); |
458 | | _finish_job.push_back(_pending_job_metas.front()); |
459 | | _pending_job_metas.pop_front(); |
460 | | } |
461 | | } |
462 | | |
463 | | #endif |
464 | | |
465 | 0 | std::tuple<int64_t, int64_t, int64_t, int64_t> CloudWarmUpManager::get_current_job_state() { |
466 | 0 | std::lock_guard lock(_mtx); |
467 | 0 | return std::make_tuple(_cur_job_id, _cur_batch_id, _pending_job_metas.size(), |
468 | 0 | _finish_job.size()); |
469 | 0 | } |
470 | | |
471 | 0 | Status CloudWarmUpManager::clear_job(int64_t job_id) { |
472 | 0 | std::lock_guard lock(_mtx); |
473 | 0 | Status st = Status::OK(); |
474 | 0 | if (job_id == _cur_job_id) { |
475 | 0 | _cur_job_id = 0; |
476 | 0 | _cur_batch_id = -1; |
477 | 0 | _pending_job_metas.clear(); |
478 | 0 | _finish_job.clear(); |
479 | 0 | } else { |
480 | 0 | st = Status::InternalError("The job {} is not current job, current job is {}", job_id, |
481 | 0 | _cur_job_id); |
482 | 0 | } |
483 | 0 | return st; |
484 | 0 | } |
485 | | |
486 | | Status CloudWarmUpManager::set_event(int64_t job_id, TWarmUpEventType::type event, bool clear, |
487 | 8 | const std::vector<int64_t>* table_ids) { |
488 | 8 | DBUG_EXECUTE_IF("CloudWarmUpManager.set_event.ignore_all", { |
489 | 8 | LOG(INFO) << "Ignore set_event request, job_id=" << job_id << ", event=" << event |
490 | 8 | << ", clear=" << clear; |
491 | 8 | return Status::OK(); |
492 | 8 | }); |
493 | 8 | std::lock_guard lock(_mtx); |
494 | 8 | Status st = Status::OK(); |
495 | 8 | if (event == TWarmUpEventType::type::LOAD) { |
496 | 7 | if (clear) { |
497 | 1 | _tablet_replica_cache.erase(job_id); |
498 | 1 | _event_driven_filters.erase(job_id); |
499 | 1 | LOG(INFO) << "Clear event driven sync, job_id=" << job_id << ", event=" << event; |
500 | 6 | } else if (!_tablet_replica_cache.contains(job_id)) { |
501 | 5 | static_cast<void>(_tablet_replica_cache[job_id]); |
502 | 5 | if (table_ids != nullptr) { |
503 | | // table-level filter: set to the given table_id set (may be empty, |
504 | | // meaning all matched tables were deleted — warm up nothing) |
505 | 4 | _event_driven_filters[job_id] = |
506 | 4 | std::unordered_set<int64_t>(table_ids->begin(), table_ids->end()); |
507 | 4 | LOG(INFO) << "Set event driven sync with table filter, job_id=" << job_id |
508 | 4 | << ", event=" << event << ", table_ids_size=" << table_ids->size(); |
509 | 4 | } else { |
510 | | // cluster-level: no filter, warm up all tables |
511 | 1 | _event_driven_filters[job_id] = std::nullopt; |
512 | 1 | LOG(INFO) << "Set event driven sync, job_id=" << job_id << ", event=" << event; |
513 | 1 | } |
514 | 5 | } else if (table_ids != nullptr) { |
515 | | // Update table_ids for an existing job (may be empty) |
516 | 1 | _event_driven_filters[job_id] = |
517 | 1 | std::unordered_set<int64_t>(table_ids->begin(), table_ids->end()); |
518 | 1 | LOG(INFO) << "Updated table filter for event driven sync, job_id=" << job_id |
519 | 1 | << ", table_ids_size=" << table_ids->size(); |
520 | 1 | } |
521 | 7 | } else { |
522 | 1 | st = Status::InternalError("The event {} is not supported yet", event); |
523 | 1 | } |
524 | 8 | return st; |
525 | 8 | } |
526 | | |
527 | | std::vector<JobReplicaInfo> CloudWarmUpManager::get_replica_info(int64_t tablet_id, |
528 | | int64_t table_id, |
529 | | bool bypass_cache, |
530 | 79.5k | bool& cache_hit) { |
531 | 79.5k | std::vector<JobReplicaInfo> replicas; |
532 | 79.5k | std::vector<int64_t> cancelled_jobs; |
533 | 79.5k | std::lock_guard<std::mutex> lock(_mtx); |
534 | 79.5k | cache_hit = false; |
535 | 79.5k | for (auto& [job_id, cache] : _tablet_replica_cache) { |
536 | | // Check table-level filter: skip this job if table_id doesn't match |
537 | | // table_id == 0 means the caller doesn't have table context (e.g., recycle_cache), |
538 | | // so skip filtering |
539 | 4 | if (table_id != 0) { |
540 | 2 | auto filter_it = _event_driven_filters.find(job_id); |
541 | 2 | if (filter_it != _event_driven_filters.end() && filter_it->second.has_value()) { |
542 | 2 | if (filter_it->second->find(table_id) == filter_it->second->end()) { |
543 | 1 | VLOG_DEBUG << "get_replica_info: table_id=" << table_id |
544 | 0 | << " not in filter for job_id=" << job_id << ", skipping"; |
545 | 1 | continue; |
546 | 1 | } |
547 | 2 | } |
548 | 2 | } |
549 | | |
550 | 3 | if (!bypass_cache) { |
551 | 3 | auto it = cache.find(tablet_id); |
552 | 3 | if (it != cache.end()) { |
553 | | // check ttl expire |
554 | 3 | auto now = std::chrono::steady_clock::now(); |
555 | 3 | auto sec = std::chrono::duration_cast<std::chrono::seconds>(now - it->second.first); |
556 | 3 | if (sec.count() < config::warmup_tablet_replica_info_cache_ttl_sec) { |
557 | 3 | replicas.push_back(JobReplicaInfo {job_id, it->second.second}); |
558 | 3 | VLOG_DEBUG << "get_replica_info: cache hit, tablet_id=" << tablet_id |
559 | 0 | << ", job_id=" << job_id; |
560 | 3 | cache_hit = true; |
561 | 3 | continue; |
562 | 3 | } else { |
563 | 0 | LOG(INFO) << "get_replica_info: cache expired, tablet_id=" << tablet_id |
564 | 0 | << ", job_id=" << job_id; |
565 | 0 | cache.erase(it); |
566 | 0 | } |
567 | 3 | } |
568 | 3 | LOG(INFO) << "get_replica_info: cache miss, tablet_id=" << tablet_id |
569 | 0 | << ", job_id=" << job_id; |
570 | 0 | } |
571 | | |
572 | 0 | if (!cache_hit) { |
573 | | // We are trying to save one retry by refresh all the remaining caches |
574 | 0 | bypass_cache = true; |
575 | 0 | } |
576 | 0 | ClusterInfo* cluster_info = ExecEnv::GetInstance()->cluster_info(); |
577 | 0 | if (cluster_info == nullptr) { |
578 | 0 | LOG(WARNING) << "get_replica_info: have not get FE Master heartbeat yet, job_id=" |
579 | 0 | << job_id; |
580 | 0 | continue; |
581 | 0 | } |
582 | 0 | TNetworkAddress master_addr = cluster_info->master_fe_addr; |
583 | 0 | if (master_addr.hostname == "" || master_addr.port == 0) { |
584 | 0 | LOG(WARNING) << "get_replica_info: have not get FE Master heartbeat yet, job_id=" |
585 | 0 | << job_id; |
586 | 0 | continue; |
587 | 0 | } |
588 | | |
589 | 0 | TGetTabletReplicaInfosRequest request; |
590 | 0 | TGetTabletReplicaInfosResult result; |
591 | 0 | request.warm_up_job_id = job_id; |
592 | 0 | request.__isset.warm_up_job_id = true; |
593 | 0 | request.tablet_ids.emplace_back(tablet_id); |
594 | 0 | Status rpc_st = ThriftRpcHelper::rpc<FrontendServiceClient>( |
595 | 0 | master_addr.hostname, master_addr.port, |
596 | 0 | [&request, &result](FrontendServiceConnection& client) { |
597 | 0 | client->getTabletReplicaInfos(result, request); |
598 | 0 | }); |
599 | |
|
600 | 0 | if (!rpc_st.ok()) { |
601 | 0 | LOG(WARNING) << "get_replica_info: rpc failed error=" << rpc_st |
602 | 0 | << ", tablet id=" << tablet_id << ", job_id=" << job_id; |
603 | 0 | continue; |
604 | 0 | } |
605 | | |
606 | 0 | auto st = Status::create(result.status); |
607 | 0 | if (!st.ok()) { |
608 | 0 | if (st.is<ErrorCode::CANCELLED>()) { |
609 | 0 | LOG(INFO) << "get_replica_info: warm up job cancelled, tablet_id=" << tablet_id |
610 | 0 | << ", job_id=" << job_id; |
611 | 0 | cancelled_jobs.push_back(job_id); |
612 | 0 | } else { |
613 | 0 | LOG(WARNING) << "get_replica_info: failed status=" << st |
614 | 0 | << ", tablet id=" << tablet_id << ", job_id=" << job_id; |
615 | 0 | } |
616 | 0 | continue; |
617 | 0 | } |
618 | 0 | VLOG_DEBUG << "get_replica_info: got " << result.tablet_replica_infos.size() |
619 | 0 | << " tablets, tablet id=" << tablet_id << ", job_id=" << job_id; |
620 | |
|
621 | 0 | for (const auto& it : result.tablet_replica_infos) { |
622 | 0 | auto tid = it.first; |
623 | 0 | VLOG_DEBUG << "get_replica_info: got " << it.second.size() |
624 | 0 | << " replica_infos, tablet id=" << tid << ", job_id=" << job_id; |
625 | 0 | for (const auto& replica : it.second) { |
626 | 0 | cache[tid] = std::make_pair(std::chrono::steady_clock::now(), replica); |
627 | 0 | replicas.push_back(JobReplicaInfo {job_id, replica}); |
628 | 0 | LOG(INFO) << "get_replica_info: cache add, tablet_id=" << tid |
629 | 0 | << ", job_id=" << job_id; |
630 | 0 | } |
631 | 0 | } |
632 | 0 | } |
633 | 79.5k | for (auto job_id : cancelled_jobs) { |
634 | 0 | LOG(INFO) << "get_replica_info: erasing cancelled job, job_id=" << job_id; |
635 | 0 | _tablet_replica_cache.erase(job_id); |
636 | 0 | } |
637 | 18.4E | VLOG_DEBUG << "get_replica_info: return " << replicas.size() |
638 | 18.4E | << " replicas, tablet id=" << tablet_id; |
639 | 79.5k | return replicas; |
640 | 79.5k | } |
641 | | |
642 | | void CloudWarmUpManager::warm_up_rowset(RowsetMeta& rs_meta, int64_t table_id, |
643 | 73.8k | int64_t sync_wait_timeout_ms) { |
644 | 73.8k | if (sync_wait_timeout_ms <= 0) { |
645 | 73.8k | auto rs_meta_pb = std::make_shared<RowsetMetaPB>(rs_meta.get_rowset_pb()); |
646 | 73.8k | auto st = _thread_pool_token->submit_func([this, rs_meta_pb, table_id, |
647 | 73.9k | sync_wait_timeout_ms]() { |
648 | 73.9k | RowsetMeta async_rs_meta; |
649 | 73.9k | bool init_succeed = async_rs_meta.init_from_pb(*rs_meta_pb); |
650 | 73.9k | TEST_SYNC_POINT_CALLBACK("CloudWarmUpManager::warm_up_rowset.async_init_from_pb", |
651 | 73.9k | &init_succeed); |
652 | 73.9k | if (!init_succeed) { |
653 | 1 | LOG(WARNING) << "Failed to init rowset meta when warming up rowset asynchronously"; |
654 | 1 | return; |
655 | 1 | } |
656 | 73.9k | _warm_up_rowset(async_rs_meta, table_id, sync_wait_timeout_ms); |
657 | 73.9k | }); |
658 | 73.8k | if (!st.ok()) { |
659 | 0 | LOG(WARNING) << "Failed to submit warm up rowset task: " << st; |
660 | 0 | file_cache_warm_up_failed_task_num << 1; |
661 | 0 | } |
662 | 73.8k | return; |
663 | 73.8k | } |
664 | | |
665 | 18.4E | bthread::Mutex mu; |
666 | 18.4E | bthread::ConditionVariable cv; |
667 | 18.4E | bool finished = false; |
668 | 18.4E | std::unique_lock<bthread::Mutex> lock(mu); |
669 | 18.4E | auto st = _thread_pool_token->submit_func([&, this]() { |
670 | 1 | _warm_up_rowset(rs_meta, table_id, sync_wait_timeout_ms); |
671 | 1 | std::unique_lock<bthread::Mutex> l(mu); |
672 | 1 | finished = true; |
673 | 1 | cv.notify_one(); |
674 | 1 | }); |
675 | 18.4E | if (!st.ok()) { |
676 | 0 | LOG(WARNING) << "Failed to submit warm up rowset task: " << st; |
677 | 0 | file_cache_warm_up_failed_task_num << 1; |
678 | 18.4E | } else { |
679 | 18.4E | while (!finished) { |
680 | 2 | TEST_SYNC_POINT_CALLBACK("CloudWarmUpManager::warm_up_rowset.before_wait", &cv); |
681 | 2 | cv.wait(lock); |
682 | 2 | } |
683 | 18.4E | } |
684 | 18.4E | } |
685 | | |
686 | | void CloudWarmUpManager::_warm_up_rowset(RowsetMeta& rs_meta, int64_t table_id, |
687 | 73.7k | int64_t sync_wait_timeout_ms) { |
688 | 73.7k | TEST_SYNC_POINT_CALLBACK("CloudWarmUpManager::_warm_up_rowset.enter", &rs_meta, |
689 | 73.7k | &sync_wait_timeout_ms); |
690 | 73.7k | bool cache_hit = false; |
691 | 73.7k | auto replicas = get_replica_info(rs_meta.tablet_id(), table_id, false, cache_hit); |
692 | 73.8k | if (replicas.empty()) { |
693 | 73.8k | VLOG_DEBUG << "There is no need to warmup tablet=" << rs_meta.tablet_id() |
694 | 6 | << ", skipping rowset=" << rs_meta.rowset_id().to_string(); |
695 | 73.8k | g_file_cache_event_driven_warm_up_skipped_rowset_num << 1; |
696 | 73.8k | return; |
697 | 73.8k | } |
698 | 18.4E | Status st = _do_warm_up_rowset(rs_meta, table_id, replicas, sync_wait_timeout_ms, !cache_hit); |
699 | 18.4E | if (cache_hit && !st.ok() && st.is<ErrorCode::TABLE_NOT_FOUND>()) { |
700 | 0 | replicas = get_replica_info(rs_meta.tablet_id(), table_id, true, cache_hit); |
701 | 0 | st = _do_warm_up_rowset(rs_meta, table_id, replicas, sync_wait_timeout_ms, true); |
702 | 0 | } |
703 | 18.4E | if (!st.ok()) { |
704 | 0 | LOG(WARNING) << "Failed to warm up rowset, tablet_id=" << rs_meta.tablet_id() |
705 | 0 | << ", rowset_id=" << rs_meta.rowset_id().to_string() << ", status=" << st; |
706 | 0 | } |
707 | 18.4E | } |
708 | | |
709 | | Status CloudWarmUpManager::_build_warm_up_rowset_result( |
710 | | const std::vector<WarmUpRowsetFailure>& failures, size_t replica_count, int64_t tablet_id, |
711 | 3 | int64_t table_id, const std::string& rowset_id) { |
712 | 3 | if (failures.empty()) { |
713 | 1 | return Status::OK(); |
714 | 1 | } |
715 | | |
716 | 2 | int code = failures.front().code; |
717 | 2 | std::string failure_msg; |
718 | 6 | for (size_t i = 0; i < failures.size(); ++i) { |
719 | 4 | if (failures[i].code == ErrorCode::TABLE_NOT_FOUND) { |
720 | 1 | code = ErrorCode::TABLE_NOT_FOUND; |
721 | 1 | } |
722 | 4 | if (i > 0) { |
723 | 2 | failure_msg.append("; "); |
724 | 2 | } |
725 | 4 | failure_msg.append(failures[i].reason); |
726 | 4 | } |
727 | | |
728 | 2 | return Status::Error(code, |
729 | 2 | "warm up rowset failed on {}/{} replicas, tablet_id={}, table_id={}, " |
730 | 2 | "rowset_id={}, failures=[{}]", |
731 | 2 | failures.size(), replica_count, tablet_id, table_id, rowset_id, |
732 | 2 | failure_msg); |
733 | 3 | } |
734 | | |
735 | | Status CloudWarmUpManager::_do_warm_up_rowset(RowsetMeta& rs_meta, int64_t table_id, |
736 | | std::vector<JobReplicaInfo>& replicas, |
737 | | int64_t sync_wait_timeout_ms, |
738 | 0 | bool skip_existence_check) { |
739 | 0 | auto tablet_id = rs_meta.tablet_id(); |
740 | 0 | int64_t now_ts = std::chrono::duration_cast<std::chrono::microseconds>( |
741 | 0 | std::chrono::system_clock::now().time_since_epoch()) |
742 | 0 | .count(); |
743 | 0 | g_file_cache_warm_up_rowset_last_call_unix_ts.set_value(now_ts); |
744 | 0 | std::vector<WarmUpRowsetFailure> failures; |
745 | 0 | auto add_failure = [&failures](const JobReplicaInfo& info, const std::string& target, |
746 | 0 | const Status& st) { |
747 | 0 | failures.push_back(WarmUpRowsetFailure { |
748 | 0 | .code = st.code(), |
749 | 0 | .reason = "job_id=" + std::to_string(info.job_id) + |
750 | 0 | ", backend_id=" + std::to_string(info.replica.backend_id) + |
751 | 0 | ", target=" + target + ", status=" + st.to_string_no_stack()}); |
752 | 0 | }; |
753 | |
|
754 | 0 | for (auto& info : replicas) { |
755 | 0 | std::string job_id_str = std::to_string(info.job_id); |
756 | 0 | std::string target = get_host_port(info.replica.host, info.replica.brpc_port); |
757 | 0 | int64_t trigger_ts_ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
758 | 0 | std::chrono::system_clock::now().time_since_epoch()) |
759 | 0 | .count(); |
760 | |
|
761 | 0 | PWarmUpRowsetRequest request; |
762 | 0 | request.add_rowset_metas()->CopyFrom(rs_meta.get_rowset_pb()); |
763 | 0 | request.set_unix_ts_us(now_ts); |
764 | 0 | request.set_sync_wait_timeout_ms(sync_wait_timeout_ms); |
765 | 0 | request.set_skip_existence_check(skip_existence_check); |
766 | 0 | request.set_job_id(info.job_id); |
767 | 0 | request.set_upstream_trigger_ts_ms(trigger_ts_ms); |
768 | | |
769 | | // send sync request |
770 | 0 | std::string host = info.replica.host; |
771 | 0 | auto dns_cache = ExecEnv::GetInstance()->dns_cache(); |
772 | 0 | if (dns_cache == nullptr) { |
773 | 0 | LOG(WARNING) << "DNS cache is not initialized, skipping hostname resolve"; |
774 | 0 | } else if (!is_valid_ip(info.replica.host)) { |
775 | 0 | Status status = dns_cache->get(info.replica.host, &host); |
776 | 0 | if (!status.ok()) { |
777 | 0 | LOG(WARNING) << "failed to get ip from host " << info.replica.host << ": " |
778 | 0 | << status.to_string(); |
779 | 0 | add_failure(info, target, status); |
780 | 0 | continue; |
781 | 0 | } |
782 | 0 | } |
783 | 0 | std::string brpc_addr = get_host_port(host, info.replica.brpc_port); |
784 | 0 | Status st = Status::OK(); |
785 | 0 | std::shared_ptr<PBackendService_Stub> brpc_stub = |
786 | 0 | ExecEnv::GetInstance()->brpc_internal_client_cache()->get_new_client_no_cache( |
787 | 0 | brpc_addr); |
788 | 0 | if (!brpc_stub) { |
789 | 0 | st = Status::RpcError("Address {} is wrong", brpc_addr); |
790 | 0 | add_failure(info, target, st); |
791 | 0 | continue; |
792 | 0 | } |
793 | | |
794 | | // update metrics |
795 | 0 | auto schema_ptr = rs_meta.tablet_schema(); |
796 | 0 | auto idx_version = schema_ptr->get_inverted_index_storage_format(); |
797 | 0 | for (int64_t segment_id = 0; segment_id < rs_meta.num_segments(); segment_id++) { |
798 | 0 | auto seg_size = rs_meta.segment_file_size(cast_set<int>(segment_id)); |
799 | |
|
800 | 0 | g_file_cache_event_driven_warm_up_requested_segment_num << 1; |
801 | 0 | g_warmup_ed_requested_segment_num.put({job_id_str}, 1); |
802 | |
|
803 | 0 | g_file_cache_event_driven_warm_up_requested_segment_size << seg_size; |
804 | 0 | g_warmup_ed_requested_segment_size.put({job_id_str}, seg_size); |
805 | |
|
806 | 0 | if (schema_ptr->has_inverted_index() || schema_ptr->has_ann_index()) { |
807 | 0 | if (idx_version == InvertedIndexStorageFormatPB::V1) { |
808 | 0 | auto&& inverted_index_info = |
809 | 0 | rs_meta.inverted_index_file_info(cast_set<int>(segment_id)); |
810 | 0 | if (inverted_index_info.index_info().empty()) { |
811 | 0 | VLOG_DEBUG << "No index info available for segment " << segment_id; |
812 | 0 | continue; |
813 | 0 | } |
814 | 0 | for (const auto& idx_info : inverted_index_info.index_info()) { |
815 | 0 | g_file_cache_event_driven_warm_up_requested_index_num << 1; |
816 | 0 | g_warmup_ed_requested_index_num.put({job_id_str}, 1); |
817 | |
|
818 | 0 | if (idx_info.index_file_size() != -1) { |
819 | 0 | g_file_cache_event_driven_warm_up_requested_index_size |
820 | 0 | << idx_info.index_file_size(); |
821 | 0 | g_warmup_ed_requested_index_size.put({job_id_str}, |
822 | 0 | idx_info.index_file_size()); |
823 | 0 | } else { |
824 | 0 | VLOG_DEBUG << "Invalid index_file_size for segment_id " << segment_id |
825 | 0 | << ", index_id " << idx_info.index_id(); |
826 | 0 | } |
827 | 0 | } |
828 | 0 | } else { // InvertedIndexStorageFormatPB::V2 |
829 | 0 | auto&& inverted_index_info = |
830 | 0 | rs_meta.inverted_index_file_info(cast_set<int>(segment_id)); |
831 | 0 | g_file_cache_event_driven_warm_up_requested_index_num << 1; |
832 | 0 | g_warmup_ed_requested_index_num.put({job_id_str}, 1); |
833 | |
|
834 | 0 | if (inverted_index_info.has_index_size()) { |
835 | 0 | g_file_cache_event_driven_warm_up_requested_index_size |
836 | 0 | << inverted_index_info.index_size(); |
837 | 0 | g_warmup_ed_requested_index_size.put({job_id_str}, |
838 | 0 | inverted_index_info.index_size()); |
839 | 0 | } else { |
840 | 0 | VLOG_DEBUG << "index_size is not set for segment " << segment_id; |
841 | 0 | } |
842 | 0 | } |
843 | 0 | } |
844 | 0 | } |
845 | | |
846 | | // Update last trigger timestamp |
847 | 0 | auto* trigger_ts = |
848 | 0 | g_warmup_ed_last_trigger_ts.get_stats(std::list<std::string> {job_id_str}); |
849 | 0 | if (trigger_ts) { |
850 | 0 | trigger_ts->set_value(trigger_ts_ms); |
851 | 0 | } |
852 | |
|
853 | 0 | brpc::Controller cntl; |
854 | 0 | if (sync_wait_timeout_ms > 0) { |
855 | 0 | cntl.set_timeout_ms(sync_wait_timeout_ms + 1000); |
856 | 0 | } |
857 | 0 | PWarmUpRowsetResponse response; |
858 | 0 | MonotonicStopWatch watch; |
859 | 0 | watch.start(); |
860 | 0 | brpc_stub->warm_up_rowset(&cntl, &request, &response, nullptr); |
861 | 0 | if (cntl.Failed()) { |
862 | 0 | LOG_WARNING("warm up rowset {} for tablet {} failed, rpc error: {}", |
863 | 0 | rs_meta.rowset_id().to_string(), tablet_id, cntl.ErrorText()); |
864 | 0 | add_failure(info, target, Status::RpcError(cntl.ErrorText())); |
865 | 0 | continue; |
866 | 0 | } |
867 | 0 | if (sync_wait_timeout_ms > 0) { |
868 | 0 | auto cost_us = watch.elapsed_time_microseconds(); |
869 | 0 | VLOG_DEBUG << "warm up rowset wait for compaction: " << cost_us << " us"; |
870 | 0 | if (cost_us / 1000 > sync_wait_timeout_ms) { |
871 | 0 | LOG_WARNING( |
872 | 0 | "Warm up rowset {} for tabelt {} wait for compaction timeout, takes {} ms", |
873 | 0 | rs_meta.rowset_id().to_string(), tablet_id, cost_us / 1000); |
874 | 0 | } |
875 | 0 | g_file_cache_warm_up_rowset_wait_for_compaction_latency << cost_us; |
876 | 0 | } |
877 | 0 | auto status = Status::create(response.status()); |
878 | 0 | if (response.has_status() && !status.ok()) { |
879 | 0 | LOG(INFO) << "warm_up_rowset failed, tablet_id=" << rs_meta.tablet_id() |
880 | 0 | << ", rowset_id=" << rs_meta.rowset_id().to_string() |
881 | 0 | << ", target=" << info.replica.host << ", skip_existence_check" |
882 | 0 | << skip_existence_check << ", status=" << status; |
883 | 0 | add_failure(info, target, status); |
884 | 0 | } |
885 | 0 | } |
886 | 0 | return _build_warm_up_rowset_result(failures, replicas.size(), tablet_id, table_id, |
887 | 0 | rs_meta.rowset_id().to_string()); |
888 | 0 | } |
889 | | |
890 | | void CloudWarmUpManager::recycle_cache(int64_t tablet_id, |
891 | 5.65k | const std::vector<RecycledRowsets>& rowsets) { |
892 | 5.65k | bthread::Mutex mu; |
893 | 5.65k | bthread::ConditionVariable cv; |
894 | 5.65k | std::unique_lock<bthread::Mutex> lock(mu); |
895 | 5.65k | auto st = _thread_pool_token->submit_func([&, this]() { |
896 | 5.65k | std::unique_lock<bthread::Mutex> l(mu); |
897 | 5.65k | _recycle_cache(tablet_id, rowsets); |
898 | 5.65k | cv.notify_one(); |
899 | 5.65k | }); |
900 | 5.65k | if (!st.ok()) { |
901 | 0 | LOG(WARNING) << "Failed to submit recycle cache task, tablet_id=" << tablet_id |
902 | 0 | << ", error=" << st; |
903 | 5.65k | } else { |
904 | 5.65k | cv.wait(lock); |
905 | 5.65k | } |
906 | 5.65k | } |
907 | | |
908 | | void CloudWarmUpManager::_recycle_cache(int64_t tablet_id, |
909 | 5.65k | const std::vector<RecycledRowsets>& rowsets) { |
910 | 5.65k | LOG(INFO) << "recycle_cache: tablet_id=" << tablet_id << ", num_rowsets=" << rowsets.size(); |
911 | 5.65k | bool cache_hit = false; |
912 | 5.65k | auto replicas = get_replica_info(tablet_id, /*table_id=*/0, false, cache_hit); |
913 | 5.65k | if (replicas.empty()) { |
914 | 5.65k | return; |
915 | 5.65k | } |
916 | | |
917 | 0 | PRecycleCacheRequest request; |
918 | 0 | for (const auto& rowset : rowsets) { |
919 | 0 | RecycleCacheMeta* meta = request.add_cache_metas(); |
920 | 0 | meta->set_tablet_id(tablet_id); |
921 | 0 | meta->set_rowset_id(rowset.rowset_id.to_string()); |
922 | 0 | meta->set_num_segments(rowset.num_segments); |
923 | 0 | for (const auto& name : rowset.index_file_names) { |
924 | 0 | meta->add_index_file_names(name); |
925 | 0 | } |
926 | 0 | g_file_cache_recycle_cache_requested_segment_num << rowset.num_segments; |
927 | 0 | g_file_cache_recycle_cache_requested_index_num << rowset.index_file_names.size(); |
928 | 0 | } |
929 | 0 | auto dns_cache = ExecEnv::GetInstance()->dns_cache(); |
930 | 0 | for (auto& replica : replicas) { |
931 | | // send sync request |
932 | 0 | std::string host = replica.replica.host; |
933 | 0 | if (dns_cache == nullptr) { |
934 | 0 | LOG(WARNING) << "DNS cache is not initialized, skipping hostname resolve"; |
935 | 0 | } else if (!is_valid_ip(replica.replica.host)) { |
936 | 0 | Status status = dns_cache->get(replica.replica.host, &host); |
937 | 0 | if (!status.ok()) { |
938 | 0 | LOG(WARNING) << "failed to get ip from host " << replica.replica.host << ": " |
939 | 0 | << status.to_string(); |
940 | 0 | return; |
941 | 0 | } |
942 | 0 | } |
943 | 0 | std::string brpc_addr = get_host_port(host, replica.replica.brpc_port); |
944 | 0 | Status st = Status::OK(); |
945 | 0 | std::shared_ptr<PBackendService_Stub> brpc_stub = |
946 | 0 | ExecEnv::GetInstance()->brpc_internal_client_cache()->get_new_client_no_cache( |
947 | 0 | brpc_addr); |
948 | 0 | if (!brpc_stub) { |
949 | 0 | st = Status::RpcError("Address {} is wrong", brpc_addr); |
950 | 0 | continue; |
951 | 0 | } |
952 | 0 | brpc::Controller cntl; |
953 | 0 | PRecycleCacheResponse response; |
954 | 0 | brpc_stub->recycle_cache(&cntl, &request, &response, nullptr); |
955 | 0 | } |
956 | 0 | } |
957 | | |
958 | | // Balance warm up cache management methods implementation |
959 | | void CloudWarmUpManager::record_balanced_tablet(int64_t tablet_id, const std::string& host, |
960 | 0 | int32_t brpc_port) { |
961 | 0 | auto& shard = get_shard(tablet_id); |
962 | 0 | std::lock_guard<std::mutex> lock(shard.mtx); |
963 | 0 | JobMeta meta; |
964 | 0 | meta.be_ip = host; |
965 | 0 | meta.brpc_port = brpc_port; |
966 | 0 | shard.tablets.emplace(tablet_id, std::move(meta)); |
967 | 0 | g_balance_tablet_be_mapping_size << 1; |
968 | 0 | schedule_remove_balanced_tablet(tablet_id); |
969 | 0 | VLOG_DEBUG << "Recorded balanced warm up cache tablet: tablet_id=" << tablet_id |
970 | 0 | << ", host=" << host << ":" << brpc_port; |
971 | 0 | } |
972 | | |
973 | 0 | void CloudWarmUpManager::schedule_remove_balanced_tablet(int64_t tablet_id) { |
974 | | // Use std::make_unique to avoid raw pointer allocation |
975 | 0 | auto tablet_id_ptr = std::make_unique<int64_t>(tablet_id); |
976 | 0 | unsigned long expired_ms = g_tablet_report_inactive_duration_ms; |
977 | 0 | if (doris::config::cache_read_from_peer_expired_seconds > 0 && |
978 | 0 | doris::config::cache_read_from_peer_expired_seconds <= |
979 | 0 | g_tablet_report_inactive_duration_ms / 1000) { |
980 | 0 | expired_ms = doris::config::cache_read_from_peer_expired_seconds * 1000; |
981 | 0 | } |
982 | 0 | bthread_timer_t timer_id; |
983 | | // ATTN: The timer callback will reclaim ownership of the tablet_id_ptr, so we need to release it after the timer is added. |
984 | 0 | if (const int rc = bthread_timer_add(&timer_id, butil::milliseconds_from_now(expired_ms), |
985 | 0 | clean_up_expired_mappings, tablet_id_ptr.get()); |
986 | 0 | rc == 0) { |
987 | 0 | tablet_id_ptr.release(); |
988 | 0 | } else { |
989 | 0 | LOG(WARNING) << "Fail to add timer for clean up expired mappings for tablet_id=" |
990 | 0 | << tablet_id << " rc=" << rc; |
991 | 0 | } |
992 | 0 | } |
993 | | |
994 | 0 | void CloudWarmUpManager::clean_up_expired_mappings(void* arg) { |
995 | 0 | std::unique_ptr<int64_t> tid(static_cast<int64_t*>(arg)); |
996 | 0 | auto& manager = ExecEnv::GetInstance()->storage_engine().to_cloud().cloud_warm_up_manager(); |
997 | 0 | manager.remove_balanced_tablet(*tid); |
998 | 0 | VLOG_DEBUG << "Removed expired balanced warm up cache tablet: tablet_id=" << *tid; |
999 | 0 | } |
1000 | | |
1001 | | std::optional<std::pair<std::string, int32_t>> CloudWarmUpManager::get_balanced_tablet_info( |
1002 | 90.5k | int64_t tablet_id) { |
1003 | 90.5k | auto& shard = get_shard(tablet_id); |
1004 | 90.5k | std::lock_guard<std::mutex> lock(shard.mtx); |
1005 | 90.5k | auto it = shard.tablets.find(tablet_id); |
1006 | 90.5k | if (it == shard.tablets.end()) { |
1007 | 90.5k | return std::nullopt; |
1008 | 90.5k | } |
1009 | 18.4E | return std::make_pair(it->second.be_ip, it->second.brpc_port); |
1010 | 90.5k | } |
1011 | | |
1012 | 0 | void CloudWarmUpManager::remove_balanced_tablet(int64_t tablet_id) { |
1013 | 0 | auto& shard = get_shard(tablet_id); |
1014 | 0 | std::lock_guard<std::mutex> lock(shard.mtx); |
1015 | 0 | auto it = shard.tablets.find(tablet_id); |
1016 | 0 | if (it != shard.tablets.end()) { |
1017 | 0 | shard.tablets.erase(it); |
1018 | 0 | g_balance_tablet_be_mapping_size << -1; |
1019 | 0 | VLOG_DEBUG << "Removed balanced warm up cache tablet by timer, tablet_id=" << tablet_id; |
1020 | 0 | } |
1021 | 0 | } |
1022 | | |
1023 | 0 | void CloudWarmUpManager::remove_balanced_tablets(const std::vector<int64_t>& tablet_ids) { |
1024 | | // Group tablet_ids by shard to minimize lock contention |
1025 | 0 | std::array<std::vector<int64_t>, SHARD_COUNT> shard_groups; |
1026 | 0 | for (int64_t tablet_id : tablet_ids) { |
1027 | 0 | shard_groups[get_shard_index(tablet_id)].push_back(tablet_id); |
1028 | 0 | } |
1029 | | |
1030 | | // Process each shard |
1031 | 0 | for (size_t i = 0; i < SHARD_COUNT; ++i) { |
1032 | 0 | if (shard_groups[i].empty()) continue; |
1033 | | |
1034 | 0 | auto& shard = _balanced_tablets_shards[i]; |
1035 | 0 | std::lock_guard<std::mutex> lock(shard.mtx); |
1036 | 0 | for (int64_t tablet_id : shard_groups[i]) { |
1037 | 0 | auto it = shard.tablets.find(tablet_id); |
1038 | 0 | if (it != shard.tablets.end()) { |
1039 | 0 | shard.tablets.erase(it); |
1040 | 0 | g_balance_tablet_be_mapping_size << -1; |
1041 | 0 | VLOG_DEBUG << "Removed balanced warm up cache tablet: tablet_id=" << tablet_id; |
1042 | 0 | } |
1043 | 0 | } |
1044 | 0 | } |
1045 | 0 | } |
1046 | | |
1047 | | std::unordered_map<int64_t, std::pair<std::string, int32_t>> |
1048 | 0 | CloudWarmUpManager::get_all_balanced_tablets() const { |
1049 | 0 | std::unordered_map<int64_t, std::pair<std::string, int32_t>> result; |
1050 | | |
1051 | | // Lock all shards to get consistent snapshot |
1052 | 0 | std::array<std::unique_lock<std::mutex>, SHARD_COUNT> locks; |
1053 | 0 | for (size_t i = 0; i < SHARD_COUNT; ++i) { |
1054 | 0 | locks[i] = std::unique_lock<std::mutex>(_balanced_tablets_shards[i].mtx); |
1055 | 0 | } |
1056 | |
|
1057 | 0 | for (const auto& shard : _balanced_tablets_shards) { |
1058 | 0 | for (const auto& [tablet_id, entry] : shard.tablets) { |
1059 | 0 | result.emplace(tablet_id, std::make_pair(entry.be_ip, entry.brpc_port)); |
1060 | 0 | } |
1061 | 0 | } |
1062 | 0 | return result; |
1063 | 0 | } |
1064 | | |
1065 | | } // namespace doris |