Skip to content

Conversation

@achow101
Copy link
Member

@achow101 achow101 commented Jun 20, 2020

BerkeleyBatch and BerkeleyDatabase are kind of messy. The goal of this is to clean up them up so that they are logically separated.

BerkeleyBatch currently handles the creation of the BerkeleyDatabase's Db handle. This is instead moved into BerkeleyDatabase and is called by BerkeleyBatch.

Instead of having BerkeleyEnvironment track each database's usage, have BerkeleyDatabase track this usage itself with the m_refcount variable that is present in WalletDatabase.

Lastly, instead of having each BerkeleyEnvironment store the fileids of the databases open in it, have a global g_fileids to track those fileids. We were already checking fileid uniqueness globally (by checking the fileids in every environment when opening a database) so it's cleaner to do this with a global variable.

All of these changes allow us to make BerkeleyBatch and BerkeleyDatabase no longer be friend classes.

The diff of this PR is currently the same as in ##18971

Requires #19334

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 20, 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:

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.

@achow101 achow101 force-pushed the bdb-cleanup-refactors branch from a2a30a5 to d69f101 Compare June 22, 2020 20:12
@achow101 achow101 force-pushed the bdb-cleanup-refactors branch 2 times, most recently from 1b155cb to 8dc24d0 Compare July 1, 2020 16:05
@achow101 achow101 force-pushed the bdb-cleanup-refactors branch from 8dc24d0 to 288bae7 Compare July 6, 2020 18:26
@achow101 achow101 force-pushed the bdb-cleanup-refactors branch from 288bae7 to 06120bf Compare July 9, 2020 16:12
@achow101 achow101 force-pushed the bdb-cleanup-refactors branch from 06120bf to 8fa346c Compare July 11, 2020 15:10
Copy link
Contributor

@ryanofsky ryanofsky 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 d416ae5. Only changes since last review were rebasing after base PR #19334 merge, and adding cs_db lock in BerkeleyDatabase destructor, which should avoid races accessing env->m_databases and env->m_fileids

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

Conditional code review ACK e73d0a1f9e28f303024d6ddec2b4e7e980783c9c if some problems below are fixed. Just reviewed last 4 commits here

  • 61da2fa6a159b0d318978cb8713f83705bb97035 walletdb: track database file use as m_refcount within BerkeleyDatabase (5/8)
  • 33554edc24a51685cfc3ca0053372c253d1fb4b9 walletdb: Move Db->open to BerkeleyDatabase::Open (6/8)
  • 8a98a83232fab6ff653c9475bb6e01249eadfa5c walletdb: Use a global g_fileids instead of m_fileids for each env (7/8)
  • e73d0a1f9e28f303024d6ddec2b4e7e980783c9c walletdb: Remove BerkeleyBatch friend class from BerkeleyDatabase (8/8)

@achow101 achow101 force-pushed the bdb-cleanup-refactors branch from e73d0a1 to ccfd611 Compare July 16, 2020 18:30
Copy link
Contributor

@ryanofsky ryanofsky 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 ccfd611ed1bb45874a2558390de5597dd32d2772 last 5 commits

  • 747ca18d5b5f6dd3780de0f543b8a06d52c74169 Combine BerkeleyEnvironment::Verify into BerkeleyDatabase::Verify (5/9)
  • 516084a2738364f3922c08d8703fd737541bb2da walletdb: track database file use as m_refcount within BerkeleyDatabase (6/9)
  • 93b5f0442bada706aba561dfb5bf3da78101522b walletdb: Move Db->open to BerkeleyDatabase::Open (7/9)
  • 0a01ba79a4f7076593b6f65cb3e227455a487219 No need to check for duplicate fileids in all dbenvs (8/9)
  • ccfd611ed1bb45874a2558390de5597dd32d2772 walletdb: Remove BerkeleyBatch friend class from BerkeleyDatabase (9/9)

Only changes since previous review are followups from #19335 (review). This is a nice cleanup and everything here is an improvement. I do think Batch/Database refcounting interaction is too complicated though and could be made simpler later (#19335 (comment)).

achow101 added 5 commits July 22, 2020 23:30
Instead of having BerkeleyEnvironment track the file use count, make
BerkeleyDatabase do it itself.
Instead of opening the Db handle in BerkeleyBatch, make BerkeleyDatabase
do that.
Since we have .walletlock in each directory, we don't need the duplicate
fileid checks across all dbenvs as it shouldn't be possible anyways.
@achow101 achow101 force-pushed the bdb-cleanup-refactors branch from df8ca1a to 74507ce Compare July 23, 2020 03:36
Copy link
Contributor

@ryanofsky ryanofsky 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 74507ce. No changes since last review other than rebase


if (fs::exists(file_path))
{
LOCK(cs_db);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

It was there to guard mapFileUseCount which we've now removed.

@laanwj
Copy link
Member

laanwj commented Jul 29, 2020

Code review ACK 74507ce

@laanwj laanwj merged commit 8db2334 into bitcoin:master Jul 29, 2020
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 31, 2020
…d BerkeleyBatch

74507ce walletdb: Remove BerkeleyBatch friend class from BerkeleyDatabase (Andrew Chow)
00f0041 No need to check for duplicate fileids in all dbenvs (Andrew Chow)
d86efab walletdb: Move Db->open to BerkeleyDatabase::Open (Andrew Chow)
4fe4b3b walletdb: track database file use as m_refcount within BerkeleyDatabase (Andrew Chow)
65fb880 Combine BerkeleyEnvironment::Verify into BerkeleyDatabase::Verify (Andrew Chow)

Pull request description:

  `BerkeleyBatch` and `BerkeleyDatabase` are kind of messy. The goal of this is to clean up them up so that they are logically separated.

  `BerkeleyBatch` currently handles the creation of the `BerkeleyDatabase`'s `Db` handle. This is instead moved into `BerkeleyDatabase` and is called by `BerkeleyBatch`.

  Instead of having `BerkeleyEnvironment` track each database's usage, have `BerkeleyDatabase` track this usage itself with the `m_refcount` variable that is present in `WalletDatabase`.

  Lastly, instead of having each `BerkeleyEnvironment` store the fileids of the databases open in it, have a global `g_fileids` to track those fileids. We were already checking fileid uniqueness globally (by checking the fileids in every environment when opening a database) so it's cleaner to do this with a global variable.

  All of these changes allow us to make `BerkeleyBatch` and `BerkeleyDatabase` no longer be friend classes.

  The diff of this PR is currently the same as in #bitcoin#18971

  Requires bitcoin#19334

ACKs for top commit:
  laanwj:
    Code review ACK 74507ce
  ryanofsky:
    Code review ACK 74507ce. No changes since last review other than rebase

Tree-SHA512: 845d84ee1a470e2bf5d2e2e3d7738183d8ce43ddd06a0bbd57edecf5779b2f55d70728b1b57f5daab0f078650a8d60c3e19dc30b75b36e7aa952ce268399d5f6
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 6, 2021
Summary:
This is a backport of [[bitcoin/bitcoin#19335 | core#19335]] [1/5]
bitcoin/bitcoin@65fb880

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D10034
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 6, 2021
Summary:
Instead of having BerkeleyEnvironment track the file use count, make
BerkeleyDatabase do it itself.

This is a backport of [[bitcoin/bitcoin#19335 | core#19335]] [2/5]
bitcoin/bitcoin@4fe4b3b

Depends on D10034

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D10035
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 6, 2021
Summary:
Instead of opening the Db handle in BerkeleyBatch, make BerkeleyDatabase
do that.

This is a backport of [[bitcoin/bitcoin#19335 | core#19335]] [3/5]
bitcoin/bitcoin@d86efab

Depends on D10035

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10036
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 6, 2021
Summary:
Since we have .walletlock in each directory, we don't need the duplicate
fileid checks across all dbenvs as it shouldn't be possible anyways.

This is a backport of [[bitcoin/bitcoin#19335 | core#19335]] [4/5]
bitcoin/bitcoin@00f0041

Depends on D10036

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D10037
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 6, 2021
Summary:
This is a backport of [[bitcoin/bitcoin#19335 | core#19335]] [5/5]
bitcoin/bitcoin@74507ce

Depends on D10037

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D10038
@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.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants