mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2025-01-10 21:11:22 +00:00
bcdd960ab1
## Proposed Changes Reduce post-merge disk usage by not storing finalized execution payloads in Lighthouse's database. ⚠️ **This is achieved in a backwards-incompatible way for networks that have already merged** ⚠️. Kiln users and shadow fork enjoyers will be unable to downgrade after running the code from this PR. The upgrade migration may take several minutes to run, and can't be aborted after it begins. The main changes are: - New column in the database called `ExecPayload`, keyed by beacon block root. - The `BeaconBlock` column now stores blinded blocks only. - Lots of places that previously used full blocks now use blinded blocks, e.g. analytics APIs, block replay in the DB, etc. - On finalization: - `prune_abanonded_forks` deletes non-canonical payloads whilst deleting non-canonical blocks. - `migrate_db` deletes finalized canonical payloads whilst deleting finalized states. - Conversions between blinded and full blocks are implemented in a compositional way, duplicating some work from Sean's PR #3134. - The execution layer has a new `get_payload_by_block_hash` method that reconstructs a payload using the EE's `eth_getBlockByHash` call. - I've tested manually that it works on Kiln, using Geth and Nethermind. - This isn't necessarily the most efficient method, and new engine APIs are being discussed to improve this: https://github.com/ethereum/execution-apis/pull/146. - We're depending on the `ethers` master branch, due to lots of recent changes. We're also using a workaround for https://github.com/gakonst/ethers-rs/issues/1134. - Payload reconstruction is used in the HTTP API via `BeaconChain::get_block`, which is now `async`. Due to the `async` fn, the `blocking_json` wrapper has been removed. - Payload reconstruction is used in network RPC to serve blocks-by-{root,range} responses. Here the `async` adjustment is messier, although I think I've managed to come up with a reasonable compromise: the handlers take the `SendOnDrop` by value so that they can drop it on _task completion_ (after the `fn` returns). Still, this is introducing disk reads onto core executor threads, which may have a negative performance impact (thoughts appreciated). ## Additional Info - [x] For performance it would be great to remove the cloning of full blocks when converting them to blinded blocks to write to disk. I'm going to experiment with a `put_block` API that takes the block by value, breaks it into a blinded block and a payload, stores the blinded block, and then re-assembles the full block for the caller. - [x] We should measure the latency of blocks-by-root and blocks-by-range responses. - [x] We should add integration tests that stress the payload reconstruction (basic tests done, issue for more extensive tests: https://github.com/sigp/lighthouse/issues/3159) - [x] We should (manually) test the schema v9 migration from several prior versions, particularly as blocks have changed on disk and some migrations rely on being able to load blocks. Co-authored-by: Paul Hauner <paul@paulhauner.com>
271 lines
8.3 KiB
Rust
271 lines
8.3 KiB
Rust
use super::*;
|
|
use crate::hot_cold_store::HotColdDBError;
|
|
use crate::metrics;
|
|
use db_key::Key;
|
|
use leveldb::compaction::Compaction;
|
|
use leveldb::database::batch::{Batch, Writebatch};
|
|
use leveldb::database::kv::KV;
|
|
use leveldb::database::Database;
|
|
use leveldb::error::Error as LevelDBError;
|
|
use leveldb::iterator::{Iterable, KeyIterator, LevelDBIterator};
|
|
use leveldb::options::{Options, ReadOptions, WriteOptions};
|
|
use parking_lot::{Mutex, MutexGuard};
|
|
use std::marker::PhantomData;
|
|
use std::path::Path;
|
|
|
|
/// A wrapped leveldb database.
|
|
pub struct LevelDB<E: EthSpec> {
|
|
db: Database<BytesKey>,
|
|
/// A mutex to synchronise sensitive read-write transactions.
|
|
transaction_mutex: Mutex<()>,
|
|
_phantom: PhantomData<E>,
|
|
}
|
|
|
|
impl<E: EthSpec> LevelDB<E> {
|
|
/// Open a database at `path`, creating a new database if one does not already exist.
|
|
pub fn open(path: &Path) -> Result<Self, Error> {
|
|
let mut options = Options::new();
|
|
|
|
options.create_if_missing = true;
|
|
|
|
let db = Database::open(path, options)?;
|
|
let transaction_mutex = Mutex::new(());
|
|
|
|
Ok(Self {
|
|
db,
|
|
transaction_mutex,
|
|
_phantom: PhantomData,
|
|
})
|
|
}
|
|
|
|
fn read_options(&self) -> ReadOptions<BytesKey> {
|
|
ReadOptions::new()
|
|
}
|
|
|
|
fn write_options(&self) -> WriteOptions {
|
|
WriteOptions::new()
|
|
}
|
|
|
|
fn write_options_sync(&self) -> WriteOptions {
|
|
let mut opts = WriteOptions::new();
|
|
opts.sync = true;
|
|
opts
|
|
}
|
|
|
|
fn put_bytes_with_options(
|
|
&self,
|
|
col: &str,
|
|
key: &[u8],
|
|
val: &[u8],
|
|
opts: WriteOptions,
|
|
) -> Result<(), Error> {
|
|
let column_key = get_key_for_col(col, key);
|
|
|
|
metrics::inc_counter(&metrics::DISK_DB_WRITE_COUNT);
|
|
metrics::inc_counter_by(&metrics::DISK_DB_WRITE_BYTES, val.len() as u64);
|
|
let timer = metrics::start_timer(&metrics::DISK_DB_WRITE_TIMES);
|
|
|
|
self.db
|
|
.put(opts, BytesKey::from_vec(column_key), val)
|
|
.map_err(Into::into)
|
|
.map(|()| {
|
|
metrics::stop_timer(timer);
|
|
})
|
|
}
|
|
|
|
pub fn keys_iter(&self) -> KeyIterator<BytesKey> {
|
|
self.db.keys_iter(self.read_options())
|
|
}
|
|
}
|
|
|
|
impl<E: EthSpec> KeyValueStore<E> for LevelDB<E> {
|
|
/// Store some `value` in `column`, indexed with `key`.
|
|
fn put_bytes(&self, col: &str, key: &[u8], val: &[u8]) -> Result<(), Error> {
|
|
self.put_bytes_with_options(col, key, val, self.write_options())
|
|
}
|
|
|
|
fn put_bytes_sync(&self, col: &str, key: &[u8], val: &[u8]) -> Result<(), Error> {
|
|
self.put_bytes_with_options(col, key, val, self.write_options_sync())
|
|
}
|
|
|
|
fn sync(&self) -> Result<(), Error> {
|
|
self.put_bytes_sync("sync", b"sync", b"sync")
|
|
}
|
|
|
|
/// Retrieve some bytes in `column` with `key`.
|
|
fn get_bytes(&self, col: &str, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
|
|
let column_key = get_key_for_col(col, key);
|
|
|
|
metrics::inc_counter(&metrics::DISK_DB_READ_COUNT);
|
|
let timer = metrics::start_timer(&metrics::DISK_DB_READ_TIMES);
|
|
|
|
self.db
|
|
.get(self.read_options(), BytesKey::from_vec(column_key))
|
|
.map_err(Into::into)
|
|
.map(|opt| {
|
|
opt.map(|bytes| {
|
|
metrics::inc_counter_by(&metrics::DISK_DB_READ_BYTES, bytes.len() as u64);
|
|
metrics::stop_timer(timer);
|
|
bytes
|
|
})
|
|
})
|
|
}
|
|
|
|
/// Return `true` if `key` exists in `column`.
|
|
fn key_exists(&self, col: &str, key: &[u8]) -> Result<bool, Error> {
|
|
let column_key = get_key_for_col(col, key);
|
|
|
|
metrics::inc_counter(&metrics::DISK_DB_EXISTS_COUNT);
|
|
|
|
self.db
|
|
.get(self.read_options(), BytesKey::from_vec(column_key))
|
|
.map_err(Into::into)
|
|
.map(|val| val.is_some())
|
|
}
|
|
|
|
/// Removes `key` from `column`.
|
|
fn key_delete(&self, col: &str, key: &[u8]) -> Result<(), Error> {
|
|
let column_key = get_key_for_col(col, key);
|
|
|
|
metrics::inc_counter(&metrics::DISK_DB_DELETE_COUNT);
|
|
|
|
self.db
|
|
.delete(self.write_options(), BytesKey::from_vec(column_key))
|
|
.map_err(Into::into)
|
|
}
|
|
|
|
fn do_atomically(&self, ops_batch: Vec<KeyValueStoreOp>) -> Result<(), Error> {
|
|
let mut leveldb_batch = Writebatch::new();
|
|
for op in ops_batch {
|
|
match op {
|
|
KeyValueStoreOp::PutKeyValue(key, value) => {
|
|
leveldb_batch.put(BytesKey::from_vec(key), &value);
|
|
}
|
|
|
|
KeyValueStoreOp::DeleteKey(key) => {
|
|
leveldb_batch.delete(BytesKey::from_vec(key));
|
|
}
|
|
}
|
|
}
|
|
self.db.write(self.write_options(), &leveldb_batch)?;
|
|
Ok(())
|
|
}
|
|
|
|
fn begin_rw_transaction(&self) -> MutexGuard<()> {
|
|
self.transaction_mutex.lock()
|
|
}
|
|
|
|
/// Compact all values in the states and states flag columns.
|
|
fn compact(&self) -> Result<(), Error> {
|
|
let endpoints = |column: DBColumn| {
|
|
(
|
|
BytesKey::from_vec(get_key_for_col(column.as_str(), Hash256::zero().as_bytes())),
|
|
BytesKey::from_vec(get_key_for_col(
|
|
column.as_str(),
|
|
Hash256::repeat_byte(0xff).as_bytes(),
|
|
)),
|
|
)
|
|
};
|
|
|
|
for (start_key, end_key) in vec![
|
|
endpoints(DBColumn::BeaconStateTemporary),
|
|
endpoints(DBColumn::BeaconState),
|
|
] {
|
|
self.db.compact(&start_key, &end_key);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Iterate through all keys and values in a particular column.
|
|
fn iter_column(&self, column: DBColumn) -> ColumnIter {
|
|
let start_key =
|
|
BytesKey::from_vec(get_key_for_col(column.into(), Hash256::zero().as_bytes()));
|
|
|
|
let iter = self.db.iter(self.read_options());
|
|
iter.seek(&start_key);
|
|
|
|
Box::new(
|
|
iter.take_while(move |(key, _)| key.matches_column(column))
|
|
.map(move |(bytes_key, value)| {
|
|
let key =
|
|
bytes_key
|
|
.remove_column(column)
|
|
.ok_or(HotColdDBError::IterationError {
|
|
unexpected_key: bytes_key,
|
|
})?;
|
|
Ok((key, value))
|
|
}),
|
|
)
|
|
}
|
|
|
|
/// Iterate through all keys and values in a particular column.
|
|
fn iter_column_keys(&self, column: DBColumn) -> ColumnKeyIter {
|
|
let start_key =
|
|
BytesKey::from_vec(get_key_for_col(column.into(), Hash256::zero().as_bytes()));
|
|
|
|
let iter = self.db.keys_iter(self.read_options());
|
|
iter.seek(&start_key);
|
|
|
|
Box::new(
|
|
iter.take_while(move |key| key.matches_column(column))
|
|
.map(move |bytes_key| {
|
|
let key =
|
|
bytes_key
|
|
.remove_column(column)
|
|
.ok_or(HotColdDBError::IterationError {
|
|
unexpected_key: bytes_key,
|
|
})?;
|
|
Ok(key)
|
|
}),
|
|
)
|
|
}
|
|
}
|
|
|
|
impl<E: EthSpec> ItemStore<E> for LevelDB<E> {}
|
|
|
|
/// Used for keying leveldb.
|
|
#[derive(Debug, PartialEq)]
|
|
pub struct BytesKey {
|
|
key: Vec<u8>,
|
|
}
|
|
|
|
impl Key for BytesKey {
|
|
fn from_u8(key: &[u8]) -> Self {
|
|
Self { key: key.to_vec() }
|
|
}
|
|
|
|
fn as_slice<T, F: Fn(&[u8]) -> T>(&self, f: F) -> T {
|
|
f(self.key.as_slice())
|
|
}
|
|
}
|
|
|
|
impl BytesKey {
|
|
/// Return `true` iff this `BytesKey` was created with the given `column`.
|
|
pub fn matches_column(&self, column: DBColumn) -> bool {
|
|
self.key.starts_with(column.as_bytes())
|
|
}
|
|
|
|
/// Remove the column from a key, returning its `Hash256` portion.
|
|
pub fn remove_column(&self, column: DBColumn) -> Option<Hash256> {
|
|
if self.matches_column(column) {
|
|
let subkey = &self.key[column.as_bytes().len()..];
|
|
if subkey.len() == 32 {
|
|
return Some(Hash256::from_slice(subkey));
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
pub fn from_vec(key: Vec<u8>) -> Self {
|
|
Self { key }
|
|
}
|
|
}
|
|
|
|
impl From<LevelDBError> for Error {
|
|
fn from(e: LevelDBError) -> Error {
|
|
Error::DBError {
|
|
message: format!("{:?}", e),
|
|
}
|
|
}
|
|
}
|