backport new column families from master to 1.6 (#18743)

* backporting bank_hash and program_costs column families from master to 1.6 for rocksdb backward compatibility

* missed a line to allow dead code

* include code for purge
This commit is contained in:
Tao Zhu
2021-07-17 11:59:42 -05:00
committed by GitHub
parent d7b381c90c
commit 1017c4851a
4 changed files with 118 additions and 11 deletions

View File

@ -122,6 +122,10 @@ pub struct BlockstoreSignals {
} }
// ledger window // ledger window
//
// NOTE: allowing dead_code only because stubbing bank_hash_cf and program_cost_cf
// to 1.6 for rocksdb backward compatibility
#[allow(dead_code)]
pub struct Blockstore { pub struct Blockstore {
ledger_path: PathBuf, ledger_path: PathBuf,
db: Arc<Database>, db: Arc<Database>,
@ -141,6 +145,8 @@ pub struct Blockstore {
blocktime_cf: LedgerColumn<cf::Blocktime>, blocktime_cf: LedgerColumn<cf::Blocktime>,
perf_samples_cf: LedgerColumn<cf::PerfSamples>, perf_samples_cf: LedgerColumn<cf::PerfSamples>,
block_height_cf: LedgerColumn<cf::BlockHeight>, block_height_cf: LedgerColumn<cf::BlockHeight>,
program_costs_cf: LedgerColumn<cf::ProgramCosts>,
bank_hash_cf: LedgerColumn<cf::BankHash>,
last_root: Arc<RwLock<Slot>>, last_root: Arc<RwLock<Slot>>,
insert_shreds_lock: Arc<Mutex<()>>, insert_shreds_lock: Arc<Mutex<()>>,
pub new_shreds_signals: Vec<SyncSender<bool>>, pub new_shreds_signals: Vec<SyncSender<bool>>,
@ -312,6 +318,8 @@ impl Blockstore {
let blocktime_cf = db.column(); let blocktime_cf = db.column();
let perf_samples_cf = db.column(); let perf_samples_cf = db.column();
let block_height_cf = db.column(); let block_height_cf = db.column();
let program_costs_cf = db.column();
let bank_hash_cf = db.column();
let db = Arc::new(db); let db = Arc::new(db);
@ -360,6 +368,8 @@ impl Blockstore {
blocktime_cf, blocktime_cf,
perf_samples_cf, perf_samples_cf,
block_height_cf, block_height_cf,
program_costs_cf,
bank_hash_cf,
new_shreds_signals: vec![], new_shreds_signals: vec![],
completed_slots_senders: vec![], completed_slots_senders: vec![],
insert_shreds_lock: Arc::new(Mutex::new(())), insert_shreds_lock: Arc::new(Mutex::new(())),

View File

@ -135,6 +135,10 @@ impl Blockstore {
.db .db
.delete_range_cf::<cf::SlotMeta>(&mut write_batch, from_slot, to_slot) .delete_range_cf::<cf::SlotMeta>(&mut write_batch, from_slot, to_slot)
.is_ok() .is_ok()
& self
.db
.delete_range_cf::<cf::BankHash>(&mut write_batch, from_slot, to_slot)
.is_ok()
& self & self
.db .db
.delete_range_cf::<cf::Root>(&mut write_batch, from_slot, to_slot) .delete_range_cf::<cf::Root>(&mut write_batch, from_slot, to_slot)
@ -264,6 +268,10 @@ impl Blockstore {
.orphans_cf .orphans_cf
.compact_range(from_slot, to_slot) .compact_range(from_slot, to_slot)
.unwrap_or(false) .unwrap_or(false)
&& self
.bank_hash_cf
.compact_range(from_slot, to_slot)
.unwrap_or(false)
&& self && self
.index_cf .index_cf
.compact_range(from_slot, to_slot) .compact_range(from_slot, to_slot)

View File

@ -47,6 +47,8 @@ const DUPLICATE_SLOTS_CF: &str = "duplicate_slots";
const ERASURE_META_CF: &str = "erasure_meta"; const ERASURE_META_CF: &str = "erasure_meta";
// Column family for orphans data // Column family for orphans data
const ORPHANS_CF: &str = "orphans"; const ORPHANS_CF: &str = "orphans";
/// Column family for bank hashes
const BANK_HASH_CF: &str = "bank_hashes";
// Column family for root data // Column family for root data
const ROOT_CF: &str = "root"; const ROOT_CF: &str = "root";
/// Column family for indexes /// Column family for indexes
@ -71,6 +73,8 @@ const BLOCKTIME_CF: &str = "blocktime";
const PERF_SAMPLES_CF: &str = "perf_samples"; const PERF_SAMPLES_CF: &str = "perf_samples";
/// Column family for BlockHeight /// Column family for BlockHeight
const BLOCK_HEIGHT_CF: &str = "block_height"; const BLOCK_HEIGHT_CF: &str = "block_height";
/// Column family for ProgramCosts
const PROGRAM_COSTS_CF: &str = "program_costs";
// 1 day is chosen for the same reasoning of DEFAULT_COMPACTION_SLOT_INTERVAL // 1 day is chosen for the same reasoning of DEFAULT_COMPACTION_SLOT_INTERVAL
const PERIODIC_COMPACTION_SECONDS: u64 = 60 * 60 * 24; const PERIODIC_COMPACTION_SECONDS: u64 = 60 * 60 * 24;
@ -131,6 +135,10 @@ pub mod columns {
/// The erasure meta column /// The erasure meta column
pub struct ErasureMeta; pub struct ErasureMeta;
#[derive(Debug)]
/// The bank hash column
pub struct BankHash;
#[derive(Debug)] #[derive(Debug)]
/// The root column /// The root column
pub struct Root; pub struct Root;
@ -174,6 +182,10 @@ pub mod columns {
#[derive(Debug)] #[derive(Debug)]
/// The block height column /// The block height column
pub struct BlockHeight; pub struct BlockHeight;
#[derive(Debug)]
// The program costs column
pub struct ProgramCosts;
} }
pub enum AccessType { pub enum AccessType {
@ -256,11 +268,7 @@ impl Rocks {
access_type: AccessType, access_type: AccessType,
recovery_mode: Option<BlockstoreRecoveryMode>, recovery_mode: Option<BlockstoreRecoveryMode>,
) -> Result<Rocks> { ) -> Result<Rocks> {
use columns::{ use columns::*;
AddressSignatures, BlockHeight, Blocktime, DeadSlots, DuplicateSlots, ErasureMeta,
Index, Orphans, PerfSamples, Rewards, Root, ShredCode, ShredData, SlotMeta,
TransactionStatus, TransactionStatusIndex,
};
fs::create_dir_all(&path)?; fs::create_dir_all(&path)?;
@ -296,6 +304,10 @@ impl Rocks {
Orphans::NAME, Orphans::NAME,
get_cf_options::<Orphans>(&access_type, &oldest_slot), get_cf_options::<Orphans>(&access_type, &oldest_slot),
); );
let bank_hash_cf_descriptor = ColumnFamilyDescriptor::new(
BankHash::NAME,
get_cf_options::<BankHash>(&access_type, &oldest_slot),
);
let root_cf_descriptor = ColumnFamilyDescriptor::new( let root_cf_descriptor = ColumnFamilyDescriptor::new(
Root::NAME, Root::NAME,
get_cf_options::<Root>(&access_type, &oldest_slot), get_cf_options::<Root>(&access_type, &oldest_slot),
@ -340,6 +352,10 @@ impl Rocks {
BlockHeight::NAME, BlockHeight::NAME,
get_cf_options::<BlockHeight>(&access_type, &oldest_slot), get_cf_options::<BlockHeight>(&access_type, &oldest_slot),
); );
let program_costs_cf_descriptor = ColumnFamilyDescriptor::new(
ProgramCosts::NAME,
get_cf_options::<ProgramCosts>(&access_type, &oldest_slot),
);
// Don't forget to add to both run_purge_with_stats() and // Don't forget to add to both run_purge_with_stats() and
// compact_storage() in ledger/src/blockstore/blockstore_purge.rs!! // compact_storage() in ledger/src/blockstore/blockstore_purge.rs!!
@ -349,6 +365,7 @@ impl Rocks {
(DuplicateSlots::NAME, duplicate_slots_cf_descriptor), (DuplicateSlots::NAME, duplicate_slots_cf_descriptor),
(ErasureMeta::NAME, erasure_meta_cf_descriptor), (ErasureMeta::NAME, erasure_meta_cf_descriptor),
(Orphans::NAME, orphans_cf_descriptor), (Orphans::NAME, orphans_cf_descriptor),
(BankHash::NAME, bank_hash_cf_descriptor),
(Root::NAME, root_cf_descriptor), (Root::NAME, root_cf_descriptor),
(Index::NAME, index_cf_descriptor), (Index::NAME, index_cf_descriptor),
(ShredData::NAME, shred_data_cf_descriptor), (ShredData::NAME, shred_data_cf_descriptor),
@ -363,6 +380,7 @@ impl Rocks {
(Blocktime::NAME, blocktime_cf_descriptor), (Blocktime::NAME, blocktime_cf_descriptor),
(PerfSamples::NAME, perf_samples_cf_descriptor), (PerfSamples::NAME, perf_samples_cf_descriptor),
(BlockHeight::NAME, block_height_cf_descriptor), (BlockHeight::NAME, block_height_cf_descriptor),
(ProgramCosts::NAME, program_costs_cf_descriptor),
]; ];
let cf_names: Vec<_> = cfs.iter().map(|c| c.0).collect(); let cf_names: Vec<_> = cfs.iter().map(|c| c.0).collect();
@ -461,11 +479,7 @@ impl Rocks {
} }
fn columns(&self) -> Vec<&'static str> { fn columns(&self) -> Vec<&'static str> {
use columns::{ use columns::*;
AddressSignatures, BlockHeight, Blocktime, DeadSlots, DuplicateSlots, ErasureMeta,
Index, Orphans, PerfSamples, Rewards, Root, ShredCode, ShredData, SlotMeta,
TransactionStatus, TransactionStatusIndex,
};
vec![ vec![
ErasureMeta::NAME, ErasureMeta::NAME,
@ -473,6 +487,7 @@ impl Rocks {
DuplicateSlots::NAME, DuplicateSlots::NAME,
Index::NAME, Index::NAME,
Orphans::NAME, Orphans::NAME,
BankHash::NAME,
Root::NAME, Root::NAME,
SlotMeta::NAME, SlotMeta::NAME,
ShredData::NAME, ShredData::NAME,
@ -484,6 +499,7 @@ impl Rocks {
Blocktime::NAME, Blocktime::NAME,
PerfSamples::NAME, PerfSamples::NAME,
BlockHeight::NAME, BlockHeight::NAME,
ProgramCosts::NAME,
] ]
} }
@ -718,6 +734,14 @@ impl ColumnName for columns::TransactionStatusIndex {
const NAME: &'static str = TRANSACTION_STATUS_INDEX_CF; const NAME: &'static str = TRANSACTION_STATUS_INDEX_CF;
} }
impl SlotColumn for columns::BankHash {}
impl ColumnName for columns::BankHash {
const NAME: &'static str = BANK_HASH_CF;
}
impl TypedColumn for columns::BankHash {
type Type = blockstore_meta::FrozenHashVersioned;
}
impl SlotColumn for columns::Rewards {} impl SlotColumn for columns::Rewards {}
impl ColumnName for columns::Rewards { impl ColumnName for columns::Rewards {
const NAME: &'static str = REWARDS_CF; const NAME: &'static str = REWARDS_CF;
@ -750,6 +774,39 @@ impl TypedColumn for columns::BlockHeight {
type Type = u64; type Type = u64;
} }
impl ColumnName for columns::ProgramCosts {
const NAME: &'static str = PROGRAM_COSTS_CF;
}
impl TypedColumn for columns::ProgramCosts {
type Type = blockstore_meta::ProgramCost;
}
impl Column for columns::ProgramCosts {
type Index = Pubkey;
fn key(pubkey: Pubkey) -> Vec<u8> {
let mut key = vec![0; 32]; // size_of Pubkey
key[0..32].clone_from_slice(&pubkey.as_ref()[0..32]);
key
}
fn index(key: &[u8]) -> Self::Index {
Pubkey::new(&key[0..32])
}
fn primary_index(_index: Self::Index) -> u64 {
unimplemented!()
}
fn slot(_index: Self::Index) -> Slot {
unimplemented!()
}
#[allow(clippy::wrong_self_convention)]
fn as_index(_index: u64) -> Self::Index {
Pubkey::default()
}
}
impl Column for columns::ShredCode { impl Column for columns::ShredCode {
type Index = (u64, u64); type Index = (u64, u64);

View File

@ -1,6 +1,6 @@
use crate::erasure::ErasureConfig; use crate::erasure::ErasureConfig;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use solana_sdk::clock::Slot; use solana_sdk::{clock::Slot, hash::Hash};
use std::{collections::BTreeSet, ops::RangeBounds}; use std::{collections::BTreeSet, ops::RangeBounds};
#[derive(Clone, Debug, Default, Deserialize, Serialize, Eq, PartialEq)] #[derive(Clone, Debug, Default, Deserialize, Serialize, Eq, PartialEq)]
@ -75,6 +75,33 @@ pub enum ErasureMetaStatus {
StillNeed(usize), StillNeed(usize),
} }
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub enum FrozenHashVersioned {
Current(FrozenHashStatus),
}
impl FrozenHashVersioned {
pub fn frozen_hash(&self) -> Hash {
match self {
FrozenHashVersioned::Current(frozen_hash_status) => frozen_hash_status.frozen_hash,
}
}
pub fn is_duplicate_confirmed(&self) -> bool {
match self {
FrozenHashVersioned::Current(frozen_hash_status) => {
frozen_hash_status.is_duplicate_confirmed
}
}
}
}
#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct FrozenHashStatus {
pub frozen_hash: Hash,
pub is_duplicate_confirmed: bool,
}
impl Index { impl Index {
pub(crate) fn new(slot: Slot) -> Self { pub(crate) fn new(slot: Slot) -> Self {
Index { Index {
@ -249,6 +276,11 @@ pub struct PerfSample {
pub sample_period_secs: u16, pub sample_period_secs: u16,
} }
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
pub struct ProgramCost {
pub cost: u64,
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;