Skip to content

Commit

Permalink
fix(network): set the record count metric as soon as we restart
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Jan 10, 2025
1 parent dc05434 commit 7a2aa13
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
13 changes: 6 additions & 7 deletions ant-networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,19 +593,18 @@ impl NetworkBuilder {
let kademlia = {
match record_store_cfg {
Some(store_cfg) => {
#[cfg(feature = "open-metrics")]
let record_stored_metrics =
metrics_recorder.as_ref().map(|r| r.records_stored.clone());

let node_record_store = NodeRecordStore::with_config(
peer_id,
store_cfg,
network_event_sender.clone(),
local_swarm_cmd_sender.clone(),
#[cfg(feature = "open-metrics")]
record_stored_metrics,
);
#[cfg(feature = "open-metrics")]
let mut node_record_store = node_record_store;
#[cfg(feature = "open-metrics")]
if let Some(metrics_recorder) = &metrics_recorder {
node_record_store = node_record_store
.set_record_count_metric(metrics_recorder.records_stored.clone());
}

let store = UnifiedRecordStore::Node(node_record_store);
debug!("Using Kademlia with NodeRecordStore!");
Expand Down
35 changes: 27 additions & 8 deletions ant-networking/src/record_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ impl NodeRecordStore {
config: NodeRecordStoreConfig,
network_event_sender: mpsc::Sender<NetworkEvent>,
swarm_cmd_sender: mpsc::Sender<LocalSwarmCmd>,
#[cfg(feature = "open-metrics")] record_count_metric: Option<Gauge>,
) -> Self {
info!("Using encryption_seed of {:?}", config.encryption_seed);
let encryption_details = derive_aes256gcm_siv_from_seed(&config.encryption_seed);
Expand Down Expand Up @@ -386,7 +387,7 @@ impl NodeRecordStore {
local_swarm_cmd_sender: swarm_cmd_sender,
responsible_distance_range: None,
#[cfg(feature = "open-metrics")]
record_count_metric: None,
record_count_metric,
received_payment_count,
encryption_details,
timestamp,
Expand All @@ -397,14 +398,12 @@ impl NodeRecordStore {

record_store.flush_historic_quoting_metrics();

record_store
}
#[cfg(feature = "open-metrics")]
if let Some(metric) = &record_store.record_count_metric {
let _ = metric.set(record_store.records.len() as i64);
}

/// Set the record_count_metric to report the number of records stored to the metrics server
#[cfg(feature = "open-metrics")]
pub fn set_record_count_metric(mut self, metric: Gauge) -> Self {
self.record_count_metric = Some(metric);
self
record_store
}

/// Returns the current distance ilog2 (aka bucket) range of CLOSE_GROUP nodes.
Expand Down Expand Up @@ -1066,6 +1065,8 @@ mod tests {
Default::default(),
network_event_sender,
swarm_cmd_sender,
#[cfg(feature = "open-metrics")]
None,
);

// An initial unverified put should not write to disk
Expand Down Expand Up @@ -1144,6 +1145,8 @@ mod tests {
store_config.clone(),
network_event_sender.clone(),
swarm_cmd_sender.clone(),
#[cfg(feature = "open-metrics")]
None,
);

// Create a chunk
Expand Down Expand Up @@ -1192,6 +1195,8 @@ mod tests {
store_config,
new_network_event_sender,
new_swarm_cmd_sender,
#[cfg(feature = "open-metrics")]
None,
);

// Verify the record still exists
Expand All @@ -1217,6 +1222,8 @@ mod tests {
store_config_diff,
diff_network_event_sender,
diff_swarm_cmd_sender,
#[cfg(feature = "open-metrics")]
None,
);

// When encryption is enabled, the record should be gone because it can't be decrypted
Expand Down Expand Up @@ -1245,6 +1252,8 @@ mod tests {
store_config,
network_event_sender,
swarm_cmd_sender,
#[cfg(feature = "open-metrics")]
None,
);

// Create a chunk
Expand Down Expand Up @@ -1309,6 +1318,8 @@ mod tests {
store_config,
network_event_sender,
swarm_cmd_sender,
#[cfg(feature = "open-metrics")]
None,
);

// Create a scratchpad
Expand Down Expand Up @@ -1403,6 +1414,8 @@ mod tests {
store_config.clone(),
network_event_sender,
swarm_cmd_sender,
#[cfg(feature = "open-metrics")]
None,
);
// keep track of everything ever stored, to check missing at the end are further away
let mut stored_records_at_some_point: Vec<RecordKey> = vec![];
Expand Down Expand Up @@ -1527,6 +1540,8 @@ mod tests {
store_config,
network_event_sender,
swarm_cmd_sender,
#[cfg(feature = "open-metrics")]
None,
);

let mut stored_records: Vec<RecordKey> = vec![];
Expand Down Expand Up @@ -1611,6 +1626,8 @@ mod tests {
store_config.clone(),
network_event_sender.clone(),
swarm_cmd_sender.clone(),
#[cfg(feature = "open-metrics")]
None,
);

store.payment_received();
Expand All @@ -1623,6 +1640,8 @@ mod tests {
store_config,
network_event_sender,
swarm_cmd_sender,
#[cfg(feature = "open-metrics")]
None,
);

assert_eq!(1, new_store.received_payment_count);
Expand Down

0 comments on commit 7a2aa13

Please sign in to comment.