Skip to content

Update BP NetworkGraph and Scorer persist frequency #2226

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

Merged
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
43 changes: 36 additions & 7 deletions lightning-background-processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const PING_TIMER: u64 = 1;
const NETWORK_PRUNE_TIMER: u64 = 60 * 60;

#[cfg(not(test))]
const SCORER_PERSIST_TIMER: u64 = 30;
const SCORER_PERSIST_TIMER: u64 = 60 * 60;
#[cfg(test)]
const SCORER_PERSIST_TIMER: u64 = 1;

Expand Down Expand Up @@ -235,9 +235,11 @@ fn handle_network_graph_update<L: Deref>(
}
}

/// Updates scorer based on event and returns whether an update occurred so we can decide whether
/// to persist.
fn update_scorer<'a, S: 'static + Deref<Target = SC> + Send + Sync, SC: 'a + WriteableScore<'a>>(
scorer: &'a S, event: &Event
) {
) -> bool {
let mut score = scorer.lock();
match event {
Event::PaymentPathFailed { ref path, short_channel_id: Some(scid), .. } => {
Expand All @@ -257,8 +259,9 @@ fn update_scorer<'a, S: 'static + Deref<Target = SC> + Send + Sync, SC: 'a + Wri
Event::ProbeFailed { path, short_channel_id: Some(scid), .. } => {
score.probe_failed(path, *scid);
},
_ => {},
_ => return false,
}
true
}

macro_rules! define_run_body {
Expand Down Expand Up @@ -351,9 +354,15 @@ macro_rules! define_run_body {
// Note that we want to run a graph prune once not long after startup before
// falling back to our usual hourly prunes. This avoids short-lived clients never
// pruning their network graph. We run once 60 seconds after startup before
// continuing our normal cadence.
// continuing our normal cadence. For RGS, since 60 seconds is likely too long,
// we prune after an initial sync completes.
let prune_timer = if have_pruned { NETWORK_PRUNE_TIMER } else { FIRST_NETWORK_PRUNE_TIMER };
if $timer_elapsed(&mut last_prune_call, prune_timer) {
let prune_timer_elapsed = $timer_elapsed(&mut last_prune_call, prune_timer);
let should_prune = match $gossip_sync {
GossipSync::Rapid(_) => !have_pruned || prune_timer_elapsed,
_ => prune_timer_elapsed,
};
if should_prune {
// The network graph must not be pruned while rapid sync completion is pending
if let Some(network_graph) = $gossip_sync.prunable_network_graph() {
#[cfg(feature = "std")] {
Expand Down Expand Up @@ -623,12 +632,19 @@ where
let network_graph = gossip_sync.network_graph();
let event_handler = &event_handler;
let scorer = &scorer;
let logger = &logger;
let persister = &persister;
async move {
if let Some(network_graph) = network_graph {
handle_network_graph_update(network_graph, &event)
}
if let Some(ref scorer) = scorer {
update_scorer(scorer, &event);
if update_scorer(scorer, &event) {
log_trace!(logger, "Persisting scorer after update");
if let Err(e) = persister.persist_scorer(&scorer) {
log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
}
}
}
event_handler(event).await;
}
Expand Down Expand Up @@ -766,7 +782,12 @@ impl BackgroundProcessor {
handle_network_graph_update(network_graph, &event)
}
if let Some(ref scorer) = scorer {
update_scorer(scorer, &event);
if update_scorer(scorer, &event) {
log_trace!(logger, "Persisting scorer after update");
if let Err(e) = persister.persist_scorer(&scorer) {
log_error!(logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
}
}
}
event_handler.handle_event(event);
};
Expand Down Expand Up @@ -1718,6 +1739,10 @@ mod tests {
if !std::thread::panicking() {
bg_processor.stop().unwrap();
}

let log_entries = nodes[0].logger.lines.lock().unwrap();
let expected_log = "Persisting scorer after update".to_string();
assert_eq!(*log_entries.get(&("lightning_background_processor".to_string(), expected_log)).unwrap(), 5);
}

#[tokio::test]
Expand Down Expand Up @@ -1760,6 +1785,10 @@ mod tests {
let t2 = tokio::spawn(async move {
do_test_payment_path_scoring!(nodes, receiver.recv().await);
exit_sender.send(()).unwrap();

let log_entries = nodes[0].logger.lines.lock().unwrap();
let expected_log = "Persisting scorer after update".to_string();
assert_eq!(*log_entries.get(&("lightning_background_processor".to_string(), expected_log)).unwrap(), 5);
});

let (r1, r2) = tokio::join!(t1, t2);
Expand Down