-
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 QuerySplitTracer to records and rebuild splits
- Loading branch information
Showing
11 changed files
with
415 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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/exec/QuerySplitReader.h" | ||
|
||
#include <folly/hash/Checksum.h> | ||
|
||
#include "velox/common/file/FileInputStream.h" | ||
#include "velox/connectors/hive/HiveConnectorSplit.h" | ||
#include "velox/exec/QueryTraceTraits.h" | ||
#include "velox/exec/QueryTraceUtil.h" | ||
|
||
using namespace facebook::velox::connector::hive; | ||
|
||
namespace facebook::velox::exec::trace { | ||
|
||
QuerySplitReader::QuerySplitReader( | ||
std::string traceDir, | ||
memory::MemoryPool* pool) | ||
: traceDir_(std::move(traceDir)), | ||
fs_(filesystems::getFileSystem(traceDir_, nullptr)), | ||
pool_(pool), | ||
splitInfoStream_(getSplitInputStream()) { | ||
VELOX_CHECK_NOT_NULL(fs_); | ||
VELOX_CHECK_NOT_NULL(splitInfoStream_); | ||
} | ||
|
||
std::vector<exec::Split> QuerySplitReader::read() const { | ||
const auto splitStrings = getSplitInfos(splitInfoStream_.get()); | ||
std::vector<exec::Split> splits; | ||
for (const auto& splitString : splitStrings) { | ||
folly::dynamic splitInfoObj = folly::parseJson(splitString); | ||
const auto split = | ||
ISerializable::deserialize<HiveConnectorSplit>(splitInfoObj); | ||
splits.emplace_back( | ||
std::make_shared<HiveConnectorSplit>( | ||
split->connectorId, | ||
split->filePath, | ||
split->fileFormat, | ||
split->start, | ||
split->length, | ||
split->partitionKeys, | ||
split->tableBucketNumber, | ||
split->customSplitInfo, | ||
split->extraFileInfo, | ||
split->serdeParameters, | ||
split->splitWeight, | ||
split->infoColumns, | ||
split->properties), | ||
-1); | ||
} | ||
return splits; | ||
} | ||
|
||
std::unique_ptr<common::FileInputStream> QuerySplitReader::getSplitInputStream() | ||
const { | ||
auto splitInfoFile = fs_->openFileForRead( | ||
fmt::format("{}/{}", traceDir_, QueryTraceTraits::kSplitInfoFileName)); | ||
// TODO: Make the buffer size configurable. | ||
return std::make_unique<common::FileInputStream>( | ||
std::move(splitInfoFile), 1 << 20, pool_); | ||
} | ||
|
||
// static | ||
std::vector<std::string> QuerySplitReader::getSplitInfos( | ||
common::FileInputStream* stream) { | ||
std::vector<std::string> splits; | ||
try { | ||
while (!stream->atEnd()) { | ||
const auto length = stream->read<uint32_t>(); | ||
std::string splitInfoString(length, '\0'); | ||
stream->readBytes( | ||
reinterpret_cast<uint8_t*>(splitInfoString.data()), length); | ||
const auto crc32 = stream->read<uint32_t>(); | ||
const auto actualCrc32 = folly::crc32( | ||
reinterpret_cast<const uint8_t*>(splitInfoString.data()), | ||
splitInfoString.size()); | ||
if (crc32 != actualCrc32) { | ||
LOG(ERROR) << "Fails to verify the checksum " << crc32 | ||
<< " does not equal to the actual checksum " << actualCrc32; | ||
break; | ||
} | ||
splits.push_back(std::move(splitInfoString)); | ||
} | ||
} catch (const VeloxException& e) { | ||
LOG(ERROR) << "Fails to deserialize split string from the stream for " | ||
<< e.message(); | ||
} | ||
return splits; | ||
} | ||
} // namespace facebook::velox::exec::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,50 @@ | ||
/* | ||
* 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 <re2/re2.h> | ||
#include "velox/common/file/FileInputStream.h" | ||
#include "velox/common/file/FileSystems.h" | ||
#include "velox/exec/Split.h" | ||
|
||
namespace facebook::velox::exec::trace { | ||
/// Used to load the input splits from a tracing 'TableScan' | ||
/// operator, and for getting the traced splits when relaying 'TableScan'. | ||
/// | ||
/// Currently, it only works with 'HiveConnectorSplit'. In the future, it will | ||
/// be extended to handle more types of splits, such as | ||
/// 'IcebergHiveConnectorSplit'. | ||
class QuerySplitReader { | ||
public: | ||
explicit QuerySplitReader(std::string traceDir, memory::MemoryPool* pool); | ||
|
||
/// Reads from 'splitInfoStream_' and deserializes to 'splitInfos'. Returns | ||
/// all the correctly traced splits. | ||
std::vector<exec::Split> read() const; | ||
|
||
private: | ||
static std::vector<std::string> getSplitInfos( | ||
common::FileInputStream* stream); | ||
|
||
std::unique_ptr<common::FileInputStream> getSplitInputStream() const; | ||
|
||
const std::string traceDir_; | ||
const std::shared_ptr<filesystems::FileSystem> fs_; | ||
memory::MemoryPool* const pool_; | ||
const std::unique_ptr<common::FileInputStream> splitInfoStream_; | ||
}; | ||
} // namespace facebook::velox::exec::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,79 @@ | ||
/* | ||
* 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/exec/QuerySplitWriter.h" | ||
|
||
#include <folly/hash/Checksum.h> | ||
#include <folly/io/Cursor.h> | ||
|
||
#include "QueryTraceUtil.h" | ||
#include "velox/connectors/hive/HiveConnectorSplit.h" | ||
#include "velox/exec/QueryTraceTraits.h" | ||
|
||
using namespace facebook::velox::connector::hive; | ||
|
||
namespace facebook::velox::exec::trace { | ||
/// Used to record and load the input splits from a tracing 'TableScan' | ||
/// operator, and for getting the traced splits when relaying 'TableScan'. | ||
/// | ||
/// Currently, it only works with 'HiveConnectorSplit'. In the future, it will | ||
/// be extended to handle more types of splits, such as | ||
/// 'IcebergHiveConnectorSplit'. | ||
QuerySplitWriter::QuerySplitWriter(std::string traceDir) | ||
: traceDir_(std::move(traceDir)), | ||
fs_(filesystems::getFileSystem(traceDir_, nullptr)) { | ||
VELOX_CHECK_NOT_NULL(fs_); | ||
splitInfoFile_ = fs_->openFileForWrite( | ||
fmt::format("{}/{}", traceDir_, QueryTraceTraits::kSplitInfoFileName)); | ||
VELOX_CHECK_NOT_NULL(splitInfoFile_); | ||
} | ||
|
||
void QuerySplitWriter::write(const exec::Split& split) const { | ||
VELOX_CHECK(!split.hasGroup(), "Do not support grouped execution"); | ||
VELOX_CHECK(split.hasConnectorSplit()); | ||
const auto splitObj = split.connectorSplit->serialize(); | ||
const auto splitJson = folly::toJson(splitObj); | ||
auto ioBuf = appendToBuffer(splitJson); | ||
splitInfoFile_->append(std::move(ioBuf)); | ||
} | ||
|
||
void QuerySplitWriter::finish() { | ||
if (finished_) { | ||
return; | ||
} | ||
|
||
VELOX_CHECK_NOT_NULL( | ||
splitInfoFile_, "The query data writer has already been finished"); | ||
splitInfoFile_->close(); | ||
splitInfoFile_.reset(); | ||
finished_ = true; | ||
} | ||
|
||
// static | ||
std::unique_ptr<folly::IOBuf> QuerySplitWriter::appendToBuffer( | ||
const std::string& split) { | ||
const uint32_t length = split.length(); | ||
const uint32_t crc32 = folly::crc32( | ||
reinterpret_cast<const uint8_t*>(split.data()), split.size()); | ||
auto ioBuf = | ||
folly::IOBuf::create(sizeof(length) + split.size() + sizeof(crc32)); | ||
folly::io::Appender appender(ioBuf.get(), 0); | ||
appender.writeLE(length); | ||
appender.push(reinterpret_cast<const uint8_t*>(split.data()), length); | ||
appender.writeLE(crc32); | ||
return ioBuf; | ||
} | ||
} // namespace facebook::velox::exec::trace |
Oops, something went wrong.