Skip to content

Conversation

jimpo
Copy link
Contributor

@jimpo jimpo commented Jan 8, 2020

Replaces #17852.

Currently, the latest index state may not be committed to disk on shutdown. The state is committed on ChainStateFlushed callbacks and the current init order unregisters the indexes as validation interfaces before the final ChainStateFlushed callback is called on them.

Issue identified by paulyc.

For review: an alternative or supplemental solution would be to call Commit at the end of BaseIndex::Stop. I don't see any harm in doing so and it makes the less prone to user error. However, the destructor would have to be modified to not call Stop because Commit calls a virtual method, so I figured it wasn't worth it. But I'm curious how others feel.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jan 8, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #14053 (Add address-based index (attempt 4?) by marcinja)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 4a6cb2f.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually bad. The moved code is after

    g_txindex.reset();
    DestroyAllBlockFilterIndexes();

which means the moved code does nothing now.

Currently, the latest index state may not be committed to disk on shutdown.
@jimpo
Copy link
Contributor Author

jimpo commented Jan 10, 2020

Thanks @promag, fixed.

@promag
Copy link
Contributor

promag commented Jan 10, 2020

Looks good now, this time I'll test.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it me or the following

    if (g_txindex) {
        g_txindex->Interrupt();
    }
    ForEachBlockFilterIndex([](BlockFilterIndex& index) { index.Interrupt(); });

should be removed from Interrupt(NodeContext& node)?

@jimpo
Copy link
Contributor Author

jimpo commented Jan 10, 2020

@promag I don't think so. Why?

@promag
Copy link
Contributor

promag commented Jan 14, 2020

Actually I think it's fine, I was missing the !m_synced check.

I was seeing this order of events:

  • Interrupt() is called which asynchronously stops the loop in ThreadSync()
  • before UnregisterValidationInterface is called, validation notifications can be delivered, which can't be handled.

But now I see the m_synced flag to guard against this case.

Anyway, would be nice to include steps/changes to reproduce the original problem.

Code review ACK 9dd58ca, but failed to test because I can't reproduce the original problem.

@paulyc
Copy link

paulyc commented Jan 21, 2020

Actually I think it's fine, I was missing the !m_synced check.

I was seeing this order of events:

  • Interrupt() is called which asynchronously stops the loop in ThreadSync()
  • before UnregisterValidationInterface is called, validation notifications can be delivered, which can't be handled.

But now I see the m_synced flag to guard against this case.

Anyway, would be nice to include steps/changes to reproduce the original problem.

Code review ACK 9dd58ca, but failed to test because I can't reproduce the original problem.

So here is how I can currently reproduce it:

Create a new datadir, start syncing with txindex=1. Let it run a while until there are decent number of block files, say to block 190000. Kill it. So when you restart, you'd expect that the txindex would restart at the same point as the block indexer at 190000 since they were both at the same point before you killed the daemon, but instead, I see the txindex starting at 39339, even though the data is in the txindex leveldb, it still has to run through every block again until it catches up and sets a new checkpoint, which is what led me to believe it wasn't being closed correctly since that's when leveldb writes out the current metadata for the next load. Here's my logfile from doing that
bitcoind.txindex.redacted.log

@promag
Copy link
Contributor

promag commented Jan 21, 2020

Thanks @paulyc, I have done that but didn't wait for that block count.

@fjahr
Copy link
Contributor

fjahr commented Jan 22, 2020

tested ACK 9dd58ca

Tested as described by @paulyc. Also ran tests locally.

@kallewoof
Copy link
Contributor

Tested ACK 9dd58ca

master branch showing txindex sync begins at block 0 upon restart
$ ./bitcoind -txindex=1 -datadir=oij
[...]
2020-01-22T06:11:44Z Shutdown: In progress...
2020-01-22T06:11:44Z net thread exit
2020-01-22T06:11:44Z UpdateTip: new best=000000000000035010292db86f97a207d96a4106af108fcb016550dee0e4f96e height=168089 version=0x00000001 log2_work=67.727578 tx=2483951 date='2012-02-23T10:53:35Z' progress=0.004994 cache=202.5MiB(1524617txo)
[...]
2020-01-22T06:12:21Z Shutdown: done
$ ./bitcoind -txindex=1 -datadir=oij
[...]
2020-01-22T06:12:56Z Opening LevelDB in /Users/me/workspace/bitcoin/src/oij/indexes/txindex
2020-01-22T06:12:57Z Opened LevelDB successfully
2020-01-22T06:12:57Z Using obfuscation key for /Users/me/workspace/bitcoin/src/oij/indexes/txindex: 0000000000000000
2020-01-22T06:12:57Z txindex thread start
2020-01-22T06:12:57Z init message: Loading wallet...
2020-01-22T06:12:57Z Syncing txindex with block chain from height 0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[...]
#17897 PR showing txindex sync prevails upon restart
$ ./bitcoind -txindex=1 -datadir=oij-jimpo
[...]
2020-01-22T06:17:35Z Opening LevelDB in /Users/me/workspace/bitcoin/src/oij-jimpo/indexes/txindex
2020-01-22T06:17:35Z Opened LevelDB successfully
2020-01-22T06:17:35Z Using obfuscation key for /Users/me/workspace/bitcoin/src/oij-jimpo/indexes/txindex: 0000000000000000
2020-01-22T06:17:35Z txindex thread start
2020-01-22T06:17:35Z txindex is enabled
2020-01-22T06:17:35Z init message: Loading wallet...
2020-01-22T06:17:35Z txindex thread exit
[...]
2020-01-22T06:39:03Z UpdateTip: new best=000000000000000a92ff09eb56af38ad02b64ee75d4b71274caa437efc1e8b6c height=172386 version=0x00000001 log2_work=67.87869 tx=2670294 date='2012-03-22T19:16:30Z' progress=0.005368 cache=213.9MiB(1617994txo)
[...]
2020-01-22T06:39:40Z Shutdown: done
$ ./bitcoind -txindex=1 -datadir=oij-jimpo
[...]
2020-01-22T06:46:12Z Loaded best chain: hashBestChain=000000000000000a92ff09eb56af38ad02b64ee75d4b71274caa437efc1e8b6c height=172386 date=2012-03-22T19:16:30Z progress=0.005368
[...]
2020-01-22T06:46:15Z Opening LevelDB in /Users/me/workspace/bitcoin/src/oij-jimpo/indexes/txindex
2020-01-22T06:46:16Z Opened LevelDB successfully
2020-01-22T06:46:16Z Using obfuscation key for /Users/me/workspace/bitcoin/src/oij-jimpo/indexes/txindex: 0000000000000000
2020-01-22T06:46:16Z txindex thread start
2020-01-22T06:46:16Z txindex is enabled at height 172386
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020-01-22T06:46:16Z txindex thread exit
[...]

@fanquake
Copy link
Member

Looks good. I've tested master (742f84d) vs this PR (9dd58ca) and am seeing the same fixed behaviour. Thanks @promag, @fjahr, @kallewoof and @paulyc for testing.

master

2020-01-22T08:56:22Z UpdateTip: new best=0000000000002d533ff0e59ee25496b4f403d8ce7b4b7fcd9181dadad8f625b3 height=118827 version=0x00000001 log2_work=61.774365 tx=416409 date='2011-04-17T13:39:54Z' progress=0.000837 cache=33.9MiB(212154txo)
^C2020-01-22T08:56:24Z tor: Thread interrupt
2020-01-22T08:56:24Z Shutdown: In progress...
<snip>
2020-01-22T08:56:25Z Shutdown: done
....
bitcoin $ ./src/bitcoind -txindex -datadir=/Users/michael/Desktop/master
2020-01-22T08:56:32Z Bitcoin Core version v0.19.99.0-742f84d0d (release build)
<snip>
2020-01-22T08:56:35Z Loaded best chain: hashBestChain=0000000000002d533ff0e59ee25496b4f403d8ce7b4b7fcd9181dadad8f625b3 height=118827 date=2011-04-17T13:39:54Z progress=0.000837
<snip>
2020-01-22T08:56:36Z Using obfuscation key for /Users/michael/Desktop/master/indexes/txindex: 0000000000000000
2020-01-22T08:56:36Z txindex thread start
2020-01-22T08:56:36Z Syncing txindex with block chain from height 65

This PR

2020-01-22T09:19:53Z Shutdown: In progress...
2020-01-22T09:19:53Z UpdateTip: new best=00000000000092c47111d9ca453b6df25b6e139faf438ab06070638417195fba height=116260 version=0x00000001 log2_work=61.415013 tx=385266 date='2011-04-02T06:16:42Z' progress=0.000774 cache=30.4MiB(196729txo)
<snip>
2020-01-22T09:20:10Z Shutdown: done
....
bitcoin $ ./src/bitcoind -txindex -datadir=/Users/michael/Desktop/17897
2020-01-22T09:20:54Z Bitcoin Core version v0.19.99.0-9dd58ca61 (release build)
<snip>
2020-01-22T09:21:02Z Loaded best chain: hashBestChain=00000000000092c47111d9ca453b6df25b6e139faf438ab06070638417195fba height=116260 date=2011-04-02T06:16:42Z progress=0.000774
<snip>
2020-01-22T09:21:07Z Using obfuscation key for /Users/michael/Desktop/17897/indexes/txindex: 0000000000000000
2020-01-22T09:21:07Z txindex thread start
2020-01-22T09:21:07Z init message: Loading wallet...
2020-01-22T09:21:07Z txindex is enabled at height 116260

fanquake added a commit that referenced this pull request Jan 22, 2020
…callback.

9dd58ca init: Stop indexes on shutdown after ChainStateFlushed callback. (Jim Posen)

Pull request description:

  Replaces #17852.

  Currently, the latest index state may not be committed to disk on shutdown. The state is committed on `ChainStateFlushed` callbacks and the current init order unregisters the indexes as validation interfaces before the final `ChainStateFlushed` callback is called on them.

  Issue identified by paulyc.

  For review: an alternative or supplemental solution would be to call `Commit` at the end of `BaseIndex::Stop`. I don't see any harm in doing so and it makes the less prone to user error. However, the destructor would have to be modified to not call `Stop` because `Commit` calls a virtual method, so I figured it wasn't worth it. But I'm curious how others feel.

ACKs for top commit:
  fjahr:
    tested ACK 9dd58ca
  paulyc:
    > Code review ACK [9dd58ca](9dd58ca), but failed to test because I can't reproduce the original problem.
  kallewoof:
    Tested ACK 9dd58ca
  promag:
    Code review ACK 9dd58ca, but failed to test because I can't reproduce the original problem.

Tree-SHA512: 2918380b699833cb7eab07456d1667dbf8ebbe2d2b5988300a3cf5b6a6cfc818b6d9086e1936ffe7881f67e409306c4b91d61a08a169cfd0a301383479d4f3cb
@fanquake fanquake merged commit 9dd58ca into bitcoin:master Jan 22, 2020
fanquake pushed a commit that referenced this pull request Jan 22, 2020
Currently, the latest index state may not be committed to disk on shutdown.

Github-Pull: #17897
Rebased-From: 9dd58ca
@fanquake
Copy link
Member

This has been backported to the 0.19 branch in 9d980c1.

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jan 24, 2020
…lushed callback.

9dd58ca init: Stop indexes on shutdown after ChainStateFlushed callback. (Jim Posen)

Pull request description:

  Replaces bitcoin#17852.

  Currently, the latest index state may not be committed to disk on shutdown. The state is committed on `ChainStateFlushed` callbacks and the current init order unregisters the indexes as validation interfaces before the final `ChainStateFlushed` callback is called on them.

  Issue identified by paulyc.

  For review: an alternative or supplemental solution would be to call `Commit` at the end of `BaseIndex::Stop`. I don't see any harm in doing so and it makes the less prone to user error. However, the destructor would have to be modified to not call `Stop` because `Commit` calls a virtual method, so I figured it wasn't worth it. But I'm curious how others feel.

ACKs for top commit:
  fjahr:
    tested ACK 9dd58ca
  paulyc:
    > Code review ACK [9dd58ca](bitcoin@9dd58ca), but failed to test because I can't reproduce the original problem.
  kallewoof:
    Tested ACK 9dd58ca
  promag:
    Code review ACK 9dd58ca, but failed to test because I can't reproduce the original problem.

Tree-SHA512: 2918380b699833cb7eab07456d1667dbf8ebbe2d2b5988300a3cf5b6a6cfc818b6d9086e1936ffe7881f67e409306c4b91d61a08a169cfd0a301383479d4f3cb
MarkLTZ added a commit to litecoinz-core/litecoinz that referenced this pull request Mar 13, 2020
[0.19] Backports bitcoin#17858
Unbreak build with Boost 1.72.0 bitcoin#17654
cli: fix Fatal LevelDB error when specifying -blockfilterindex=basic twice bitcoin#17687
rpc: require second argument only for scantxoutset start action bitcoin#17728
wallet: Fix origfee return for bumpfee with feerate arg bitcoin#17643
test: fix "bitcoind already running" warnings on macOS bitcoin#17488
net: Log to net category for exceptions in ProcessMessages bitcoin#17762
Updates to appveyor config for VS2019 and Qt5.9.8 + msvc project fixes bitcoin#17364
Appveyor improvement - text file for vcpkg package list bitcoin#17416
Update msvc build for Visual Studio 2019 v16.4 bitcoin#17736
scripts: fix symbol-check & security-check argument passing bitcoin#17857
qt: Periodic translations update for 0.19 branch
IsUsedDestination should count any known single-key address bitcoin#17621
init: Stop indexes on shutdown after ChainStateFlushed callback. bitcoin#17897
qt: Translations update pre-rc1
wallet: Reset reused transactions cache bitcoin#17843
Squashed 'src/univalue/' changes from 7890db9..98261b1
0.19: Update univalue subtree bitcoin#18100
qt: Pre-rc2 translations update
MarkLTZ added a commit to litecoinz-core/litecoinz that referenced this pull request Mar 13, 2020
[0.19] Backports bitcoin#17858
Unbreak build with Boost 1.72.0 bitcoin#17654
cli: fix Fatal LevelDB error when specifying -blockfilterindex=basic twice bitcoin#17687
rpc: require second argument only for scantxoutset start action bitcoin#17728
wallet: Fix origfee return for bumpfee with feerate arg bitcoin#17643
test: fix "bitcoind already running" warnings on macOS bitcoin#17488
net: Log to net category for exceptions in ProcessMessages bitcoin#17762
Updates to appveyor config for VS2019 and Qt5.9.8 + msvc project fixes bitcoin#17364
Appveyor improvement - text file for vcpkg package list bitcoin#17416
Update msvc build for Visual Studio 2019 v16.4 bitcoin#17736
scripts: fix symbol-check & security-check argument passing bitcoin#17857
qt: Periodic translations update for 0.19 branch
IsUsedDestination should count any known single-key address bitcoin#17621
init: Stop indexes on shutdown after ChainStateFlushed callback. bitcoin#17897
qt: Translations update pre-rc1
wallet: Reset reused transactions cache bitcoin#17843
Squashed 'src/univalue/' changes from 7890db9..98261b1
0.19: Update univalue subtree bitcoin#18100
qt: Pre-rc2 translations update
[0.19] Further 0.19 backports bitcoin#18218
build: don't embed a build-id when building libdmg-hfsplus bitcoin#18004
sidhujag pushed a commit to syscoin-core/syscoin that referenced this pull request Nov 10, 2020
…lushed callback.

9dd58ca init: Stop indexes on shutdown after ChainStateFlushed callback. (Jim Posen)

Pull request description:

  Replaces bitcoin#17852.

  Currently, the latest index state may not be committed to disk on shutdown. The state is committed on `ChainStateFlushed` callbacks and the current init order unregisters the indexes as validation interfaces before the final `ChainStateFlushed` callback is called on them.

  Issue identified by paulyc.

  For review: an alternative or supplemental solution would be to call `Commit` at the end of `BaseIndex::Stop`. I don't see any harm in doing so and it makes the less prone to user error. However, the destructor would have to be modified to not call `Stop` because `Commit` calls a virtual method, so I figured it wasn't worth it. But I'm curious how others feel.

ACKs for top commit:
  fjahr:
    tested ACK 9dd58ca
  paulyc:
    > Code review ACK [9dd58ca](bitcoin@9dd58ca), but failed to test because I can't reproduce the original problem.
  kallewoof:
    Tested ACK 9dd58ca
  promag:
    Code review ACK 9dd58ca, but failed to test because I can't reproduce the original problem.

Tree-SHA512: 2918380b699833cb7eab07456d1667dbf8ebbe2d2b5988300a3cf5b6a6cfc818b6d9086e1936ffe7881f67e409306c4b91d61a08a169cfd0a301383479d4f3cb
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Dec 14, 2020
Summary:
> Currently, the latest index state may not be committed to disk on shutdown.
> The state is committed on ChainStateFlushed callbacks and the current init order unregisters the indexes as validation interfaces before the final ChainStateFlushed callback is called on them.
> This was causing the txindexing progress to have fallen behind to its last random checkpoint the next time bitcoind was started.

This is a backport of Core [[bitcoin/bitcoin#17897 | PR17897]]

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D8663
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants