Skip to content

Commit 184af02

Browse files
authored
Beacon sync metrics managemnt update (#3016)
* Sync scheduler provides an independent `ticker` loop process why: Can be used to update `metrics` and for debug logging. While an event driven solution would stall if there are no events at the moment (e.g. when the syncer hibernates, the `ticker` will run regardless. * Use `runTicker()` loop interface alike for updating ticker why: Not event driven anymore so it will not stall when the syncer hibernates. * Re-implement logging ticker by running it within the `runTicker()` driver why: Simplifies implementation * Re-name metrics variable to better fit into the current naming schemes * Fix copyright header
1 parent 4b893eb commit 184af02

File tree

13 files changed

+168
-226
lines changed

13 files changed

+168
-226
lines changed

nimbus/db/ledger.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Nimbus
2-
# Copyright (c) 2023-2024 Status Research & Development GmbH
2+
# Copyright (c) 2023-2025 Status Research & Development GmbH
33
# Licensed under either of
44
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
55
# http://www.apache.org/licenses/LICENSE-2.0)

nimbus/sync/beacon.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Nimbus
2-
# Copyright (c) 2023-2024 Status Research & Development GmbH
2+
# Copyright (c) 2023-2025 Status Research & Development GmbH
33
# Licensed under either of
44
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
55
# http://www.apache.org/licenses/LICENSE-2.0)
@@ -36,6 +36,9 @@ proc runRelease(ctx: BeaconCtxRef) =
3636
proc runDaemon(ctx: BeaconCtxRef) {.async: (raises: []).} =
3737
await worker.runDaemon(ctx, "RunDaemon")
3838

39+
proc runTicker(ctx: BeaconCtxRef) =
40+
worker.runTicker(ctx, "RunTicker")
41+
3942
proc runStart(buddy: BeaconBuddyRef): bool =
4043
worker.start(buddy, "RunStart")
4144

nimbus/sync/beacon/README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -307,19 +307,19 @@ The following metrics are defined in *worker/update/metrics.nim* which will
307307
be available if *nimbus* is compiled with the additional make flags
308308
*NIMFLAGS="-d:metrics \-\-threads:on"*:
309309

310-
| *Variable* | *Logic type* | *Short description* |
311-
|:-------------------|:------------:|:--------------------|
312-
| | | |
313-
| beacon_base | block height | **B**, *increasing* |
314-
| beacon_latest | block height | **L**, *increasing* |
315-
| beacon_coupler | block height | **C**, *increasing* |
316-
| beacon_dangling | block height | **D** |
317-
| beacon_head | block height | **H**, *increasing* |
318-
| beacon_target | block height | **T**, *increasing* |
319-
| | | |
320-
| beacon_header_lists_staged | size | # of staged header list records |
321-
| beacon_headers_unprocessed | size | # of accumulated header block numbers|
322-
| beacon_block_lists_staged | size | # of staged block list records |
323-
| beacon_blocks_unprocessed | size | # of accumulated body block numbers |
324-
| | | |
325-
| beacon_buddies | size | # of peers working concurrently |
310+
| *Variable* | *Logic type* | *Short description* |
311+
|:-----------------------------|:------------:|:--------------------|
312+
| | | |
313+
| nec_base | block height | **B**, *increasing* |
314+
| nec_execution_head | block height | **L**, *increasing* |
315+
| nec_sync_coupler | block height | **C**, *increasing* |
316+
| nec_sync_dangling | block height | **D** |
317+
| nec_sync_head | block height | **H**, *increasing* |
318+
| nec_consensus_head | block height | **T**, *increasing* |
319+
| | | |
320+
| nec_sync_header_lists_staged | size | # of staged header list records |
321+
| nec_sync_headers_unprocessed | size | # of accumulated header block numbers|
322+
| nec_sync_block_lists_staged | size | # of staged block list records |
323+
| nec_sync_blocks_unprocessed | size | # of accumulated body block numbers |
324+
| | | |
325+
| nec_sync_peers | size | # of peers working concurrently |

nimbus/sync/beacon/worker.nim

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Nimbus
2-
# Copyright (c) 2023-2024 Status Research & Development GmbH
2+
# Copyright (c) 2023-2025 Status Research & Development GmbH
33
# Licensed and distributed under either of
44
# * MIT license (license terms in the root directory or at
55
# https://opensource.org/licenses/MIT).
@@ -15,6 +15,7 @@ import
1515
pkg/eth/[common, p2p],
1616
pkg/stew/[interval_set, sorted_set],
1717
../../common,
18+
./worker/update/[metrics, ticker],
1819
./worker/[blocks_staged, headers_staged, headers_unproc, start_stop, update],
1920
./worker_desc
2021

@@ -59,15 +60,11 @@ proc setup*(ctx: BeaconCtxRef; info: static[string]): bool =
5960

6061
# Load initial state from database if there is any
6162
ctx.setupDatabase info
62-
63-
# Debugging stuff, might be an empty template
64-
ctx.setupTicker()
6563
true
6664

6765
proc release*(ctx: BeaconCtxRef; info: static[string]) =
6866
## Global clean up
6967
ctx.destroyRpcMagic()
70-
ctx.destroyTicker()
7168

7269

7370
proc start*(buddy: BeaconBuddyRef; info: static[string]): bool =
@@ -96,15 +93,18 @@ proc stop*(buddy: BeaconBuddyRef; info: static[string]) =
9693
# Public functions
9794
# ------------------------------------------------------------------------------
9895

96+
proc runTicker*(ctx: BeaconCtxRef; info: static[string]) =
97+
## Global background job that is started every few seconds. It is to be
98+
## intended for updating metrics, debug logging etc.
99+
ctx.updateMetrics()
100+
ctx.updateTicker()
101+
99102
proc runDaemon*(
100103
ctx: BeaconCtxRef;
101104
info: static[string];
102105
) {.async: (raises: []).} =
103106
## Global background job that will be re-started as long as the variable
104-
## `ctx.daemon` is set `true`. If that job was stopped due to re-setting
105-
## `ctx.daemon` to `false`, it will be restarted next after it was reset
106-
## as `true` not before there is some activity on the `runPool()`,
107-
## `runSingle()`, or `runMulti()` functions.
107+
## `ctx.daemon` is set `true`.
108108
##
109109
## On a fresh start, the flag `ctx.daemon` will not be set `true` before the
110110
## first usable request from the CL (via RPC) stumbles in.

nimbus/sync/beacon/worker/blocks_staged.nim

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import
1717
../../../core/chain,
1818
../worker_desc,
1919
./blocks_staged/bodies,
20-
./update/metrics,
2120
"."/[blocks_unproc, db, helpers, update]
2221

2322
# ------------------------------------------------------------------------------
@@ -194,9 +193,6 @@ proc blocksStagedCollect*(
194193
# response length from the network.
195194
nBlkBlocks = blk.blocks.len
196195

197-
# Update, so it can be followed nicely
198-
ctx.updateMetrics()
199-
200196
# Fetch and extend staging record
201197
if not await buddy.fetchAndCheck(ivReq, blk, info):
202198
haveError = true
@@ -258,9 +254,6 @@ proc blocksStagedCollect*(
258254
nBlocks=blk.blocks.len, nStaged=ctx.blk.staged.len,
259255
bdyErrors=buddy.bdyErrors
260256

261-
# Update, so it can be followed nicely
262-
ctx.updateMetrics()
263-
264257
return true
265258

266259

@@ -320,9 +313,6 @@ proc blocksStagedImport*(
320313
maxImport = ctx.chain.latestNumber()
321314
break importLoop
322315

323-
# Update, so it can be followed nicely
324-
ctx.updateMetrics()
325-
326316
# Allow pseudo/async thread switch.
327317
(await ctx.updateAsyncTasks()).isOkOr:
328318
maxImport = ctx.chain.latestNumber()
@@ -358,9 +348,6 @@ proc blocksStagedImport*(
358348
for bn in iv.minPt .. maxImport:
359349
ctx.dbHeaderUnstash bn
360350

361-
# Update, so it can be followed nicely
362-
ctx.updateMetrics()
363-
364351
info "Import done", iv, nBlocks, base=ctx.chain.baseNumber.bnStr,
365352
head=ctx.chain.latestNumber.bnStr, target=ctx.layout.final.bnStr
366353
return true

nimbus/sync/beacon/worker/headers_staged.nim

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import
1717
pkg/stew/[interval_set, sorted_set],
1818
../../../common,
1919
../worker_desc,
20-
./update/metrics,
2120
./headers_staged/[headers, linked_hchain],
2221
"."/[headers_unproc, update]
2322

@@ -234,9 +233,6 @@ proc headersStagedProcess*(ctx: BeaconCtxRef; info: static[string]): int =
234233
# anymore.
235234
discard ctx.hdr.staged.delete(iv.maxPt)
236235

237-
# Update, so it can be followed nicely
238-
ctx.updateMetrics()
239-
240236
if qItem.data.hash != ctx.dbHeaderParentHash(dangling).expect "Hash32":
241237
# Discard wrong chain and merge back the range into the `unproc` list.
242238
ctx.headersUnprocCommit(0,iv)
@@ -257,9 +253,6 @@ proc headersStagedProcess*(ctx: BeaconCtxRef; info: static[string]): int =
257253
if headersStagedQueueLengthLwm < ctx.hdr.staged.len:
258254
ctx.poolMode = true
259255

260-
# Update, so it can be followed nicely
261-
ctx.updateMetrics()
262-
263256

264257
proc headersStagedReorg*(ctx: BeaconCtxRef; info: static[string]) =
265258
## Some pool mode intervention. The effect is that all concurrent peers
@@ -295,9 +288,6 @@ proc headersStagedReorg*(ctx: BeaconCtxRef; info: static[string]) =
295288
ctx.headersUnprocCommit(0, key - nHeaders + 1, key)
296289
discard ctx.hdr.staged.delete key
297290

298-
# Update, so it can be followed nicely
299-
ctx.updateMetrics()
300-
301291
# ------------------------------------------------------------------------------
302292
# End
303293
# ------------------------------------------------------------------------------

nimbus/sync/beacon/worker/start_stop.nim

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Nimbus
2-
# Copyright (c) 2023-2024 Status Research & Development GmbH
2+
# Copyright (c) 2023-2025 Status Research & Development GmbH
33
# Licensed and distributed under either of
44
# * MIT license (license terms in the root directory or at
55
# https://opensource.org/licenses/MIT).
@@ -17,46 +17,12 @@ import
1717
../worker_desc,
1818
./blocks_staged/staged_queue,
1919
./headers_staged/staged_queue,
20-
./update/metrics,
2120
"."/[blocks_unproc, db, headers_unproc, update]
2221

23-
when enableTicker:
24-
import ./start_stop/ticker
25-
2622
# ------------------------------------------------------------------------------
2723
# Private functions
2824
# ------------------------------------------------------------------------------
2925

30-
when enableTicker:
31-
proc tickerUpdater(ctx: BeaconCtxRef): TickerStatsUpdater =
32-
## Legacy stuff, will be probably be superseded by `metrics`
33-
return proc: auto =
34-
TickerStats(
35-
base: ctx.chain.baseNumber(),
36-
latest: ctx.chain.latestNumber(),
37-
coupler: ctx.layout.coupler,
38-
dangling: ctx.layout.dangling,
39-
head: ctx.layout.head,
40-
headOk: ctx.layout.lastState != idleSyncState,
41-
target: ctx.target.consHead.number,
42-
targetOk: ctx.target.final != 0,
43-
44-
nHdrStaged: ctx.headersStagedQueueLen(),
45-
hdrStagedTop: ctx.headersStagedQueueTopKey(),
46-
hdrUnprocTop: ctx.headersUnprocTop(),
47-
nHdrUnprocessed: ctx.headersUnprocTotal() + ctx.headersUnprocBorrowed(),
48-
nHdrUnprocFragm: ctx.headersUnprocChunks(),
49-
50-
nBlkStaged: ctx.blocksStagedQueueLen(),
51-
blkStagedBottom: ctx.blocksStagedQueueBottomKey(),
52-
blkUnprocBottom: ctx.blocksUnprocBottom(),
53-
nBlkUnprocessed: ctx.blocksUnprocTotal() + ctx.blocksUnprocBorrowed(),
54-
nBlkUnprocFragm: ctx.blocksUnprocChunks(),
55-
56-
reorg: ctx.pool.nReorg,
57-
nBuddies: ctx.pool.nBuddies)
58-
59-
6026
proc updateBeaconHeaderCB(
6127
ctx: BeaconCtxRef;
6228
info: static[string];
@@ -93,22 +59,6 @@ proc updateBeaconHeaderCB(
9359
# Public functions
9460
# ------------------------------------------------------------------------------
9561

96-
when enableTicker:
97-
proc setupTicker*(ctx: BeaconCtxRef) =
98-
## Helper for `setup()`: Start ticker
99-
ctx.pool.ticker = TickerRef.init(ctx.tickerUpdater)
100-
101-
proc destroyTicker*(ctx: BeaconCtxRef) =
102-
## Helper for `release()`
103-
ctx.pool.ticker.destroy()
104-
ctx.pool.ticker = TickerRef(nil)
105-
106-
else:
107-
template setupTicker*(ctx: BeaconCtxRef) = discard
108-
template destroyTicker*(ctx: BeaconCtxRef) = discard
109-
110-
# ---------
111-
11262
proc setupDatabase*(ctx: BeaconCtxRef; info: static[string]) =
11363
## Initalise database related stuff
11464

@@ -157,13 +107,11 @@ proc startBuddy*(buddy: BeaconBuddyRef): bool =
157107
peer = buddy.peer
158108
if peer.supports(protocol.eth) and peer.state(protocol.eth).initialized:
159109
ctx.pool.nBuddies.inc
160-
ctx.updateMetrics()
161110
return true
162111

163112
proc stopBuddy*(buddy: BeaconBuddyRef) =
164113
let ctx = buddy.ctx
165114
ctx.pool.nBuddies.dec
166-
ctx.updateMetrics(force=(ctx.pool.nBuddies == 0))
167115

168116
# ------------------------------------------------------------------------------
169117
# End

nimbus/sync/beacon/worker/update.nim

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import
1616
pkg/stew/[byteutils, sorted_set],
1717
../../../core/chain,
1818
../worker_desc,
19-
./update/metrics,
2019
./blocks_staged/staged_queue,
2120
./headers_staged/staged_queue,
2221
"."/[blocks_unproc, db, headers_unproc, helpers]
@@ -123,9 +122,6 @@ proc startHibernating(ctx: BeaconCtxRef; info: static[string]) =
123122

124123
info "Suspending syncer", head=ctx.chain.latestNumber.bnStr
125124

126-
# Update, so it can be followed nicely
127-
ctx.updateMetrics()
128-
129125

130126
proc setupCollectingHeaders(ctx: BeaconCtxRef; info: static[string]) =
131127
## Set up sync target (see clause *(9)* in `README.md`) by modifying
@@ -171,9 +167,6 @@ proc setupCollectingHeaders(ctx: BeaconCtxRef; info: static[string]) =
171167
# Update range
172168
ctx.headersUnprocSet(c+1, h-1)
173169

174-
# Update, so it can be followed nicely
175-
ctx.updateMetrics()
176-
177170
# Mark target used, reset for re-fill
178171
ctx.target.changed = false
179172

@@ -238,9 +231,6 @@ proc linkIntoFc(ctx: BeaconCtxRef; info: static[string]): bool =
238231

239232
# Save layout state
240233
ctx.dbStoreSyncStateLayout info
241-
242-
# Update, so it can be followed nicely
243-
ctx.updateMetrics()
244234
return true
245235

246236
trace info & ": cannot link into FC", B=b.bnStr, L=l.bnStr,
@@ -341,9 +331,6 @@ proc updateFinalBlockHeader*(
341331
info "Activating syncer", base=b.bnStr, head=ctx.chain.latestNumber.bnStr,
342332
finalised=f.bnStr, target=ctx.target.consHead.bnStr
343333

344-
# Update, so it can be followed nicely
345-
ctx.updateMetrics()
346-
347334

348335
proc updateAsyncTasks*(
349336
ctx: BeaconCtxRef;

0 commit comments

Comments
 (0)