Skip to content

Commit

Permalink
fix tbb wait and recursive lock each other bug. network thread pool a…
Browse files Browse the repository at this point in the history
…ll locked in calculate merkle tree
  • Loading branch information
JimmyShi22 committed Jan 17, 2024
1 parent 70ae0db commit 77fd245
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions bcos-ledger/src/libledger/Ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1467,26 +1467,39 @@ static std::vector<h256> getMerkleTreeFromCache(int64_t blockNumber, Ledger::Cac
RecursiveMutex& mutex, const std::string& cacheName, const MerkleType& merkle,
const HashRangeType& hashesRange)
{
std::shared_ptr<std::vector<h256>> merkleTree;
std::shared_ptr<std::vector<h256>> merkleTree = std::make_shared<std::vector<h256>>();
{
RecursiveGuard l(mutex);
if (!cache.contains(blockNumber))
auto merkleTreePtr = cache.get(blockNumber);
if (!merkleTreePtr.has_value())
{
merkleTree = std::make_shared<std::vector<h256>>();
merkle.template generateMerkle(hashesRange, *merkleTree);
cache.insert(blockNumber, merkleTree);
LEDGER_LOG(DEBUG)
<< LOG_BADGE(cacheName)
<< LOG_DESC("Failed to hit the cache and build a new Merkel tree from scratch")
<< LOG_KV("blockNumber", blockNumber);
}
else
else if (!merkleTreePtr.get()->empty())
{
merkleTree = *(cache.get(blockNumber));
merkleTree = merkleTreePtr.get();
LEDGER_LOG(DEBUG) << LOG_BADGE(cacheName) << LOG_DESC("Hit cache")
<< LOG_KV("blockNumber", blockNumber);
}
}

if (merkleTree->empty())
{
auto newMerkleTree = std::make_shared<std::vector<h256>>();
// Notice: generateMerkle will use tbb thread. Should not place in RecursiveGuard below to
// avoid locking each other in tbb thread pool and RecursiveGuard
merkle.template generateMerkle(hashesRange, *newMerkleTree);
{
RecursiveGuard l(mutex);
merkleTree.reset(newMerkleTree.get());
}

LEDGER_LOG(DEBUG) << LOG_BADGE(cacheName)
<< LOG_DESC(
"Failed to hit the cache and build a new Merkel tree from scratch")
<< LOG_KV("blockNumber", blockNumber);
}

return *merkleTree;
}

Expand Down

0 comments on commit 77fd245

Please sign in to comment.