forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
295 lines (260 loc) · 11.2 KB
/
Copy pathutils.cpp
File metadata and controls
295 lines (260 loc) · 11.2 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
#include "agent/utils.h"
// IWYU pragma: no_include <bthread/errno.h>
#include <errno.h> // IWYU pragma: keep
#include <fmt/format.h>
#include <gen_cpp/FrontendService.h>
#include <gen_cpp/HeartbeatService_types.h>
#include <gen_cpp/Types_types.h>
#include <glog/logging.h>
#include <rapidjson/document.h>
#include <rapidjson/encodings.h>
#include <rapidjson/rapidjson.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <thrift/transport/TTransportException.h>
#include <cstdio>
#include <exception>
#include <fstream>
#include <memory>
#include <utility>
#include "common/config.h"
#include "common/status.h"
#include "runtime/client_cache.h"
#include "runtime/cluster_info.h"
namespace doris {
class TConfirmUnusedRemoteFilesRequest;
class TConfirmUnusedRemoteFilesResult;
class TFinishTaskRequest;
class TMasterResult;
class TReportRequest;
} // namespace doris
using apache::thrift::transport::TTransportException;
namespace doris {
static std::unique_ptr<MasterServerClient> s_client;
MasterServerClient* MasterServerClient::create(const ClusterInfo* cluster_info) {
s_client.reset(new MasterServerClient(cluster_info));
return s_client.get();
}
MasterServerClient* MasterServerClient::instance() {
return s_client.get();
}
MasterServerClient::MasterServerClient(const ClusterInfo* cluster_info)
: _cluster_info(cluster_info),
_client_cache(std::make_unique<FrontendServiceClientCache>(
config::max_master_fe_client_cache_size)) {
_client_cache->init_metrics("master_fe");
}
Status MasterServerClient::finish_task(const TFinishTaskRequest& request, TMasterResult* result) {
Status client_status;
FrontendServiceConnection client(_client_cache.get(), _cluster_info->master_fe_addr,
config::thrift_rpc_timeout_ms, &client_status);
if (!client_status.ok()) {
LOG(WARNING) << "fail to get master client from cache. "
<< "host=" << _cluster_info->master_fe_addr.hostname
<< ", port=" << _cluster_info->master_fe_addr.port
<< ", code=" << client_status.code();
return Status::InternalError("Failed to get master client");
}
try {
try {
client->finishTask(*result, request);
} catch ([[maybe_unused]] TTransportException& e) {
#ifndef ADDRESS_SANITIZER
LOG(WARNING) << "master client, retry finishTask: " << e.what();
#endif
client_status = client.reopen(config::thrift_rpc_timeout_ms);
if (!client_status.ok()) {
#ifndef ADDRESS_SANITIZER
LOG(WARNING) << "fail to get master client from cache. "
<< "host=" << _cluster_info->master_fe_addr.hostname
<< ", port=" << _cluster_info->master_fe_addr.port
<< ", code=" << client_status.code();
#endif
return Status::RpcError("Master client finish task failed");
}
client->finishTask(*result, request);
}
} catch (std::exception& e) {
RETURN_IF_ERROR(client.reopen(config::thrift_rpc_timeout_ms));
LOG(WARNING) << "fail to finish_task. "
<< "host=" << _cluster_info->master_fe_addr.hostname
<< ", port=" << _cluster_info->master_fe_addr.port << ", error=" << e.what();
return Status::InternalError("Fail to finish task");
}
return Status::OK();
}
Status MasterServerClient::report(const TReportRequest& request, TMasterResult* result) {
Status client_status;
FrontendServiceConnection client(_client_cache.get(), _cluster_info->master_fe_addr,
config::thrift_rpc_timeout_ms, &client_status);
if (!client_status.ok()) {
LOG(WARNING) << "fail to get master client from cache. "
<< "host=" << _cluster_info->master_fe_addr.hostname
<< ", port=" << _cluster_info->master_fe_addr.port
<< ", code=" << client_status;
return Status::InternalError("Fail to get master client from cache");
}
try {
try {
client->report(*result, request);
} catch (TTransportException& e) {
TTransportException::TTransportExceptionType type = e.getType();
if (type != TTransportException::TTransportExceptionType::TIMED_OUT) {
#ifndef ADDRESS_SANITIZER
// if not TIMED_OUT, retry
LOG(WARNING) << "master client, retry finishTask: " << e.what();
#endif
client_status = client.reopen(config::thrift_rpc_timeout_ms);
if (!client_status.ok()) {
#ifndef ADDRESS_SANITIZER
LOG(WARNING) << "fail to get master client from cache. "
<< "host=" << _cluster_info->master_fe_addr.hostname
<< ", port=" << _cluster_info->master_fe_addr.port
<< ", code=" << client_status.code();
#endif
return Status::InternalError("Fail to get master client from cache");
}
client->report(*result, request);
} else {
// TIMED_OUT exception. do not retry
// actually we don't care what FE returns.
#ifndef ADDRESS_SANITIZER
LOG(WARNING) << "fail to report to master: " << e.what();
#endif
return Status::InternalError("Fail to report to master");
}
}
} catch (std::exception& e) {
RETURN_IF_ERROR(client.reopen(config::thrift_rpc_timeout_ms));
LOG(WARNING) << "fail to report to master. "
<< "host=" << _cluster_info->master_fe_addr.hostname
<< ", port=" << _cluster_info->master_fe_addr.port
<< ", code=" << client_status.code() << ", reason=" << e.what();
return Status::InternalError("Fail to report to master");
}
return Status::OK();
}
Status MasterServerClient::confirm_unused_remote_files(
const TConfirmUnusedRemoteFilesRequest& request, TConfirmUnusedRemoteFilesResult* result) {
Status client_status;
FrontendServiceConnection client(_client_cache.get(), _cluster_info->master_fe_addr,
config::thrift_rpc_timeout_ms, &client_status);
if (!client_status.ok()) {
return Status::InternalError(
"fail to get master client from cache. host={}, port={}, code={}",
_cluster_info->master_fe_addr.hostname, _cluster_info->master_fe_addr.port,
client_status.code());
}
try {
try {
client->confirmUnusedRemoteFiles(*result, request);
} catch (TTransportException& e) {
TTransportException::TTransportExceptionType type = e.getType();
if (type != TTransportException::TTransportExceptionType::TIMED_OUT) {
#ifndef ADDRESS_SANITIZER
// if not TIMED_OUT, retry
LOG(WARNING) << "master client, retry finishTask: " << e.what();
#endif
client_status = client.reopen(config::thrift_rpc_timeout_ms);
if (!client_status.ok()) {
return Status::InternalError(
"fail to get master client from cache. host={}, port={}, code={}",
_cluster_info->master_fe_addr.hostname,
_cluster_info->master_fe_addr.port, client_status.code());
}
client->confirmUnusedRemoteFiles(*result, request);
} else {
// TIMED_OUT exception. do not retry
// actually we don't care what FE returns.
return Status::InternalError(
"fail to confirm unused remote files. host={}, port={}, code={}, reason={}",
_cluster_info->master_fe_addr.hostname, _cluster_info->master_fe_addr.port,
client_status.code(), e.what());
}
}
} catch (std::exception& e) {
RETURN_IF_ERROR(client.reopen(config::thrift_rpc_timeout_ms));
return Status::InternalError(
"fail to confirm unused remote files. host={}, port={}, code={}, reason={}",
_cluster_info->master_fe_addr.hostname, _cluster_info->master_fe_addr.port,
client_status.code(), e.what());
}
return Status::OK();
}
bool AgentUtils::exec_cmd(const std::string& command, std::string* errmsg, bool redirect_stderr) {
// The exit status of the command.
uint32_t rc = 0;
// Redirect stderr to stdout to get error message.
std::string cmd = command;
if (redirect_stderr) {
cmd += " 2>&1";
}
// Execute command.
FILE* fp = popen(cmd.c_str(), "r");
if (fp == nullptr) {
*errmsg = fmt::format("popen failed. {}, with errno: {}.\n", strerror(errno), errno);
return false;
}
// Get command output.
char result[1024] = {'\0'};
while (fgets(result, sizeof(result), fp) != nullptr) {
*errmsg += result;
}
// Waits for the associated process to terminate and returns.
rc = pclose(fp);
if (rc == -1) {
if (errno == ECHILD) {
*errmsg += "pclose cannot obtain the child status.\n";
} else {
*errmsg += fmt::format("Close popen failed. {}, with errno: {}.\n", strerror(errno),
errno);
}
return false;
}
// Get return code of command.
int32_t status_child = WEXITSTATUS(rc);
if (status_child == 0) {
return true;
} else {
return false;
}
}
bool AgentUtils::write_json_to_file(const std::map<std::string, std::string>& info,
const std::string& path) {
rapidjson::Document json_info(rapidjson::kObjectType);
for (auto& it : info) {
json_info.AddMember(rapidjson::Value(it.first.c_str(), json_info.GetAllocator()).Move(),
rapidjson::Value(it.second.c_str(), json_info.GetAllocator()).Move(),
json_info.GetAllocator());
}
rapidjson::StringBuffer json_info_str;
rapidjson::Writer<rapidjson::StringBuffer> writer(json_info_str);
json_info.Accept(writer);
std::ofstream fp(path);
if (!fp) {
return false;
}
fp << json_info_str.GetString() << std::endl;
fp.close();
return true;
}
} // namespace doris