Skip to content

Commit b303f55

Browse files
authored
Merge pull request #947 from synonymdev/fix/logs-spam-and-size
fix: improve persisted logs and peer recovery
2 parents a9aa230 + b32bb88 commit b303f55

12 files changed

Lines changed: 389 additions & 90 deletions

File tree

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ suspend fun getData(): Result<Data> = withContext(Dispatchers.IO) {
188188
- NEVER manually append the `Throwable`'s message or any other props to the string passed as the 1st param of `Logger.*` calls, its internals are already enriching the final log message with the details of the `Throwable` passed via the `e` arg
189189
- ALWAYS wrap parameter values in log messages with single quotes, e.g. `Logger.info("Received event '$eventName'", context = TAG)`
190190
- ALWAYS start log messages with a verb, e.g. `Logger.info("Received payment for '$hash'", context = TAG)`
191+
- ALWAYS keep log names, tags, labels, and references mechanically traceable to the caller. Do not rewrite them into long descriptions.
191192
- ALWAYS log errors at the final handling layer where the error is acted upon, not in intermediate layers that just propagate it
192193
- ALWAYS use the Result API instead of try-catch
193194
- NEVER wrap methods returning `Result<T>` in try-catch

app/src/main/java/to/bitkit/async/ServiceQueue.kt

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import kotlinx.coroutines.SupervisorJob
66
import kotlinx.coroutines.asCoroutineDispatcher
77
import kotlinx.coroutines.runBlocking
88
import kotlinx.coroutines.withContext
9-
import to.bitkit.ext.callerName
109
import to.bitkit.utils.AppError
11-
import to.bitkit.utils.measured
1210
import java.util.concurrent.Executors
1311
import java.util.concurrent.ThreadFactory
1412
import kotlin.coroutines.CoroutineContext
@@ -20,30 +18,16 @@ enum class ServiceQueue {
2018

2119
fun <T> blocking(
2220
coroutineContext: CoroutineContext = scope.coroutineContext,
23-
functionName: String = Thread.currentThread().callerName,
2421
block: suspend CoroutineScope.() -> T,
2522
): T = runBlocking(coroutineContext) {
26-
runCatching {
27-
measured(label = functionName, context = TAG) {
28-
block()
29-
}
30-
}.getOrElse { throw AppError(it) }
23+
runCatching { block() }.getOrElse { throw AppError(it) }
3124
}
3225

3326
suspend fun <T> background(
3427
coroutineContext: CoroutineContext = scope.coroutineContext,
35-
functionName: String = Thread.currentThread().callerName,
3628
block: suspend CoroutineScope.() -> T,
3729
): T = withContext(coroutineContext) {
38-
runCatching {
39-
measured(label = functionName, context = TAG) {
40-
block()
41-
}
42-
}.getOrElse { throw AppError(it) }
43-
}
44-
45-
companion object {
46-
private const val TAG = "ServiceQueue"
30+
runCatching { block() }.getOrElse { throw AppError(it) }
4731
}
4832
}
4933

app/src/main/java/to/bitkit/fcm/WakeNodeWorker.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class WakeNodeWorker @AssistedInject constructor(
6262
private var notificationPayload: JsonObject? = null
6363

6464
private val timeout = 2.minutes
65+
private val slowWakeNodeThreshold = 10.seconds
6566
private val deliverSignal = CompletableDeferred<Unit>()
6667

6768
override suspend fun doWork(): Result {
@@ -80,7 +81,7 @@ class WakeNodeWorker @AssistedInject constructor(
8081
}
8182

8283
return runCatching {
83-
measured(label = "doWork", context = TAG) {
84+
measured(label = "doWork", context = TAG, slowThreshold = slowWakeNodeThreshold) {
8485
lightningRepo.start(
8586
walletIndex = 0,
8687
timeout = timeout,

app/src/main/java/to/bitkit/repositories/LightningRepo.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ class LightningRepo @Inject constructor(
351351

352352
// Initial state sync
353353
syncState()
354+
logNodeSupportSummary("node started")
354355
updateGeoBlockState()
355356
refreshChannelCache()
356357

@@ -377,6 +378,7 @@ class LightningRepo @Inject constructor(
377378
connectToTrustedPeers().onFailure {
378379
Logger.error("Failed to connect to trusted peers", it, context = TAG)
379380
}
381+
logNodeSupportSummary("trusted peers connected")
380382

381383
sync().onFailure { e ->
382384
Logger.warn("Initial sync failed, event-driven sync will retry", e, context = TAG)
@@ -1326,6 +1328,52 @@ class LightningRepo @Inject constructor(
13261328
}
13271329
}
13281330

1331+
private fun logNodeSupportSummary(reason: String) {
1332+
val state = _lightningState.value
1333+
val connectedPeers = state.peers.count { it.isConnected }
1334+
val persistedPeers = state.peers.count { it.isPersisted }
1335+
val readyChannels = state.channels.count { it.isChannelReady }
1336+
val usableChannels = state.channels.count { it.isUsable }
1337+
1338+
Logger.info(
1339+
"Collected node support summary for '$reason': " +
1340+
"nodeId='${state.nodeId}', " +
1341+
"lifecycle='${state.nodeLifecycleState}', " +
1342+
"peers='${state.peers.size}', " +
1343+
"connectedPeers='$connectedPeers', " +
1344+
"persistedPeers='$persistedPeers', " +
1345+
"channels='${state.channels.size}', " +
1346+
"readyChannels='$readyChannels', " +
1347+
"usableChannels='$usableChannels'",
1348+
context = TAG,
1349+
)
1350+
1351+
state.peers.forEach {
1352+
Logger.info(
1353+
"Collected peer support summary for '$reason': " +
1354+
"nodeId='${it.nodeId}', " +
1355+
"address='${it.address}', " +
1356+
"connected='${it.isConnected}', " +
1357+
"persisted='${it.isPersisted}'",
1358+
context = TAG,
1359+
)
1360+
}
1361+
1362+
state.channels.forEach {
1363+
Logger.info(
1364+
"Collected channel support summary for '$reason': " +
1365+
"channelId='${it.channelId}', " +
1366+
"counterparty='${it.counterpartyNodeId}', " +
1367+
"ready='${it.isChannelReady}', " +
1368+
"usable='${it.isUsable}', " +
1369+
"announced='${it.isAnnounced}', " +
1370+
"outboundMsat='${it.outboundCapacityMsat}', " +
1371+
"inboundMsat='${it.inboundCapacityMsat}'",
1372+
context = TAG,
1373+
)
1374+
}
1375+
}
1376+
13291377
suspend fun awaitPeerConnected(timeout: Duration = 30.seconds) = withContext(bgDispatcher) {
13301378
if (lightningService.peers?.any { it.isConnected } == true) return@withContext
13311379
Logger.debug("Waiting for peer to reconnect (timeout='$timeout')...", context = TAG)

0 commit comments

Comments
 (0)