Skip to content

Cleanup and refactor accountsdb index #419

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

Merged
merged 5 commits into from
Jul 12, 2025
Merged

Conversation

bmuddha
Copy link
Contributor

@bmuddha bmuddha commented Jun 26, 2025

Main changes:

  1. Use a single environment for all the accountsdb indexes
  2. Introduce table abstraction to make the code more clean
  3. Disable read ahead in the lmdb env configuration, which was unnecessary due to the mostly random access nature of operations.

Due to the use of a single environment instead of previously used 3, this is a breaking change, which requires running a migration script to fix up the index directory.

Greptile Summary

Major refactoring of the AccountsDB index system to improve performance and code organization by consolidating database environments and optimizing LMDB operations.

  • Consolidated three separate LMDB environments into a single environment for all indices in magicblock-accounts-db/src/index.rs
  • Added new Table abstraction in magicblock-accounts-db/src/index/table.rs to encapsulate database operations cleanly
  • Disabled LMDB read-ahead for better performance with random access patterns
  • Changed program account retrieval to return empty vector instead of NotFound error when no accounts exist
  • Breaking Change: Requires migration script to fix index directory structure due to environment consolidation

@bmuddha bmuddha self-assigned this Jun 26, 2025
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

LGTM

9 files reviewed, no comments
Edit PR Review Bot Settings | Greptile

Copy link
Collaborator

@thlorenz thlorenz left a comment

Choose a reason for hiding this comment

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

LGTM. Please link the migration script in the PR.

Copy link
Contributor

@GabrielePicco GabrielePicco left a comment

Choose a reason for hiding this comment

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

LGTM! Can the migration script be run automatically on restart?

@bmuddha bmuddha mentioned this pull request Jul 12, 2025
@bmuddha bmuddha force-pushed the bmuddha/refactor/accounts-db branch from 8fc7900 to 2bd3c68 Compare July 12, 2025 07:44
@bmuddha bmuddha changed the base branch from master to dev July 12, 2025 09:41
@bmuddha bmuddha merged commit 3fef41f into dev Jul 12, 2025
4 checks passed
@bmuddha bmuddha deleted the bmuddha/refactor/accounts-db branch July 12, 2025 12:55
thlorenz added a commit that referenced this pull request Jul 15, 2025
* dev:
  Cleanup and refactor accountsdb index (#419)
  chore: create dev branch
thlorenz added a commit that referenced this pull request Jul 15, 2025
* dev:
  Cleanup and refactor accountsdb index (#419)
  chore: create dev branch
  fix: don't block the accounts update loop with subscription (#420)
  chore: increase compute budget for table mania create lookup table (#450)
thlorenz added a commit that referenced this pull request Jul 15, 2025
* dev:
  fix: prevent new subscriptions if no free connections exist (#456)
  chore: avoid CU budget exceeded for lookup table management (#455)
  Cleanup and refactor accountsdb index (#419)
  chore: create dev branch
bmuddha added a commit that referenced this pull request Jul 15, 2025
Modify account layout in accounts database:
1. reuse slack space which is available in block allocation if possible,
   this avoids reallocation of accounts if the data field grows
   insignificantly.
2. track account owner change to avoid index checks in happy path

depends on #419 
closes #443
closes #441  

<!-- greptile_comment -->

## Greptile Summary

Major refactoring of AccountsDB storage and indexing system to optimize
account data storage and improve performance through block space reuse
and owner tracking.

- Consolidated multiple LMDB environments into a single environment and
introduced new `Table` abstraction in
`magicblock-accounts-db/src/index/table.rs` for better database
operation encapsulation
- Modified account serialization in `magicblock-accounts-db/src/lib.rs`
to reuse slack space in block allocations, reducing unnecessary
reallocations for small data growth
- Added owner change tracking to optimize index checks by skipping them
in the happy path when owner hasn't changed
- Improved LMDB iteration safety and efficiency in
`magicblock-accounts-db/src/index/iterator.rs` by removing manual cursor
management
- Changed program account iteration behavior to return empty iterators
instead of NotFound errors for better API consistency



<!-- /greptile_comment -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants