Revert "Bloom upgrade in beta" (#2635)

This commit is contained in:
Arkadiy Paronyan 2016-10-15 13:15:45 +02:00 committed by GitHub
parent 18062f11dc
commit 2ce43434f1
2 changed files with 13 additions and 44 deletions

View File

@ -17,7 +17,7 @@
//! Bloom upgrade //! Bloom upgrade
use client::{DB_COL_EXTRA, DB_COL_HEADERS, DB_NO_OF_COLUMNS, DB_COL_STATE, DB_COL_ACCOUNT_BLOOM}; use client::{DB_COL_EXTRA, DB_COL_HEADERS, DB_NO_OF_COLUMNS, DB_COL_STATE, DB_COL_ACCOUNT_BLOOM};
use state_db::{ACCOUNT_BLOOM_SPACE, DEFAULT_ACCOUNT_PRESET, StateDB, ACCOUNT_BLOOM_HASHCOUNT_KEY, ACCOUNT_BLOOM_SPACE_KEY}; use state_db::{ACCOUNT_BLOOM_SPACE, DEFAULT_ACCOUNT_PRESET, StateDB, ACCOUNT_BLOOM_HASHCOUNT_KEY};
use util::trie::TrieDB; use util::trie::TrieDB;
use views::HeaderView; use views::HeaderView;
use bloomfilter::Bloom; use bloomfilter::Bloom;
@ -26,7 +26,6 @@ use util::journaldb;
use util::{H256, FixedHash, BytesConvertable}; use util::{H256, FixedHash, BytesConvertable};
use util::{Database, DatabaseConfig, DBTransaction, CompactionProfile}; use util::{Database, DatabaseConfig, DBTransaction, CompactionProfile};
use std::path::Path; use std::path::Path;
use byteorder::{LittleEndian, ByteOrder};
fn check_bloom_exists(db: &Database) -> bool { fn check_bloom_exists(db: &Database) -> bool {
let hash_count_entry = db.get(DB_COL_ACCOUNT_BLOOM, ACCOUNT_BLOOM_HASHCOUNT_KEY) let hash_count_entry = db.get(DB_COL_ACCOUNT_BLOOM, ACCOUNT_BLOOM_HASHCOUNT_KEY)
@ -35,22 +34,10 @@ fn check_bloom_exists(db: &Database) -> bool {
hash_count_entry.is_some() hash_count_entry.is_some()
} }
fn check_space_match(db: &Database) -> Result<(), usize> {
let db_space = db.get(DB_COL_ACCOUNT_BLOOM, ACCOUNT_BLOOM_SPACE_KEY)
.expect("Low-level database error")
.map(|val| LittleEndian::read_u64(&val[..]) as usize)
// this was the initial size of the bloom which was not written in the database
.unwrap_or(1048576);
if db_space == ACCOUNT_BLOOM_SPACE { Ok(()) } else { Err(db_space) }
}
/// Account bloom upgrade routine. If bloom already present, does nothing. /// Account bloom upgrade routine. If bloom already present, does nothing.
/// If database empty (no best block), does nothing. /// If database empty (no best block), does nothing.
/// Can be called on upgraded database with no issues (will do nothing). /// Can be called on upgraded database with no issues (will do nothing).
pub fn upgrade_account_bloom(db_path: &Path) -> Result<(), Error> { pub fn upgrade_account_bloom(db_path: &Path) -> Result<(), Error> {
let mut progress = ::util::migration::Progress::default();
let path = try!(db_path.to_str().ok_or(Error::MigrationImpossible)); let path = try!(db_path.to_str().ok_or(Error::MigrationImpossible));
trace!(target: "migration", "Account bloom upgrade at {:?}", db_path); trace!(target: "migration", "Account bloom upgrade at {:?}", db_path);
@ -80,34 +67,14 @@ pub fn upgrade_account_bloom(db_path: &Path) -> Result<(), Error> {
}; };
let state_root = HeaderView::new(&best_block_header).state_root(); let state_root = HeaderView::new(&best_block_header).state_root();
let db = ::std::sync::Arc::new(source); if check_bloom_exists(&source) {
let batch = DBTransaction::new(&db); // bloom already exists, nothing to do
trace!(target: "migration", "Bloom already present, skipping");
if check_bloom_exists(&*db) {
match check_space_match(&*db) {
Ok(_) => {
// bloom already exists and desired and stored spaces match, nothing to do
trace!(target: "migration", "Bloom already present of the right space, skipping");
return Ok(()) return Ok(())
},
Err(wrong_size) => {
// nullify existing bloom entries
trace!(target: "migration", "Clearing old bloom of space {}", &wrong_size);
let mut key = [0u8; 8];
for idx in 0..(wrong_size as u64/8) {
LittleEndian::write_u64(&mut key, idx);
try!(batch.put(DB_COL_ACCOUNT_BLOOM, &key, &[0u8; 8]));
if idx % 10000 == 1 { progress.tick(); };
} }
LittleEndian::write_u64(&mut key, ACCOUNT_BLOOM_SPACE as u64); println!("Adding accounts bloom (one-time upgrade)");
try!(batch.put(DB_COL_ACCOUNT_BLOOM, ACCOUNT_BLOOM_SPACE_KEY, &key)); let db = ::std::sync::Arc::new(source);
},
}
}
println!("Adding/expanding accounts bloom (one-time upgrade)");
let bloom_journal = { let bloom_journal = {
let mut bloom = Bloom::new(ACCOUNT_BLOOM_SPACE, DEFAULT_ACCOUNT_PRESET); let mut bloom = Bloom::new(ACCOUNT_BLOOM_SPACE, DEFAULT_ACCOUNT_PRESET);
// no difference what algorithm is passed, since there will be no writes // no difference what algorithm is passed, since there will be no writes
@ -119,7 +86,6 @@ pub fn upgrade_account_bloom(db_path: &Path) -> Result<(), Error> {
for (ref account_key, _) in account_trie.iter() { for (ref account_key, _) in account_trie.iter() {
let account_key_hash = H256::from_slice(&account_key); let account_key_hash = H256::from_slice(&account_key);
bloom.set(account_key_hash.as_slice()); bloom.set(account_key_hash.as_slice());
progress.tick();
} }
bloom.drain_journal() bloom.drain_journal()
@ -127,10 +93,12 @@ pub fn upgrade_account_bloom(db_path: &Path) -> Result<(), Error> {
trace!(target: "migration", "Generated {} bloom updates", bloom_journal.entries.len()); trace!(target: "migration", "Generated {} bloom updates", bloom_journal.entries.len());
let batch = DBTransaction::new(&db);
try!(StateDB::commit_bloom(&batch, bloom_journal).map_err(|_| Error::Custom("Failed to commit bloom".to_owned()))); try!(StateDB::commit_bloom(&batch, bloom_journal).map_err(|_| Error::Custom("Failed to commit bloom".to_owned())));
try!(db.write(batch)); try!(db.write(batch));
trace!(target: "migration", "Finished bloom update"); trace!(target: "migration", "Finished bloom update");
Ok(()) Ok(())
} }

View File

@ -87,11 +87,10 @@ pub struct StateDB {
commit_number: Option<BlockNumber>, commit_number: Option<BlockNumber>,
} }
pub const ACCOUNT_BLOOM_SPACE: usize = 4 * 1048576; pub const ACCOUNT_BLOOM_SPACE: usize = 1048576;
pub const DEFAULT_ACCOUNT_PRESET: usize = 100 * 1000000; pub const DEFAULT_ACCOUNT_PRESET: usize = 1000000;
pub const ACCOUNT_BLOOM_HASHCOUNT_KEY: &'static [u8] = b"account_hash_count"; pub const ACCOUNT_BLOOM_HASHCOUNT_KEY: &'static [u8] = b"account_hash_count";
pub const ACCOUNT_BLOOM_SPACE_KEY: &'static [u8] = b"account_space";
impl StateDB { impl StateDB {
/// Loads accounts bloom from the database /// Loads accounts bloom from the database
@ -139,11 +138,13 @@ impl StateDB {
} }
pub fn check_account_bloom(&self, address: &Address) -> bool { pub fn check_account_bloom(&self, address: &Address) -> bool {
trace!(target: "account_bloom", "Check account bloom: {:?}", address);
let bloom = self.account_bloom.lock(); let bloom = self.account_bloom.lock();
bloom.check(address.sha3().as_slice()) bloom.check(address.sha3().as_slice())
} }
pub fn note_account_bloom(&self, address: &Address) { pub fn note_account_bloom(&self, address: &Address) {
trace!(target: "account_bloom", "Note account bloom: {:?}", address);
let mut bloom = self.account_bloom.lock(); let mut bloom = self.account_bloom.lock();
bloom.set(address.sha3().as_slice()); bloom.set(address.sha3().as_slice());
} }