-
Notifications
You must be signed in to change notification settings - Fork 85
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
Parallel Hash Index #2615
Merged
Merged
Parallel Hash Index #2615
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
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
135 changes: 135 additions & 0 deletions
135
src/include/processor/operator/persistent/index_builder.h
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,135 @@ | ||
#pragma once | ||
|
||
#include <variant> | ||
|
||
#include "common/copy_constructors.h" | ||
#include "common/mpsc_queue.h" | ||
#include "common/static_vector.h" | ||
#include "common/types/internal_id_t.h" | ||
#include "common/types/types.h" | ||
#include "processor/execution_context.h" | ||
#include "storage/index/hash_index_builder.h" | ||
#include "storage/store/column_chunk.h" | ||
|
||
namespace kuzu { | ||
namespace processor { | ||
|
||
constexpr size_t BUFFER_SIZE = 1024; | ||
using IntBuffer = common::StaticVector<std::pair<int64_t, common::offset_t>, BUFFER_SIZE>; | ||
using StringBuffer = common::StaticVector<std::pair<std::string, common::offset_t>, BUFFER_SIZE>; | ||
|
||
class IndexBuilderGlobalQueues { | ||
public: | ||
explicit IndexBuilderGlobalQueues(std::unique_ptr<storage::PrimaryKeyIndexBuilder> pkIndex); | ||
|
||
void flushToDisk() const; | ||
|
||
void insert(size_t index, StringBuffer elem); | ||
void insert(size_t index, IntBuffer elem); | ||
|
||
void consume(); | ||
|
||
common::LogicalTypeID pkTypeID() const { return pkIndex->keyTypeID(); } | ||
|
||
private: | ||
void maybeConsumeIndex(size_t index); | ||
|
||
std::array<std::mutex, storage::NUM_HASH_INDEXES> mutexes; | ||
std::unique_ptr<storage::PrimaryKeyIndexBuilder> pkIndex; | ||
|
||
using StringQueues = std::array<common::MPSCQueue<StringBuffer>, storage::NUM_HASH_INDEXES>; | ||
using IntQueues = std::array<common::MPSCQueue<IntBuffer>, storage::NUM_HASH_INDEXES>; | ||
|
||
// Queues for distributing primary keys. | ||
std::variant<StringQueues, IntQueues> queues; | ||
}; | ||
|
||
class IndexBuilderLocalBuffers { | ||
public: | ||
explicit IndexBuilderLocalBuffers(IndexBuilderGlobalQueues& globalQueues); | ||
|
||
void insert(std::string key, common::offset_t value); | ||
void insert(int64_t key, common::offset_t value); | ||
|
||
void flush(); | ||
|
||
private: | ||
IndexBuilderGlobalQueues* globalQueues; | ||
|
||
// These arrays are much too large to be inline. | ||
using StringBuffers = std::array<StringBuffer, storage::NUM_HASH_INDEXES>; | ||
using IntBuffers = std::array<IntBuffer, storage::NUM_HASH_INDEXES>; | ||
std::unique_ptr<StringBuffers> stringBuffers; | ||
std::unique_ptr<IntBuffers> intBuffers; | ||
}; | ||
|
||
class IndexBuilderSharedState { | ||
friend class IndexBuilder; | ||
|
||
public: | ||
explicit IndexBuilderSharedState(std::unique_ptr<storage::PrimaryKeyIndexBuilder> pkIndex); | ||
void consume() { globalQueues.consume(); } | ||
void flush() { globalQueues.flushToDisk(); } | ||
|
||
void addProducer() { producers.fetch_add(1, std::memory_order_relaxed); } | ||
void quitProducer(); | ||
bool isDone() { return done.load(std::memory_order_relaxed); } | ||
|
||
private: | ||
IndexBuilderGlobalQueues globalQueues; | ||
|
||
std::atomic<size_t> producers; | ||
std::atomic<bool> done; | ||
}; | ||
|
||
// RAII for producer counting. | ||
class ProducerToken { | ||
public: | ||
explicit ProducerToken(std::shared_ptr<IndexBuilderSharedState> sharedState) | ||
: sharedState(std::move(sharedState)) { | ||
this->sharedState->addProducer(); | ||
} | ||
DELETE_COPY_DEFAULT_MOVE(ProducerToken); | ||
|
||
void quit() { | ||
Riolku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sharedState->quitProducer(); | ||
sharedState.reset(); | ||
} | ||
~ProducerToken() { | ||
if (sharedState) { | ||
quit(); | ||
} | ||
} | ||
|
||
private: | ||
std::shared_ptr<IndexBuilderSharedState> sharedState; | ||
}; | ||
|
||
class IndexBuilder { | ||
explicit IndexBuilder(std::shared_ptr<IndexBuilderSharedState> sharedState); | ||
|
||
public: | ||
DELETE_COPY_DEFAULT_MOVE(IndexBuilder); | ||
explicit IndexBuilder(std::unique_ptr<storage::PrimaryKeyIndexBuilder> pkIndex); | ||
|
||
IndexBuilder clone() { return IndexBuilder(sharedState); } | ||
|
||
void initGlobalStateInternal(ExecutionContext* /*context*/) {} | ||
void initLocalStateInternal(ExecutionContext* /*context*/) {} | ||
void insert( | ||
storage::ColumnChunk* chunk, common::offset_t nodeOffset, common::offset_t numNodes); | ||
|
||
ProducerToken getProducerToken() const { return ProducerToken(sharedState); } | ||
Riolku marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
void finishedProducing(); | ||
void finalize(ExecutionContext* context); | ||
|
||
private: | ||
void checkNonNullConstraint(storage::NullColumnChunk* nullChunk, common::offset_t numNodes); | ||
std::shared_ptr<IndexBuilderSharedState> sharedState; | ||
|
||
IndexBuilderLocalBuffers localBuffers; | ||
}; | ||
|
||
} // namespace processor | ||
} // namespace kuzu |
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.
What's this change here for?
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.
This is to make ClientContext get the correct number of threads (i.e. the number from the system config).