Skip to content

Commit

Permalink
WIP metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Oct 3, 2024
1 parent b551fa3 commit 12f9140
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ pub(crate) const DEPRECATED_SPENDABLE_OUTPUT_INFO_PERSISTENCE_PRIMARY_NAMESPACE:
"spendable_outputs";
pub(crate) const DEPRECATED_SPENDABLE_OUTPUT_INFO_PERSISTENCE_SECONDARY_NAMESPACE: &str = "";

/// The node metrics will be persisted under this key.
pub(crate) const NODE_METRICS_PRIMARY_NAMESPACE: &str = "";
pub(crate) const NODE_METRICS_SECONDARY_NAMESPACE: &str = "";
pub(crate) const NODE_METRICS_KEY: &str = "node_metrics";

/// RapidGossipSync's `latest_sync_timestamp` will be persisted under this key.
pub(crate) const LATEST_RGS_SYNC_TIMESTAMP_PRIMARY_NAMESPACE: &str = "";
pub(crate) const LATEST_RGS_SYNC_TIMESTAMP_SECONDARY_NAMESPACE: &str = "";
Expand Down
49 changes: 48 additions & 1 deletion src/io/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ use crate::config::WALLET_KEYS_SEED_LEN;

use crate::chain::ChainSource;
use crate::fee_estimator::OnchainFeeEstimator;
use crate::io::{
NODE_METRICS_KEY, NODE_METRICS_PRIMARY_NAMESPACE, NODE_METRICS_SECONDARY_NAMESPACE,
};
use crate::logger::{log_error, FilesystemLogger};
use crate::peer_store::PeerStore;
use crate::sweep::DeprecatedSpendableOutputInfo;
use crate::types::{Broadcaster, DynStore, KeysManager, Sweeper};
use crate::wallet::ser::{ChangeSetDeserWrapper, ChangeSetSerWrapper};
use crate::{Error, EventQueue, PaymentDetails};
use crate::{Error, EventQueue, NodeMetrics, PaymentDetails};

use lightning::io::Cursor;
use lightning::ln::msgs::DecodeError;
Expand Down Expand Up @@ -342,6 +345,50 @@ where
Ok(())
}

pub(crate) fn read_node_metrics<L: Deref>(
kv_store: Arc<DynStore>, logger: L,
) -> Result<NodeMetrics, std::io::Error>
where
L::Target: Logger,
{
let mut reader = Cursor::new(kv_store.read(
NODE_METRICS_PRIMARY_NAMESPACE,
NODE_METRICS_SECONDARY_NAMESPACE,
NODE_METRICS_KEY,
)?);
NodeMetrics::read(&mut reader).map_err(|e| {
log_error!(logger, "Failed to deserialize NodeMetrics: {}", e);
std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to deserialize NodeMetrics")
})
}

pub(crate) fn write_node_metrics<L: Deref>(
node_metrics: NodeMetrics, kv_store: Arc<DynStore>, logger: L,
) -> Result<(), Error>
where
L::Target: Logger,
{
let data = node_metrics.encode();
kv_store
.write(
NODE_METRICS_PRIMARY_NAMESPACE,
NODE_METRICS_SECONDARY_NAMESPACE,
NODE_METRICS_KEY,
&data,
)
.map_err(|e| {
log_error!(
logger,
"Writing data to key {}/{}/{} failed due to: {}",
NODE_METRICS_PRIMARY_NAMESPACE,
NODE_METRICS_SECONDARY_NAMESPACE,
NODE_METRICS_KEY,
e
);
Error::PersistenceFailed
})
}

pub(crate) fn read_latest_rgs_sync_timestamp<L: Deref>(
kv_store: Arc<DynStore>, logger: L,
) -> Result<u32, std::io::Error>
Expand Down
24 changes: 19 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ use connection::ConnectionManager;
use event::{EventHandler, EventQueue};
use gossip::GossipSource;
use graph::NetworkGraph;
use io::utils::write_node_metrics;
use liquidity::LiquiditySource;
use payment::store::PaymentStore;
use payment::{
Expand All @@ -146,6 +147,7 @@ use logger::{log_error, log_info, log_trace, FilesystemLogger, Logger};

use lightning::chain::BestBlock;
use lightning::events::bump_transaction::Wallet as LdkWallet;
use lightning::impl_writeable_tlv_based;
use lightning::ln::channel_state::ChannelShutdownState;
use lightning::ln::channelmanager::PaymentId;
use lightning::ln::msgs::SocketAddress;
Expand Down Expand Up @@ -196,11 +198,7 @@ pub struct Node {
peer_store: Arc<PeerStore<Arc<FilesystemLogger>>>,
payment_store: Arc<PaymentStore<Arc<FilesystemLogger>>>,
is_listening: Arc<AtomicBool>,
latest_wallet_sync_timestamp: Arc<RwLock<Option<u64>>>,
latest_onchain_wallet_sync_timestamp: Arc<RwLock<Option<u64>>>,
latest_fee_rate_cache_update_timestamp: Arc<RwLock<Option<u64>>>,
latest_rgs_snapshot_timestamp: Arc<RwLock<Option<u64>>>,
latest_node_announcement_broadcast_timestamp: Arc<RwLock<Option<u64>>>,
node_metrics: Arc<RwLock<NodeMetrics>>,
}

impl Node {
Expand Down Expand Up @@ -1451,6 +1449,22 @@ pub struct NodeStatus {
pub latest_node_announcement_broadcast_timestamp: Option<u64>,
}

/// Status fields that are persisted across restarts.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct NodeMetrics {
latest_onchain_wallet_sync_timestamp: Option<u64>,
latest_fee_rate_cache_update_timestamp: Option<u64>,
latest_rgs_snapshot_timestamp: Option<u64>,
latest_node_announcement_broadcast_timestamp: Option<u64>,
}

impl_writeable_tlv_based!(NodeMetrics, {
(0, latest_onchain_wallet_sync_timestamp, option),
(2, latest_fee_rate_cache_update_timestamp, option),
(4, latest_rgs_snapshot_timestamp, option),
(6, latest_node_announcement_broadcast_timestamp, option),
});

pub(crate) fn total_anchor_channels_reserve_sats(
channel_manager: &ChannelManager, config: &Config,
) -> u64 {
Expand Down

0 comments on commit 12f9140

Please sign in to comment.