-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(functions): Add support for REST based remote functions #10911
Open
Joe-Abraham
wants to merge
1
commit into
facebookincubator:main
Choose a base branch
from
Joe-Abraham:udf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please write comments explaining the signature and the parameters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the documentation |
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add empty line between the system and velox includes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added new line