ledger path reform: use Path/PathBuf instead of strings (#5344)

This commit is contained in:
Michael Vines
2019-07-30 15:53:41 -07:00
committed by GitHub
parent 294d9288d2
commit c78db6a94b
15 changed files with 76 additions and 54 deletions

View File

@ -27,6 +27,7 @@ use std::cell::RefCell;
use std::cmp;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::mpsc::{sync_channel, Receiver, SyncSender, TrySendError};
use std::sync::{Arc, RwLock};
@ -113,14 +114,12 @@ pub const INDEX_CF: &str = "index";
impl Blocktree {
/// Opens a Ledger in directory, provides "infinite" window of blobs
pub fn open(ledger_path: &str) -> Result<Blocktree> {
use std::path::Path;
pub fn open(ledger_path: &Path) -> Result<Blocktree> {
fs::create_dir_all(&ledger_path)?;
let ledger_path = Path::new(&ledger_path).join(BLOCKTREE_DIRECTORY);
let blocktree_path = ledger_path.join(BLOCKTREE_DIRECTORY);
// Open the database
let db = Database::open(&ledger_path)?;
let db = Database::open(&blocktree_path)?;
let batch_processor = unsafe { Arc::new(RwLock::new(db.batch_processor())) };
@ -162,7 +161,7 @@ impl Blocktree {
}
pub fn open_with_signal(
ledger_path: &str,
ledger_path: &Path,
) -> Result<(Self, Receiver<bool>, CompletedSlotsReceiver)> {
let mut blocktree = Self::open(ledger_path)?;
let (signal_sender, signal_receiver) = sync_channel(1);
@ -174,11 +173,11 @@ impl Blocktree {
Ok((blocktree, signal_receiver, completed_slots_receiver))
}
pub fn destroy(ledger_path: &str) -> Result<()> {
// Database::destroy() fails is the path doesn't exist
pub fn destroy(ledger_path: &Path) -> Result<()> {
// Database::destroy() fails if the path doesn't exist
fs::create_dir_all(ledger_path)?;
let path = std::path::Path::new(ledger_path).join(BLOCKTREE_DIRECTORY);
Database::destroy(&path)
let blocktree_path = ledger_path.join(BLOCKTREE_DIRECTORY);
Database::destroy(&blocktree_path)
}
pub fn meta(&self, slot: u64) -> Result<Option<SlotMeta>> {
@ -1958,7 +1957,7 @@ fn slot_has_updates(slot_meta: &SlotMeta, slot_meta_backup: &Option<SlotMeta>) -
// Creates a new ledger with slot 0 full of ticks (and only ticks).
//
// Returns the blockhash that can be used to append entries with.
pub fn create_new_ledger(ledger_path: &str, genesis_block: &GenesisBlock) -> Result<Hash> {
pub fn create_new_ledger(ledger_path: &Path, genesis_block: &GenesisBlock) -> Result<Hash> {
let ticks_per_slot = genesis_block.ticks_per_slot;
Blocktree::destroy(ledger_path)?;
genesis_block.write(&ledger_path)?;
@ -1971,7 +1970,7 @@ pub fn create_new_ledger(ledger_path: &str, genesis_block: &GenesisBlock) -> Res
Ok(entries.last().unwrap().hash)
}
pub fn genesis<'a, I>(ledger_path: &str, keypair: &Keypair, entries: I) -> Result<()>
pub fn genesis<'a, I>(ledger_path: &Path, keypair: &Keypair, entries: I) -> Result<()>
where
I: IntoIterator<Item = &'a Entry>,
{
@ -2008,12 +2007,18 @@ macro_rules! get_tmp_ledger_path {
};
}
pub fn get_tmp_ledger_path(name: &str) -> String {
pub fn get_tmp_ledger_path(name: &str) -> PathBuf {
use std::env;
let out_dir = env::var("FARF_DIR").unwrap_or_else(|_| "farf".to_string());
let keypair = Keypair::new();
let path = format!("{}/ledger/{}-{}", out_dir, name, keypair.pubkey());
let path = [
out_dir,
"ledger".to_string(),
format!("{}-{}", name, keypair.pubkey()),
]
.iter()
.collect();
// whack any possible collision
let _ignored = fs::remove_dir_all(&path);
@ -2032,7 +2037,7 @@ macro_rules! create_new_tmp_ledger {
//
// Note: like `create_new_ledger` the returned ledger will have slot 0 full of ticks (and only
// ticks)
pub fn create_new_tmp_ledger(name: &str, genesis_block: &GenesisBlock) -> (String, Hash) {
pub fn create_new_tmp_ledger(name: &str, genesis_block: &GenesisBlock) -> (PathBuf, Hash) {
let ledger_path = get_tmp_ledger_path(name);
let blockhash = create_new_ledger(&ledger_path, genesis_block).unwrap();
(ledger_path, blockhash)
@ -2045,7 +2050,7 @@ macro_rules! tmp_copy_blocktree {
};
}
pub fn tmp_copy_blocktree(from: &str, name: &str) -> String {
pub fn tmp_copy_blocktree(from: &Path, name: &str) -> PathBuf {
let path = get_tmp_ledger_path(name);
let blocktree = Blocktree::open(from).unwrap();

View File

@ -241,6 +241,7 @@ mod test {
use solana_sdk::hash::Hash;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
use std::path::Path;
use std::sync::atomic::AtomicBool;
use std::sync::mpsc::channel;
use std::sync::{Arc, RwLock};
@ -255,7 +256,7 @@ mod test {
fn setup_dummy_broadcast_service(
leader_pubkey: &Pubkey,
ledger_path: &str,
ledger_path: &Path,
entry_receiver: Receiver<WorkingBankEntries>,
) -> MockBroadcastStage {
// Make the database ledger

View File

@ -22,6 +22,7 @@ use solana_sdk::timing::{
NUM_CONSECUTIVE_LEADER_SLOTS,
};
use solana_sdk::transport::TransportError;
use std::path::Path;
use std::thread::sleep;
use std::time::Duration;
@ -94,7 +95,7 @@ pub fn fullnode_exit(entry_point_info: &ContactInfo, nodes: usize) {
}
}
pub fn verify_ledger_ticks(ledger_path: &str, ticks_per_slot: usize) {
pub fn verify_ledger_ticks(ledger_path: &Path, ticks_per_slot: usize) {
let ledger = Blocktree::open(ledger_path).unwrap();
let zeroth_slot = ledger.get_slot_entries(0, 0, None).unwrap();
let last_id = zeroth_slot.last().unwrap().hash;

View File

@ -334,6 +334,7 @@ pub mod test {
use solana_sdk::signature::Signable;
use solana_sdk::signature::{Keypair, KeypairUtil};
use std::borrow::Borrow;
use std::path::Path;
/// Specifies the contents of a 16-data-blob and 4-coding-blob erasure set
/// Exists to be passed to `generate_blocktree_with_coding`
@ -748,7 +749,7 @@ pub mod test {
/// Genarates a ledger according to the given specs.
/// Blocktree should have correct SlotMeta and ErasureMeta and so on but will not have done any
/// possible recovery.
pub fn generate_blocktree_with_coding(ledger_path: &str, specs: &[SlotSpec]) -> Blocktree {
pub fn generate_blocktree_with_coding(ledger_path: &Path, specs: &[SlotSpec]) -> Blocktree {
let blocktree = Blocktree::open(ledger_path).unwrap();
let model = generate_ledger_model(specs);

View File

@ -27,6 +27,7 @@ use solana_vote_api::vote_state::VoteState;
use std::collections::HashMap;
use std::fs::remove_dir_all;
use std::io::{Error, ErrorKind, Result};
use std::path::PathBuf;
use std::sync::Arc;
use solana_librapay_api::librapay_transaction;
@ -36,17 +37,17 @@ pub struct ValidatorInfo {
pub keypair: Arc<Keypair>,
pub voting_keypair: Arc<Keypair>,
pub storage_keypair: Arc<Keypair>,
pub ledger_path: String,
pub ledger_path: PathBuf,
pub contact_info: ContactInfo,
}
pub struct ReplicatorInfo {
pub replicator_storage_pubkey: Pubkey,
pub ledger_path: String,
pub ledger_path: PathBuf,
}
impl ReplicatorInfo {
fn new(storage_pubkey: Pubkey, ledger_path: String) -> Self {
fn new(storage_pubkey: Pubkey, ledger_path: PathBuf) -> Self {
Self {
replicator_storage_pubkey: storage_pubkey,
ledger_path,
@ -395,7 +396,7 @@ impl LocalCluster {
.chain(self.replicator_infos.values().map(|info| &info.ledger_path))
{
remove_dir_all(&ledger_path)
.unwrap_or_else(|_| panic!("Unable to remove {}", ledger_path));
.unwrap_or_else(|_| panic!("Unable to remove {:?}", ledger_path));
}
}

View File

@ -62,7 +62,7 @@ pub struct Replicator {
struct ReplicatorMeta {
slot: u64,
slots_per_segment: u64,
ledger_path: String,
ledger_path: PathBuf,
signature: Signature,
ledger_data_file_encrypted: PathBuf,
sampling_offsets: Vec<u64>,
@ -200,7 +200,7 @@ impl Replicator {
/// * `keypair` - Keypair for this replicator
#[allow(clippy::new_ret_no_self)]
pub fn new(
ledger_path: &str,
ledger_path: &Path,
node: Node,
cluster_entrypoint: ContactInfo,
keypair: Arc<Keypair>,
@ -263,7 +263,7 @@ impl Replicator {
let exit = exit.clone();
let node_info = node.info.clone();
let mut meta = ReplicatorMeta {
ledger_path: ledger_path.to_string(),
ledger_path: ledger_path.to_path_buf(),
..ReplicatorMeta::default()
};
spawn(move || {
@ -516,8 +516,7 @@ impl Replicator {
}
fn encrypt_ledger(meta: &mut ReplicatorMeta, blocktree: &Arc<Blocktree>) -> Result<()> {
let ledger_path = Path::new(&meta.ledger_path);
meta.ledger_data_file_encrypted = ledger_path.join(ENCRYPTED_FILENAME);
meta.ledger_data_file_encrypted = meta.ledger_path.join(ENCRYPTED_FILENAME);
{
let mut ivec = [0u8; 64];

View File

@ -26,6 +26,7 @@ use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::timing::{timestamp, DEFAULT_SLOTS_PER_TURN};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::Receiver;
use std::sync::{Arc, Mutex, RwLock};
@ -79,7 +80,7 @@ impl Validator {
pub fn new(
mut node: Node,
keypair: &Arc<Keypair>,
ledger_path: &str,
ledger_path: &Path,
vote_account: &Pubkey,
voting_keypair: &Arc<Keypair>,
storage_keypair: &Arc<Keypair>,
@ -340,7 +341,7 @@ fn get_bank_forks(
}
pub fn new_banks_from_blocktree(
blocktree_path: &str,
blocktree_path: &Path,
account_paths: Option<String>,
snapshot_path: Option<String>,
verify_ledger: bool,
@ -401,7 +402,7 @@ impl Service for Validator {
}
}
pub fn new_validator_for_tests() -> (Validator, ContactInfo, Keypair, String) {
pub fn new_validator_for_tests() -> (Validator, ContactInfo, Keypair, PathBuf) {
use crate::blocktree::create_new_tmp_ledger;
use crate::genesis_utils::{create_genesis_block_with_leader, GenesisBlockInfo};