-
Notifications
You must be signed in to change notification settings - Fork 725
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
Store linear blocks in freezer db #5905
Draft
dapplion
wants to merge
4
commits into
sigp:unstable
Choose a base branch
from
dapplion:linear-frozen-blocks-2
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
beacon_node/beacon_chain/src/schema_change/migration_schema_v20.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use crate::BeaconChainTypes; | ||
use slog::{info, Logger}; | ||
use std::sync::Arc; | ||
use store::{get_key_for_col, DBColumn, Error, HotColdDB, KeyValueStore, KeyValueStoreOp}; | ||
use types::{Hash256, Slot}; | ||
|
||
/// Chunk size for freezer block roots in the old database schema. | ||
const OLD_SCHEMA_CHUNK_SIZE: u64 = 128; | ||
|
||
fn old_schema_chunk_key(cindex: u64) -> [u8; 8] { | ||
(cindex + 1).to_be_bytes() | ||
} | ||
|
||
pub fn upgrade_to_v20<T: BeaconChainTypes>( | ||
db: Arc<HotColdDB<T::EthSpec, T::HotStore, T::ColdStore>>, | ||
log: Logger, | ||
) -> Result<Vec<KeyValueStoreOp>, Error> { | ||
info!(log, "Upgrading freezer database schema"); | ||
upgrade_freezer_database::<T>(&db, &log)?; | ||
|
||
// No hot DB changes | ||
return Ok(vec![]); | ||
} | ||
|
||
fn upgrade_freezer_database<T: BeaconChainTypes>( | ||
db: &HotColdDB<T::EthSpec, T::HotStore, T::ColdStore>, | ||
log: &Logger, | ||
) -> Result<(), Error> { | ||
let mut cold_db_ops = vec![]; | ||
|
||
// Re-write the beacon block roots array. | ||
let mut freezer_block_roots = vec![]; | ||
let oldest_block_slot = db.get_oldest_block_slot(); | ||
let mut current_slot = oldest_block_slot; | ||
|
||
for result in db | ||
.cold_db | ||
.iter_column::<Vec<u8>>(DBColumn::BeaconBlockRoots) | ||
{ | ||
let (chunk_key, chunk_bytes) = result?; | ||
|
||
// Stage this chunk for deletion. | ||
cold_db_ops.push(KeyValueStoreOp::DeleteKey(get_key_for_col( | ||
DBColumn::BeaconBlockRoots.into(), | ||
&chunk_key, | ||
))); | ||
|
||
// Skip the 0x0 key which is for the genesis block. | ||
if chunk_key.iter().all(|b| *b == 0u8) { | ||
continue; | ||
} | ||
// Skip the 0x00..01 key which is for slot 0. | ||
if chunk_key == old_schema_chunk_key(0).as_slice() && current_slot != 0 { | ||
continue; | ||
} | ||
|
||
let current_chunk_index = current_slot.as_u64() / OLD_SCHEMA_CHUNK_SIZE; | ||
if chunk_key != old_schema_chunk_key(current_chunk_index).as_slice() { | ||
return Err(Error::DBError { | ||
message: format!( | ||
"expected chunk index {} but got {:?}", | ||
current_chunk_index, chunk_key | ||
), | ||
}); | ||
} | ||
|
||
for (i, block_root_bytes) in chunk_bytes.chunks_exact(32).enumerate() { | ||
let block_root = Hash256::from_slice(block_root_bytes); | ||
|
||
if block_root.is_zero() { | ||
continue; | ||
} | ||
|
||
let slot = Slot::new(current_chunk_index * OLD_SCHEMA_CHUNK_SIZE + i as u64); | ||
if slot != current_slot { | ||
return Err(Error::DBError { | ||
message: format!( | ||
"expected block root for slot {} but got {}", | ||
current_slot, slot | ||
), | ||
}); | ||
} | ||
freezer_block_roots.push((slot, block_root)); | ||
current_slot += 1; | ||
} | ||
} | ||
|
||
// Write the freezer block roots in the new schema. | ||
for (slot, block_root) in freezer_block_roots { | ||
cold_db_ops.push(KeyValueStoreOp::PutKeyValue( | ||
get_key_for_col( | ||
DBColumn::BeaconBlockRoots.into(), | ||
&slot.as_u64().to_be_bytes(), | ||
), | ||
block_root.as_bytes().to_vec(), | ||
)); | ||
} | ||
|
||
db.cold_db.do_atomically(cold_db_ops)?; | ||
info!( | ||
log, | ||
"Freezer database upgrade complete"; | ||
"oldest_block_slot" => oldest_block_slot, | ||
"newest_block_slot" => current_slot - 1 | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michaelsproul thoughts on migrating all finalized blocks? If we just delete that's going to be a lot of bandwidth on the entire network to backfill. However, migrating all blocks will take a while. Are DB migrations blocking the start routine of Lighthouse?