diff --git a/src/ripple/nodestore/impl/DatabaseShardImp.cpp b/src/ripple/nodestore/impl/DatabaseShardImp.cpp index 94d9a457dce..38aa231a9db 100644 --- a/src/ripple/nodestore/impl/DatabaseShardImp.cpp +++ b/src/ripple/nodestore/impl/DatabaseShardImp.cpp @@ -256,39 +256,47 @@ DatabaseShardImp::init() boost::optional DatabaseShardImp::prepareLedger(std::uint32_t validLedgerSeq) { - std::lock_guard lock(mutex_); - assert(init_); + boost::optional shardIndex; - if (acquireIndex_ != 0) { - if (auto it {shards_.find(acquireIndex_)}; it != shards_.end()) - return it->second.shard->prepare(); - assert(false); - return boost::none; - } + std::lock_guard lock(mutex_); + assert(init_); - if (!canAdd_) - return boost::none; + if (acquireIndex_ != 0) + { + if (auto it {shards_.find(acquireIndex_)}; it != shards_.end()) + return it->second.shard->prepare(); + assert(false); + return boost::none; + } - // Check available storage space - if (fileSz_ + avgShardFileSz_ > maxFileSz_) - { - JLOG(j_.debug()) << "maximum storage size reached"; - canAdd_ = false; - return boost::none; - } - if (avgShardFileSz_ > available()) - { - JLOG(j_.error()) << "insufficient storage space available"; - canAdd_ = false; - return boost::none; + if (!canAdd_) + return boost::none; + + // Check available storage space + if (fileSz_ + avgShardFileSz_ > maxFileSz_) + { + JLOG(j_.debug()) << "maximum storage size reached"; + canAdd_ = false; + return boost::none; + } + if (avgShardFileSz_ > available()) + { + JLOG(j_.error()) << "insufficient storage space available"; + canAdd_ = false; + return boost::none; + } + + shardIndex = findAcquireIndex(validLedgerSeq, lock); } - auto const shardIndex {findAcquireIndex(validLedgerSeq, lock)}; if (!shardIndex) { JLOG(j_.debug()) << "no new shards to add"; - canAdd_ = false; + { + std::lock_guard lock(mutex_); + canAdd_ = false; + } return boost::none; } @@ -297,10 +305,13 @@ DatabaseShardImp::prepareLedger(std::uint32_t validLedgerSeq) return boost::none; auto const seq {shard->prepare()}; - shards_.emplace( - *shardIndex, - ShardInfo(std::move(shard), ShardInfo::State::acquire)); - acquireIndex_ = *shardIndex; + { + std::lock_guard lock(mutex_); + shards_.emplace( + *shardIndex, + ShardInfo(std::move(shard), ShardInfo::State::acquire)); + acquireIndex_ = *shardIndex; + } return seq; }