Skip to content

Commit

Permalink
[FOLD] Narrow lock scope in prepareLedger function
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelportilla committed Feb 21, 2020
1 parent 6cb5c36 commit 96810ea
Showing 1 changed file with 39 additions and 28 deletions.
67 changes: 39 additions & 28 deletions src/ripple/nodestore/impl/DatabaseShardImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,39 +256,47 @@ DatabaseShardImp::init()
boost::optional<std::uint32_t>
DatabaseShardImp::prepareLedger(std::uint32_t validLedgerSeq)
{
std::lock_guard lock(mutex_);
assert(init_);
boost::optional<std::uint32_t> 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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 96810ea

Please sign in to comment.