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

chore(dot/state/pruner): refactoring and fixes #2831

Closed
wants to merge 11 commits into from

Conversation

qdm12
Copy link
Contributor

@qdm12 qdm12 commented Sep 13, 2022

Changes

  • Correctly prune storage database
  • Prune journal database
  • Everything stored on disk so that it handles a Gossamer restart
  • Event driven pruning instead of periodic pruning
    • Does not allow to have more than the number of blocks to retain on disk (in storage and journal databases)
    • No performance impact since IO is already being done (storing journal data)
    • Fix thread safety locking for the entire store journal record operation
  • Do not store inserted node hashes in journal database (unneeded)
  • Safer/transactional database batches
  • Support node hash re-insertion when deleted by one or more ancestor/uncle blocks
  • Check descendant of a block using the block state (injected) to ensure we can remove a node hash to be pruned
  • Minor refactoring
    • Move to internal/pruner/full and internal/pruner/archive
    • Use node hash + common.Hash instead of merkle value + string, since it's only 32B node hashes
    • Inject database and tables to the pruner constructor
    • Remove no longer needed NewIterator database method from production code
    • Rename StoreJournalRecord to RecordAndPrune
  • Optimisations (create issue):
    • Read cache to reduce database lookups
    • Use custom binary encoding for quicker appends than scale decode + re-encode

Tests

Issues

Fixes #2835

Primary Reviewer

@EclesioMeloJunior

@codecov
Copy link

codecov bot commented Sep 13, 2022

Codecov Report

Merging #2831 (194f1f2) into development (ef40637) will increase coverage by 0.59%.
The diff coverage is 66.95%.

❗ Current head 194f1f2 differs from pull request most recent head ed2de43. Consider uploading reports for the commit ed2de43 to get more accurate results

Additional details and impacted files
@@               Coverage Diff               @@
##           development    #2831      +/-   ##
===============================================
+ Coverage        51.57%   52.17%   +0.59%     
===============================================
  Files              219      223       +4     
  Lines            27984    28124     +140     
===============================================
+ Hits             14434    14674     +240     
+ Misses           12267    12204      -63     
+ Partials          1283     1246      -37     

Comment on lines -31 to +32
// DefaultPruningMode is the default pruning mode
DefaultPruningMode = "archive"
// DefaultPruningEnabled describes if online pruning should be enabled by default.
DefaultPruningEnabled = false
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me know if you prefer to keep it as a string (might be needed for more pruning options later), but I changed it to a boolean to keep it simpler for now.

@qdm12 qdm12 changed the title chore(dot/state/pruner): refactoring of code and surroundings chore(dot/state/pruner): refactoring and fixes Jan 6, 2023
@qdm12 qdm12 force-pushed the qdm12/state/pruner/refactor branch from ce064cf to 194f1f2 Compare January 6, 2023 16:53
Comment on lines -159 to -160
// storePruningData stores the pruner configuration.
func (s *BaseState) storePruningData(mode pruner.Config) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was persisting the pruning mode, whereas really it should be given at start from the toml config or flags, and be able to change from one mode to the other.

Comment on lines +79 to +81
// Temporary work-around until we drop merkle values for node hashes in another PR (already opened).
insertedNodeHashes := make(map[common.Hash]struct{}, len(insertedMerkleValues))
for k := range insertedMerkleValues {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Conversion will be removed once #2915 gets merged.

Comment on lines +428 to +433
if dstv.IsNil() {
keyType := dstv.Type().Key()
valueType := dstv.Type().Elem()
aMapType := reflect.MapOf(keyType, valueType)
dstv.Set(reflect.MakeMapWithSize(aMapType, int(numberOfTuples)))
}
Copy link
Contributor Author

@qdm12 qdm12 Jan 6, 2023

Choose a reason for hiding this comment

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

this is so it can be decoded to a nil typed destination map. I don't think it's needed so much anymore but it was when I had a struct with map fields (and it sucks to init all maps)

@qdm12 qdm12 force-pushed the qdm12/state/pruner/refactor branch from 194f1f2 to ad3101e Compare January 12, 2023 14:59
logger.EXPECT().Debugf("journal data stored for block number %d and block hash %s",
uint32(1), "0x66000000...00000000")
err = pruner.RecordAndPrune(
map[common.Hash]struct{}{{3}: {}}, // deleted node hashes
Copy link
Contributor Author

@qdm12 qdm12 Jan 13, 2023

Choose a reason for hiding this comment

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

Should we prune 3? How should we handle forks??

EDIT: we should handle forks, this is WIP

@@ -176,9 +175,9 @@ func TestService_StorageTriePruning(t *testing.T) {
config := Config{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test TestService_StorageTriePruning which was modified in #3063 should now be unskipped.

- Move dot/state/pruner to internal/pruner/archive and internal/pruner/full
- Define pruner as dot/state local interface
- Inject pruner in NewStorageState
- Unexport newStorageState
- `NewArchiveNode` constructor
- Change pruner config from `Mode` to bool
  - It's either on or off really
  - Change `--pruning` to be a boolean flag
  - Change pruning toml field to be a boolean
- Pruner config changes
  - Do not store and load pruning config from base state
  - Move Config struct definition in `dot/state`
  - Set pruning state service config from user config in `dot`
- Database batches for storing journal records
- Unit and integration tests
- Rename `StoreJournalRecord` to `RecordAndPrune`
- Genesis block: no journal data and no pruning
@qdm12 qdm12 force-pushed the qdm12/state/pruner/refactor branch from ad3101e to ed2de43 Compare January 26, 2023 15:22
@qdm12 qdm12 marked this pull request as draft January 26, 2023 15:23
@q9f q9f closed this Jan 12, 2024
@q9f q9f deleted the qdm12/state/pruner/refactor branch January 12, 2024 12:08
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.

dot/state/pruner: refactor code to be more understandable and modular
2 participants