Skip to content

Commit fcbdaee

Browse files
committed
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.
1 parent 2ec89f1 commit fcbdaee

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

src/index/base.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,6 @@ static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain&
163163

164164
void BaseIndex::ThreadSync()
165165
{
166-
// Wait for a possible reindex-chainstate to finish until continuing
167-
// with the index sync
168-
while (!g_indexes_ready_to_sync) {
169-
if (!m_interrupt.sleep_for(std::chrono::milliseconds(500))) return;
170-
}
171-
172166
const CBlockIndex* pindex = m_best_block_index.load();
173167
if (!m_synced) {
174168
std::chrono::steady_clock::time_point last_log_time{0s};

src/init.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,9 +1570,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15701570
// Init indexes
15711571
for (auto index : node.indexes) if (!index->Init()) return false;
15721572

1573-
// Now that all indexes are loaded, start them
1574-
if (!StartIndexBackgroundSync(node)) return false;
1575-
15761573
// ********************************************************* Step 9: load wallet
15771574
for (const auto& client : node.chain_clients) {
15781575
if (!client->load()) {
@@ -1656,9 +1653,15 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16561653
vImportFiles.push_back(fs::PathFromString(strFile));
16571654
}
16581655

1659-
chainman.m_thread_load = std::thread(&util::TraceThread, "initload", [=, &chainman, &args] {
1656+
chainman.m_thread_load = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] {
16601657
// Import blocks
16611658
ImportBlocks(chainman, vImportFiles);
1659+
// Start indexes initial sync
1660+
if (!StartIndexBackgroundSync(node)) {
1661+
bilingual_str err_str = _("Failed to start indexes, shutting down..");
1662+
chainman.GetNotifications().fatalError(err_str.original, err_str);
1663+
return;
1664+
}
16621665
// Load mempool from disk
16631666
chainman.ActiveChainstate().LoadMempool(ShouldPersistMempool(args) ? MempoolPath(args) : fs::path{});
16641667
});

test/functional/feature_index_prune.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test indices in conjunction with prune."""
6+
import os
67
from test_framework.test_framework import BitcoinTestFramework
78
from test_framework.util import (
89
assert_equal,
@@ -127,8 +128,9 @@ def run_test(self):
127128
self.log.info("make sure we get an init error when starting the nodes again with the indices")
128129
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)"
129130
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)"
131+
end_msg = f"{os.linesep}Error: Failed to start indexes, shutting down.."
130132
for i, msg in enumerate([filter_msg, stats_msg, filter_msg]):
131-
self.nodes[i].assert_start_raises_init_error(extra_args=self.extra_args[i], expected_msg=msg)
133+
self.nodes[i].assert_start_raises_init_error(extra_args=self.extra_args[i], expected_msg=msg+end_msg)
132134

133135
self.log.info("make sure the nodes start again with the indices and an additional -reindex arg")
134136
for i in range(3):

0 commit comments

Comments
 (0)