mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-30 08:35:52 +00:00
Add a flag to defer cache reads while the cache is being updated (#97)
This commit is contained in:
@@ -7,6 +7,8 @@ SimpleCache::update(
|
|||||||
uint32_t seq,
|
uint32_t seq,
|
||||||
bool isBackground)
|
bool isBackground)
|
||||||
{
|
{
|
||||||
|
deferReads_ = true;
|
||||||
|
{
|
||||||
std::unique_lock lck{mtx_};
|
std::unique_lock lck{mtx_};
|
||||||
if (seq > latestSeq_)
|
if (seq > latestSeq_)
|
||||||
{
|
{
|
||||||
@@ -32,10 +34,14 @@ SimpleCache::update(
|
|||||||
deletes_.insert(obj.key);
|
deletes_.insert(obj.key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
deferReads_ = false;
|
||||||
}
|
}
|
||||||
std::optional<LedgerObject>
|
std::optional<LedgerObject>
|
||||||
SimpleCache::getSuccessor(ripple::uint256 const& key, uint32_t seq) const
|
SimpleCache::getSuccessor(ripple::uint256 const& key, uint32_t seq) const
|
||||||
{
|
{
|
||||||
|
if (deferReads_)
|
||||||
|
return {};
|
||||||
if (!full_)
|
if (!full_)
|
||||||
return {};
|
return {};
|
||||||
std::shared_lock{mtx_};
|
std::shared_lock{mtx_};
|
||||||
@@ -49,6 +55,8 @@ SimpleCache::getSuccessor(ripple::uint256 const& key, uint32_t seq) const
|
|||||||
std::optional<LedgerObject>
|
std::optional<LedgerObject>
|
||||||
SimpleCache::getPredecessor(ripple::uint256 const& key, uint32_t seq) const
|
SimpleCache::getPredecessor(ripple::uint256 const& key, uint32_t seq) const
|
||||||
{
|
{
|
||||||
|
if (deferReads_)
|
||||||
|
return {};
|
||||||
if (!full_)
|
if (!full_)
|
||||||
return {};
|
return {};
|
||||||
std::shared_lock lck{mtx_};
|
std::shared_lock lck{mtx_};
|
||||||
@@ -63,6 +71,8 @@ SimpleCache::getPredecessor(ripple::uint256 const& key, uint32_t seq) const
|
|||||||
std::optional<Blob>
|
std::optional<Blob>
|
||||||
SimpleCache::get(ripple::uint256 const& key, uint32_t seq) const
|
SimpleCache::get(ripple::uint256 const& key, uint32_t seq) const
|
||||||
{
|
{
|
||||||
|
if (deferReads_)
|
||||||
|
return {};
|
||||||
if (seq > latestSeq_)
|
if (seq > latestSeq_)
|
||||||
return {};
|
return {};
|
||||||
std::shared_lock lck{mtx_};
|
std::shared_lock lck{mtx_};
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ class SimpleCache
|
|||||||
};
|
};
|
||||||
std::map<ripple::uint256, CacheEntry> map_;
|
std::map<ripple::uint256, CacheEntry> map_;
|
||||||
mutable std::shared_mutex mtx_;
|
mutable std::shared_mutex mtx_;
|
||||||
|
// flag set in update to prevent reads from starving the update, and to
|
||||||
|
// prevent reads from piling up behind the update
|
||||||
|
std::atomic_bool deferReads_ = false;
|
||||||
uint32_t latestSeq_ = 0;
|
uint32_t latestSeq_ = 0;
|
||||||
std::atomic_bool full_ = false;
|
std::atomic_bool full_ = false;
|
||||||
// temporary set to prevent background thread from writing already deleted
|
// temporary set to prevent background thread from writing already deleted
|
||||||
|
|||||||
Reference in New Issue
Block a user