-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[Velox] Table writer 2: Add write protocol interface #2845
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * 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/connectors/WriteProtocol.h" | ||
| #include "velox/connectors/Connector.h" | ||
| #include "velox/vector/ConstantVector.h" | ||
|
|
||
| #include <unordered_map> | ||
|
|
||
| namespace facebook::velox::connector { | ||
|
|
||
| namespace { | ||
|
|
||
| using RegisteredWriteProtocols = std::unordered_map< | ||
| WriteProtocol::CommitStrategy, | ||
| std::shared_ptr<WriteProtocol>>; | ||
|
|
||
| RegisteredWriteProtocols& registeredWriteProtocols() { | ||
| // Default write protocol registered | ||
| static RegisteredWriteProtocols protocols{ | ||
| {WriteProtocol::CommitStrategy::kNoCommit, | ||
| std::make_shared<DefaultWriteProtocol>()}}; | ||
| return protocols; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| // static | ||
| bool WriteProtocol::registerWriteProtocol( | ||
| WriteProtocol::CommitStrategy commitStrategy, | ||
| std::shared_ptr<WriteProtocol> writeProtocol) { | ||
| return registeredWriteProtocols() | ||
| .insert_or_assign(commitStrategy, writeProtocol) | ||
| .second; | ||
| } | ||
|
|
||
| // static | ||
| std::shared_ptr<WriteProtocol> WriteProtocol::getWriteProtocol( | ||
| CommitStrategy commitStrategy) { | ||
| const auto iter = registeredWriteProtocols().find(commitStrategy); | ||
| // Fail if no WriteProtocol has been registered for the given CommitStrategy. | ||
| VELOX_CHECK( | ||
| iter != registeredWriteProtocols().end(), | ||
| "No write protocol found for commit strategy {}", | ||
| commitStrategyToString(commitStrategy)); | ||
| return iter->second; | ||
| } | ||
|
|
||
| RowVectorPtr DefaultWriteProtocol::commit( | ||
| const WriteInfo& writeInfo, | ||
| velox::memory::MemoryPool* FOLLY_NONNULL pool) { | ||
| return std::make_shared<RowVector>( | ||
| pool, | ||
| ROW({"rowCount"}, {BIGINT()}), | ||
| BufferPtr(nullptr), | ||
| 1, | ||
| std::vector<VectorPtr>{std::make_shared<ConstantVector<int64_t>>( | ||
| pool, 1, false, BIGINT(), writeInfo.numWrittenRows())}); | ||
| } | ||
|
|
||
| } // namespace facebook::velox::connector |
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,123 @@ | ||
| /* | ||
| * 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 "velox/vector/ComplexVector.h" | ||
|
|
||
| namespace facebook::velox::connector { | ||
|
|
||
| class ConnectorInsertTableHandle; | ||
| class ConnectorQueryCtx; | ||
|
|
||
| /// Interface to provide key parameters for writers. Ex., write and commit | ||
| /// locations, append or overwrite, etc. | ||
| class WriterParameters { | ||
| public: | ||
| virtual ~WriterParameters() = default; | ||
| }; | ||
|
|
||
| /// Interface that includes all info from write. It will be passed to commit() | ||
| /// of WriteProtocol. | ||
| class WriteInfo { | ||
| public: | ||
| virtual ~WriteInfo() = default; | ||
|
|
||
| virtual vector_size_t numWrittenRows() const = 0; | ||
| }; | ||
|
|
||
| /// Abstraction for write behaviors. Systems register WriteProtocols | ||
| /// by CommitStrategy. Writers call getWriteProtocol() to get the registered | ||
| /// instance of the WriteProtocol when needed. | ||
| class WriteProtocol { | ||
| public: | ||
| /// Represents the commit strategy of a write protocol. | ||
| enum class CommitStrategy { | ||
| kNoCommit, // No more commit actions are needed. | ||
| kTaskCommit // Task level commit is needed. | ||
| }; | ||
|
|
||
| virtual ~WriteProtocol() {} | ||
|
|
||
| /// Return the commit strategy of the write protocol. It will be the commit | ||
| /// strategy that the write protocol registers for. | ||
| virtual CommitStrategy commitStrategy() const = 0; | ||
|
|
||
| /// Return a string encoding of the given commit strategy. | ||
| static std::string commitStrategyToString(CommitStrategy commitStrategy) { | ||
| switch (commitStrategy) { | ||
| case CommitStrategy::kNoCommit: | ||
| return "NO_COMMIT"; | ||
| case CommitStrategy::kTaskCommit: | ||
| return "TASK_COMMIT"; | ||
| default: | ||
| VELOX_UNREACHABLE(); | ||
| } | ||
| } | ||
|
|
||
| /// Perform actions of commit. It would be called by the writers and could | ||
| /// return outputs that would be included in writer outputs. Return nullptr if | ||
| /// the commit action does not need to add output to the table writer output. | ||
| virtual RowVectorPtr commit( | ||
| const WriteInfo& writeInfo, | ||
| velox::memory::MemoryPool* FOLLY_NONNULL pool) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| /// Return parameters for writers. Ex., write and commit locations. Return | ||
| /// nullptr if the writer does not need parameters from the write protocol. | ||
| virtual std::shared_ptr<const WriterParameters> getWriterParameters( | ||
| const std::shared_ptr<const velox::connector::ConnectorInsertTableHandle>& | ||
| tableHandle, | ||
| const velox::connector::ConnectorQueryCtx* FOLLY_NONNULL | ||
| connectorQueryCtx) const = 0; | ||
|
|
||
| /// Register a WriteProtocol implementation for the given CommitStrategy. If | ||
| /// the CommitStrategy has already been registered, it will replace the old | ||
| /// WriteProtocol implementation with the new one and return false; otherwise | ||
| /// return true. | ||
| static bool registerWriteProtocol( | ||
| CommitStrategy commitStrategy, | ||
| std::shared_ptr<WriteProtocol> writeProtocol); | ||
|
|
||
| /// Return the instance of the WriteProtocol registered for | ||
| /// the given CommitStrategy. | ||
| static std::shared_ptr<WriteProtocol> getWriteProtocol( | ||
| CommitStrategy commitStrategy); | ||
| }; | ||
|
|
||
| class DefaultWriteProtocol : public WriteProtocol { | ||
| public: | ||
| ~DefaultWriteProtocol() override {} | ||
|
|
||
| CommitStrategy commitStrategy() const override { | ||
| return CommitStrategy::kNoCommit; | ||
| } | ||
|
|
||
| RowVectorPtr commit( | ||
|
||
| const WriteInfo& writeInfo, | ||
| velox::memory::MemoryPool* FOLLY_NONNULL pool) override; | ||
|
|
||
| std::shared_ptr<const WriterParameters> getWriterParameters( | ||
| const std::shared_ptr<const velox::connector::ConnectorInsertTableHandle>& | ||
| tableHandle, | ||
| const velox::connector::ConnectorQueryCtx* FOLLY_NONNULL | ||
| connectorQueryCtx) const override { | ||
| return std::make_shared<WriterParameters>(); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace facebook::velox::connector | ||
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
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.
Any particular reason this API doesn't take CommitStrategy? It would be more natural to specify both CommitStrategy and WriteProtocol when registering as opposed to fetching CommitStrategy from the protocol.
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.
Okay, used to pass CommitStrategy to this func together wi/ the WriteProtocol. Changing it back to the more natural way.