Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
framework to preserve optimistic_slot in blockstore (backport #25362) (
Browse files Browse the repository at this point in the history
…#26131)

* framework to preserve optimistic_slot in blockstore (#25362)

(cherry picked from commit 8caf0aa)

# Conflicts:
#	ledger/src/blockstore.rs
#	ledger/src/blockstore_db.rs
#	ledger/src/blockstore_metrics.rs

* merge

* merge

Co-authored-by: Jeff Biseda <[email protected]>
  • Loading branch information
mergify[bot] and jbiseda authored Jun 22, 2022
1 parent f7221e6 commit 8a9f596
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
8 changes: 8 additions & 0 deletions ledger/src/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ pub struct Blockstore {
block_height_cf: LedgerColumn<cf::BlockHeight>,
program_costs_cf: LedgerColumn<cf::ProgramCosts>,
bank_hash_cf: LedgerColumn<cf::BankHash>,
optimistic_slots_cf: LedgerColumn<cf::OptimisticSlots>,
last_root: RwLock<Slot>,
insert_shreds_lock: Mutex<()>,
new_shreds_signals: Mutex<Vec<Sender<bool>>>,
Expand Down Expand Up @@ -578,6 +579,7 @@ impl Blockstore {
let block_height_cf = db.column();
let program_costs_cf = db.column();
let bank_hash_cf = db.column();
let optimistic_slots_cf = db.column();

let db = Arc::new(db);

Expand Down Expand Up @@ -629,6 +631,7 @@ impl Blockstore {
block_height_cf,
program_costs_cf,
bank_hash_cf,
optimistic_slots_cf,
new_shreds_signals: Mutex::default(),
completed_slots_senders: Mutex::default(),
insert_shreds_lock: Mutex::<()>::default(),
Expand Down Expand Up @@ -1031,6 +1034,11 @@ impl Blockstore {
"program_costs",
column_options
));
self.submit_rocksdb_cf_metrics::<cf::OptimisticSlots>(rocksdb_metric_header!(
"blockstore_rocksdb_cfs",
"optimistic_slots",
column_options
));
}

/// Collects and reports [`BlockstoreRocksDbColumnFamilyMetrics`] for the
Expand Down
8 changes: 8 additions & 0 deletions ledger/src/blockstore/blockstore_purge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ impl Blockstore {
& self
.db
.delete_range_cf::<cf::BlockHeight>(&mut write_batch, from_slot, to_slot)
.is_ok()
& self
.db
.delete_range_cf::<cf::OptimisticSlots>(&mut write_batch, from_slot, to_slot)
.is_ok();
let mut w_active_transaction_status_index =
self.active_transaction_status_index.write().unwrap();
Expand Down Expand Up @@ -314,6 +318,10 @@ impl Blockstore {
&& self
.block_height_cf
.compact_range(from_slot, to_slot)
.unwrap_or(false)
&& self
.optimistic_slots_cf
.compact_range(from_slot, to_slot)
.unwrap_or(false);
compact_timer.stop();
if !result {
Expand Down
16 changes: 16 additions & 0 deletions ledger/src/blockstore_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ const PERF_SAMPLES_CF: &str = "perf_samples";
const BLOCK_HEIGHT_CF: &str = "block_height";
/// Column family for ProgramCosts
const PROGRAM_COSTS_CF: &str = "program_costs";
/// Column family for optimistic slots
const OPTIMISTIC_SLOTS_CF: &str = "optimistic_slots";

// 1 day is chosen for the same reasoning of DEFAULT_COMPACTION_SLOT_INTERVAL
const PERIODIC_COMPACTION_SECONDS: u64 = 60 * 60 * 24;
Expand Down Expand Up @@ -212,6 +214,10 @@ pub mod columns {
/// The program costs column
pub struct ProgramCosts;

#[derive(Debug)]
/// The optimistic slot column
pub struct OptimisticSlots;

// When adding a new column ...
// - Add struct below and implement `Column` and `ColumnName` traits
// - Add descriptor in Rocks::cf_descriptors() and name in Rocks::columns()
Expand Down Expand Up @@ -433,6 +439,7 @@ impl Rocks {
new_cf_descriptor::<PerfSamples>(options, oldest_slot),
new_cf_descriptor::<BlockHeight>(options, oldest_slot),
new_cf_descriptor::<ProgramCosts>(options, oldest_slot),
new_cf_descriptor::<OptimisticSlots>(options, oldest_slot),
]
}

Expand All @@ -459,6 +466,7 @@ impl Rocks {
PerfSamples::NAME,
BlockHeight::NAME,
ProgramCosts::NAME,
OptimisticSlots::NAME,
]
}

Expand Down Expand Up @@ -955,6 +963,14 @@ impl TypedColumn for columns::ErasureMeta {
type Type = blockstore_meta::ErasureMeta;
}

impl SlotColumn for columns::OptimisticSlots {}
impl ColumnName for columns::OptimisticSlots {
const NAME: &'static str = OPTIMISTIC_SLOTS_CF;
}
impl TypedColumn for columns::OptimisticSlots {
type Type = blockstore_meta::OptimisticSlotMetaVersioned;
}

#[derive(Debug, Clone)]
pub struct Database {
backend: Arc<Rocks>,
Expand Down
33 changes: 32 additions & 1 deletion ledger/src/blockstore_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use {
shred::{Shred, ShredType},
},
serde::{Deserialize, Deserializer, Serialize, Serializer},
solana_sdk::{clock::Slot, hash::Hash},
solana_sdk::{
clock::{Slot, UnixTimestamp},
hash::Hash,
},
std::{
collections::BTreeSet,
ops::{Range, RangeBounds},
Expand Down Expand Up @@ -324,6 +327,34 @@ pub struct ProgramCost {
pub cost: u64,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
pub struct OptimisticSlotMetaV0 {
pub hash: Hash,
pub timestamp: UnixTimestamp,
}

#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub enum OptimisticSlotMetaVersioned {
V0(OptimisticSlotMetaV0),
}

impl OptimisticSlotMetaVersioned {
pub fn new(hash: Hash, timestamp: UnixTimestamp) -> Self {
OptimisticSlotMetaVersioned::V0(OptimisticSlotMetaV0 { hash, timestamp })
}

pub fn hash(&self) -> Hash {
match self {
OptimisticSlotMetaVersioned::V0(meta) => meta.hash,
}
}

pub fn timestamp(&self) -> UnixTimestamp {
match self {
OptimisticSlotMetaVersioned::V0(meta) => meta.timestamp,
}
}
}
#[cfg(test)]
mod test {
use {
Expand Down

0 comments on commit 8a9f596

Please sign in to comment.