mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Fix rand_int assert in shard store
This commit is contained in:
committed by
Nik Bougalis
parent
4b1970afa9
commit
56eac5c9a1
@@ -1019,29 +1019,46 @@ boost::optional<std::uint32_t>
|
|||||||
DatabaseShardImp::findShardIndexToAdd(
|
DatabaseShardImp::findShardIndexToAdd(
|
||||||
std::uint32_t validLedgerSeq, std::lock_guard<std::mutex>&)
|
std::uint32_t validLedgerSeq, std::lock_guard<std::mutex>&)
|
||||||
{
|
{
|
||||||
auto maxShardIndex {seqToShardIndex(validLedgerSeq)};
|
auto const maxShardIndex {[this, validLedgerSeq]()
|
||||||
if (validLedgerSeq != lastLedgerSeq(maxShardIndex))
|
{
|
||||||
--maxShardIndex;
|
auto shardIndex {seqToShardIndex(validLedgerSeq)};
|
||||||
|
if (validLedgerSeq != lastLedgerSeq(shardIndex))
|
||||||
|
--shardIndex;
|
||||||
|
return shardIndex;
|
||||||
|
}()};
|
||||||
|
auto const numShards {complete_.size() +
|
||||||
|
(incomplete_ ? 1 : 0) + preShards_.size()};
|
||||||
|
|
||||||
auto const numShards {complete_.size() + (incomplete_ ? 1 : 0)};
|
// Check if the shard store has all shards
|
||||||
// If equal, have all the shards
|
if (numShards >= maxShardIndex)
|
||||||
if (numShards >= maxShardIndex + 1)
|
|
||||||
return boost::none;
|
return boost::none;
|
||||||
|
|
||||||
if (maxShardIndex < 1024 || float(numShards) / maxShardIndex > 0.5f)
|
if (maxShardIndex < 1024 ||
|
||||||
|
static_cast<float>(numShards) / maxShardIndex > 0.5f)
|
||||||
{
|
{
|
||||||
// Small or mostly full index space to sample
|
// Small or mostly full index space to sample
|
||||||
// Find the available indexes and select one at random
|
// Find the available indexes and select one at random
|
||||||
std::vector<std::uint32_t> available;
|
std::vector<std::uint32_t> available;
|
||||||
available.reserve(maxShardIndex - numShards + 1);
|
available.reserve(maxShardIndex - numShards + 1);
|
||||||
for (std::uint32_t i = earliestShardIndex(); i <= maxShardIndex; ++i)
|
|
||||||
|
for (auto shardIndex = earliestShardIndex();
|
||||||
|
shardIndex <= maxShardIndex;
|
||||||
|
++shardIndex)
|
||||||
{
|
{
|
||||||
if (complete_.find(i) == complete_.end() &&
|
if (complete_.find(shardIndex) == complete_.end() &&
|
||||||
(!incomplete_ || incomplete_->index() != i) &&
|
(!incomplete_ || incomplete_->index() != shardIndex) &&
|
||||||
preShards_.find(i) == preShards_.end())
|
preShards_.find(shardIndex) == preShards_.end())
|
||||||
available.push_back(i);
|
{
|
||||||
|
available.push_back(shardIndex);
|
||||||
}
|
}
|
||||||
if (!available.empty())
|
}
|
||||||
|
|
||||||
|
if (available.empty())
|
||||||
|
return boost::none;
|
||||||
|
|
||||||
|
if (available.size() == 1)
|
||||||
|
return available.front();
|
||||||
|
|
||||||
return available[rand_int(0u,
|
return available[rand_int(0u,
|
||||||
static_cast<std::uint32_t>(available.size() - 1))];
|
static_cast<std::uint32_t>(available.size() - 1))];
|
||||||
}
|
}
|
||||||
@@ -1051,13 +1068,16 @@ DatabaseShardImp::findShardIndexToAdd(
|
|||||||
// chances of running more than 30 times is less than 1 in a billion
|
// chances of running more than 30 times is less than 1 in a billion
|
||||||
for (int i = 0; i < 40; ++i)
|
for (int i = 0; i < 40; ++i)
|
||||||
{
|
{
|
||||||
auto const r {rand_int(earliestShardIndex(), maxShardIndex)};
|
auto const shardIndex {rand_int(earliestShardIndex(), maxShardIndex)};
|
||||||
if (complete_.find(r) == complete_.end() &&
|
if (complete_.find(shardIndex) == complete_.end() &&
|
||||||
(!incomplete_ || incomplete_->index() != r) &&
|
(!incomplete_ || incomplete_->index() != shardIndex) &&
|
||||||
preShards_.find(r) == preShards_.end())
|
preShards_.find(shardIndex) == preShards_.end())
|
||||||
return r;
|
{
|
||||||
|
return shardIndex;
|
||||||
}
|
}
|
||||||
assert(0);
|
}
|
||||||
|
|
||||||
|
assert(false);
|
||||||
return boost::none;
|
return boost::none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user