-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(functions): Add support for REST based remote functions
Co-authored-by: Wills Feng <[email protected]>
- Loading branch information
1 parent
dcafd32
commit a312e97
Showing
15 changed files
with
1,002 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* 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. | ||
*/ | ||
|
||
#include "velox/functions/remote/client/RestClient.h" | ||
|
||
#include <cpr/cpr.h> | ||
#include <folly/io/IOBufQueue.h> | ||
|
||
#include "velox/common/base/Exceptions.h" | ||
|
||
using namespace folly; | ||
namespace facebook::velox::functions { | ||
|
||
std::unique_ptr<IOBuf> RestClient::invokeFunction( | ||
const std::string& fullUrl, | ||
std::unique_ptr<IOBuf> requestPayload) { | ||
IOBufQueue inputBufQueue(IOBufQueue::cacheChainLength()); | ||
inputBufQueue.append(std::move(requestPayload)); | ||
|
||
std::string requestBody; | ||
for (auto range : *inputBufQueue.front()) { | ||
requestBody.append( | ||
reinterpret_cast<const char*>(range.data()), range.size()); | ||
} | ||
|
||
cpr::Response response = cpr::Post( | ||
cpr::Url{fullUrl}, | ||
cpr::Header{ | ||
{"Content-Type", "application/X-presto-pages"}, | ||
{"Accept", "application/X-presto-pages"}}, | ||
cpr::Body{requestBody}); | ||
|
||
if (response.error) { | ||
VELOX_FAIL(fmt::format( | ||
"Error communicating with server: {} URL: {}", | ||
response.error.message, | ||
fullUrl)); | ||
} | ||
|
||
auto outputBuf = IOBuf::copyBuffer(response.text); | ||
return outputBuf; | ||
} | ||
|
||
std::unique_ptr<HttpClient> getRestClient() { | ||
return std::make_unique<RestClient>(); | ||
} | ||
|
||
} // namespace facebook::velox::functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <folly/io/IOBuf.h> | ||
#include <memory> | ||
#include <string> | ||
|
||
namespace facebook::velox::functions { | ||
|
||
/// @brief Abstract interface for an HTTP client. | ||
/// Provides a method to invoke a function by sending an HTTP request | ||
/// and receiving a response, both in Presto's serialized wire format. | ||
class HttpClient { | ||
public: | ||
virtual ~HttpClient() = default; | ||
|
||
/// @brief Invokes a function over HTTP. | ||
/// @param url The endpoint URL to send the request to. | ||
/// @param requestPayload The request payload in Presto's serialized wire | ||
/// format. | ||
/// @return A unique pointer to the response payload in Presto's serialized | ||
/// wire format. | ||
virtual std::unique_ptr<folly::IOBuf> invokeFunction( | ||
const std::string& url, | ||
std::unique_ptr<folly::IOBuf> requestPayload) = 0; | ||
}; | ||
|
||
/// @brief Concrete implementation of HttpClient using REST. | ||
/// Handles HTTP communication by sending requests and receiving responses | ||
/// using RESTful APIs with payloads in Presto's serialized wire format. | ||
class RestClient : public HttpClient { | ||
public: | ||
/// @brief Invokes a function over HTTP using CURL. | ||
/// Sends an HTTP POST request to the specified URL with the request payload | ||
/// and receives the response payload. Both payloads are in Presto's | ||
/// serialized wire format. | ||
/// @param url The endpoint URL to send the request to. | ||
/// @param requestPayload The request payload in Presto's serialized wire | ||
/// format. | ||
/// @return A unique pointer to the response payload in Presto's serialized | ||
/// wire format. | ||
/// @throws VeloxException if there is an error initializing CURL or during | ||
/// the request. | ||
std::unique_ptr<folly::IOBuf> invokeFunction( | ||
const std::string& url, | ||
std::unique_ptr<folly::IOBuf> requestPayload) override; | ||
}; | ||
|
||
/// @brief Factory function to create an instance of RestClient. | ||
/// @return A unique pointer to an HttpClient implementation. | ||
std::unique_ptr<HttpClient> getRestClient(); | ||
|
||
} // namespace facebook::velox::functions |
Oops, something went wrong.