Coverage Report

Created: 2026-04-10 18:35

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