-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Improve online delete backend lock #3342
Closed
+115
−87
Closed
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,15 +55,54 @@ DatabaseRotatingImp::DatabaseRotatingImp( | |
setParent(parent); | ||
} | ||
|
||
std::shared_ptr<Backend> | ||
DatabaseRotatingImp::rotateBackends( | ||
std::shared_ptr<Backend> newBackend, | ||
std::lock_guard<std::mutex> const&) | ||
void | ||
DatabaseRotatingImp::rotateWithLock( | ||
std::function<std::unique_ptr<NodeStore::Backend>( | ||
std::string const& writableBackendName)> const& f) | ||
{ | ||
auto oldBackend{std::move(archiveBackend_)}; | ||
std::lock_guard lock(mutex_); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be injected by the caller, described above. |
||
|
||
auto newBackend = f(writableBackend_->getName()); | ||
archiveBackend_->setDeletePath(); | ||
archiveBackend_ = std::move(writableBackend_); | ||
writableBackend_ = std::move(newBackend); | ||
return oldBackend; | ||
} | ||
|
||
std::string | ||
DatabaseRotatingImp::getName() const | ||
{ | ||
std::lock_guard lock(mutex_); | ||
return writableBackend_->getName(); | ||
} | ||
|
||
std::int32_t | ||
DatabaseRotatingImp::getWriteLoad() const | ||
{ | ||
std::lock_guard lock(mutex_); | ||
return writableBackend_->getWriteLoad(); | ||
} | ||
|
||
void | ||
DatabaseRotatingImp::import(Database& source) | ||
{ | ||
auto const backend = [&] { | ||
std::lock_guard lock(mutex_); | ||
return writableBackend_; | ||
}(); | ||
|
||
importInternal(*backend, source); | ||
} | ||
|
||
bool | ||
DatabaseRotatingImp::storeLedger(std::shared_ptr<Ledger const> const& srcLedger) | ||
{ | ||
auto const backend = [&] { | ||
std::lock_guard lock(mutex_); | ||
return writableBackend_; | ||
}(); | ||
|
||
return Database::storeLedger( | ||
*srcLedger, backend, pCache_, nCache_, nullptr); | ||
} | ||
|
||
void | ||
|
@@ -75,7 +114,13 @@ DatabaseRotatingImp::store( | |
{ | ||
auto nObj = NodeObject::createObject(type, std::move(data), hash); | ||
pCache_->canonicalize_replace_cache(hash, nObj); | ||
getWritableBackend()->store(nObj); | ||
|
||
auto const backend = [&] { | ||
std::lock_guard lock(mutex_); | ||
return writableBackend_; | ||
}(); | ||
backend->store(nObj); | ||
|
||
nCache_->erase(hash); | ||
storeStats(nObj->getData().size()); | ||
} | ||
|
@@ -90,6 +135,7 @@ DatabaseRotatingImp::asyncFetch( | |
object = pCache_->fetch(hash); | ||
if (object || nCache_->touch_if_exists(hash)) | ||
return true; | ||
|
||
// Otherwise post a read | ||
Database::asyncFetch(hash, seq, pCache_, nCache_); | ||
return false; | ||
|
@@ -114,19 +160,48 @@ DatabaseRotatingImp::sweep() | |
std::shared_ptr<NodeObject> | ||
DatabaseRotatingImp::fetchFrom(uint256 const& hash, std::uint32_t seq) | ||
{ | ||
Backends b = getBackends(); | ||
auto nObj = fetchInternal(hash, b.writableBackend); | ||
auto [writable, archive] = [&] { | ||
std::lock_guard lock(mutex_); | ||
return std::make_pair(writableBackend_, archiveBackend_); | ||
}(); | ||
|
||
// Try to fetch from the writable backend | ||
auto nObj = fetchInternal(hash, writable); | ||
if (!nObj) | ||
{ | ||
nObj = fetchInternal(hash, b.archiveBackend); | ||
// Otherwise try to fetch from the archive backend | ||
nObj = fetchInternal(hash, archive); | ||
if (nObj) | ||
{ | ||
getWritableBackend()->store(nObj); | ||
{ | ||
// Refresh the writable backend pointer | ||
std::lock_guard lock(mutex_); | ||
writable = writableBackend_; | ||
} | ||
|
||
// Update writable backend with data from the archive backend | ||
writable->store(nObj); | ||
nCache_->erase(hash); | ||
} | ||
} | ||
return nObj; | ||
} | ||
|
||
void | ||
DatabaseRotatingImp::for_each( | ||
std::function<void(std::shared_ptr<NodeObject>)> f) | ||
{ | ||
auto [writable, archive] = [&] { | ||
std::lock_guard lock(mutex_); | ||
return std::make_pair(writableBackend_, archiveBackend_); | ||
}(); | ||
|
||
// Iterate the writable backend | ||
writable->for_each(f); | ||
|
||
// Iterate the archive backend | ||
archive->for_each(f); | ||
} | ||
|
||
} // namespace NodeStore | ||
} // namespace ripple |
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
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.
The previous locking behavior needs to be there for rotateBackends() and for clearCaches()--the same lock needs to cover them both. For the fullbelow cache, each call to clear() increments an integer called "m_gen" (generation). The generation determines if the cache entry is valid for the existing backends, or if it applies to backends that just rotated.