-
Notifications
You must be signed in to change notification settings - Fork 5.5k
[native] Migrate TaskStatus, TaskUpdateRequest, TaskInfo to Thrift with JSON fields in CPP #25079
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
Merged
Merged
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 hidden or 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 |
---|---|---|
|
@@ -208,27 +208,37 @@ proxygen::RequestHandler* TaskResource::createOrUpdateTaskImpl( | |
const std::vector<std::string>& pathMatch, | ||
const std::function<std::unique_ptr<protocol::TaskInfo>( | ||
const protocol::TaskId& taskId, | ||
const std::string& updateJson, | ||
const std::string& requestBody, | ||
const bool summarize, | ||
long startProcessCpuTime)>& createOrUpdateFunc) { | ||
long startProcessCpuTime, | ||
bool receiveThrift)>& createOrUpdateFunc) { | ||
protocol::TaskId taskId = pathMatch[1]; | ||
bool summarize = message->hasQueryParam("summarize"); | ||
|
||
auto& headers = message->getHeaders(); | ||
const auto& acceptHeader = headers.getSingleOrEmpty(proxygen::HTTP_HEADER_ACCEPT); | ||
const auto sendThrift = | ||
acceptHeader.find(http::kMimeTypeApplicationThrift) != std::string::npos; | ||
const auto& contentHeader = headers.getSingleOrEmpty(proxygen::HTTP_HEADER_CONTENT_TYPE); | ||
const auto receiveThrift = | ||
contentHeader.find(http::kMimeTypeApplicationThrift) != std::string::npos; | ||
|
||
return new http::CallbackRequestHandler( | ||
[this, taskId, summarize, createOrUpdateFunc]( | ||
[this, taskId, summarize, createOrUpdateFunc, sendThrift, receiveThrift]( | ||
proxygen::HTTPMessage* /*message*/, | ||
const std::vector<std::unique_ptr<folly::IOBuf>>& body, | ||
proxygen::ResponseHandler* downstream, | ||
std::shared_ptr<http::CallbackRequestHandlerState> handlerState) { | ||
folly::via( | ||
httpSrvCpuExecutor_, | ||
[this, &body, taskId, summarize, createOrUpdateFunc]() { | ||
[this, &body, taskId, summarize, createOrUpdateFunc, receiveThrift]() { | ||
const auto startProcessCpuTimeNs = util::getProcessCpuTimeNs(); | ||
std::string updateJson = util::extractMessageBody(body); | ||
std::string requestBody = util::extractMessageBody(body); | ||
|
||
std::unique_ptr<protocol::TaskInfo> taskInfo; | ||
try { | ||
taskInfo = createOrUpdateFunc( | ||
taskId, updateJson, summarize, startProcessCpuTimeNs); | ||
taskId, requestBody, summarize, startProcessCpuTimeNs, receiveThrift); | ||
} catch (const velox::VeloxException& e) { | ||
// Creating an empty task, putting errors inside so that next | ||
// status fetch from coordinator will catch the error and well | ||
|
@@ -243,12 +253,19 @@ proxygen::RequestHandler* TaskResource::createOrUpdateTaskImpl( | |
throw; | ||
} | ||
} | ||
return json(*taskInfo); | ||
return taskInfo; | ||
}) | ||
.via(folly::EventBaseManager::get()->getEventBase()) | ||
.thenValue([downstream, handlerState](auto&& taskInfoJson) { | ||
.thenValue([downstream, handlerState, sendThrift](auto taskInfo) { | ||
if (!handlerState->requestExpired()) { | ||
http::sendOkResponse(downstream, taskInfoJson); | ||
if (sendThrift) { | ||
thrift::TaskInfo thriftTaskInfo; | ||
toThrift(*taskInfo, thriftTaskInfo); | ||
http::sendOkThriftResponse( | ||
downstream, thriftWrite(thriftTaskInfo)); | ||
} else { | ||
http::sendOkResponse(downstream, json(*taskInfo)); | ||
} | ||
} | ||
}) | ||
.thenError( | ||
|
@@ -275,11 +292,12 @@ proxygen::RequestHandler* TaskResource::createOrUpdateBatchTask( | |
message, | ||
pathMatch, | ||
[&](const protocol::TaskId& taskId, | ||
const std::string& updateJson, | ||
const std::string& requestBody, | ||
const bool summarize, | ||
long startProcessCpuTime) { | ||
long startProcessCpuTime, | ||
bool /*receiveThrift*/) { | ||
protocol::BatchTaskUpdateRequest batchUpdateRequest = | ||
json::parse(updateJson); | ||
json::parse(requestBody); | ||
auto updateRequest = batchUpdateRequest.taskUpdateRequest; | ||
VELOX_USER_CHECK_NOT_NULL(updateRequest.fragment); | ||
|
||
|
@@ -327,16 +345,22 @@ proxygen::RequestHandler* TaskResource::createOrUpdateTask( | |
message, | ||
pathMatch, | ||
[&](const protocol::TaskId& taskId, | ||
const std::string& updateJson, | ||
const std::string& requestBody, | ||
const bool summarize, | ||
long startProcessCpuTime) { | ||
protocol::TaskUpdateRequest updateRequest = json::parse(updateJson); | ||
long startProcessCpuTime, | ||
bool receiveThrift) { | ||
protocol::TaskUpdateRequest updateRequest; | ||
if (receiveThrift) { | ||
auto thriftTaskUpdateRequest = std::make_shared<thrift::TaskUpdateRequest>(); | ||
thriftRead(requestBody, thriftTaskUpdateRequest); | ||
fromThrift(*thriftTaskUpdateRequest, updateRequest); | ||
} else { | ||
updateRequest = json::parse(requestBody); | ||
} | ||
velox::core::PlanFragment planFragment; | ||
std::shared_ptr<velox::core::QueryCtx> queryCtx; | ||
if (updateRequest.fragment) { | ||
auto fragment = | ||
velox::encoding::Base64::decode(*updateRequest.fragment); | ||
protocol::PlanFragment prestoPlan = json::parse(fragment); | ||
protocol::PlanFragment prestoPlan = json::parse(receiveThrift ? *updateRequest.fragment : velox::encoding::Base64::decode(*updateRequest.fragment)); | ||
|
||
queryCtx = | ||
taskManager_.getQueryContextManager()->findOrCreateQueryCtx( | ||
|
@@ -510,12 +534,12 @@ proxygen::RequestHandler* TaskResource::getTaskStatus( | |
auto maxWait = getMaxWait(message); | ||
|
||
auto& headers = message->getHeaders(); | ||
auto acceptHeader = headers.getSingleOrEmpty(proxygen::HTTP_HEADER_ACCEPT); | ||
auto useThrift = | ||
const auto& acceptHeader = headers.getSingleOrEmpty(proxygen::HTTP_HEADER_ACCEPT); | ||
const auto sendThrift = | ||
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. Do we need to do the same for TaskInfo? 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. Discussed offline. Needs Java changes. Will be added as a follow up |
||
acceptHeader.find(http::kMimeTypeApplicationThrift) != std::string::npos; | ||
|
||
return new http::CallbackRequestHandler( | ||
[this, useThrift, taskId, currentState, maxWait]( | ||
[this, sendThrift, taskId, currentState, maxWait]( | ||
proxygen::HTTPMessage* /*message*/, | ||
const std::vector<std::unique_ptr<folly::IOBuf>>& /*body*/, | ||
proxygen::ResponseHandler* downstream, | ||
|
@@ -525,7 +549,7 @@ proxygen::RequestHandler* TaskResource::getTaskStatus( | |
httpSrvCpuExecutor_, | ||
[this, | ||
evb, | ||
useThrift, | ||
sendThrift, | ||
taskId, | ||
currentState, | ||
maxWait, | ||
|
@@ -535,10 +559,10 @@ proxygen::RequestHandler* TaskResource::getTaskStatus( | |
.getTaskStatus(taskId, currentState, maxWait, handlerState) | ||
.via(evb) | ||
.thenValue( | ||
[useThrift, downstream, taskId, handlerState]( | ||
[sendThrift, downstream, taskId, handlerState]( | ||
std::unique_ptr<protocol::TaskStatus> taskStatus) { | ||
if (!handlerState->requestExpired()) { | ||
if (useThrift) { | ||
if (sendThrift) { | ||
thrift::TaskStatus thriftTaskStatus; | ||
toThrift(*taskStatus, thriftTaskStatus); | ||
http::sendOkThriftResponse( | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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
103 changes: 103 additions & 0 deletions
103
presto-native-execution/presto_cpp/main/tests/TaskInfoTest.cpp
This file contains hidden or 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,103 @@ | ||
/* | ||
* 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 <gtest/gtest.h> | ||
#include "presto_cpp/main/thrift/ProtocolToThrift.h" | ||
#include "presto_cpp/presto_protocol/core/Duration.h" | ||
#include "presto_cpp/main/common/tests/test_json.h" | ||
#include "presto_cpp/main/connectors/PrestoToVeloxConnector.h" | ||
|
||
using namespace facebook; | ||
using namespace facebook::presto::protocol; | ||
|
||
class TaskInfoTest : public ::testing::Test {}; | ||
|
||
const std::string BASE_DATA_PATH = "/github/presto-trunk/presto-native-execution/presto_cpp/main/tests/data/"; | ||
|
||
TEST_F(TaskInfoTest, duration) { | ||
double thrift = 0; | ||
facebook::presto::thrift::toThrift(Duration(123, TimeUnit::MILLISECONDS), thrift); | ||
ASSERT_EQ(thrift, 123); | ||
} | ||
|
||
TEST_F(TaskInfoTest, binaryMetadataUpdates) { | ||
std::string str = slurp(getDataPath(BASE_DATA_PATH, "MetadataUpdates.json")); | ||
json j = json::parse(str); | ||
registerPrestoToVeloxConnector(std::make_unique<facebook::presto::HivePrestoToVeloxConnector>("hive")); | ||
MetadataUpdates metadataUpdates = j; | ||
std::unique_ptr<std::string> thriftMetadataUpdates = std::make_unique<std::string>(); | ||
facebook::presto::thrift::toThrift(metadataUpdates, *thriftMetadataUpdates); | ||
|
||
json thriftJson = json::parse(*thriftMetadataUpdates); | ||
ASSERT_EQ(j, thriftJson); | ||
|
||
presto::unregisterPrestoToVeloxConnector("hive"); | ||
} | ||
|
||
TEST_F(TaskInfoTest, taskInfo) { | ||
std::string str = slurp(getDataPath(BASE_DATA_PATH, "TaskInfo.json")); | ||
json j = json::parse(str); | ||
registerPrestoToVeloxConnector(std::make_unique<facebook::presto::HivePrestoToVeloxConnector>("hive")); | ||
TaskInfo taskInfo = j; | ||
facebook::presto::thrift::TaskInfo thriftTaskInfo; | ||
facebook::presto::thrift::toThrift(taskInfo, thriftTaskInfo); | ||
|
||
json thriftJson = json::parse(*thriftTaskInfo.metadataUpdates()->metadataUpdates()); | ||
ASSERT_EQ(taskInfo.metadataUpdates, thriftJson); | ||
ASSERT_EQ(thriftTaskInfo.needsPlan(), false); | ||
ASSERT_EQ(thriftTaskInfo.outputBuffers()->buffers()->size(), 2); | ||
ASSERT_EQ(thriftTaskInfo.outputBuffers()->buffers()[0].bufferId()->id(), 100); | ||
ASSERT_EQ(thriftTaskInfo.outputBuffers()->buffers()[1].bufferId()->id(), 200); | ||
ASSERT_EQ(thriftTaskInfo.stats()->blockedReasons()->count(facebook::presto::thrift::BlockedReason::WAITING_FOR_MEMORY), 1); | ||
ASSERT_EQ(thriftTaskInfo.stats()->runtimeStats()->metrics()->size(), 2); | ||
ASSERT_EQ(thriftTaskInfo.stats()->runtimeStats()->metrics()["test_metric1"].sum(), 123); | ||
ASSERT_EQ(thriftTaskInfo.stats()->runtimeStats()->metrics()["test_metric2"].name(), "test_metric2"); | ||
|
||
presto::unregisterPrestoToVeloxConnector("hive"); | ||
} | ||
|
||
TEST_F(TaskInfoTest, taskId) { | ||
TaskId taskId = "queryId.1.2.3.4"; | ||
facebook::presto::thrift::TaskId thriftTaskId; | ||
facebook::presto::thrift::toThrift(taskId, thriftTaskId); | ||
|
||
ASSERT_EQ(thriftTaskId.stageExecutionId()->stageId()->queryId(), "queryId"); | ||
ASSERT_EQ(thriftTaskId.stageExecutionId()->stageId()->id(), 1); | ||
ASSERT_EQ(thriftTaskId.stageExecutionId()->id(), 2); | ||
ASSERT_EQ(thriftTaskId.id(), 3); | ||
ASSERT_EQ(thriftTaskId.attemptNumber(), 4); | ||
} | ||
|
||
|
||
TEST_F(TaskInfoTest, operatorStatsEmptyBlockedReason) { | ||
std::string str = slurp(getDataPath(BASE_DATA_PATH, "OperatorStatsEmptyBlockedReason.json")); | ||
json j = json::parse(str); | ||
OperatorStats operatorStats = j; | ||
facebook::presto::thrift::OperatorStats thriftOperatorStats; | ||
facebook::presto::thrift::toThrift(operatorStats, thriftOperatorStats); | ||
|
||
ASSERT_EQ(thriftOperatorStats.blockedReason().has_value(), false); | ||
ASSERT_EQ(thriftOperatorStats.blockedWall(), 80); | ||
ASSERT_EQ(thriftOperatorStats.finishCpu(), 1000); | ||
} | ||
|
||
TEST_F(TaskInfoTest, operatorStats) { | ||
std::string str = slurp(getDataPath(BASE_DATA_PATH, "OperatorStats.json")); | ||
json j = json::parse(str); | ||
OperatorStats operatorStats = j; | ||
facebook::presto::thrift::OperatorStats thriftOperatorStats; | ||
facebook::presto::thrift::toThrift(operatorStats, thriftOperatorStats); | ||
|
||
ASSERT_EQ(thriftOperatorStats.blockedReason(), facebook::presto::thrift::BlockedReason::WAITING_FOR_MEMORY); | ||
} |
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.
why does this need to be a shared_ptr?
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.
because thriftRead expects a shared_ptr for the thrift object
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.
correct me if I am mistake but thriftTaskUpdateRequest is just used to populate updateRequest?
As a followup: can we make thriftRead to just take in a mutable reference and not shared_ptr, thereby avoiding heap allocation?