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