-
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.
Add partitioned output trace replayer
- Loading branch information
1 parent
96944d5
commit be72382
Showing
14 changed files
with
499 additions
and
123 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
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,170 @@ | ||
/* | ||
* 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 <folly/executors/IOThreadPoolExecutor.h> | ||
|
||
#include "velox/common/memory/Memory.h" | ||
#include "velox/exec/PartitionedOutput.h" | ||
#include "velox/exec/QueryTraceUtil.h" | ||
#include "velox/exec/tests/utils/AssertQueryBuilder.h" | ||
#include "velox/exec/tests/utils/PlanBuilder.h" | ||
#include "velox/tool/trace/PartitionedOutputReplayer.h" | ||
|
||
using namespace facebook::velox; | ||
using namespace facebook::velox::exec; | ||
using namespace facebook::velox::exec::test; | ||
|
||
namespace facebook::velox::tool::trace { | ||
namespace { | ||
std::shared_ptr<core::QueryCtx> createQueryContext( | ||
const std::unordered_map<std::string, std::string>& config, | ||
folly::Executor* executor) { | ||
return core::QueryCtx::create( | ||
executor, core::QueryConfig(std::move(config))); | ||
} | ||
|
||
std::function<core::PlanNodePtr(std::string, core::PlanNodePtr)> | ||
partitionedOutputNodeFactory( | ||
const core::PartitionedOutputNode* originalNode) { | ||
return [=](const core::PlanNodeId& nodeId, | ||
const core::PlanNodePtr& source) -> core::PlanNodePtr { | ||
return std::make_shared<core::PartitionedOutputNode>( | ||
nodeId, | ||
originalNode->kind(), | ||
originalNode->keys(), | ||
originalNode->numPartitions(), | ||
originalNode->isReplicateNullsAndAny(), | ||
originalNode->partitionFunctionSpecPtr(), | ||
originalNode->outputType(), | ||
source); | ||
}; | ||
} | ||
|
||
std::vector<std::unique_ptr<folly::IOBuf>> getData( | ||
const std::shared_ptr<exec::OutputBufferManager>& bufferManager, | ||
const std::string& taskId, | ||
int destination, | ||
int64_t sequence, | ||
folly::Executor* executor) { | ||
auto [promise, semiFuture] = | ||
folly::makePromiseContract<std::vector<std::unique_ptr<folly::IOBuf>>>(); | ||
VELOX_CHECK(bufferManager->getData( | ||
taskId, | ||
destination, | ||
exec::PartitionedOutput::kMinDestinationSize, | ||
sequence, | ||
[result = std::make_shared< | ||
folly::Promise<std::vector<std::unique_ptr<folly::IOBuf>>>>( | ||
std::move(promise))]( | ||
std::vector<std::unique_ptr<folly::IOBuf>> pages, | ||
int64_t /*inSequence*/, | ||
std::vector<int64_t> /*remainingBytes*/) { | ||
result->setValue(std::move(pages)); | ||
})); | ||
auto future = std::move(semiFuture).via(executor); | ||
future.wait(std::chrono::seconds{10}); | ||
VELOX_CHECK(future.isReady()); | ||
return std::move(future).value(); | ||
} | ||
} // namespace | ||
|
||
std::vector<std::vector<std::unique_ptr<folly::IOBuf>>> getAllData( | ||
const std::shared_ptr<exec::OutputBufferManager>& bufferManager, | ||
const std::string& taskId, | ||
uint32_t numPartitions, | ||
folly::Executor* executor) { | ||
std::vector<std::thread> consumerThreads; | ||
std::vector<std::vector<std::unique_ptr<folly::IOBuf>>> partitionedResults; | ||
consumerThreads.reserve(numPartitions); | ||
partitionedResults.reserve(numPartitions); | ||
partitionedResults.resize(numPartitions); | ||
for (uint32_t i = 0; i < numPartitions; i++) { | ||
consumerThreads.push_back(std::thread([&, partition = i]() { | ||
bool finished{false}; | ||
while (!finished) { | ||
std::vector<std::unique_ptr<folly::IOBuf>> pages; | ||
{ | ||
pages = getData( | ||
bufferManager, | ||
taskId, | ||
partition, | ||
partitionedResults[partition].size(), | ||
executor); | ||
} | ||
for (auto& page : pages) { | ||
if (page) { | ||
partitionedResults[partition].push_back(std::move(page)); | ||
} else { | ||
// Null page indicates this buffer is finished. | ||
bufferManager->deleteResults(taskId, partition); | ||
finished = true; | ||
} | ||
} | ||
} | ||
})); | ||
} | ||
|
||
for (auto& thread : consumerThreads) { | ||
thread.join(); | ||
} | ||
return partitionedResults; | ||
} | ||
|
||
PartitionedOutputReplayer::PartitionedOutputReplayer( | ||
const std::string& rootDir, | ||
const std::string& taskId, | ||
const std::string& nodeId, | ||
const int32_t pipelineId, | ||
const std::string& operatorType) | ||
: OperatorReplayerBase(rootDir, taskId, nodeId, pipelineId, operatorType), | ||
originalNode_(dynamic_cast<const core::PartitionedOutputNode*>( | ||
core::PlanNode::findFirstNode( | ||
planFragment_.get(), | ||
[this](const core::PlanNode* node) { | ||
return node->id() == nodeId_; | ||
}))) { | ||
VELOX_CHECK_NOT_NULL(originalNode_); | ||
} | ||
|
||
RowVectorPtr PartitionedOutputReplayer::run() { | ||
auto task = Task::create( | ||
"local://partitioned-output-replayer", | ||
core::PlanFragment{createPlan()}, | ||
0, | ||
createQueryContext(queryConfigs_, executor_.get()), | ||
Task::ExecutionMode::kParallel); | ||
task->start(maxDrivers_); | ||
|
||
auto partitionedResults = getAllData( | ||
bufferManager_, | ||
task->taskId(), | ||
originalNode_->numPartitions(), | ||
executor_.get()); | ||
common::testutil::TestValue::adjust( | ||
"facebook::velox::tool::PartitionedOutputReplayer::run", | ||
&partitionedResults); | ||
return nullptr; | ||
} | ||
|
||
core::PlanNodePtr PartitionedOutputReplayer::createPlan() const { | ||
return PlanBuilder() | ||
.traceScan(nodeDir_, exec::trace::getDataType(planFragment_, nodeId_)) | ||
.addNode( | ||
partitionedOutputNodeFactory(originalNode_)) | ||
.planNode(); | ||
} | ||
|
||
} // namespace facebook::velox::tool::trace |
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,58 @@ | ||
/* | ||
* 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 <utility> | ||
|
||
#include "velox/core/PlanNode.h" | ||
#include "velox/tool/trace/OperatorReplayerBase.h" | ||
|
||
namespace facebook::velox::tool::trace { | ||
|
||
/// Concurrently gets all partitioned buffer content (vec<IOBuf>) for every | ||
/// partition. | ||
std::vector<std::vector<std::unique_ptr<folly::IOBuf>>> getAllData( | ||
const std::shared_ptr<exec::OutputBufferManager>& bufferManager, | ||
const std::string& taskId, | ||
uint32_t numPartitions, | ||
folly::Executor* executor); | ||
|
||
/// The replayer to replay the traced 'PartitionedOutput' operator. | ||
class PartitionedOutputReplayer final : public OperatorReplayerBase { | ||
public: | ||
PartitionedOutputReplayer( | ||
const std::string& rootDir, | ||
const std::string& taskId, | ||
const std::string& nodeId, | ||
const int32_t pipelineId, | ||
const std::string& operatorType); | ||
|
||
RowVectorPtr run() override; | ||
|
||
private: | ||
core::PlanNodePtr createPlan() const; | ||
|
||
const core::PartitionedOutputNode* originalNode_; | ||
|
||
std::unique_ptr<folly::Executor> executor_{ | ||
std::make_unique<folly::CPUThreadPoolExecutor>( | ||
std::thread::hardware_concurrency())}; | ||
|
||
const std::shared_ptr<exec::OutputBufferManager> bufferManager_{ | ||
exec::OutputBufferManager::getInstance().lock()}; | ||
}; | ||
} // namespace facebook::velox::tool::trace |
Oops, something went wrong.