-
Notifications
You must be signed in to change notification settings - Fork 112
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
feat(trin-storage): add filter to store #1286
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1e0f78d
feat(trin-storage): add filter to store
njgheorghita 73b6d48
fix: update filter to complete pagination before deleting
njgheorghita 0c52247
feat: add script to purge invalid history content
njgheorghita 870ee84
fix: update script to use direct sql query
njgheorghita c3c19e8
fix: add binary to dockerfile
njgheorghita 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
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,103 @@ | ||
use alloy_primitives::B256; | ||
use anyhow::Result; | ||
use clap::Parser; | ||
use discv5::enr::{CombinedKey, Enr}; | ||
use tracing::info; | ||
|
||
use ethportal_api::{types::portal_wire::ProtocolId, HistoryContentKey}; | ||
use portalnet::utils::db::{configure_node_data_dir, configure_trin_data_dir}; | ||
use trin_storage::{ | ||
versioned::{create_store, ContentType, IdIndexedV1Store, IdIndexedV1StoreConfig}, | ||
PortalStorageConfigFactory, | ||
}; | ||
use trin_utils::log::init_tracing_logger; | ||
|
||
/// iterates history store and removes any invalid network entries | ||
pub fn main() -> Result<()> { | ||
init_tracing_logger(); | ||
let script_config = PurgeConfig::parse(); | ||
|
||
let trin_data_dir = configure_trin_data_dir(false /* ephemeral */)?; | ||
let (node_data_dir, mut private_key) = configure_node_data_dir( | ||
trin_data_dir, | ||
script_config.private_key, | ||
"mainnet".to_string(), | ||
)?; | ||
let enr_key = CombinedKey::secp256k1_from_bytes(private_key.as_mut_slice()) | ||
.expect("Failed to create ENR key"); | ||
let enr = Enr::empty(&enr_key).unwrap(); | ||
let node_id = enr.node_id(); | ||
info!("Purging data for NodeID: {node_id}"); | ||
info!("DB Path: {node_data_dir:?}"); | ||
|
||
let config = PortalStorageConfigFactory::new( | ||
script_config.capacity as u64, | ||
&["history".to_string()], | ||
node_id, | ||
node_data_dir, | ||
) | ||
.unwrap() | ||
.create("history"); | ||
let config = IdIndexedV1StoreConfig::new(ContentType::History, ProtocolId::History, config); | ||
let sql_connection_pool = config.sql_connection_pool.clone(); | ||
let store: IdIndexedV1Store<HistoryContentKey> = | ||
create_store(ContentType::History, config, sql_connection_pool).unwrap(); | ||
let total_entry_count = store.usage_stats().entry_count; | ||
info!("total entry count: {total_entry_count}"); | ||
let sql_connection_pool = store.config.sql_connection_pool.clone(); | ||
let lookup_result = sql_connection_pool | ||
.get() | ||
.unwrap() | ||
.query_row(&lookup_query(), [], |row| row.get::<usize, u64>(0)) | ||
.expect("Failed to fetch history content"); | ||
info!("found {} epoch accumulators", lookup_result); | ||
if script_config.evict { | ||
let _: Vec<String> = sql_connection_pool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we used It would be something like: let deleted = sql_connection_pool.get().unwrap().execute(&delete_query(), []).unwrap(); I think with this api we don't even need extra |
||
.get() | ||
.unwrap() | ||
.prepare(&delete_query()) | ||
.unwrap() | ||
.query_map([], |row| row.get::<usize, String>(0)) | ||
.unwrap() | ||
.map(|r| r.unwrap()) | ||
.collect(); | ||
let changes = sql_connection_pool.get().unwrap().changes(); | ||
info!("removed {} invalid history content values", changes,); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn lookup_query() -> String { | ||
r#"SELECT COUNT(*) as count FROM ii1_history WHERE hex(content_key) LIKE "03%""#.to_string() | ||
} | ||
|
||
fn delete_query() -> String { | ||
r#"DELETE FROM ii1_history WHERE hex(content_key) LIKE '03%'"#.to_string() | ||
} | ||
|
||
// CLI Parameter Handling | ||
#[derive(Parser, Debug, PartialEq)] | ||
#[command( | ||
name = "Trin DB Purge Invalid History Content", | ||
about = "Remove invalid data from Trin History Store" | ||
)] | ||
pub struct PurgeConfig { | ||
#[arg( | ||
long, | ||
help = "(unsafe) Hex private key to generate node id for database namespace (with 0x prefix)" | ||
)] | ||
pub private_key: Option<B256>, | ||
|
||
#[arg( | ||
long, | ||
help = "Storage capacity. Must be larger than the current capacity otherwise it will prune data!" | ||
)] | ||
pub capacity: usize, | ||
|
||
#[arg( | ||
long, | ||
help = "Actually evict the history data from db", | ||
default_value = "false" | ||
)] | ||
pub evict: bool, | ||
} |
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.
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.
You already have
sql_connection_pool
, at line 42. Therefor, you don't needconfig
instore
to be public (changes in other file).Also, I would argue that you don't need store at all. All you are doing is getting the number of entries, which you can do by executing:
Up to you if you want to create instance of store and get entry count from there or execute it manually from here.