-
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
e86ff05
commit 97483aa
Showing
14 changed files
with
1,077 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* 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 <curl/curl.h> | ||
#include <folly/io/IOBufQueue.h> | ||
|
||
#include "velox/common/base/Exceptions.h" | ||
|
||
using namespace folly; | ||
namespace facebook::velox::functions { | ||
namespace { | ||
|
||
// Callback function for CURL to read data from the request payload. | ||
// @param dest Destination buffer to copy data into. | ||
// @param size Size of each data element. | ||
// @param nmemb Number of elements to read. | ||
// @param userp Pointer to user data (IOBufQueue containing the request | ||
// payload). | ||
// @return Number of bytes actually copied. | ||
size_t readCallback(char* dest, size_t size, size_t nmemb, void* userp) { | ||
auto* inputBufQueue = static_cast<IOBufQueue*>(userp); | ||
size_t bufferSize = size * nmemb; | ||
size_t totalCopied = 0; | ||
|
||
while (totalCopied < bufferSize && !inputBufQueue->empty()) { | ||
auto buf = inputBufQueue->front(); | ||
size_t remainingSize = bufferSize - totalCopied; | ||
size_t copySize = std::min(remainingSize, buf->length()); | ||
std::memcpy(dest + totalCopied, buf->data(), copySize); | ||
totalCopied += copySize; | ||
inputBufQueue->pop_front(); | ||
} | ||
|
||
return totalCopied; | ||
} | ||
|
||
// Callback function for CURL to write data to the response payload. | ||
// @param ptr Pointer to the received data. | ||
// @param size Size of each data element. | ||
// @param nmemb Number of elements received. | ||
// @param userData Pointer to user data (IOBufQueue to store the response | ||
// payload). | ||
// @return Number of bytes actually written. | ||
size_t writeCallback(char* ptr, size_t size, size_t nmemb, void* userData) { | ||
auto* outputBuf = static_cast<IOBufQueue*>(userData); | ||
size_t totalSize = size * nmemb; | ||
auto buf = IOBuf::copyBuffer(ptr, totalSize); | ||
outputBuf->append(std::move(buf)); | ||
return totalSize; | ||
} | ||
} // namespace | ||
|
||
std::unique_ptr<IOBuf> RestClient::invokeFunction( | ||
const std::string& fullUrl, | ||
std::unique_ptr<IOBuf> requestPayload) { | ||
try { | ||
IOBufQueue inputBufQueue(IOBufQueue::cacheChainLength()); | ||
inputBufQueue.append(std::move(requestPayload)); | ||
|
||
CURL* curl = curl_easy_init(); | ||
if (!curl) { | ||
VELOX_FAIL(fmt::format( | ||
"Error initializing CURL: {}", | ||
curl_easy_strerror(CURLE_FAILED_INIT))); | ||
} | ||
|
||
curl_easy_setopt(curl, CURLOPT_URL, fullUrl.c_str()); | ||
curl_easy_setopt(curl, CURLOPT_POST, 1L); | ||
curl_easy_setopt(curl, CURLOPT_READFUNCTION, readCallback); | ||
curl_easy_setopt(curl, CURLOPT_READDATA, &inputBufQueue); | ||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback); | ||
|
||
IOBufQueue outputBuf(IOBufQueue::cacheChainLength()); | ||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &outputBuf); | ||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); | ||
|
||
struct curl_slist* headers = nullptr; | ||
headers = | ||
curl_slist_append(headers, "Content-Type: application/X-presto-pages"); | ||
headers = curl_slist_append(headers, "Accept: application/X-presto-pages"); | ||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | ||
|
||
curl_easy_setopt( | ||
curl, | ||
CURLOPT_POSTFIELDSIZE, | ||
static_cast<long>(inputBufQueue.chainLength())); | ||
|
||
CURLcode res = curl_easy_perform(curl); | ||
if (res != CURLE_OK) { | ||
curl_slist_free_all(headers); | ||
curl_easy_cleanup(curl); | ||
VELOX_FAIL(fmt::format( | ||
"Error communicating with server: {}\nURL: {}\nCURL Error: {}", | ||
curl_easy_strerror(res), | ||
fullUrl.c_str(), | ||
curl_easy_strerror(res))); | ||
} | ||
|
||
curl_slist_free_all(headers); | ||
curl_easy_cleanup(curl); | ||
|
||
return outputBuf.move(); | ||
|
||
} catch (const std::exception& e) { | ||
VELOX_FAIL(fmt::format("Exception during CURL request: {}", e.what())); | ||
} | ||
} | ||
|
||
std::unique_ptr<HttpClient> getRestClient() { | ||
return std::make_unique<RestClient>(); | ||
} | ||
|
||
} // namespace facebook::velox::functions |
Oops, something went wrong.