Coverage Report

Created: 2026-03-16 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/service/http/action/snapshot_action.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 "service/http/action/snapshot_action.h"
19
20
#include <gen_cpp/AgentService_types.h>
21
22
#include <boost/lexical_cast.hpp>
23
#include <boost/lexical_cast/bad_lexical_cast.hpp>
24
#include <sstream>
25
#include <string>
26
27
#include "common/logging.h"
28
#include "common/status.h"
29
#include "service/http/http_channel.h"
30
#include "service/http/http_request.h"
31
#include "service/http/http_status.h"
32
#include "storage/snapshot/snapshot_manager.h"
33
#include "storage/storage_engine.h"
34
35
namespace doris {
36
#include "common/compile_check_begin.h"
37
38
const std::string TABLET_ID = "tablet_id";
39
const std::string SCHEMA_HASH = "schema_hash";
40
41
SnapshotAction::SnapshotAction(ExecEnv* exec_env, StorageEngine& engine, TPrivilegeHier::type hier,
42
                               TPrivilegeType::type type)
43
6
        : HttpHandlerWithAuth(exec_env, hier, type), _engine(engine) {}
44
45
0
void SnapshotAction::handle(HttpRequest* req) {
46
0
    if (!config::enable_snapshot_action) {
47
0
        HttpChannel::send_reply(req, HttpStatus::BAD_REQUEST, "feature disabled");
48
0
        return;
49
0
    }
50
0
    LOG(INFO) << "accept one request " << req->debug_string();
51
    // Get tablet id
52
0
    const std::string& tablet_id_str = req->param(TABLET_ID);
53
0
    if (tablet_id_str.empty()) {
54
0
        std::string error_msg = std::string("parameter " + TABLET_ID + " not specified in url.");
55
56
0
        HttpChannel::send_reply(req, HttpStatus::BAD_REQUEST, error_msg);
57
0
        return;
58
0
    }
59
60
    // Get schema hash
61
0
    const std::string& schema_hash_str = req->param(SCHEMA_HASH);
62
0
    if (schema_hash_str.empty()) {
63
0
        std::string error_msg = std::string("parameter " + SCHEMA_HASH + " not specified in url.");
64
0
        HttpChannel::send_reply(req, HttpStatus::BAD_REQUEST, error_msg);
65
0
        return;
66
0
    }
67
68
    // valid str format
69
0
    int64_t tablet_id;
70
0
    int32_t schema_hash;
71
0
    try {
72
0
        tablet_id = boost::lexical_cast<int64_t>(tablet_id_str);
73
0
        schema_hash = boost::lexical_cast<int32_t>(schema_hash_str);
74
0
    } catch (boost::bad_lexical_cast& e) {
75
0
        std::string error_msg = std::string("param format is invalid: ") + std::string(e.what());
76
0
        HttpChannel::send_reply(req, HttpStatus::BAD_REQUEST, error_msg);
77
0
        return;
78
0
    }
79
80
0
    VLOG_ROW << "get make snapshot tablet info: " << tablet_id << "-" << schema_hash;
81
82
0
    std::string snapshot_path;
83
0
    int64_t ret = _make_snapshot(tablet_id, schema_hash, &snapshot_path);
84
0
    if (ret != 0L) {
85
0
        std::string error_msg = std::string("make snapshot failed");
86
0
        HttpChannel::send_reply(req, HttpStatus::INTERNAL_SERVER_ERROR, error_msg);
87
0
        return;
88
0
    } else {
89
0
        std::stringstream result;
90
0
        result << snapshot_path;
91
0
        std::string result_str = result.str();
92
0
        HttpChannel::send_reply(req, result_str);
93
0
    }
94
95
0
    LOG(INFO) << "deal with snapshot request finished! tablet id: " << tablet_id;
96
0
}
97
98
int64_t SnapshotAction::_make_snapshot(int64_t tablet_id, int32_t schema_hash,
99
0
                                       std::string* snapshot_path) {
100
0
    TSnapshotRequest request;
101
0
    request.tablet_id = tablet_id;
102
0
    request.schema_hash = schema_hash;
103
104
0
    Status res = Status::OK();
105
0
    bool allow_incremental_clone; // not used
106
0
    res = _engine.snapshot_mgr()->make_snapshot(request, snapshot_path, &allow_incremental_clone);
107
0
    if (!res.ok()) {
108
0
        LOG(WARNING) << "make snapshot failed. status: " << res << ", signature: " << tablet_id;
109
0
        return -1L;
110
0
    } else {
111
0
        LOG(INFO) << "make snapshot success. status: " << res << ", signature: " << tablet_id
112
0
                  << ". path: " << *snapshot_path;
113
0
    }
114
115
0
    return 0L;
116
0
}
117
118
#include "common/compile_check_end.h"
119
} // end namespace doris