Skip to content

Commit 9426c9c

Browse files
committed
feat(native): Add http2 support for exchange client
1 parent 9dfe771 commit 9426c9c

File tree

6 files changed

+38
-6
lines changed

6 files changed

+38
-6
lines changed

presto-native-execution/presto_cpp/main/PrestoExchangeSource.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <sstream>
2020

2121
#include "presto_cpp/main/QueryContextManager.h"
22+
#include "presto_cpp/main/common/Configs.h"
2223
#include "presto_cpp/main/common/Counters.h"
2324
#include "velox/common/base/Exceptions.h"
2425
#include "velox/common/testutil/TestValue.h"
@@ -278,9 +279,11 @@ void PrestoExchangeSource::processDataResponse(
278279
return;
279280
}
280281
auto* headers = response->headers();
281-
VELOX_CHECK(
282-
!headers->getIsChunked(),
283-
"Chunked http transferring encoding is not supported.");
282+
if (!SystemConfig::instance()->exchangeHttp2Enabled()) {
283+
VELOX_CHECK(
284+
!headers->getIsChunked(),
285+
"Chunked http transferring encoding is not supported.");
286+
}
284287
const uint64_t contentLength =
285288
atol(headers->getHeaders()
286289
.getSingleOrEmpty(proxygen::HTTP_HEADER_CONTENT_LENGTH)

presto-native-execution/presto_cpp/main/common/Configs.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ SystemConfig::SystemConfig() {
232232
NUM_PROP(kLogNumZombieTasks, 20),
233233
NUM_PROP(kAnnouncementMaxFrequencyMs, 30'000), // 30s
234234
NUM_PROP(kHeartbeatFrequencyMs, 0),
235+
BOOL_PROP(kExchangeHttp2Enabled, false),
235236
STR_PROP(kExchangeMaxErrorDuration, "3m"),
236237
STR_PROP(kExchangeRequestTimeout, "20s"),
237238
STR_PROP(kExchangeConnectTimeout, "20s"),
@@ -832,6 +833,10 @@ uint64_t SystemConfig::heartbeatFrequencyMs() const {
832833
return optionalProperty<uint64_t>(kHeartbeatFrequencyMs).value();
833834
}
834835

836+
bool SystemConfig::exchangeHttp2Enabled() const {
837+
return optionalProperty<bool>(kExchangeHttp2Enabled).value();
838+
}
839+
835840
std::chrono::duration<double> SystemConfig::exchangeMaxErrorDuration() const {
836841
return velox::config::toDuration(
837842
optionalProperty(kExchangeMaxErrorDuration).value());

presto-native-execution/presto_cpp/main/common/Configs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,10 @@ class SystemConfig : public ConfigBase {
638638
static constexpr std::string_view kHeartbeatFrequencyMs{
639639
"heartbeat-frequency-ms"};
640640

641+
/// Whether HTTP/2 should be enabled for exchange HTTP client.
642+
static constexpr std::string_view kExchangeHttp2Enabled{
643+
"exchange.http-client.http2-enabled"};
644+
641645
static constexpr std::string_view kExchangeMaxErrorDuration{
642646
"exchange.max-error-duration"};
643647

@@ -1013,6 +1017,8 @@ class SystemConfig : public ConfigBase {
10131017

10141018
uint64_t heartbeatFrequencyMs() const;
10151019

1020+
bool exchangeHttp2Enabled() const;
1021+
10161022
std::chrono::duration<double> exchangeMaxErrorDuration() const;
10171023

10181024
std::chrono::duration<double> exchangeRequestTimeoutMs() const;

presto-native-execution/presto_cpp/main/common/Utils.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <fmt/format.h>
1717
#include <folly/io/Cursor.h>
1818
#include <sys/resource.h>
19+
#include "presto_cpp/main/common/Configs.h"
1920
#include "velox/common/process/ThreadDebugInfo.h"
2021

2122
namespace facebook::presto::util {
@@ -37,7 +38,11 @@ std::shared_ptr<folly::SSLContext> createSSLContext(
3738
sslContext->loadCertKeyPairFromFiles(
3839
clientCertAndKeyPath.c_str(), clientCertAndKeyPath.c_str());
3940
sslContext->setCiphersOrThrow(ciphers);
40-
sslContext->setAdvertisedNextProtocols({"http/1.1"});
41+
if (SystemConfig::instance()->exchangeHttp2Enabled()) {
42+
sslContext->setAdvertisedNextProtocols({"h2", "http/1.1"});
43+
} else {
44+
sslContext->setAdvertisedNextProtocols({"http/1.1"});
45+
}
4146
return sslContext;
4247
} catch (const std::exception& ex) {
4348
LOG(FATAL) << fmt::format(

presto-native-execution/presto_cpp/main/http/HttpClient.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#endif // PRESTO_ENABLE_JWT
1919
#include <folly/io/async/EventBaseManager.h>
2020
#include <folly/synchronization/Latch.h>
21+
#include <proxygen/lib/http/codec/CodecProtocol.h>
2122
#include <velox/common/base/Exceptions.h>
2223
#include "presto_cpp/main/common/Configs.h"
2324
#include "presto_cpp/main/common/Counters.h"
@@ -200,13 +201,21 @@ class ResponseHandler : public proxygen::HTTPTransactionHandler {
200201
return promise_.getSemiFuture();
201202
}
202203

203-
void setTransaction(proxygen::HTTPTransaction* /* txn */) noexcept override {}
204+
void setTransaction(proxygen::HTTPTransaction* txn) noexcept override {
205+
if (txn) {
206+
protocol_ = txn->getTransport().getCodec().getProtocol();
207+
}
208+
}
209+
204210
void detachTransaction() noexcept override {
205211
self_.reset();
206212
}
207213

208214
void onHeadersComplete(
209215
std::unique_ptr<proxygen::HTTPMessage> msg) noexcept override {
216+
if (protocol_.has_value()) {
217+
VLOG(2) << "HttpClient Receive " << proxygen::getCodecProtocolString(protocol_.value());
218+
}
210219
response_ = std::make_unique<HttpResponse>(
211220
std::move(msg),
212221
client_->memoryPool(),
@@ -267,6 +276,7 @@ class ResponseHandler : public proxygen::HTTPTransactionHandler {
267276
folly::Promise<std::unique_ptr<HttpResponse>> promise_;
268277
std::shared_ptr<ResponseHandler> self_;
269278
std::shared_ptr<HttpClient> client_;
279+
std::optional<proxygen::CodecProtocol> protocol_;
270280
};
271281

272282
// Responsible for making an HTTP request. The request will be made in 2

presto-native-execution/presto_cpp/main/http/HttpClient.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <proxygen/lib/http/connpool/SessionPool.h>
1919
#include <proxygen/lib/http/session/HTTPUpstreamSession.h>
2020
#include <velox/common/memory/MemoryPool.h>
21+
#include "presto_cpp/main/common/Configs.h"
2122
#include "presto_cpp/main/http/HttpConstants.h"
2223
#include "velox/common/base/Exceptions.h"
2324

@@ -215,7 +216,9 @@ class HttpClient : public std::enable_shared_from_this<HttpClient> {
215216
class RequestBuilder {
216217
public:
217218
RequestBuilder() {
218-
headers_.setHTTPVersion(1, 1);
219+
if (!SystemConfig::instance()->exchangeHttp2Enabled()) {
220+
headers_.setHTTPVersion(1, 1);
221+
}
219222
}
220223

221224
RequestBuilder& method(proxygen::HTTPMethod method) {

0 commit comments

Comments
 (0)