forked from opencurve/curve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curvefs_tool_metric.cpp
127 lines (112 loc) · 4.2 KB
/
curvefs_tool_metric.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (c) 2021 NetEase Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Project: curve
* Created Date: 2021-10-25
* Author: chengyi01
*/
#include "curvefs/src/tools/curvefs_tool_metric.h"
DECLARE_uint32(rpcTimeoutMs);
DECLARE_uint32(rpcRetryTimes);
namespace curvefs {
namespace tools {
MetricStatusCode MetricClient::GetMetric(const std::string& addr,
const std::string& subUri,
std::string* value) {
brpc::Channel httpChannel;
brpc::ChannelOptions options;
brpc::Controller cntl;
options.protocol = brpc::PROTOCOL_HTTP;
int res = httpChannel.Init(addr.c_str(), &options);
if (res != 0) {
std::cerr << "init httpChannel to " << addr << " fail!" << std::endl;
return MetricStatusCode::kOtherErr;
}
cntl.http_request().uri() = addr + subUri;
// TODO(chengyi01): move to while
cntl.set_timeout_ms(FLAGS_rpcTimeoutMs);
httpChannel.CallMethod(nullptr, &cntl, nullptr, nullptr, nullptr);
if (!cntl.Failed()) {
*value = cntl.response_attachment().to_string();
return MetricStatusCode::kOK;
}
bool needRetry =
(cntl.Failed() && cntl.ErrorCode() != EHOSTDOWN &&
cntl.ErrorCode() != ETIMEDOUT && cntl.ErrorCode() != brpc::ELOGOFF &&
cntl.ErrorCode() != brpc::ERPCTIMEDOUT);
uint64_t retryTimes = 0;
while (needRetry && retryTimes < FLAGS_rpcRetryTimes) {
cntl.Reset();
cntl.http_request().uri() = addr + subUri;
cntl.set_timeout_ms(FLAGS_rpcTimeoutMs);
httpChannel.CallMethod(nullptr, &cntl, nullptr, nullptr, nullptr);
if (cntl.Failed()) {
retryTimes++;
continue;
}
*value = cntl.response_attachment().to_string();
}
// Hand over the printing process to the outside
bool notExist = cntl.ErrorCode() == brpc::EHTTP &&
cntl.http_response().status_code() == kHttpCodeNotFound;
return notExist ? MetricStatusCode::kNotFound : MetricStatusCode::kOtherErr;
}
int MetricClient::GetKeyValueFromJson(const std::string& strJson,
const std::string& key,
std::string* value) {
Json::CharReaderBuilder reader;
std::stringstream ss(strJson);
std::string err;
Json::Value json;
bool parseCode = Json::parseFromStream(reader, ss, &json, &err);
if (!parseCode) {
std::cerr << "parse " << ss.str() << " fail " << err << std::endl;
return -1;
}
int ret = -1;
if (json[key].isNull()) {
std::cerr << "there is no " << key << " in " << json.asString()
<< std::endl;
} else if (json[key].isString()) {
*value = json[key].asString();
ret = 0;
} else {
std::cerr << "the key " << key << " in " << json.asString()
<< " is not a string value." << std::endl;
}
return ret;
}
int MetricClient::GetKeyValueFromString(const std::string& str,
const std::string& key,
std::string* value) {
auto pos = str.find(":");
if (pos == std::string::npos) {
std::cerr << "parse " << key << " fail! it should be like: ***:***."
<< std::endl;
return -1;
}
*value = str.substr(pos + 1);
TrimMetricString(value);
return 0;
}
void MetricClient::TrimMetricString(std::string* str) {
str->erase(0, str->find_first_not_of(" "));
str->erase(str->find_last_not_of("\r\n") + 1);
str->erase(0, str->find_first_not_of("\""));
str->erase(str->find_last_not_of("\"") + 1);
}
} // namespace tools
} // namespace curvefs