Coverage Report

Created: 2026-06-17 03:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/cloud_backend_service.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_backend_service.h"
19
20
#include <brpc/controller.h>
21
22
#include "cloud/cloud_storage_engine.h"
23
#include "cloud/cloud_tablet.h"
24
#include "cloud/cloud_tablet_hotspot.h"
25
#include "cloud/cloud_tablet_mgr.h"
26
#include "cloud/cloud_warm_up_manager.h"
27
#include "common/config.h"
28
#include "common/logging.h"
29
#include "common/status.h"
30
#include "io/cache/block_file_cache_downloader.h"
31
#include "io/cache/block_file_cache_factory.h"
32
#include "load/stream_load/stream_load_context.h"
33
#include "load/stream_load/stream_load_recorder.h"
34
#include "util/brpc_client_cache.h" // BrpcClientCache
35
#include "util/stack_util.h"
36
#include "util/thrift_server.h"
37
38
namespace doris {
39
40
bvar::Adder<uint64_t> g_file_cache_warm_up_cache_async_submitted_segment_num(
41
        "file_cache_warm_up_cache_async_submitted_segment_num");
42
bvar::Adder<uint64_t> g_file_cache_warm_up_cache_async_submitted_task_num(
43
        "file_cache_warm_up_cache_async_submitted_task_num");
44
bvar::Adder<uint64_t> g_file_cache_warm_up_cache_async_submitted_tablet_num(
45
        "file_cache_warm_up_cache_async_submitted_tablet_num");
46
47
CloudBackendService::CloudBackendService(CloudStorageEngine& engine, ExecEnv* exec_env)
48
0
        : BaseBackendService(exec_env), _engine(engine) {}
49
50
0
CloudBackendService::~CloudBackendService() = default;
51
52
0
Status CloudBackendService::start_thrift_dependencies() {
53
0
    _agent_server->cloud_start_workers(_engine, _exec_env);
54
0
    return Status::OK();
55
0
}
56
57
void CloudBackendService::sync_load_for_tablets(TSyncLoadForTabletsResponse&,
58
0
                                                const TSyncLoadForTabletsRequest& request) {
59
0
    auto f = [this, tablet_ids = request.tablet_ids]() {
60
0
        std::for_each(tablet_ids.cbegin(), tablet_ids.cend(), [this](int64_t tablet_id) {
61
0
            CloudTabletSPtr tablet;
62
0
            auto result = _engine.tablet_mgr().get_tablet(tablet_id, true);
63
0
            if (!result.has_value()) {
64
0
                return;
65
0
            }
66
0
            SyncOptions options;
67
0
            options.warmup_delta_data = true;
68
0
            Status st = result.value()->sync_rowsets(options);
69
0
            if (!st.ok()) {
70
0
                LOG_WARNING("failed to sync load for tablet").error(st);
71
0
            }
72
0
        });
73
0
    };
74
0
    static_cast<void>(_engine.sync_load_for_tablets_thread_pool().submit_func(std::move(f)));
75
0
}
76
77
void CloudBackendService::get_top_n_hot_partitions(TGetTopNHotPartitionsResponse& response,
78
0
                                                   const TGetTopNHotPartitionsRequest& request) {
79
0
    _engine.tablet_hotspot().get_top_n_hot_partition(&response.hot_tables);
80
0
    response.file_cache_size = io::FileCacheFactory::instance()->get_capacity();
81
0
    response.__isset.hot_tables = !response.hot_tables.empty();
82
0
}
83
84
void CloudBackendService::warm_up_tablets(TWarmUpTabletsResponse& response,
85
0
                                          const TWarmUpTabletsRequest& request) {
86
0
    Status st;
87
0
    auto& manager = _engine.cloud_warm_up_manager();
88
0
    switch (request.type) {
89
0
    case TWarmUpTabletsRequestType::SET_JOB: {
90
0
        LOG_INFO("receive the warm up request.")
91
0
                .tag("request_type", "SET_JOB")
92
0
                .tag("job_id", request.job_id);
93
0
        if (request.__isset.event) {
94
0
            const std::vector<int64_t>* table_ids_ptr = nullptr;
95
0
            if (request.__isset.table_ids) {
96
0
                table_ids_ptr = &request.table_ids;
97
0
            }
98
0
            st = manager.set_event(request.job_id, request.event, false, table_ids_ptr);
99
0
            if (st.ok()) {
100
0
                break;
101
0
            }
102
0
        } else {
103
0
            st = manager.check_and_set_job_id(request.job_id);
104
0
        }
105
0
        if (!st.ok()) {
106
0
            LOG_WARNING("SET_JOB failed.").error(st);
107
0
            break;
108
0
        }
109
0
        [[fallthrough]];
110
0
    }
111
0
    case TWarmUpTabletsRequestType::SET_BATCH: {
112
0
        LOG_INFO("receive the warm up request.")
113
0
                .tag("request_type", "SET_BATCH")
114
0
                .tag("job_id", request.job_id)
115
0
                .tag("batch_id", request.batch_id)
116
0
                .tag("jobs size", request.job_metas.size())
117
0
                .tag("tablet num of first meta",
118
0
                     request.job_metas.empty() ? 0 : request.job_metas[0].tablet_ids.size());
119
0
        bool retry = false;
120
0
        st = manager.check_and_set_batch_id(request.job_id, request.batch_id, &retry);
121
0
        if (!retry && st) {
122
0
            manager.add_job(request.job_metas);
123
0
        } else {
124
0
            if (retry) {
125
0
                LOG_WARNING("retry the job.")
126
0
                        .tag("job_id", request.job_id)
127
0
                        .tag("batch_id", request.batch_id);
128
0
            } else {
129
0
                LOG_WARNING("SET_BATCH failed.").error(st);
130
0
            }
131
0
        }
132
0
        break;
133
0
    }
134
0
    case TWarmUpTabletsRequestType::GET_CURRENT_JOB_STATE_AND_LEASE: {
135
0
        auto [job_id, batch_id, pending_job_size, finish_job_size] =
136
0
                manager.get_current_job_state();
137
0
        LOG_INFO("receive the warm up request.")
138
0
                .tag("request_type", "GET_CURRENT_JOB_STATE_AND_LEASE")
139
0
                .tag("job_id", job_id)
140
0
                .tag("batch_id", batch_id)
141
0
                .tag("pending_job_size", pending_job_size)
142
0
                .tag("finish_job_size", finish_job_size);
143
0
        response.__set_job_id(job_id);
144
0
        response.__set_batch_id(batch_id);
145
0
        response.__set_pending_job_size(pending_job_size);
146
0
        response.__set_finish_job_size(finish_job_size);
147
0
        break;
148
0
    }
149
0
    case TWarmUpTabletsRequestType::CLEAR_JOB: {
150
0
        LOG_INFO("receive the warm up request.")
151
0
                .tag("request_type", "CLEAR_JOB")
152
0
                .tag("job_id", request.job_id);
153
0
        if (request.__isset.event) {
154
0
            st = manager.set_event(request.job_id, request.event, /* clear: */ true);
155
0
        } else {
156
0
            st = manager.clear_job(request.job_id);
157
0
        }
158
0
        break;
159
0
    }
160
0
    default:
161
0
        DCHECK(false);
162
0
    };
163
0
    st.to_thrift(&response.status);
164
0
}
165
166
static Status run_rpc_get_file_cache_meta(std::shared_ptr<PBackendService_Stub> brpc_stub,
167
                                          const std::string& brpc_addr,
168
                                          PGetFileCacheMetaRequest brpc_request,
169
0
                                          PGetFileCacheMetaResponse& brpc_response) {
170
0
    brpc::Controller cntl;
171
0
    cntl.set_timeout_ms(20 * 1000); // 20s
172
0
    brpc_stub->get_file_cache_meta_by_tablet_id(&cntl, &brpc_request, &brpc_response, nullptr);
173
0
    if (cntl.Failed()) {
174
0
        LOG(WARNING) << "warm_up_cache_async: brpc call failed, addr=" << brpc_addr
175
0
                     << ", error=" << cntl.ErrorText() << ", error code=" << cntl.ErrorCode();
176
0
        return Status::RpcError("{} isn't connected, error code={}", brpc_addr, cntl.ErrorCode());
177
0
    }
178
0
    VLOG_DEBUG << "warm_up_cache_async: request=" << brpc_request.DebugString()
179
0
               << ", response=" << brpc_response.DebugString();
180
0
    g_file_cache_warm_up_cache_async_submitted_segment_num
181
0
            << brpc_response.file_cache_block_metas().size();
182
0
    return Status::OK();
183
0
}
184
185
void CloudBackendService::_warm_up_cache(TWarmUpCacheAsyncResponse& response,
186
0
                                         const TWarmUpCacheAsyncRequest& request) {
187
0
    std::ostringstream oss;
188
0
    oss << "[";
189
0
    for (size_t i = 0; i < request.tablet_ids.size() && i < 10; ++i) {
190
0
        if (i > 0) oss << ",";
191
0
        oss << request.tablet_ids[i];
192
0
    }
193
0
    oss << "]";
194
0
    g_file_cache_warm_up_cache_async_submitted_tablet_num << request.tablet_ids.size();
195
0
    LOG(INFO) << "warm_up_cache_async: enter, request=" << request.host << ":" << request.brpc_port
196
0
              << ", tablets num=" << request.tablet_ids.size() << ", tablet_ids=" << oss.str();
197
198
0
    auto& manager = ExecEnv::GetInstance()->storage_engine().to_cloud().cloud_warm_up_manager();
199
    // Record each tablet in manager
200
0
    for (int64_t tablet_id : request.tablet_ids) {
201
0
        manager.record_balanced_tablet(tablet_id, request.host, request.brpc_port);
202
0
    }
203
204
0
    std::string host = request.host;
205
0
    auto* dns_cache = ExecEnv::GetInstance()->dns_cache();
206
0
    if (dns_cache == nullptr) {
207
0
        LOG(WARNING) << "DNS cache is not initialized, skipping hostname resolve";
208
0
    } else if (!is_valid_ip(request.host)) {
209
0
        Status status = dns_cache->get(request.host, &host);
210
0
        if (!status.ok()) {
211
0
            LOG(WARNING) << "failed to get ip from host " << request.host << ": "
212
0
                         << status.to_string();
213
0
            return;
214
0
        }
215
0
    }
216
0
    std::string brpc_addr = get_host_port(host, request.brpc_port);
217
0
    std::shared_ptr<PBackendService_Stub> brpc_stub =
218
0
            _exec_env->brpc_internal_client_cache()->get_new_client_no_cache(brpc_addr);
219
0
    if (!brpc_stub) {
220
0
        LOG(WARNING) << "warm_up_cache_async: failed to get brpc_stub for addr " << brpc_addr;
221
0
        return;
222
0
    }
223
0
    PGetFileCacheMetaRequest brpc_request;
224
0
    PGetFileCacheMetaResponse brpc_response;
225
0
    for (int64_t tablet_id : request.tablet_ids) {
226
0
        brpc_request.add_tablet_ids(tablet_id);
227
0
    }
228
229
0
    Status rpc_status = run_rpc_get_file_cache_meta(brpc_stub, brpc_addr, std::move(brpc_request),
230
0
                                                    brpc_response);
231
0
    if (rpc_status.ok()) {
232
0
        _engine.file_cache_block_downloader().submit_download_task(
233
0
                std::move(*brpc_response.mutable_file_cache_block_metas()));
234
0
    } else {
235
0
        LOG(WARNING) << "warm_up_cache_async: rpc failed for addr=" << brpc_addr
236
0
                     << ", status=" << rpc_status;
237
0
    }
238
0
}
239
240
void CloudBackendService::warm_up_cache_async(TWarmUpCacheAsyncResponse& response,
241
0
                                              const TWarmUpCacheAsyncRequest& request) {
242
    // just submit the task to the thread pool, no need to wait for the result
243
0
    auto do_warm_up = [this, request, &response]() { this->_warm_up_cache(response, request); };
244
0
    g_file_cache_warm_up_cache_async_submitted_task_num << 1;
245
0
    Status submit_st = _engine.warmup_cache_async_thread_pool().submit_func(std::move(do_warm_up));
246
0
    if (!submit_st.ok()) {
247
0
        LOG(WARNING) << "warm_up_cache_async: fail to submit heavy task to "
248
0
                        "warmup_cache_async_thread_pool, status="
249
0
                     << submit_st.to_string() << ", execute synchronously";
250
0
        do_warm_up();
251
0
    }
252
0
    TStatus t_status;
253
0
    submit_st.to_thrift(&t_status);
254
0
    response.status = std::move(t_status);
255
0
}
256
257
void CloudBackendService::check_warm_up_cache_async(TCheckWarmUpCacheAsyncResponse& response,
258
0
                                                    const TCheckWarmUpCacheAsyncRequest& request) {
259
0
    std::ostringstream oss;
260
0
    oss << "[";
261
0
    for (size_t i = 0; i < request.tablets.size() && i < 10; ++i) {
262
0
        if (i > 0) oss << ",";
263
0
        oss << request.tablets[i];
264
0
    }
265
0
    oss << "]";
266
0
    LOG(INFO) << "check_warm_up_cache_async: enter, request tablets num=" << request.tablets.size()
267
0
              << ", tablet_ids=" << oss.str();
268
0
    std::map<int64_t, bool> task_done;
269
0
    _engine.file_cache_block_downloader().check_download_task(request.tablets, &task_done);
270
0
    DBUG_EXECUTE_IF("CloudBackendService.check_warm_up_cache_async.return_task_false", {
271
0
        for (auto& it : task_done) {
272
0
            it.second = false;
273
0
        }
274
0
    });
275
0
    response.__set_task_done(task_done);
276
277
0
    for (const auto& [tablet_id, done] : task_done) {
278
0
        VLOG_DEBUG << "check_warm_up_cache_async: tablet_id=" << tablet_id << ", done=" << done;
279
0
    }
280
281
0
    Status st = Status::OK();
282
0
    TStatus t_status;
283
0
    st.to_thrift(&t_status);
284
0
    response.status = t_status;
285
0
}
286
287
void CloudBackendService::get_stream_load_record(TStreamLoadRecordResult& result,
288
0
                                                 int64_t last_stream_record_time) {
289
0
    BaseBackendService::get_stream_load_record(result, last_stream_record_time,
290
0
                                               _engine.get_stream_load_recorder());
291
0
}
292
293
} // namespace doris