-
Notifications
You must be signed in to change notification settings - Fork 143
Support tiered data storage #692
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
Open
enigbe
wants to merge
4
commits into
lightningdevkit:main
Choose a base branch
from
enigbe:2025-10-tiered-data-storage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,7 @@ use crate::event::EventQueue; | |
| use crate::fee_estimator::OnchainFeeEstimator; | ||
| use crate::gossip::GossipSource; | ||
| use crate::io::sqlite_store::SqliteStore; | ||
| use crate::io::tier_store::TierStore; | ||
| use crate::io::utils::{ | ||
| read_all_objects, read_event_queue, read_external_pathfinding_scores_from_cache, | ||
| read_network_graph, read_node_metrics, read_output_sweeper, read_peer_info, read_scorer, | ||
|
|
@@ -154,6 +155,21 @@ impl std::fmt::Debug for LogWriterConfig { | |
| } | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| struct TierStoreConfig { | ||
| ephemeral: Option<Arc<DynStore>>, | ||
| backup_storage_dir_path: Option<PathBuf>, | ||
| } | ||
|
|
||
| impl std::fmt::Debug for TierStoreConfig { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("TierStoreConfig") | ||
| .field("ephemeral", &self.ephemeral.as_ref().map(|_| "Arc<DynStore>")) | ||
| .field("backup_storage_dir_path", &self.backup_storage_dir_path) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| /// An error encountered during building a [`Node`]. | ||
| /// | ||
| /// [`Node`]: crate::Node | ||
|
|
@@ -200,6 +216,11 @@ pub enum BuildError { | |
| AsyncPaymentsConfigMismatch, | ||
| /// An attempt to setup a DNS Resolver failed. | ||
| DNSResolverSetupFailed, | ||
| /// The configured backup storage path conflicts with the primary storage path. | ||
| /// | ||
| /// Backup storage must use a distinct local directory so that the primary and | ||
| /// backup stores do not point to the same SQLite database. | ||
| BackupStorePathConflict, | ||
| } | ||
|
|
||
| impl fmt::Display for BuildError { | ||
|
|
@@ -237,6 +258,12 @@ impl fmt::Display for BuildError { | |
| Self::DNSResolverSetupFailed => { | ||
| write!(f, "An attempt to setup a DNS resolver has failed.") | ||
| }, | ||
| Self::BackupStorePathConflict => { | ||
| write!( | ||
| f, | ||
| "The configured backup storage path conflicts with the primary storage path." | ||
| ) | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -289,6 +316,7 @@ pub struct NodeBuilder { | |
| liquidity_source_config: Option<LiquiditySourceConfig>, | ||
| log_writer_config: Option<LogWriterConfig>, | ||
| async_payments_role: Option<AsyncPaymentsRole>, | ||
| tier_store_config: Option<TierStoreConfig>, | ||
| runtime_handle: Option<tokio::runtime::Handle>, | ||
| pathfinding_scores_sync_config: Option<PathfindingScoresSyncConfig>, | ||
| recovery_mode: bool, | ||
|
|
@@ -307,6 +335,7 @@ impl NodeBuilder { | |
| let gossip_source_config = None; | ||
| let liquidity_source_config = None; | ||
| let log_writer_config = None; | ||
| let tier_store_config = None; | ||
| let runtime_handle = None; | ||
| let pathfinding_scores_sync_config = None; | ||
| let recovery_mode = false; | ||
|
|
@@ -316,6 +345,7 @@ impl NodeBuilder { | |
| gossip_source_config, | ||
| liquidity_source_config, | ||
| log_writer_config, | ||
| tier_store_config, | ||
| runtime_handle, | ||
| async_payments_role: None, | ||
| pathfinding_scores_sync_config, | ||
|
|
@@ -625,6 +655,42 @@ impl NodeBuilder { | |
| self | ||
| } | ||
|
|
||
| /// Configures a local SQLite backup store for disaster recovery. | ||
| /// | ||
| /// When building with tiered storage, a SQLite store will be created at the | ||
| /// given directory path using [`SQLITE_BACKUP_DB_FILE_NAME`] as its database | ||
| /// file name. It receives a second durable copy of data written to the | ||
| /// primary store. | ||
| /// | ||
| /// Writes and removals for primary-backed data only succeed once both the | ||
| /// primary and backup SQLite stores complete successfully. | ||
| /// | ||
| /// The configured path must point to a distinct local directory from the | ||
| /// primary storage path. If the backup path equals the primary storage path, | ||
| /// building will fail with [`BuildError::BackupStorePathConflict`]. | ||
| /// | ||
| /// If not set, durable data will be stored only in the primary store. | ||
| /// | ||
| /// [`SQLITE_BACKUP_DB_FILE_NAME`]: crate::io::sqlite_store::SQLITE_BACKUP_DB_FILE_NAME | ||
| pub fn set_backup_storage_dir_path(&mut self, backup_storage_dir_path: String) -> &mut Self { | ||
| let tier_store_config = self.tier_store_config.get_or_insert(TierStoreConfig::default()); | ||
| tier_store_config.backup_storage_dir_path = Some(backup_storage_dir_path.into()); | ||
| self | ||
| } | ||
|
|
||
| /// Configures the ephemeral store for non-critical, frequently-accessed data. | ||
| /// | ||
| /// When building with tiered storage, this store is used for ephemeral data like | ||
| /// the network graph and scorer data to reduce latency for reads. Data stored here | ||
| /// can be rebuilt if lost. | ||
| /// | ||
| /// If not set, non-critical data will be stored in the primary store. | ||
| pub fn set_ephemeral_store(&mut self, ephemeral_store: Arc<DynStore>) -> &mut Self { | ||
| let tier_store_config = self.tier_store_config.get_or_insert(TierStoreConfig::default()); | ||
| tier_store_config.ephemeral = Some(ephemeral_store); | ||
| self | ||
| } | ||
|
|
||
| /// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options | ||
| /// previously configured. | ||
| pub fn build(&self, node_entropy: NodeEntropy) -> Result<Node, BuildError> { | ||
|
|
@@ -780,11 +846,18 @@ impl NodeBuilder { | |
| } | ||
|
|
||
| /// Builds a [`Node`] instance according to the options previously configured. | ||
| /// | ||
| /// The provided `kv_store` will be used as the primary storage backend. Optionally, | ||
| /// an ephemeral store for frequently-accessed non-critical data (e.g., network graph, scorer) | ||
| /// and a local SQLite backup store for disaster recovery can be configured via | ||
| /// [`set_ephemeral_store`] and [`set_backup_storage_dir_path`]. | ||
| /// | ||
| /// [`set_ephemeral_store`]: Self::set_ephemeral_store | ||
| /// [`set_backup_storage_dir_path`]: Self::set_backup_storage_dir_path | ||
| pub fn build_with_store<S: SyncAndAsyncKVStore + Send + Sync + 'static>( | ||
| &self, node_entropy: NodeEntropy, kv_store: S, | ||
| ) -> Result<Node, BuildError> { | ||
| let logger = setup_logger(&self.log_writer_config, &self.config)?; | ||
|
|
||
| self.build_with_store_and_logger(node_entropy, kv_store, logger) | ||
| } | ||
|
|
||
|
|
@@ -800,6 +873,36 @@ impl NodeBuilder { | |
| })?) | ||
| }; | ||
|
|
||
| let ts_config = self.tier_store_config.as_ref(); | ||
| let primary_store = Arc::new(DynStoreWrapper(kv_store)); | ||
| let mut tier_store = TierStore::new(primary_store, Arc::clone(&logger)); | ||
| if let Some(config) = ts_config { | ||
| config.ephemeral.as_ref().map(|s| tier_store.set_ephemeral_store(Arc::clone(s))); | ||
| if let Some(backup_storage_dir_path) = config.backup_storage_dir_path.as_ref() { | ||
| let primary_storage_dir_path = PathBuf::from(&self.config.storage_dir_path); | ||
| if primary_storage_dir_path == *backup_storage_dir_path { | ||
| log_error!( | ||
| logger, | ||
| "Backup storage path must differ from primary storage path: {}", | ||
| backup_storage_dir_path.display() | ||
| ); | ||
| return Err(BuildError::BackupStorePathConflict); | ||
| } | ||
|
|
||
| let backup_store = SqliteStore::new( | ||
| backup_storage_dir_path.clone(), | ||
| Some(io::sqlite_store::SQLITE_BACKUP_DB_FILE_NAME.to_string()), | ||
| Some(io::sqlite_store::KV_TABLE_NAME.to_string()), | ||
| ) | ||
| .map_err(|e| { | ||
| log_error!(logger, "Failed to setup backup SQLite store: {}", e); | ||
| BuildError::KVStoreSetupFailed | ||
| })?; | ||
| let backup_store: Arc<DynStore> = Arc::new(DynStoreWrapper(backup_store)); | ||
| tier_store.set_backup_store(backup_store); | ||
| } | ||
| } | ||
|
|
||
| let seed_bytes = node_entropy.to_seed_bytes(); | ||
| let config = Arc::new(self.config.clone()); | ||
|
|
||
|
|
@@ -814,7 +917,7 @@ impl NodeBuilder { | |
| seed_bytes, | ||
| runtime, | ||
| logger, | ||
| Arc::new(DynStoreWrapper(kv_store)), | ||
| Arc::new(DynStoreWrapper(tier_store)), | ||
| ) | ||
| } | ||
| } | ||
|
|
@@ -1109,6 +1212,38 @@ impl ArcedNodeBuilder { | |
| self.inner.write().expect("lock").set_wallet_recovery_mode(); | ||
| } | ||
|
|
||
| /// Configures a local SQLite backup store for disaster recovery. | ||
| /// | ||
| /// When building with tiered storage, a SQLite store will be created at the | ||
| /// given directory path using [`SQLITE_BACKUP_DB_FILE_NAME`] as its database | ||
| /// file name. It receives a second durable copy of data written to the | ||
| /// primary store. | ||
| /// | ||
| /// Writes and removals for primary-backed data only succeed once both the | ||
| /// primary and backup SQLite stores complete successfully. | ||
| /// | ||
| /// The configured path must point to a distinct local directory from the | ||
| /// primary storage path. If the backup path equals the primary storage path, | ||
| /// building will fail with [`BuildError::BackupStorePathConflict`]. | ||
| /// | ||
| /// If not set, durable data will be stored only in the primary store. | ||
| /// | ||
| /// [`SQLITE_BACKUP_DB_FILE_NAME`]: crate::io::sqlite_store::SQLITE_BACKUP_DB_FILE_NAME | ||
| pub fn set_backup_storage_dir_path(&self, backup_storage_dir_path: String) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess adjusting |
||
| self.inner.write().expect("lock").set_backup_storage_dir_path(backup_storage_dir_path); | ||
| } | ||
|
|
||
| /// Configures the ephemeral store for non-critical, frequently-accessed data. | ||
| /// | ||
| /// When building with tiered storage, this store is used for ephemeral data like | ||
| /// the network graph and scorer data to reduce latency for reads. Data stored here | ||
| /// can be rebuilt if lost. | ||
| /// | ||
| /// If not set, non-critical data will be stored in the primary store. | ||
| pub fn set_ephemeral_store(&self, ephemeral_store: Arc<DynStore>) { | ||
| self.inner.write().expect("lock").set_ephemeral_store(ephemeral_store); | ||
| } | ||
|
|
||
| /// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options | ||
| /// previously configured. | ||
| pub fn build(&self, node_entropy: Arc<NodeEntropy>) -> Result<Arc<Node>, BuildError> { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the ephemeral store should always be a simple SQLite. Note that
DynStoremight currently not even be constructed outside of the crate right now.