-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add paths to the store asynchronously #13798
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
Open
edolstra
wants to merge
1
commit into
master
Choose a base branch
from
async-path-writer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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
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
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
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,151 @@ | ||
#include "nix/store/async-path-writer.hh" | ||
#include "nix/util/archive.hh" | ||
|
||
#include <thread> | ||
#include <future> | ||
|
||
namespace nix { | ||
|
||
struct AsyncPathWriterImpl : AsyncPathWriter | ||
{ | ||
ref<Store> store; | ||
|
||
struct Item | ||
{ | ||
StorePath storePath; | ||
std::string contents; | ||
std::string name; | ||
Hash hash; | ||
StorePathSet references; | ||
RepairFlag repair; | ||
std::promise<void> promise; | ||
}; | ||
|
||
struct State | ||
{ | ||
std::vector<Item> items; | ||
std::unordered_map<StorePath, std::shared_future<void>> futures; | ||
bool quit = false; | ||
}; | ||
|
||
Sync<State> state_; | ||
|
||
std::thread workerThread; | ||
|
||
std::condition_variable wakeupCV; | ||
|
||
AsyncPathWriterImpl(ref<Store> store) | ||
: store(store) | ||
{ | ||
workerThread = std::thread([&]() { | ||
while (true) { | ||
std::vector<Item> items; | ||
|
||
{ | ||
auto state(state_.lock()); | ||
while (!state->quit && state->items.empty()) | ||
state.wait(wakeupCV); | ||
if (state->items.empty() && state->quit) | ||
return; | ||
std::swap(items, state->items); | ||
} | ||
|
||
try { | ||
writePaths(items); | ||
for (auto & item : items) | ||
item.promise.set_value(); | ||
} catch (...) { | ||
for (auto & item : items) | ||
item.promise.set_exception(std::current_exception()); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
~AsyncPathWriterImpl() | ||
{ | ||
state_.lock()->quit = true; | ||
wakeupCV.notify_all(); | ||
workerThread.join(); | ||
} | ||
|
||
StorePath | ||
addPath(std::string contents, std::string name, StorePathSet references, RepairFlag repair, bool readOnly) override | ||
{ | ||
auto hash = hashString(HashAlgorithm::SHA256, contents); | ||
|
||
auto storePath = store->makeFixedOutputPathFromCA( | ||
name, | ||
TextInfo{ | ||
.hash = hash, | ||
.references = references, | ||
}); | ||
|
||
if (!readOnly) { | ||
auto state(state_.lock()); | ||
std::promise<void> promise; | ||
state->futures.insert_or_assign(storePath, promise.get_future()); | ||
state->items.push_back( | ||
Item{ | ||
.storePath = storePath, | ||
.contents = std::move(contents), | ||
.name = std::move(name), | ||
.hash = hash, | ||
.references = std::move(references), | ||
.repair = repair, | ||
.promise = std::move(promise), | ||
}); | ||
wakeupCV.notify_all(); | ||
} | ||
|
||
return storePath; | ||
} | ||
|
||
void waitForPath(const StorePath & path) override | ||
{ | ||
auto future = ({ | ||
auto state = state_.lock(); | ||
auto i = state->futures.find(path); | ||
if (i == state->futures.end()) | ||
return; | ||
i->second; | ||
}); | ||
future.get(); | ||
} | ||
|
||
void waitForAllPaths() override | ||
{ | ||
auto futures = ({ | ||
auto state(state_.lock()); | ||
std::move(state->futures); | ||
}); | ||
for (auto & future : futures) | ||
future.second.get(); | ||
} | ||
|
||
void writePaths(const std::vector<Item> & items) | ||
{ | ||
// FIXME: use addMultipeToStore() once it doesn't require a | ||
// NAR hash from the client for CA objects. | ||
|
||
for (auto & item : items) { | ||
StringSource source(item.contents); | ||
auto storePath = store->addToStoreFromDump( | ||
source, | ||
item.storePath.name(), | ||
FileSerialisationMethod::Flat, | ||
ContentAddressMethod::Raw::Text, | ||
HashAlgorithm::SHA256, | ||
item.references, | ||
item.repair); | ||
assert(storePath == item.storePath); | ||
} | ||
} | ||
}; | ||
|
||
ref<AsyncPathWriter> AsyncPathWriter::make(ref<Store> store) | ||
{ | ||
return make_ref<AsyncPathWriterImpl>(store); | ||
} | ||
|
||
} // namespace nix |
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.
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.
As we discussed briefly in the meeting, we could instead implement pipelining, which does not require an update to the worker protocol.
Another benefit of pipelining that was not brought up is that it also allows the first response to come in before any of the other requests, reducing the latency of individual add operations.