diff --git a/src/ripple/nodestore/impl/Shard.cpp b/src/ripple/nodestore/impl/Shard.cpp index ad0d685fa..76c31253d 100644 --- a/src/ripple/nodestore/impl/Shard.cpp +++ b/src/ripple/nodestore/impl/Shard.cpp @@ -228,7 +228,17 @@ Shard::open(Scheduler& scheduler, nudb::context& ctx) std::string("exception ") + e.what() + " in function " + __func__); } - setBackendCache(lock); + // Set backend caches + { + auto const size{config.getValueFor(SizedItem::nodeCacheSize, 0)}; + auto const age{std::chrono::seconds{ + config.getValueFor(SizedItem::nodeCacheAge, 0)}}; + auto const name{"shard " + std::to_string(index_)}; + + pCache_ = std::make_shared(name, size, age, stopwatch(), j_); + nCache_ = std::make_shared(name, stopwatch(), size, age); + } + if (!initSQLite(lock)) return fail({}); @@ -306,7 +316,6 @@ Shard::store(std::shared_ptr const& ledger) return false; backendComplete_ = true; - setBackendCache(lock); } JLOG(j_.debug()) << "shard " << index_ << " stored ledger sequence " << seq @@ -523,6 +532,15 @@ Shard::finalize( std::shared_ptr ledger; std::shared_ptr next; auto const lastLedgerHash{hash}; + auto& shardFamily{*app_.getShardFamily()}; + auto const fullBelowCache{shardFamily.getFullBelowCache(lastSeq_)}; + auto const treeNodeCache{shardFamily.getTreeNodeCache(lastSeq_)}; + + // Reset caches to reduce memory usage + pCache_->reset(); + nCache_->reset(); + fullBelowCache->reset(); + treeNodeCache->reset(); // Start with the last ledger in the shard and walk backwards from // child to parent until we reach the first ledger @@ -539,7 +557,7 @@ Shard::finalize( ledger = std::make_shared( deserializePrefixedHeader(makeSlice(nObj->getData())), app_.config(), - *app_.getShardFamily()); + shardFamily); if (ledger->info().seq != seq) return fail("invalid ledger sequence"); if (ledger->info().hash != hash) @@ -573,6 +591,11 @@ Shard::finalize( hash = ledger->info().parentHash; next = std::move(ledger); --seq; + + pCache_->reset(); + nCache_->reset(); + fullBelowCache->reset(); + treeNodeCache->reset(); } JLOG(j_.debug()) << "shard " << index_ << " is valid"; @@ -621,9 +644,7 @@ Shard::finalize( try { backend_->store(nObj); - std::lock_guard lock(mutex_); - final_ = true; // Remove the acquire SQLite database if present if (acquireInfo_) @@ -634,6 +655,7 @@ Shard::finalize( return fail("failed to initialize SQLite databases"); setFileStats(lock); + final_ = true; } catch (std::exception const& e) { @@ -644,40 +666,6 @@ Shard::finalize( return true; } -void -Shard::setBackendCache(std::lock_guard const&) -{ - // Complete shards use the smallest cache and - // fastest expiration to reduce memory consumption. - // An incomplete shard is set according to configuration. - - Config const& config{app_.config()}; - if (!pCache_) - { - auto const name{"shard " + std::to_string(index_)}; - auto const sz{config.getValueFor( - SizedItem::nodeCacheSize, - backendComplete_ ? boost::optional(0) : boost::none)}; - auto const age{std::chrono::seconds{config.getValueFor( - SizedItem::nodeCacheAge, - backendComplete_ ? boost::optional(0) : boost::none)}}; - - pCache_ = std::make_shared(name, sz, age, stopwatch(), j_); - nCache_ = std::make_shared(name, stopwatch(), sz, age); - } - else - { - auto const sz{config.getValueFor(SizedItem::nodeCacheSize, 0)}; - pCache_->setTargetSize(sz); - nCache_->setTargetSize(sz); - - auto const age{std::chrono::seconds{ - config.getValueFor(SizedItem::nodeCacheAge, 0)}}; - pCache_->setTargetAge(age); - nCache_->setTargetAge(age); - } -} - bool Shard::initSQLite(std::lock_guard const&) { diff --git a/src/ripple/nodestore/impl/Shard.h b/src/ripple/nodestore/impl/Shard.h index 3ac3859eb..55fb348cd 100644 --- a/src/ripple/nodestore/impl/Shard.h +++ b/src/ripple/nodestore/impl/Shard.h @@ -239,11 +239,6 @@ private: // Determines if the shard directory should be removed in the destructor std::atomic removeOnDestroy_{false}; - // Set the backend cache - // Lock over mutex_ required - void - setBackendCache(std::lock_guard const& lock); - // Open/Create SQLite databases // Lock over mutex_ required bool diff --git a/src/ripple/shamap/impl/ShardFamily.cpp b/src/ripple/shamap/impl/ShardFamily.cpp index ea80f85ba..ee4a7c83c 100644 --- a/src/ripple/shamap/impl/ShardFamily.cpp +++ b/src/ripple/shamap/impl/ShardFamily.cpp @@ -38,8 +38,8 @@ ShardFamily::ShardFamily(Application& app, CollectorManager& cm) , db_(getShardStore(app)) , cm_(cm) , j_(app.journal("ShardFamily")) - , tnTargetSize_(app.config().getValueFor(SizedItem::treeCacheSize)) - , tnTargetAge_(app.config().getValueFor(SizedItem::treeCacheAge)) + , tnTargetSize_(app.config().getValueFor(SizedItem::treeCacheSize, 0)) + , tnTargetAge_(app.config().getValueFor(SizedItem::treeCacheAge, 0)) { }