Skip to content

Commit fc07160

Browse files
committed
Only log if we actually persisted LiquidityManager data
Previously, we logged "Persisting LiquidityManager..." on each background processor wakeup, which can be very spammy, even on TRACE level. Here, we opt to only log if something actually needed to be repersisted and we did so (in case of failure we're logging that anyways, too).
1 parent bb5504e commit fc07160

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,12 +1228,20 @@ where
12281228
}
12291229

12301230
if let Some(liquidity_manager) = liquidity_manager.as_ref() {
1231-
log_trace!(logger, "Persisting LiquidityManager...");
12321231
let fut = async {
1233-
liquidity_manager.get_lm().persist().await.map_err(|e| {
1234-
log_error!(logger, "Persisting LiquidityManager failed: {}", e);
1235-
e
1236-
})
1232+
liquidity_manager
1233+
.get_lm()
1234+
.persist()
1235+
.await
1236+
.map(|did_persist| {
1237+
if did_persist {
1238+
log_trace!(logger, "Persisted LiquidityManager.");
1239+
}
1240+
})
1241+
.map_err(|e| {
1242+
log_error!(logger, "Persisting LiquidityManager failed: {}", e);
1243+
e
1244+
})
12371245
};
12381246
futures.set_e(Box::pin(fut));
12391247
}

lightning-liquidity/src/events/event_queue.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ where
129129
EventQueueNotifierGuard(self)
130130
}
131131

132-
pub async fn persist(&self) -> Result<(), lightning::io::Error> {
132+
pub async fn persist(&self) -> Result<bool, lightning::io::Error> {
133133
let fut = {
134134
let mut state_lock = self.state.lock().unwrap();
135135

136136
if !state_lock.needs_persist {
137-
return Ok(());
137+
return Ok(false);
138138
}
139139

140140
state_lock.needs_persist = false;
@@ -153,7 +153,7 @@ where
153153
e
154154
})?;
155155

156-
Ok(())
156+
Ok(true)
157157
}
158158
}
159159

lightning-liquidity/src/lsps2/service.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,15 +1786,16 @@ where
17861786
})
17871787
}
17881788

1789-
pub(crate) async fn persist(&self) -> Result<(), lightning::io::Error> {
1789+
pub(crate) async fn persist(&self) -> Result<bool, lightning::io::Error> {
17901790
// TODO: We should eventually persist in parallel, however, when we do, we probably want to
17911791
// introduce some batching to upper-bound the number of requests inflight at any given
17921792
// time.
1793+
let mut did_persist = false;
17931794

17941795
if self.persistence_in_flight.fetch_add(1, Ordering::AcqRel) > 0 {
17951796
// If we're not the first event processor to get here, just return early, the increment
17961797
// we just did will be treated as "go around again" at the end.
1797-
return Ok(());
1798+
return Ok(did_persist);
17981799
}
17991800

18001801
loop {
@@ -1820,6 +1821,7 @@ where
18201821
for counterparty_node_id in need_persist.into_iter() {
18211822
debug_assert!(!need_remove.contains(&counterparty_node_id));
18221823
self.persist_peer_state(counterparty_node_id).await?;
1824+
did_persist = true;
18231825
}
18241826

18251827
for counterparty_node_id in need_remove {
@@ -1854,6 +1856,7 @@ where
18541856
}
18551857
if let Some(future) = future_opt {
18561858
future.await?;
1859+
did_persist = true;
18571860
} else {
18581861
self.persist_peer_state(counterparty_node_id).await?;
18591862
}
@@ -1868,7 +1871,7 @@ where
18681871
break;
18691872
}
18701873

1871-
Ok(())
1874+
Ok(did_persist)
18721875
}
18731876

18741877
pub(crate) fn peer_disconnected(&self, counterparty_node_id: PublicKey) {

lightning-liquidity/src/lsps5/service.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,17 @@ where
244244
})
245245
}
246246

247-
pub(crate) async fn persist(&self) -> Result<(), lightning::io::Error> {
247+
pub(crate) async fn persist(&self) -> Result<bool, lightning::io::Error> {
248248
// TODO: We should eventually persist in parallel, however, when we do, we probably want to
249249
// introduce some batching to upper-bound the number of requests inflight at any given
250250
// time.
251251

252+
let mut did_persist = false;
253+
252254
if self.persistence_in_flight.fetch_add(1, Ordering::AcqRel) > 0 {
253255
// If we're not the first event processor to get here, just return early, the increment
254256
// we just did will be treated as "go around again" at the end.
255-
return Ok(());
257+
return Ok(did_persist);
256258
}
257259

258260
loop {
@@ -277,6 +279,7 @@ where
277279
for client_id in need_persist.into_iter() {
278280
debug_assert!(!need_remove.contains(&client_id));
279281
self.persist_peer_state(client_id).await?;
282+
did_persist = true;
280283
}
281284

282285
for client_id in need_remove {
@@ -311,6 +314,7 @@ where
311314
}
312315
if let Some(future) = future_opt {
313316
future.await?;
317+
did_persist = true;
314318
} else {
315319
self.persist_peer_state(client_id).await?;
316320
}
@@ -325,7 +329,7 @@ where
325329
break;
326330
}
327331

328-
Ok(())
332+
Ok(did_persist)
329333
}
330334

331335
fn check_prune_stale_webhooks<'a>(

lightning-liquidity/src/manager.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -670,23 +670,27 @@ where
670670
self.pending_events.get_and_clear_pending_events()
671671
}
672672

673-
/// Persists the state of the service handlers towards the given [`KVStore`] implementation.
673+
/// Persists the state of the service handlers towards the given [`KVStore`] implementation if
674+
/// needed.
675+
///
676+
/// Returns `true` if it persisted sevice handler data.
674677
///
675678
/// This will be regularly called by LDK's background processor if necessary and only needs to
676679
/// be called manually if it's not utilized.
677-
pub async fn persist(&self) -> Result<(), lightning::io::Error> {
680+
pub async fn persist(&self) -> Result<bool, lightning::io::Error> {
678681
// TODO: We should eventually persist in parallel.
679-
self.pending_events.persist().await?;
682+
let mut did_persist = false;
683+
did_persist |= self.pending_events.persist().await?;
680684

681685
if let Some(lsps2_service_handler) = self.lsps2_service_handler.as_ref() {
682-
lsps2_service_handler.persist().await?;
686+
did_persist |= lsps2_service_handler.persist().await?;
683687
}
684688

685689
if let Some(lsps5_service_handler) = self.lsps5_service_handler.as_ref() {
686-
lsps5_service_handler.persist().await?;
690+
did_persist |= lsps5_service_handler.persist().await?;
687691
}
688692

689-
Ok(())
693+
Ok(did_persist)
690694
}
691695

692696
fn handle_lsps_message(

0 commit comments

Comments
 (0)