From fcbdaeef4d5a63e3e5b479c6fcad730eb86fb923 Mon Sep 17 00:00:00 2001 From: furszy Date: Wed, 17 May 2023 16:06:55 -0300 Subject: [PATCH] init: don't start indexes sync thread prematurely By moving the 'StartIndexes()' call into the 'initload' thread, we can remove the threads active wait. Optimizing the available resources. The only difference with the current state is that now the indexes threads will only be started when they can process work and not before it. --- src/index/base.cpp | 6 ------ src/init.cpp | 11 +++++++---- test/functional/feature_index_prune.py | 4 +++- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/index/base.cpp b/src/index/base.cpp index 8accc2a6a44..1babfacb157 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -163,12 +163,6 @@ static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain& void BaseIndex::ThreadSync() { - // Wait for a possible reindex-chainstate to finish until continuing - // with the index sync - while (!g_indexes_ready_to_sync) { - if (!m_interrupt.sleep_for(std::chrono::milliseconds(500))) return; - } - const CBlockIndex* pindex = m_best_block_index.load(); if (!m_synced) { std::chrono::steady_clock::time_point last_log_time{0s}; diff --git a/src/init.cpp b/src/init.cpp index 102e7932cd0..389654fe4f8 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1570,9 +1570,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) // Init indexes for (auto index : node.indexes) if (!index->Init()) return false; - // Now that all indexes are loaded, start them - if (!StartIndexBackgroundSync(node)) return false; - // ********************************************************* Step 9: load wallet for (const auto& client : node.chain_clients) { if (!client->load()) { @@ -1656,9 +1653,15 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) vImportFiles.push_back(fs::PathFromString(strFile)); } - chainman.m_thread_load = std::thread(&util::TraceThread, "initload", [=, &chainman, &args] { + chainman.m_thread_load = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] { // Import blocks ImportBlocks(chainman, vImportFiles); + // Start indexes initial sync + if (!StartIndexBackgroundSync(node)) { + bilingual_str err_str = _("Failed to start indexes, shutting down.."); + chainman.GetNotifications().fatalError(err_str.original, err_str); + return; + } // Load mempool from disk chainman.ActiveChainstate().LoadMempool(ShouldPersistMempool(args) ? MempoolPath(args) : fs::path{}); }); diff --git a/test/functional/feature_index_prune.py b/test/functional/feature_index_prune.py index 77a056346a6..d6e802b399e 100755 --- a/test/functional/feature_index_prune.py +++ b/test/functional/feature_index_prune.py @@ -3,6 +3,7 @@ # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test indices in conjunction with prune.""" +import os from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, @@ -127,8 +128,9 @@ class FeatureIndexPruneTest(BitcoinTestFramework): self.log.info("make sure we get an init error when starting the nodes again with the indices") filter_msg = "Error: basic block filter index best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)" stats_msg = "Error: coinstatsindex best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)" + end_msg = f"{os.linesep}Error: Failed to start indexes, shutting down.." for i, msg in enumerate([filter_msg, stats_msg, filter_msg]): - self.nodes[i].assert_start_raises_init_error(extra_args=self.extra_args[i], expected_msg=msg) + self.nodes[i].assert_start_raises_init_error(extra_args=self.extra_args[i], expected_msg=msg+end_msg) self.log.info("make sure the nodes start again with the indices and an additional -reindex arg") for i in range(3):