openethereum/util/src/journaldb/refcounteddb.rs

338 lines
9.8 KiB
Rust
Raw Normal View History

2016-12-11 19:30:54 +01:00
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
2016-03-13 18:07:10 +01:00
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
2016-04-06 10:07:24 +02:00
//! Disk-backed, ref-counted `JournalDB` implementation.
2016-03-13 18:07:10 +01:00
use common::*;
use rlp::*;
use hashdb::*;
Snapshot creation and restoration (#1679) * to_rlp takes self by-reference * clean up some derefs * out-of-order insertion for blockchain * implement block rebuilder without verification * group block chunk header into struct * block rebuilder does verification * integrate snapshot service with client service; flesh out implementation more * initial implementation of snapshot service * remove snapshottaker trait * snapshot writer trait with packed and loose implementations * write chunks using "snapshotwriter" in service * have snapshot taking use snapshotwriter * implement snapshot readers * back up client dbs when replacing * use snapshot reader in snapshot service * describe offset format * use new get_db_path in parity, allow some errors in service * blockchain formatting * implement parity snapshot * implement snapshot restore * force blocks to be submitted in order * fix bug loading block hashes in packed reader * fix seal field loading * fix uncle hash computation * fix a few bugs * store genesis state in db. reverse block chunk order in packed writer * allow out-of-order import for blocks * bring restoration types together * only snapshot the last 30000 blocks * restore into overlaydb instead of journaldb * commit version to database * use memorydbs and commit directly * fix trie test compilation * fix failing tests * sha3_null_rlp, not H256::zero * move overlaydb to ref_overlaydb, add new overlaydb without on-disk rc * port archivedb to new overlaydb * add deletion mode tests for overlaydb * use new overlaydb, check state root at end * share chain info between state and block snapshotting * create blocks snapshot using blockchain directly * allow snapshot from arbitrary block, remove panickers from snapshot creation * begin test framework * blockchain chunking test * implement stateproducer::tick * state snapshot test * create block and state chunks concurrently, better restoration informant * fix tests * add deletion mode tests for overlaydb * address comments * more tests * Fix up tests. * remove a few printlns * add a little more documentation to `commit` * fix tests * fix ref_overlaydb test names * snapshot command skeleton * revert ref_overlaydb renaming * reimplement snapshot commands * fix many errors * everything but inject * get ethcore compiling * get snapshot tests passing again * instrument snapshot commands again * fix fallout from other changes, mark snapshots as experimental * optimize injection patterns * do two injections * fix up tests * take snapshots from 1000 blocks efore * address minor comments * fix a few io crate related errors * clarify names about total difficulty [ci skip]
2016-08-05 17:00:46 +02:00
use overlaydb::OverlayDB;
use memorydb::MemoryDB;
use super::{DB_PREFIX_LEN, LATEST_ERA_KEY};
2016-03-13 18:07:10 +01:00
use super::traits::JournalDB;
use kvdb::{Database, DBTransaction};
2016-03-13 18:07:10 +01:00
#[cfg(test)]
use std::env;
2016-04-06 10:07:24 +02:00
/// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay
2016-03-13 18:07:10 +01:00
/// and latent-removal semantics.
///
2016-04-06 10:07:24 +02:00
/// Like `OverlayDB`, there is a memory overlay; `commit()` must be called in order to
/// write operations out to disk. Unlike `OverlayDB`, `remove()` operations do not take effect
2016-03-13 18:07:10 +01:00
/// immediately. Rather some age (based on a linear but arbitrary metric) must pass before
/// the removals actually take effect.
///
/// journal format:
/// ```
/// [era, 0] => [ id, [insert_0, ...], [remove_0, ...] ]
/// [era, 1] => [ id, [insert_0, ...], [remove_0, ...] ]
/// [era, n] => [ ... ]
/// ```
///
/// when we make a new commit, we journal the inserts and removes.
/// for each `end_era` that we journaled that we are no passing by,
/// we remove all of its removes assuming it is canonical and all
/// of its inserts otherwise.
// TODO: store last_era, reclaim_period.
2016-03-13 18:07:10 +01:00
pub struct RefCountedDB {
forward: OverlayDB,
backing: Arc<Database>,
latest_era: Option<u64>,
2016-03-13 18:07:10 +01:00
inserts: Vec<H256>,
removes: Vec<H256>,
column: Option<u32>,
2016-03-13 18:07:10 +01:00
}
2016-03-13 21:21:30 +01:00
const PADDING : [u8; 10] = [ 0u8; 10 ];
2016-03-13 18:07:10 +01:00
impl RefCountedDB {
/// Create a new instance given a `backing` database.
pub fn new(backing: Arc<Database>, col: Option<u32>) -> RefCountedDB {
let latest_era = backing.get(col, &LATEST_ERA_KEY).expect("Low-level database error.").map(|val| decode::<u64>(&val));
2016-03-13 18:07:10 +01:00
RefCountedDB {
forward: OverlayDB::new(backing.clone(), col),
2016-03-13 18:07:10 +01:00
backing: backing,
inserts: vec![],
removes: vec![],
latest_era: latest_era,
column: col,
2016-03-13 18:07:10 +01:00
}
}
/// Create a new instance with an anonymous temporary database.
#[cfg(test)]
fn new_temp() -> RefCountedDB {
let mut dir = env::temp_dir();
dir.push(H32::random().hex());
let backing = Arc::new(Database::open_default(dir.to_str().unwrap()).unwrap());
Self::new(backing, None)
2016-03-13 18:07:10 +01:00
}
}
impl HashDB for RefCountedDB {
fn keys(&self) -> HashMap<H256, i32> { self.forward.keys() }
2016-10-26 13:53:47 +02:00
fn get(&self, key: &H256) -> Option<DBValue> { self.forward.get(key) }
fn contains(&self, key: &H256) -> bool { self.forward.contains(key) }
2016-03-13 18:07:10 +01:00
fn insert(&mut self, value: &[u8]) -> H256 { let r = self.forward.insert(value); self.inserts.push(r.clone()); r }
2016-10-26 13:53:47 +02:00
fn emplace(&mut self, key: H256, value: DBValue) { self.inserts.push(key.clone()); self.forward.emplace(key, value); }
fn remove(&mut self, key: &H256) { self.removes.push(key.clone()); }
2016-03-13 18:07:10 +01:00
}
impl JournalDB for RefCountedDB {
2016-03-28 09:42:50 +02:00
fn boxed_clone(&self) -> Box<JournalDB> {
2016-03-13 18:07:10 +01:00
Box::new(RefCountedDB {
forward: self.forward.clone(),
backing: self.backing.clone(),
latest_era: self.latest_era,
2016-03-13 18:07:10 +01:00
inserts: self.inserts.clone(),
removes: self.removes.clone(),
column: self.column.clone(),
2016-03-13 18:07:10 +01:00
})
}
fn mem_used(&self) -> usize {
self.inserts.heap_size_of_children() + self.removes.heap_size_of_children()
}
fn is_empty(&self) -> bool {
self.latest_era.is_none()
2016-03-13 18:07:10 +01:00
}
fn backing(&self) -> &Arc<Database> {
&self.backing
}
2016-04-12 03:42:50 +02:00
fn latest_era(&self) -> Option<u64> { self.latest_era }
2016-04-12 00:51:14 +02:00
fn state(&self, id: &H256) -> Option<Bytes> {
self.backing.get_by_prefix(self.column, &id[0..DB_PREFIX_LEN]).map(|b| b.to_vec())
}
fn journal_under(&mut self, batch: &mut DBTransaction, now: u64, id: &H256) -> Result<u32, UtilError> {
2016-03-13 18:07:10 +01:00
// record new commit's details.
let mut index = 0usize;
let mut last;
2016-03-13 18:07:10 +01:00
while try!(self.backing.get(self.column, {
2016-03-13 18:07:10 +01:00
let mut r = RlpStream::new_list(3);
r.append(&now);
r.append(&index);
r.append(&&PADDING[..]);
last = r.drain();
&last
})).is_some() {
index += 1;
}
2016-03-18 10:14:19 +01:00
let mut r = RlpStream::new_list(3);
r.append(id);
r.append(&self.inserts);
r.append(&self.removes);
batch.put(self.column, &last, r.as_raw());
2016-03-13 21:21:30 +01:00
let ops = self.inserts.len() + self.removes.len();
trace!(target: "rcdb", "new journal for time #{}.{} => {}: inserts={:?}, removes={:?}", now, index, id, self.inserts, self.removes);
self.inserts.clear();
self.removes.clear();
if self.latest_era.map_or(true, |e| now > e) {
batch.put(self.column, &LATEST_ERA_KEY, &encode(&now));
self.latest_era = Some(now);
2016-03-13 18:07:10 +01:00
}
Ok(ops as u32)
}
fn mark_canonical(&mut self, batch: &mut DBTransaction, end_era: u64, canon_id: &H256) -> Result<u32, UtilError> {
2016-03-13 18:07:10 +01:00
// apply old commits' details
let mut index = 0usize;
let mut last;
while let Some(rlp_data) = {
try!(self.backing.get(self.column, {
let mut r = RlpStream::new_list(3);
r.append(&end_era);
r.append(&index);
r.append(&&PADDING[..]);
last = r.drain();
&last
}))
} {
let rlp = Rlp::new(&rlp_data);
let our_id: H256 = rlp.val_at(0);
let to_remove: Vec<H256> = rlp.val_at(if *canon_id == our_id {2} else {1});
trace!(target: "rcdb", "delete journal for time #{}.{}=>{}, (canon was {}): deleting {:?}", end_era, index, our_id, canon_id, to_remove);
for i in &to_remove {
self.forward.remove(i);
2016-03-13 18:07:10 +01:00
}
batch.delete(self.column, &last);
index += 1;
2016-03-13 18:07:10 +01:00
}
let r = try!(self.forward.commit_to_batch(batch));
2016-03-13 18:07:10 +01:00
Ok(r)
}
fn inject(&mut self, batch: &mut DBTransaction) -> Result<u32, UtilError> {
self.inserts.clear();
for remove in self.removes.drain(..) {
self.forward.remove(&remove);
}
self.forward.commit_to_batch(batch)
}
fn consolidate(&mut self, mut with: MemoryDB) {
for (key, (value, rc)) in with.drain() {
for _ in 0..rc {
self.emplace(key.clone(), value.clone());
}
for _ in rc..0 {
self.remove(&key);
}
}
}
2016-03-13 18:07:10 +01:00
}
#[cfg(test)]
mod tests {
2016-03-18 10:14:19 +01:00
#![cfg_attr(feature="dev", allow(blacklisted_name))]
2016-04-06 10:07:24 +02:00
#![cfg_attr(feature="dev", allow(similar_names))]
2016-03-18 10:14:19 +01:00
2016-03-13 18:07:10 +01:00
use common::*;
2016-11-28 17:05:37 +01:00
use hashdb::{HashDB, DBValue};
2016-03-13 18:07:10 +01:00
use super::*;
use super::super::traits::JournalDB;
#[test]
fn long_history() {
// history is 3
let mut jdb = RefCountedDB::new_temp();
let h = jdb.insert(b"foo");
jdb.commit_batch(0, &b"0".sha3(), None).unwrap();
assert!(jdb.contains(&h));
2016-03-13 18:07:10 +01:00
jdb.remove(&h);
jdb.commit_batch(1, &b"1".sha3(), None).unwrap();
assert!(jdb.contains(&h));
jdb.commit_batch(2, &b"2".sha3(), None).unwrap();
assert!(jdb.contains(&h));
jdb.commit_batch(3, &b"3".sha3(), Some((0, b"0".sha3()))).unwrap();
assert!(jdb.contains(&h));
jdb.commit_batch(4, &b"4".sha3(), Some((1, b"1".sha3()))).unwrap();
assert!(!jdb.contains(&h));
2016-03-13 18:07:10 +01:00
}
2016-04-12 00:51:14 +02:00
#[test]
fn latest_era_should_work() {
// history is 3
let mut jdb = RefCountedDB::new_temp();
assert_eq!(jdb.latest_era(), None);
let h = jdb.insert(b"foo");
jdb.commit_batch(0, &b"0".sha3(), None).unwrap();
2016-04-12 00:51:14 +02:00
assert_eq!(jdb.latest_era(), Some(0));
jdb.remove(&h);
jdb.commit_batch(1, &b"1".sha3(), None).unwrap();
2016-04-12 00:51:14 +02:00
assert_eq!(jdb.latest_era(), Some(1));
jdb.commit_batch(2, &b"2".sha3(), None).unwrap();
2016-04-12 00:51:14 +02:00
assert_eq!(jdb.latest_era(), Some(2));
jdb.commit_batch(3, &b"3".sha3(), Some((0, b"0".sha3()))).unwrap();
2016-04-12 00:51:14 +02:00
assert_eq!(jdb.latest_era(), Some(3));
jdb.commit_batch(4, &b"4".sha3(), Some((1, b"1".sha3()))).unwrap();
2016-04-12 00:51:14 +02:00
assert_eq!(jdb.latest_era(), Some(4));
}
2016-03-13 18:07:10 +01:00
#[test]
fn complex() {
// history is 1
let mut jdb = RefCountedDB::new_temp();
let foo = jdb.insert(b"foo");
let bar = jdb.insert(b"bar");
jdb.commit_batch(0, &b"0".sha3(), None).unwrap();
assert!(jdb.contains(&foo));
assert!(jdb.contains(&bar));
2016-03-13 18:07:10 +01:00
jdb.remove(&foo);
jdb.remove(&bar);
let baz = jdb.insert(b"baz");
jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap();
assert!(jdb.contains(&foo));
assert!(jdb.contains(&bar));
assert!(jdb.contains(&baz));
2016-03-13 18:07:10 +01:00
let foo = jdb.insert(b"foo");
jdb.remove(&baz);
jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap();
assert!(jdb.contains(&foo));
assert!(!jdb.contains(&bar));
assert!(jdb.contains(&baz));
2016-03-13 18:07:10 +01:00
jdb.remove(&foo);
jdb.commit_batch(3, &b"3".sha3(), Some((2, b"2".sha3()))).unwrap();
assert!(jdb.contains(&foo));
assert!(!jdb.contains(&bar));
assert!(!jdb.contains(&baz));
2016-03-13 18:07:10 +01:00
jdb.commit_batch(4, &b"4".sha3(), Some((3, b"3".sha3()))).unwrap();
assert!(!jdb.contains(&foo));
assert!(!jdb.contains(&bar));
assert!(!jdb.contains(&baz));
2016-03-13 18:07:10 +01:00
}
#[test]
fn fork() {
// history is 1
let mut jdb = RefCountedDB::new_temp();
let foo = jdb.insert(b"foo");
let bar = jdb.insert(b"bar");
jdb.commit_batch(0, &b"0".sha3(), None).unwrap();
assert!(jdb.contains(&foo));
assert!(jdb.contains(&bar));
2016-03-13 18:07:10 +01:00
jdb.remove(&foo);
let baz = jdb.insert(b"baz");
jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap();
2016-03-13 18:07:10 +01:00
jdb.remove(&bar);
jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap();
2016-03-13 18:07:10 +01:00
assert!(jdb.contains(&foo));
assert!(jdb.contains(&bar));
assert!(jdb.contains(&baz));
2016-03-13 18:07:10 +01:00
jdb.commit_batch(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap();
assert!(jdb.contains(&foo));
assert!(!jdb.contains(&baz));
assert!(!jdb.contains(&bar));
2016-03-13 18:07:10 +01:00
}
#[test]
fn inject() {
let mut jdb = RefCountedDB::new_temp();
let key = jdb.insert(b"dog");
jdb.inject_batch().unwrap();
2016-10-26 13:53:47 +02:00
assert_eq!(jdb.get(&key).unwrap(), DBValue::from_slice(b"dog"));
jdb.remove(&key);
jdb.inject_batch().unwrap();
assert!(jdb.get(&key).is_none());
}
2016-03-13 18:07:10 +01:00
}