Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database hotswap #242

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions storage/blockchain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,18 @@ For examples of the higher-level APIs, see:
```rust
use cuprate_blockchain::{
cuprate_database::{
ConcreteEnv,
Env, EnvInner,
DatabaseRo, DatabaseRw, TxRo, TxRw,
},
config::ConfigBuilder,
tables::{Tables, TablesMut, OpenTables},
};

#[cfg(feature = "heed")]
use cuprate_blockchain::cuprate_database::HeedEnv as ConcreteEnv;
#[cfg(all(feature = "redb", not(feature = "heed")))]
use cuprate_blockchain::cuprate_database::RedbEnv as ConcreteEnv;

# fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a configuration for the database environment.
let tmp_dir = tempfile::tempdir()?;
Expand All @@ -83,7 +87,7 @@ let config = ConfigBuilder::new()
.build();

// Initialize the database environment.
let env = cuprate_blockchain::open(config)?;
let env = cuprate_blockchain::open::<ConcreteEnv>(config)?;

// Open up a transaction + tables for writing.
let env_inner = env.env_inner();
Expand Down
22 changes: 11 additions & 11 deletions storage/blockchain/src/config/backend.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
//! SOMEDAY

//---------------------------------------------------------------------------------------------------- Import
use std::{
borrow::Cow,
num::NonZeroUsize,
path::{Path, PathBuf},
};
//use std::{
// borrow::Cow,
// num::NonZeroUsize,
// path::{Path, PathBuf},
//};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use cuprate_helper::fs::cuprate_blockchain_dir;
//use cuprate_helper::fs::cuprate_blockchain_dir;

use crate::{
config::{ReaderThreads, SyncMode},
constants::DATABASE_DATA_FILENAME,
resize::ResizeAlgorithm,
};
//use crate::{
// config::{ReaderThreads, SyncMode},
// constants::DATABASE_DATA_FILENAME,
// resize::ResizeAlgorithm,
//};

//---------------------------------------------------------------------------------------------------- Backend
/// SOMEDAY: allow runtime hot-swappable backends.
Expand Down
6 changes: 5 additions & 1 deletion storage/blockchain/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use cuprate_database::{config::SyncMode, resize::ResizeAlgorithm};
use cuprate_helper::fs::cuprate_blockchain_dir;

use crate::config::ReaderThreads;
use crate::config::{Backend, ReaderThreads};

//---------------------------------------------------------------------------------------------------- ConfigBuilder
/// Builder for [`Config`].
Expand Down Expand Up @@ -65,6 +65,7 @@ impl ConfigBuilder {
.build();

Config {
backend: Backend::default(),
db_config,
reader_threads,
}
Expand Down Expand Up @@ -149,6 +150,9 @@ impl Default for ConfigBuilder {
#[derive(Debug, Clone, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Config {
/// The database backend.
pub backend: Backend,

/// The database configuration.
pub db_config: cuprate_database::config::Config,

Expand Down
12 changes: 10 additions & 2 deletions storage/blockchain/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
//! config::{ConfigBuilder, ReaderThreads},
//! };
//!
//!#[cfg(feature = "heed")]
//!use cuprate_blockchain::cuprate_database::HeedEnv as ConcreteEnv;
//!#[cfg(all(feature = "redb", not(feature = "heed")))]
//!use cuprate_blockchain::cuprate_database::RedbEnv as ConcreteEnv;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let tmp_dir = tempfile::tempdir()?;
//! let db_dir = tmp_dir.path().to_owned();
Expand All @@ -34,9 +39,9 @@
//! .build();
//!
//! // Start a database `service` using this configuration.
//! let (reader_handle, _) = cuprate_blockchain::service::init(config.clone())?;
//! let (_, _, env) = cuprate_blockchain::service::do_init::<ConcreteEnv>(config.clone())?;
//! // It's using the config we provided.
//! assert_eq!(reader_handle.env().config(), &config.db_config);
//! assert_eq!(env.config(), &config.db_config);
//! # Ok(()) }
//! ```

Expand All @@ -45,3 +50,6 @@ pub use config::{Config, ConfigBuilder};

mod reader_threads;
pub use reader_threads::ReaderThreads;

mod backend;
pub use backend::Backend;
6 changes: 3 additions & 3 deletions storage/blockchain/src/free.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! General free functions (related to the database).

//---------------------------------------------------------------------------------------------------- Import
use cuprate_database::{ConcreteEnv, Env, EnvInner, InitError, RuntimeError, TxRw};
use cuprate_database::{Env, EnvInner, InitError, RuntimeError, TxRw};

use crate::{config::Config, tables::OpenTables};

Expand All @@ -22,9 +22,9 @@ use crate::{config::Config, tables::OpenTables};
/// - A table could not be created/opened
#[cold]
#[inline(never)] // only called once
pub fn open(config: Config) -> Result<ConcreteEnv, InitError> {
pub fn open<E: Env>(config: Config) -> Result<E, InitError> {
// Attempt to open the database environment.
let env = <ConcreteEnv as Env>::open(config.db_config)?;
let env = E::open(config.db_config)?;

/// Convert runtime errors to init errors.
///
Expand Down
8 changes: 6 additions & 2 deletions storage/blockchain/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
//! use cuprate_test_utils::data::block_v16_tx0;
//! use cuprate_blockchain::{
//! cuprate_database::{
//! ConcreteEnv,
//! Env, EnvInner,
//! DatabaseRo, DatabaseRw, TxRo, TxRw,
//! },
Expand All @@ -66,6 +65,11 @@
//! ops::block::{add_block, pop_block},
//! };
//!
//! #[cfg(feature = "heed")]
//! use cuprate_blockchain::cuprate_database::HeedEnv as ConcreteEnv;
//! #[cfg(all(feature = "redb",not(feature = "heed")))]
//! use cuprate_blockchain::cuprate_database::RedbEnv as ConcreteEnv;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a configuration for the database environment.
//! let tmp_dir = tempfile::tempdir()?;
Expand All @@ -75,7 +79,7 @@
//! .build();
//!
//! // Initialize the database environment.
//! let env = cuprate_blockchain::open(config)?;
//! let env = cuprate_blockchain::open::<ConcreteEnv>(config)?;
//!
//! // Open up a transaction + tables for writing.
//! let env_inner = env.env_inner();
Expand Down
32 changes: 26 additions & 6 deletions storage/blockchain/src/service/free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
//---------------------------------------------------------------------------------------------------- Import
use std::sync::Arc;

use cuprate_database::InitError;
use cuprate_database::{Env, InitError};

use crate::{
config::Config,
config::{Backend, Config},
service::{DatabaseReadHandle, DatabaseWriteHandle},
};

//---------------------------------------------------------------------------------------------------- Init
#[allow(unreachable_patterns)]
#[cold]
#[inline(never)] // Only called once (?)
#[inline(never)] // Only called once
/// Initialize a database & thread-pool, and return a read/write handle to it.
///
/// Once the returned handles are [`Drop::drop`]ed, the reader
Expand All @@ -21,18 +22,37 @@ use crate::{
/// # Errors
/// This will forward the error if [`crate::open`] failed.
pub fn init(config: Config) -> Result<(DatabaseReadHandle, DatabaseWriteHandle), InitError> {
let (reader, writer);

match config.backend {
#[cfg(feature = "heed")]
Backend::Heed => (reader, writer, _) = do_init::<cuprate_database::HeedEnv>(config)?,
#[cfg(feature = "redb")]
Backend::Redb => (reader, writer, _) = do_init::<cuprate_database::RedbEnv>(config)?,
_ => panic!("Selected database backend not available in this build."),
};

Ok((reader, writer))
}

#[cold]
#[inline(never)]
/// Like [`init()`], but generic over database backend and returning the database environment too.
/// Only exported for use in tests.
pub fn do_init<E: Env + Send + Sync + 'static>(config: Config) -> Result<(DatabaseReadHandle, DatabaseWriteHandle, Arc<E>), InitError> {
let reader_threads = config.reader_threads;

// Initialize the database itself.
let db = Arc::new(crate::open(config)?);
let db = Arc::new(crate::open::<E>(config)?);

// Spawn the Reader thread pool and Writer.
let readers = DatabaseReadHandle::init(&db, reader_threads);
let writer = DatabaseWriteHandle::init(db);
let writer = DatabaseWriteHandle::init(Arc::clone(&db));

Ok((readers, writer))
Ok((readers, writer, db))
}


//---------------------------------------------------------------------------------------------------- Compact history
/// Given a position in the compact history, returns the height offset that should be in that position.
///
Expand Down
1 change: 1 addition & 0 deletions storage/blockchain/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub use write::DatabaseWriteHandle;

mod free;
pub use free::init;
pub use free::do_init;

// Internal type aliases for `service`.
mod types;
Expand Down
Loading
Loading