Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(deps): CometBFT no longer imports cometbft-db #4601

Open
wants to merge 62 commits into
base: feature/remove-cometbftdb
Choose a base branch
from

Conversation

alesforz
Copy link
Contributor

@alesforz alesforz commented Dec 3, 2024

Partially addresses #4486.

Context

CometBFT will stop importing cometbft-db as a dependency to support multiple database backends. Instead, it will use pebble by default.

Changes

This PR removes cometbft-db as a dependency and changes all the code to use the new internal/storage pkg instead.

TODO


PR checklist

  • Tests written/updated
  • [ ] Changelog entry added in .changelog (we use unclog to manage our changelog)
  • Updated relevant documentation (docs/ or spec/) and code comments

- `NewMemDB` to create an in-memory instance of pebble (for testing).
- `PrefixIterator` to create an iterator over keys that begin with a given prefix.
- `incrementBigEndian` to increase the value of a byte slice (read as a big-endian
   unsigned integer) by one.
@alesforz alesforz self-assigned this Dec 3, 2024
@alesforz alesforz added dependencies Dependency updates storage P:tech-debt Priority: Technical debt that needs to be paid off to enable us to move faster, reliably labels Dec 3, 2024
The test was not closing the databases it was creating in succession, thus
causing a "lock held by current process" error when opening them a 2nd time.
The refactored code creats an on-disk instance of pebble, but the original code
created an in-memory database.
The benchmark now creates an in-memory instance of pebble.
github-merge-queue bot pushed a commit that referenced this pull request Dec 10, 2024
…ronment` (#4639)

### Context
Because `rpc.Environment` does not store a `GenesisDoc` in memory
anymore, (see #1290), we don't need to create it as a singleton. The
risk of storing multiple copies of the genesis in memory isn't there
anymore, because we now load it from disk.

### This Change
This PR removes the `sync.Once` construct that we put in place.

### Additional Note
The change also ensures that
[`TestProvider`](https://github.com/cometbft/cometbft/blob/b16c6fc2c8b2fc5a468fead32a8fe9057d6cce2f/light/provider/http/http_test.go#L36)
in the `light/provider/http` package behaves as expected. In `main`,
this test passes because it's set up using a
[`MemDB`](https://github.com/cometbft/cometbft-db/blob/4cf60c715fe8daccb9dce3b24295575bd461d5d8/memdb.go#L52),
which is a dummy in-memory store rather than a real database. Thus,
database `Get` operations always succeed.

However, in the context of the [work to remove
cometbft-db](#4601), we now use
a "real" database, i.e., one that a `Node` closes when it shuts down.
Since the `Environment` object was treated as a singleton, each
iteration of `TestProvider` created a new `Node` using the same
underlying database. This database would be closed at the end of the
first iteration when that iteration's `Node` shut down. Subsequent
iterations then attempted to call `Get` on a closed database, causing a
panic.

This change fixes that issue.

---

#### PR checklist

- [x] Tests written/updated
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- ~[ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments~
github-merge-queue bot pushed a commit that referenced this pull request Dec 11, 2024
…4642)

### Context
The
[`TxIndexer`](https://github.com/cometbft/cometbft/blob/2b1db1c16bf2db16b81b49fef3581e79679fbed6/state/txindex/indexer.go#L17)
interface defines how to index and search transactions. Its implementers
need to interact with a database, which callers are typically expected
to close when done. However, `TxIndexer` does not provide a `Close`
method. This prevents closing the database used by the transaction
indexer, causing goroutines associated with that database to leak.

Two
[tests](https://github.com/cometbft/cometbft/blob/2b1db1c16bf2db16b81b49fef3581e79679fbed6/internal/inspect/inspect_test.go#L29),
`TestInspectConstructor` and `TestInspectRun`, show this problem
indirectly. These tests check whether an
[`Inspector`](https://github.com/cometbft/cometbft/blob/2b1db1c16bf2db16b81b49fef3581e79679fbed6/internal/inspect/inspect.go#L32)
leaks goroutines. The `Inspector` sets up three databases: a block
database, a state database, and a transaction indexer database. It
closes only the block and state databases because it cannot close the
transaction indexer’s database (because of the missing `Close` method).
Therefore, the transaction indexer database's goroutines leak, and the
two tests above detect that.

In `main`, both tests pass because they use a
[`MemDB`](https://github.com/cometbft/cometbft-db/blob/4cf60c715fe8daccb9dce3b24295575bd461d5d8/memdb.go#L52),
an in-memory store that does not spawn goroutines. However, in the [work
to remove cometbft-db](#4601),
the tests switch to a “real” database that does spawn goroutines.
Without a proper `Close` method in `TxIndexer`, these goroutines remain
active, causing `TestInspectConstructor` and `TestInspectRun` to fail.

### This Change 
This PR adds a `Close` method to the `TxIndexer` interface and updates
the existing implementation to properly close the underlying database.
It also modifies the code to call `TxIndexer.Close` where needed,
ensuring that database resources are released and preventing goroutine
leaks.

---

#### PR checklist

- [x] Tests written/updated
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
The test unfolds too quickly for the node's indexer to finish its operations.
Therefore, the goroutine below that calls n.Stop() will close the indexer's
database right when the indexer is trying to write to it at line
state/txindex/indexer_service.go:109
thus causing a panic.  This small sleep allows the indexer to finish its
write operation before the node is shut down.
Unfortunately, there is no graceful shutdown available for the database layer,
i.e., make the database wait for all the active connections before closing.

The test does not fail in `main` because the code in `main` does not close
the indexer's database when shutting down a node (which causes its goroutines
to leak. See #4642).
@alesforz alesforz marked this pull request as ready for review December 11, 2024 17:35
@alesforz alesforz requested review from a team as code owners December 11, 2024 17:35
Copy link
Contributor

@melekes melekes left a comment

Choose a reason for hiding this comment

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

great work @alesforz 👍

abci/example/kvstore/kvstore.go Outdated Show resolved Hide resolved
light/example_test.go Outdated Show resolved Hide resolved
light/store/db/db.go Outdated Show resolved Hide resolved
### Context
In reviewing #4601, we discussed about exporting the `DB`, `Batch`, and
`Iterator` interfaces instead of leaving them in an `internal` package.
After some thinking we decided to export them in a new non-internal
package `cmtdb`. However, our implementation of those interfaces remain
unexported.

### Changes
This PR adds pkg `cmtdb` exporting the `DB`, `Batch`, and `Iterator`
interfaces. Additionally, it changes the code to use this new package
rather than the now obsolete `internal/storage` (which this PR removes).
Copy link
Contributor

@melekes melekes left a comment

Choose a reason for hiding this comment

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

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Dependency updates P:tech-debt Priority: Technical debt that needs to be paid off to enable us to move faster, reliably storage
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

2 participants