-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathHTTPLookupService.cc
503 lines (435 loc) · 20.9 KB
/
HTTPLookupService.cc
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
* 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 "HTTPLookupService.h"
#include <curl/curl.h>
#include <pulsar/Version.h>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include "ExecutorService.h"
#include "LogUtils.h"
#include "NamespaceName.h"
#include "SchemaUtils.h"
#include "ServiceNameResolver.h"
#include "TopicName.h"
namespace ptree = boost::property_tree;
DECLARE_LOG_OBJECT()
namespace pulsar {
const static std::string V1_PATH = "/lookup/v2/destination/";
const static std::string V2_PATH = "/lookup/v2/topic/";
const static std::string ADMIN_PATH_V1 = "/admin/";
const static std::string ADMIN_PATH_V2 = "/admin/v2/";
const static int MAX_HTTP_REDIRECTS = 20;
const static std::string PARTITION_METHOD_NAME = "partitions";
const static int NUMBER_OF_LOOKUP_THREADS = 1;
static inline bool needRedirection(long code) { return (code == 307 || code == 302 || code == 301); }
HTTPLookupService::CurlInitializer::CurlInitializer() {
// Once per application - https://curl.haxx.se/mail/lib-2015-11/0052.html
curl_global_init(CURL_GLOBAL_ALL);
}
HTTPLookupService::CurlInitializer::~CurlInitializer() { curl_global_cleanup(); }
HTTPLookupService::CurlInitializer HTTPLookupService::curlInitializer;
HTTPLookupService::HTTPLookupService(ServiceNameResolver &serviceNameResolver,
const ClientConfiguration &clientConfiguration,
const AuthenticationPtr &authData)
: executorProvider_(std::make_shared<ExecutorServiceProvider>(NUMBER_OF_LOOKUP_THREADS)),
serviceNameResolver_(serviceNameResolver),
authenticationPtr_(authData),
lookupTimeoutInSeconds_(clientConfiguration.getOperationTimeoutSeconds()),
tlsPrivateFilePath_(clientConfiguration.getTlsPrivateKeyFilePath()),
tlsCertificateFilePath_(clientConfiguration.getTlsCertificateFilePath()),
tlsTrustCertsFilePath_(clientConfiguration.getTlsTrustCertsFilePath()),
isUseTls_(clientConfiguration.isUseTls()),
tlsAllowInsecure_(clientConfiguration.isTlsAllowInsecureConnection()),
tlsValidateHostname_(clientConfiguration.isValidateHostName()) {}
auto HTTPLookupService::getBroker(const TopicName &topicName) -> LookupResultFuture {
LookupResultPromise promise;
const auto &url = serviceNameResolver_.resolveHost();
std::stringstream completeUrlStream;
if (topicName.isV2Topic()) {
completeUrlStream << url << V2_PATH << topicName.getDomain() << "/" << topicName.getProperty() << '/'
<< topicName.getNamespacePortion() << '/' << topicName.getEncodedLocalName();
} else {
completeUrlStream << url << V1_PATH << topicName.getDomain() << "/" << topicName.getProperty() << '/'
<< topicName.getCluster() << '/' << topicName.getNamespacePortion() << '/'
<< topicName.getEncodedLocalName();
}
const auto completeUrl = completeUrlStream.str();
auto self = shared_from_this();
executorProvider_->get()->postWork([this, self, promise, completeUrl] {
std::string responseData;
Result result = sendHTTPRequest(completeUrl, responseData);
if (result != ResultOk) {
promise.setFailed(result);
} else {
const auto lookupDataResultPtr = parseLookupData(responseData);
const auto brokerAddress = (serviceNameResolver_.useTls() ? lookupDataResultPtr->getBrokerUrlTls()
: lookupDataResultPtr->getBrokerUrl());
promise.setValue({brokerAddress, brokerAddress});
}
});
return promise.getFuture();
}
Future<Result, LookupDataResultPtr> HTTPLookupService::getPartitionMetadataAsync(
const TopicNamePtr &topicName) {
LookupPromise promise;
std::stringstream completeUrlStream;
const auto &url = serviceNameResolver_.resolveHost();
if (topicName->isV2Topic()) {
completeUrlStream << url << ADMIN_PATH_V2 << topicName->getDomain() << '/' << topicName->getProperty()
<< '/' << topicName->getNamespacePortion() << '/'
<< topicName->getEncodedLocalName() << '/' << PARTITION_METHOD_NAME;
} else {
completeUrlStream << url << ADMIN_PATH_V1 << topicName->getDomain() << '/' << topicName->getProperty()
<< '/' << topicName->getCluster() << '/' << topicName->getNamespacePortion() << '/'
<< topicName->getEncodedLocalName() << '/' << PARTITION_METHOD_NAME;
}
completeUrlStream << "?checkAllowAutoCreation=true";
executorProvider_->get()->postWork(std::bind(&HTTPLookupService::handleLookupHTTPRequest,
shared_from_this(), promise, completeUrlStream.str(),
PartitionMetaData));
return promise.getFuture();
}
Future<Result, NamespaceTopicsPtr> HTTPLookupService::getTopicsOfNamespaceAsync(
const NamespaceNamePtr &nsName) {
NamespaceTopicsPromise promise;
std::stringstream completeUrlStream;
const auto &url = serviceNameResolver_.resolveHost();
if (nsName->isV2()) {
completeUrlStream << url << ADMIN_PATH_V2 << "namespaces" << '/' << nsName->toString() << '/'
<< "topics";
} else {
completeUrlStream << url << ADMIN_PATH_V1 << "namespaces" << '/' << nsName->toString() << '/'
<< "destinations";
}
executorProvider_->get()->postWork(std::bind(&HTTPLookupService::handleNamespaceTopicsHTTPRequest,
shared_from_this(), promise, completeUrlStream.str()));
return promise.getFuture();
}
Future<Result, boost::optional<SchemaInfo>> HTTPLookupService::getSchema(const TopicNamePtr &topicName) {
Promise<Result, boost::optional<SchemaInfo>> promise;
std::stringstream completeUrlStream;
const auto &url = serviceNameResolver_.resolveHost();
if (topicName->isV2Topic()) {
completeUrlStream << url << ADMIN_PATH_V2 << "schemas/" << topicName->getProperty() << '/'
<< topicName->getNamespacePortion() << '/' << topicName->getEncodedLocalName()
<< "/schema";
} else {
completeUrlStream << url << ADMIN_PATH_V1 << "schemas/" << topicName->getProperty() << '/'
<< topicName->getCluster() << '/' << topicName->getNamespacePortion() << '/'
<< topicName->getEncodedLocalName() << "/schema";
}
executorProvider_->get()->postWork(std::bind(&HTTPLookupService::handleGetSchemaHTTPRequest,
shared_from_this(), promise, completeUrlStream.str()));
return promise.getFuture();
}
static size_t curlWriteCallback(void *contents, size_t size, size_t nmemb, void *responseDataPtr) {
((std::string *)responseDataPtr)->append((char *)contents, size * nmemb);
return size * nmemb;
}
void HTTPLookupService::handleNamespaceTopicsHTTPRequest(NamespaceTopicsPromise promise,
const std::string completeUrl) {
std::string responseData;
Result result = sendHTTPRequest(completeUrl, responseData);
if (result != ResultOk) {
promise.setFailed(result);
} else {
promise.setValue(parseNamespaceTopicsData(responseData));
}
}
Result HTTPLookupService::sendHTTPRequest(std::string completeUrl, std::string &responseData) {
long responseCode = -1;
return sendHTTPRequest(completeUrl, responseData, responseCode);
}
Result HTTPLookupService::sendHTTPRequest(std::string completeUrl, std::string &responseData,
long &responseCode) {
uint16_t reqCount = 0;
Result retResult = ResultOk;
while (++reqCount <= MAX_HTTP_REDIRECTS) {
CURL *handle;
CURLcode res;
std::string version = std::string("Pulsar-CPP-v") + PULSAR_VERSION_STR;
handle = curl_easy_init();
if (!handle) {
LOG_ERROR("Unable to curl_easy_init for url " << completeUrl);
// No curl_easy_cleanup required since handle not initialized
return ResultLookupError;
}
// set URL
curl_easy_setopt(handle, CURLOPT_URL, completeUrl.c_str());
// Write callback
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, curlWriteCallback);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &responseData);
// New connection is made for each call
curl_easy_setopt(handle, CURLOPT_FRESH_CONNECT, 1L);
curl_easy_setopt(handle, CURLOPT_FORBID_REUSE, 1L);
// Skipping signal handling - results in timeouts not honored during the DNS lookup
curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
// Timer
curl_easy_setopt(handle, CURLOPT_TIMEOUT, lookupTimeoutInSeconds_);
// Set User Agent
curl_easy_setopt(handle, CURLOPT_USERAGENT, version.c_str());
// Fail if HTTP return code >=400
curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1L);
// Authorization data
AuthenticationDataPtr authDataContent;
Result authResult = authenticationPtr_->getAuthData(authDataContent);
if (authResult != ResultOk) {
LOG_ERROR("Failed to getAuthData: " << authResult);
curl_easy_cleanup(handle);
return authResult;
}
struct curl_slist *list = NULL;
if (authDataContent->hasDataForHttp()) {
list = curl_slist_append(list, authDataContent->getHttpHeaders().c_str());
}
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, list);
// TLS
if (isUseTls_) {
if (curl_easy_setopt(handle, CURLOPT_SSLENGINE, NULL) != CURLE_OK) {
LOG_ERROR("Unable to load SSL engine for url " << completeUrl);
curl_easy_cleanup(handle);
return ResultConnectError;
}
if (curl_easy_setopt(handle, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK) {
LOG_ERROR("Unable to load SSL engine as default, for url " << completeUrl);
curl_easy_cleanup(handle);
return ResultConnectError;
}
curl_easy_setopt(handle, CURLOPT_SSLCERTTYPE, "PEM");
if (tlsAllowInsecure_) {
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
} else {
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 1L);
}
if (!tlsTrustCertsFilePath_.empty()) {
curl_easy_setopt(handle, CURLOPT_CAINFO, tlsTrustCertsFilePath_.c_str());
}
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, tlsValidateHostname_ ? 1L : 0L);
if (authDataContent->hasDataForTls()) {
curl_easy_setopt(handle, CURLOPT_SSLCERT, authDataContent->getTlsCertificates().c_str());
curl_easy_setopt(handle, CURLOPT_SSLKEY, authDataContent->getTlsPrivateKey().c_str());
} else {
if (!tlsPrivateFilePath_.empty() && !tlsCertificateFilePath_.empty()) {
curl_easy_setopt(handle, CURLOPT_SSLCERT, tlsCertificateFilePath_.c_str());
curl_easy_setopt(handle, CURLOPT_SSLKEY, tlsPrivateFilePath_.c_str());
}
}
}
LOG_INFO("Curl [" << reqCount << "] Lookup Request sent for " << completeUrl);
// Make get call to server
res = curl_easy_perform(handle);
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &responseCode);
LOG_INFO("Response received for url " << completeUrl << " responseCode " << responseCode
<< " curl res " << res);
// Free header list
curl_slist_free_all(list);
switch (res) {
case CURLE_OK:
if (responseCode == 200) {
retResult = ResultOk;
} else if (needRedirection(responseCode)) {
char *url = NULL;
curl_easy_getinfo(handle, CURLINFO_REDIRECT_URL, &url);
LOG_INFO("Response from url " << completeUrl << " to new url " << url);
completeUrl = url;
retResult = ResultLookupError;
} else {
retResult = ResultLookupError;
}
break;
case CURLE_COULDNT_CONNECT:
LOG_ERROR("Response failed for url " << completeUrl << ". Error Code " << res);
retResult = ResultRetryable;
break;
case CURLE_COULDNT_RESOLVE_PROXY:
case CURLE_COULDNT_RESOLVE_HOST:
case CURLE_HTTP_RETURNED_ERROR:
LOG_ERROR("Response failed for url " << completeUrl << ". Error Code " << res);
retResult = ResultConnectError;
break;
case CURLE_READ_ERROR:
LOG_ERROR("Response failed for url " << completeUrl << ". Error Code " << res);
retResult = ResultReadError;
break;
case CURLE_OPERATION_TIMEDOUT:
LOG_ERROR("Response failed for url " << completeUrl << ". Error Code " << res);
retResult = ResultTimeout;
break;
default:
LOG_ERROR("Response failed for url " << completeUrl << ". Error Code " << res);
retResult = ResultLookupError;
break;
}
curl_easy_cleanup(handle);
if (!needRedirection(responseCode)) {
break;
}
}
return retResult;
}
LookupDataResultPtr HTTPLookupService::parsePartitionData(const std::string &json) {
ptree::ptree root;
std::stringstream stream;
stream << json;
try {
ptree::read_json(stream, root);
} catch (ptree::json_parser_error &e) {
LOG_ERROR("Failed to parse json of Partition Metadata: " << e.what() << "\nInput Json = " << json);
return LookupDataResultPtr();
}
LookupDataResultPtr lookupDataResultPtr = std::make_shared<LookupDataResult>();
lookupDataResultPtr->setPartitions(root.get<int>("partitions", 0));
LOG_INFO("parsePartitionData = " << *lookupDataResultPtr);
return lookupDataResultPtr;
}
LookupDataResultPtr HTTPLookupService::parseLookupData(const std::string &json) {
ptree::ptree root;
std::stringstream stream;
stream << json;
try {
ptree::read_json(stream, root);
} catch (ptree::json_parser_error &e) {
LOG_ERROR("Failed to parse json : " << e.what() << "\nInput Json = " << json);
return LookupDataResultPtr();
}
const std::string defaultNotFoundString = "Url Not found";
const std::string brokerUrl = root.get<std::string>("brokerUrl", defaultNotFoundString);
if (brokerUrl == defaultNotFoundString) {
LOG_ERROR("malformed json! - brokerUrl not present" << json);
return LookupDataResultPtr();
}
std::string brokerUrlTls = root.get<std::string>("brokerUrlTls", defaultNotFoundString);
if (brokerUrlTls == defaultNotFoundString) {
brokerUrlTls = root.get<std::string>("brokerUrlSsl", defaultNotFoundString);
if (brokerUrlTls == defaultNotFoundString) {
LOG_ERROR("malformed json! - brokerUrlTls not present" << json);
return LookupDataResultPtr();
}
}
LookupDataResultPtr lookupDataResultPtr = std::make_shared<LookupDataResult>();
lookupDataResultPtr->setBrokerUrl(brokerUrl);
lookupDataResultPtr->setBrokerUrlTls(brokerUrlTls);
LOG_INFO("parseLookupData = " << *lookupDataResultPtr);
return lookupDataResultPtr;
}
NamespaceTopicsPtr HTTPLookupService::parseNamespaceTopicsData(const std::string &json) {
LOG_DEBUG("GetNamespaceTopics json = " << json);
ptree::ptree root;
std::stringstream stream;
stream << json;
try {
ptree::read_json(stream, root);
} catch (ptree::json_parser_error &e) {
LOG_ERROR("Failed to parse json of Topics of Namespace: " << e.what() << "\nInput Json = " << json);
return NamespaceTopicsPtr();
}
// passed in json is like: ["topic1", "topic2"...]
// root will be an array of topics
std::set<std::string> topicSet;
// get all topics
for (const auto &item : root) {
// remove partition part
const std::string topicName = item.second.get_value<std::string>();
int pos = topicName.find("-partition-");
std::string filteredName = topicName.substr(0, pos);
// filter duped topic name
if (topicSet.find(filteredName) == topicSet.end()) {
topicSet.insert(filteredName);
}
}
NamespaceTopicsPtr topicsResultPtr =
std::make_shared<std::vector<std::string>>(topicSet.begin(), topicSet.end());
return topicsResultPtr;
}
void HTTPLookupService::handleLookupHTTPRequest(LookupPromise promise, const std::string completeUrl,
RequestType requestType) {
std::string responseData;
Result result = sendHTTPRequest(completeUrl, responseData);
if (result != ResultOk) {
promise.setFailed(result);
} else {
promise.setValue((requestType == PartitionMetaData) ? parsePartitionData(responseData)
: parseLookupData(responseData));
}
}
void HTTPLookupService::handleGetSchemaHTTPRequest(GetSchemaPromise promise, const std::string completeUrl) {
std::string responseData;
long responseCode = -1;
Result result = sendHTTPRequest(completeUrl, responseData, responseCode);
if (responseCode == 404) {
promise.setValue(boost::none);
} else if (result != ResultOk) {
promise.setFailed(result);
} else {
ptree::ptree root;
std::stringstream stream(responseData);
try {
ptree::read_json(stream, root);
} catch (ptree::json_parser_error &e) {
LOG_ERROR("Failed to parse json of Partition Metadata: " << e.what()
<< "\nInput Json = " << responseData);
promise.setFailed(ResultInvalidMessage);
return;
}
const std::string defaultNotFoundString = "Not found";
auto schemaTypeStr = root.get<std::string>("type", defaultNotFoundString);
if (schemaTypeStr == defaultNotFoundString) {
LOG_ERROR("malformed json! - type not present" << responseData);
promise.setFailed(ResultInvalidMessage);
return;
}
auto schemaData = root.get<std::string>("data", defaultNotFoundString);
if (schemaData == defaultNotFoundString) {
LOG_ERROR("malformed json! - data not present" << responseData);
promise.setFailed(ResultInvalidMessage);
return;
}
auto schemaType = enumSchemaType(schemaTypeStr);
if (schemaType == KEY_VALUE) {
ptree::ptree kvRoot;
std::stringstream kvStream(schemaData);
try {
ptree::read_json(kvStream, kvRoot);
} catch (ptree::json_parser_error &e) {
LOG_ERROR("Failed to parse json of Partition Metadata: " << e.what()
<< "\nInput Json = " << schemaData);
promise.setFailed(ResultInvalidMessage);
return;
}
std::stringstream keyStream;
ptree::write_json(keyStream, kvRoot.get_child("key"), false);
std::stringstream valueStream;
ptree::write_json(valueStream, kvRoot.get_child("value"), false);
auto keyData = keyStream.str();
auto valueData = valueStream.str();
// Remove the last line break.
keyData.pop_back();
valueData.pop_back();
schemaData = mergeKeyValueSchema(keyData, valueData);
}
StringMap properties;
auto propertiesTree = root.get_child("properties");
for (const auto &item : propertiesTree) {
properties[item.first] = item.second.get_value<std::string>();
}
SchemaInfo schemaInfo = SchemaInfo(schemaType, "", schemaData, properties);
promise.setValue(schemaInfo);
}
}
} // namespace pulsar