Coverage Report

Created: 2026-06-05 09:36

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
1
        : BaseBackendService(exec_env), _engine(engine) {}
49
50
0
CloudBackendService::~CloudBackendService() = default;
51
52
1
Status CloudBackendService::start_thrift_dependencies() {
53
1
    _agent_server->cloud_start_workers(_engine, _exec_env);
54
1
    return Status::OK();
55
1
}
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
1
                                                   const TGetTopNHotPartitionsRequest& request) {
79
1
    _engine.tablet_hotspot().get_top_n_hot_partition(&response.hot_tables);
80
1
    response.file_cache_size = io::FileCacheFactory::instance()->get_capacity();
81
1
    response.__isset.hot_tables = !response.hot_tables.empty();
82
1
}
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
            st = manager.set_event(request.job_id, request.event);
95
0
            if (st.ok()) {
96
0
                break;
97
0
            }
98
0
        } else {
99
0
            st = manager.check_and_set_job_id(request.job_id);
100
0
        }
101
0
        if (!st.ok()) {
102
0
            LOG_WARNING("SET_JOB failed.").error(st);
103
0
            break;
104
0
        }
105
0
        [[fallthrough]];
106
0
    }
107
0
    case TWarmUpTabletsRequestType::SET_BATCH: {
108
0
        LOG_INFO("receive the warm up request.")
109
0
                .tag("request_type", "SET_BATCH")
110
0
                .tag("job_id", request.job_id)
111
0
                .tag("batch_id", request.batch_id)
112
0
                .tag("jobs size", request.job_metas.size())
113
0
                .tag("tablet num of first meta",
114
0
                     request.job_metas.empty() ? 0 : request.job_metas[0].tablet_ids.size());
115
0
        bool retry = false;
116
0
        st = manager.check_and_set_batch_id(request.job_id, request.batch_id, &retry);
117
0
        if (!retry && st) {
118
0
            manager.add_job(request.job_metas);
119
0
        } else {
120
0
            if (retry) {
121
0
                LOG_WARNING("retry the job.")
122
0
                        .tag("job_id", request.job_id)
123
0
                        .tag("batch_id", request.batch_id);
124
0
            } else {
125
0
                LOG_WARNING("SET_BATCH failed.").error(st);
126
0
            }
127
0
        }
128
0
        break;
129
0
    }
130
0
    case TWarmUpTabletsRequestType::GET_CURRENT_JOB_STATE_AND_LEASE: {
131
0
        auto [job_id, batch_id, pending_job_size, finish_job_size] =
132
0
                manager.get_current_job_state();
133
0
        LOG_INFO("receive the warm up request.")
134
0
                .tag("request_type", "GET_CURRENT_JOB_STATE_AND_LEASE")
135
0
                .tag("job_id", job_id)
136
0
                .tag("batch_id", batch_id)
137
0
                .tag("pending_job_size", pending_job_size)
138
0
                .tag("finish_job_size", finish_job_size);
139
0
        response.__set_job_id(job_id);
140
0
        response.__set_batch_id(batch_id);
141
0
        response.__set_pending_job_size(pending_job_size);
142
0
        response.__set_finish_job_size(finish_job_size);
143
0
        break;
144
0
    }
145
0
    case TWarmUpTabletsRequestType::CLEAR_JOB: {
146
0
        LOG_INFO("receive the warm up request.")
147
0
                .tag("request_type", "CLEAR_JOB")
148
0
                .tag("job_id", request.job_id);
149
0
        if (request.__isset.event) {
150
0
            st = manager.set_event(request.job_id, request.event, /* clear: */ true);
151
0
        } else {
152
0
            st = manager.clear_job(request.job_id);
153
0
        }
154
0
        break;
155
0
    }
156
0
    default:
157
0
        DCHECK(false);
158
0
    };
159
0
    st.to_thrift(&response.status);
160
0
}
161
162
static Status run_rpc_get_file_cache_meta(std::shared_ptr<PBackendService_Stub> brpc_stub,
163
                                          const std::string& brpc_addr,
164
                                          PGetFileCacheMetaRequest brpc_request,
165
0
                                          PGetFileCacheMetaResponse& brpc_response) {
166
0
    brpc::Controller cntl;
167
0
    cntl.set_timeout_ms(20 * 1000); // 20s
168
0
    brpc_stub->get_file_cache_meta_by_tablet_id(&cntl, &brpc_request, &brpc_response, nullptr);
169
0
    if (cntl.Failed()) {
170
0
        LOG(WARNING) << "warm_up_cache_async: brpc call failed, addr=" << brpc_addr
171
0
                     << ", error=" << cntl.ErrorText() << ", error code=" << cntl.ErrorCode();
172
0
        return Status::RpcError("{} isn't connected, error code={}", brpc_addr, cntl.ErrorCode());
173
0
    }
174
0
    VLOG_DEBUG << "warm_up_cache_async: request=" << brpc_request.DebugString()
175
0
               << ", response=" << brpc_response.DebugString();
176
0
    g_file_cache_warm_up_cache_async_submitted_segment_num
177
0
            << brpc_response.file_cache_block_metas().size();
178
0
    return Status::OK();
179
0
}
180
181
void CloudBackendService::_warm_up_cache(TWarmUpCacheAsyncResponse& response,
182
0
                                         const TWarmUpCacheAsyncRequest& request) {
183
0
    std::ostringstream oss;
184
0
    oss << "[";
185
0
    for (size_t i = 0; i < request.tablet_ids.size() && i < 10; ++i) {
186
0
        if (i > 0) oss << ",";
187
0
        oss << request.tablet_ids[i];
188
0
    }
189
0
    oss << "]";
190
0
    g_file_cache_warm_up_cache_async_submitted_tablet_num << request.tablet_ids.size();
191
0
    LOG(INFO) << "warm_up_cache_async: enter, request=" << request.host << ":" << request.brpc_port
192
0
              << ", tablets num=" << request.tablet_ids.size() << ", tablet_ids=" << oss.str();
193
194
0
    auto& manager = ExecEnv::GetInstance()->storage_engine().to_cloud().cloud_warm_up_manager();
195
    // Record each tablet in manager
196
0
    for (int64_t tablet_id : request.tablet_ids) {
197
0
        manager.record_balanced_tablet(tablet_id, request.host, request.brpc_port);
198
0
    }
199
200
0
    std::string host = request.host;
201
0
    auto* dns_cache = ExecEnv::GetInstance()->dns_cache();
202
0
    if (dns_cache == nullptr) {
203
0
        LOG(WARNING) << "DNS cache is not initialized, skipping hostname resolve";
204
0
    } else if (!is_valid_ip(request.host)) {
205
0
        Status status = dns_cache->get(request.host, &host);
206
0
        if (!status.ok()) {
207
0
            LOG(WARNING) << "failed to get ip from host " << request.host << ": "
208
0
                         << status.to_string();
209
0
            return;
210
0
        }
211
0
    }
212
0
    std::string brpc_addr = get_host_port(host, request.brpc_port);
213
0
    std::shared_ptr<PBackendService_Stub> brpc_stub =
214
0
            _exec_env->brpc_internal_client_cache()->get_new_client_no_cache(brpc_addr);
215
0
    if (!brpc_stub) {
216
0
        LOG(WARNING) << "warm_up_cache_async: failed to get brpc_stub for addr " << brpc_addr;
217
0
        return;
218
0
    }
219
0
    PGetFileCacheMetaRequest brpc_request;
220
0
    PGetFileCacheMetaResponse brpc_response;
221
0
    for (int64_t tablet_id : request.tablet_ids) {
222
0
        brpc_request.add_tablet_ids(tablet_id);
223
0
    }
224
225
0
    Status rpc_status = run_rpc_get_file_cache_meta(brpc_stub, brpc_addr, std::move(brpc_request),
226
0
                                                    brpc_response);
227
0
    if (rpc_status.ok()) {
228
0
        _engine.file_cache_block_downloader().submit_download_task(
229
0
                std::move(*brpc_response.mutable_file_cache_block_metas()));
230
0
    } else {
231
0
        LOG(WARNING) << "warm_up_cache_async: rpc failed for addr=" << brpc_addr
232
0
                     << ", status=" << rpc_status;
233
0
    }
234
0
}
235
236
void CloudBackendService::warm_up_cache_async(TWarmUpCacheAsyncResponse& response,
237
0
                                              const TWarmUpCacheAsyncRequest& request) {
238
    // just submit the task to the thread pool, no need to wait for the result
239
0
    auto do_warm_up = [this, request, &response]() { this->_warm_up_cache(response, request); };
240
0
    g_file_cache_warm_up_cache_async_submitted_task_num << 1;
241
0
    Status submit_st = _engine.warmup_cache_async_thread_pool().submit_func(std::move(do_warm_up));
242
0
    if (!submit_st.ok()) {
243
0
        LOG(WARNING) << "warm_up_cache_async: fail to submit heavy task to "
244
0
                        "warmup_cache_async_thread_pool, status="
245
0
                     << submit_st.to_string() << ", execute synchronously";
246
0
        do_warm_up();
247
0
    }
248
0
    TStatus t_status;
249
0
    submit_st.to_thrift(&t_status);
250
0
    response.status = std::move(t_status);
251
0
}
252
253
void CloudBackendService::check_warm_up_cache_async(TCheckWarmUpCacheAsyncResponse& response,
254
0
                                                    const TCheckWarmUpCacheAsyncRequest& request) {
255
0
    std::ostringstream oss;
256
0
    oss << "[";
257
0
    for (size_t i = 0; i < request.tablets.size() && i < 10; ++i) {
258
0
        if (i > 0) oss << ",";
259
0
        oss << request.tablets[i];
260
0
    }
261
0
    oss << "]";
262
0
    LOG(INFO) << "check_warm_up_cache_async: enter, request tablets num=" << request.tablets.size()
263
0
              << ", tablet_ids=" << oss.str();
264
0
    std::map<int64_t, bool> task_done;
265
0
    _engine.file_cache_block_downloader().check_download_task(request.tablets, &task_done);
266
0
    DBUG_EXECUTE_IF("CloudBackendService.check_warm_up_cache_async.return_task_false", {
267
0
        for (auto& it : task_done) {
268
0
            it.second = false;
269
0
        }
270
0
    });
271
0
    response.__set_task_done(task_done);
272
273
0
    for (const auto& [tablet_id, done] : task_done) {
274
0
        VLOG_DEBUG << "check_warm_up_cache_async: tablet_id=" << tablet_id << ", done=" << done;
275
0
    }
276
277
0
    Status st = Status::OK();
278
0
    TStatus t_status;
279
0
    st.to_thrift(&t_status);
280
0
    response.status = t_status;
281
0
}
282
283
void CloudBackendService::get_stream_load_record(TStreamLoadRecordResult& result,
284
31
                                                 int64_t last_stream_record_time) {
285
31
    BaseBackendService::get_stream_load_record(result, last_stream_record_time,
286
31
                                               _engine.get_stream_load_recorder());
287
31
}
288
289
} // namespace doris