be/src/service/http/action/checksum_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/checksum_action.h" |
19 | | |
20 | | #include <boost/lexical_cast/bad_lexical_cast.hpp> |
21 | | #include <sstream> |
22 | | #include <string> |
23 | | |
24 | | #include "boost/lexical_cast.hpp" |
25 | | #include "common/logging.h" |
26 | | #include "common/status.h" |
27 | | #include "service/http/http_channel.h" |
28 | | #include "service/http/http_request.h" |
29 | | #include "service/http/http_status.h" |
30 | | #include "storage/storage_engine.h" |
31 | | #include "storage/task/engine_checksum_task.h" |
32 | | |
33 | | namespace doris { |
34 | | |
35 | | const std::string TABLET_ID = "tablet_id"; |
36 | | // do not use name "VERSION", |
37 | | // or will be conflict with "VERSION" in thrift/config.h |
38 | | const std::string TABLET_VERSION = "version"; |
39 | | const std::string SCHEMA_HASH = "schema_hash"; |
40 | | |
41 | | ChecksumAction::ChecksumAction(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 ChecksumAction::handle(HttpRequest* req) { |
46 | 0 | LOG(INFO) << "accept one request " << req->debug_string(); |
47 | | // Get tablet id |
48 | 0 | const std::string& tablet_id_str = req->param(TABLET_ID); |
49 | 0 | if (tablet_id_str.empty()) { |
50 | 0 | std::string error_msg = std::string("parameter " + TABLET_ID + " not specified in url."); |
51 | |
|
52 | 0 | HttpChannel::send_reply(req, HttpStatus::BAD_REQUEST, error_msg); |
53 | 0 | return; |
54 | 0 | } |
55 | | |
56 | | // Get version |
57 | 0 | const std::string& version_str = req->param(TABLET_VERSION); |
58 | 0 | if (version_str.empty()) { |
59 | 0 | std::string error_msg = |
60 | 0 | std::string("parameter " + TABLET_VERSION + " not specified in url."); |
61 | 0 | HttpChannel::send_reply(req, HttpStatus::BAD_REQUEST, error_msg); |
62 | 0 | return; |
63 | 0 | } |
64 | | |
65 | | // Get schema hash |
66 | 0 | const std::string& schema_hash_str = req->param(SCHEMA_HASH); |
67 | 0 | if (schema_hash_str.empty()) { |
68 | 0 | std::string error_msg = std::string("parameter " + SCHEMA_HASH + " not specified in url."); |
69 | 0 | HttpChannel::send_reply(req, HttpStatus::BAD_REQUEST, error_msg); |
70 | 0 | return; |
71 | 0 | } |
72 | | |
73 | | // valid str format |
74 | 0 | int64_t tablet_id; |
75 | 0 | int64_t version; |
76 | 0 | int32_t schema_hash; |
77 | 0 | try { |
78 | 0 | tablet_id = boost::lexical_cast<int64_t>(tablet_id_str); |
79 | 0 | version = boost::lexical_cast<int64_t>(version_str); |
80 | 0 | schema_hash = boost::lexical_cast<int32_t>(schema_hash_str); |
81 | 0 | } catch (boost::bad_lexical_cast& e) { |
82 | 0 | std::string error_msg = std::string("param format is invalid: ") + std::string(e.what()); |
83 | 0 | HttpChannel::send_reply(req, HttpStatus::BAD_REQUEST, error_msg); |
84 | 0 | return; |
85 | 0 | } |
86 | | |
87 | 0 | VLOG_ROW << "get checksum tablet info: " << tablet_id << "-" << version << "-" << schema_hash; |
88 | |
|
89 | 0 | int64_t checksum = do_checksum(tablet_id, version, schema_hash, req); |
90 | 0 | if (checksum == -1L) { |
91 | 0 | std::string error_msg = std::string("checksum failed"); |
92 | 0 | HttpChannel::send_reply(req, HttpStatus::INTERNAL_SERVER_ERROR, error_msg); |
93 | 0 | return; |
94 | 0 | } else { |
95 | 0 | std::stringstream result; |
96 | 0 | result << checksum; |
97 | 0 | std::string result_str = result.str(); |
98 | 0 | HttpChannel::send_reply(req, result_str); |
99 | 0 | } |
100 | | |
101 | 0 | LOG(INFO) << "deal with checksum request finished! tablet id: " << tablet_id; |
102 | 0 | } |
103 | | |
104 | | int64_t ChecksumAction::do_checksum(int64_t tablet_id, int64_t version, int32_t schema_hash, |
105 | 0 | HttpRequest* req) { |
106 | 0 | Status res = Status::OK(); |
107 | 0 | uint32_t checksum; |
108 | 0 | EngineChecksumTask engine_task(_engine, tablet_id, schema_hash, version, &checksum); |
109 | 0 | SCOPED_ATTACH_TASK(engine_task.mem_tracker()); |
110 | 0 | res = engine_task.execute(); |
111 | 0 | if (!res.ok()) { |
112 | 0 | LOG(WARNING) << "checksum failed. status: " << res << ", signature: " << tablet_id; |
113 | 0 | return -1L; |
114 | 0 | } else { |
115 | 0 | LOG(INFO) << "checksum success. status: " << res << ", signature: " << tablet_id |
116 | 0 | << ". checksum: " << checksum; |
117 | 0 | } |
118 | | |
119 | 0 | return static_cast<int64_t>(checksum); |
120 | 0 | } |
121 | | |
122 | | } // end namespace doris |