From 153177cf4adee620a1ac3192f27616062d5bfd08 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Mon, 27 Nov 2023 21:57:55 +0300 Subject: [PATCH] fix: move CheckQuorumConnections to a separate thread --- src/llmq/quorums.cpp | 27 ++++++++++++++++++++++++--- src/llmq/quorums.h | 1 + 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/llmq/quorums.cpp b/src/llmq/quorums.cpp index 67b464770b3be0..899de1dbdb55a1 100644 --- a/src/llmq/quorums.cpp +++ b/src/llmq/quorums.cpp @@ -278,9 +278,7 @@ void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, bool fInitial return; } - for (const auto& params : Params().GetConsensus().llmqs) { - CheckQuorumConnections(params, pindexNew); - } + StartCheckQuorumConnectionsThreads(pindexNew); { // Cleanup expired data requests @@ -299,8 +297,26 @@ void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, bool fInitial CleanupOldQuorumData(pindexNew); } +void CQuorumManager::StartCheckQuorumConnectionsThreads(const CBlockIndex* pindexNew) const +{ + for (const auto& params : Params().GetConsensus().llmqs) { + // do not block the caller thread + workerPool.push([params, pindexNew, this](int threadId) { + CheckQuorumConnections(params, pindexNew); + }); + } +} + void CQuorumManager::CheckQuorumConnections(const Consensus::LLMQParams& llmqParams, const CBlockIndex* pindexNew) const { + cxxtimer::Timer t(/*start=*/ true); + LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] h[%d] start\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight); + + if (quorumThreadInterrupt) { + LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] h[%d] aborted. time=%d\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, t.count()); + return; + } + auto lastQuorums = ScanQuorums(llmqParams.type, pindexNew, (size_t)llmqParams.keepOldConnections); auto connmanQuorumsToDelete = connman.GetMasternodeQuorums(llmqParams.type); @@ -335,6 +351,10 @@ void CQuorumManager::CheckQuorumConnections(const Consensus::LLMQParams& llmqPar }); for (const auto& quorum : lastQuorums) { + if (quorumThreadInterrupt) { + LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] h[%d] aborted. time=%d\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, t.count()); + return; + } if (utils::EnsureQuorumConnections(llmqParams, quorum->m_quorum_base_block_index, connman, myProTxHash)) { if (connmanQuorumsToDelete.erase(quorum->qc->quorumHash) > 0) { LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] h[%d] keeping mn quorum connections for quorum: [%d:%s]\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, quorum->m_quorum_base_block_index->nHeight, quorum->m_quorum_base_block_index->GetBlockHash().ToString()); @@ -361,6 +381,7 @@ void CQuorumManager::CheckQuorumConnections(const Consensus::LLMQParams& llmqPar LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- removing masternodes quorum connections for quorum %s:\n", __func__, quorumHash.ToString()); connman.RemoveMasternodeQuorumNodes(llmqParams.type, quorumHash); } + LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] h[%d] done. time=%d\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, t.count()); } CQuorumPtr CQuorumManager::BuildQuorumFromCommitment(const Consensus::LLMQType llmqType, gsl::not_null pQuorumBaseBlockIndex) const diff --git a/src/llmq/quorums.h b/src/llmq/quorums.h index bc2eba6ae42509..48d5276def9144 100644 --- a/src/llmq/quorums.h +++ b/src/llmq/quorums.h @@ -263,6 +263,7 @@ class CQuorumManager private: // all private methods here are cs_main-free + void StartCheckQuorumConnectionsThreads(const CBlockIndex *pindexNew) const; void CheckQuorumConnections(const Consensus::LLMQParams& llmqParams, const CBlockIndex *pindexNew) const; CQuorumPtr BuildQuorumFromCommitment(Consensus::LLMQType llmqType, gsl::not_null pQuorumBaseBlockIndex) const;