Repository navigation
feat(besu): report the sync phase through the healthcheck #72
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| name: Node status test | ||
|
|
||
| on: | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| repository: dappnode/staker-package-scripts | ||
| path: staker-package-scripts | ||
| - name: Run the node status test | ||
| run: sh test/node_status_test.sh | ||
| env: | ||
| NODE_STATUS_TOOLS: staker-package-scripts/scripts/node_status_tools.sh | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| #!/bin/sh | ||
|
|
||
| # Node status report of the Besu execution client, run as the Docker healthcheck. | ||
| # Prints one JSON line (see node_status_tools.sh in dappnode/staker-package-scripts) | ||
| # telling the sync phase only Besu knows; the dappmanager decides by itself whether | ||
| # the node is synced, waiting for the consensus client or stalled. | ||
| # | ||
| # Signals (Besu 26.11): | ||
| # - eth_syncing is false once the initial sync is done, and an object | ||
| # {startingBlock, currentBlock, highestBlock} for the whole snap sync, where | ||
| # currentBlock is the last block whose body and receipts the chain download stored. | ||
| # - besu_synchronizer_sync_duration_count{name=<phase>} exists from the moment a | ||
| # snap sync phase starts in this process, 0 while it runs and 1 once it ended. | ||
| # - besu_synchronizer_fast_sync_pivot_block_current is the snap sync pivot block, | ||
| # 0 until the consensus client gave one. | ||
| # | ||
| # Environment: | ||
| # NODE_STATUS_TOOLS: Path of node_status_tools.sh (default /etc/profile.d/node_status_tools.sh) | ||
| # BESU_METRICS_PORT: Metrics port, read by Besu itself as well (default 9545) | ||
| # NS_TIMEOUT: Timeout in seconds of each request (default 2) | ||
|
|
||
| # shellcheck source=/dev/null | ||
| . "${NODE_STATUS_TOOLS:-/etc/profile.d/node_status_tools.sh}" || exit 1 | ||
|
|
||
| # A handful of calls stay well inside the 10 s healthcheck timeout | ||
| NS_TIMEOUT=${NS_TIMEOUT:-2} | ||
| METRICS_URL="http://127.0.0.1:${BESU_METRICS_PORT:-9545}/metrics" | ||
| BACKGROUND="Finishing background sync tasks" | ||
|
|
||
| # The JSON-RPC does not answer yet: Besu is still opening its database | ||
| if ! syncing=$(ns_rpc eth_syncing); then | ||
| ns_emit starting | ||
| exit $? | ||
| fi | ||
|
|
||
| state=syncing | ||
| [ "$syncing" = false ] && state=synced | ||
|
|
||
| # Value of a sync duration timer: 0 while the phase runs, 1 once it ended, nothing when | ||
| # the phase never started in this process | ||
| timer() { | ||
| printf '%s\n' "$metrics" | ns_metric besu_synchronizer_sync_duration_count "name=$1" | ||
| } | ||
|
|
||
| # Every call below can take NS_TIMEOUT seconds: stop at the first one that fails | ||
| set -- | ||
| answered=false | ||
| if head=$(ns_block_head); then | ||
| # shellcheck disable=SC2086 # head is "height=<n> headTime=<n>" | ||
| set -- $head | ||
| if peers=$(ns_rpc net_peerCount); then | ||
| set -- "$@" peers="$(ns_json_get "$peers")" | ||
| answered=true | ||
| fi | ||
| fi | ||
|
|
||
| # Synced, or nothing more to learn | ||
| if [ "$state" = synced ] || [ "$answered" = false ]; then | ||
| ns_emit "$state" "$@" | ||
| exit $? | ||
| fi | ||
|
|
||
| # eth_syncing is an object while the snap sync runs: work that may go on near the head | ||
| set -- "$@" background="$BACKGROUND" | ||
|
|
||
| # Only the lines read below, so that each lookup scans a few lines | ||
| metrics=$(ns_http_get "$METRICS_URL" | | ||
| grep -E '^besu_synchronizer_(sync_duration_count|fast_sync_pivot_block_current)[{ ]') | ||
| total=$(timer TOTAL_SYNC_DURATION) | ||
|
|
||
| # The sync already ended in this process: nothing to add | ||
| if [ -n "$total" ] && [ "$total" != 0 ]; then | ||
| ns_emit syncing "$@" | ||
| exit $? | ||
| fi | ||
|
|
||
| world_state=$(timer SNAP_INITIAL_WORLD_STATE_DOWNLOAD_DURATION) | ||
| healing=$(timer SNAP_WORLD_STATE_HEALING_DURATION) | ||
| flat_heal=$(timer FLAT_DB_HEAL) | ||
| chain=$(timer CHAIN_DOWNLOAD_DURATION) | ||
| pivot=$(ns_hex_to_dec "$(printf '%s\n' "$metrics" | ns_metric besu_synchronizer_fast_sync_pivot_block_current)") || | ||
| pivot=0 | ||
| [ "$pivot" -gt 0 ] && set -- "$@" detail="Pivot block:$pivot" | ||
|
|
||
| # The world state and the chain download in parallel; the state takes longer, so it names the phase | ||
| if [ "$world_state" = 0 ]; then | ||
| ns_emit syncing phase="Downloading state" "$@" | ||
| exit $? | ||
| fi | ||
| if [ "$healing" = 0 ]; then | ||
| ns_emit syncing phase="Healing state" "$@" | ||
| exit $? | ||
| fi | ||
| if [ "$flat_heal" = 0 ]; then | ||
| ns_emit syncing phase="Healing the flat database" "$@" | ||
| exit $? | ||
| fi | ||
|
|
||
| if [ "$chain" = 0 ]; then | ||
| starting=$(ns_hex_to_dec "$(ns_json_get "$syncing" .startingBlock)") || starting=0 | ||
| current=$(ns_hex_to_dec "$(ns_json_get "$syncing" .currentBlock)") || current=0 | ||
| highest=$(ns_hex_to_dec "$(ns_json_get "$syncing" .highestBlock)") || highest=0 | ||
| # Headers are downloaded backwards from the pivot first; only the bodies and receipts | ||
| # that follow move currentBlock | ||
| if [ "$current" -le "$starting" ]; then | ||
| ns_emit syncing phase="Downloading block headers" "$@" | ||
| exit $? | ||
| fi | ||
| # The chain download runs up to the pivot | ||
| target=$pivot | ||
| [ "$target" -gt "$current" ] || target=$highest | ||
| [ "$target" -gt "$current" ] || target="" | ||
| ns_emit syncing phase="Downloading blocks" position="$current" target="$target" "$@" | ||
| exit $? | ||
| fi | ||
|
|
||
| # The sync runs (its timer exists, so the metrics answered) but neither download started: | ||
| # no pivot block yet, which comes from the consensus client | ||
| if [ "$total" = 0 ] && [ -z "$world_state" ] && [ -z "$chain" ] && [ "$pivot" -eq 0 ]; then | ||
| ns_emit syncing awaitingTarget=true "$@" | ||
| exit $? | ||
| fi | ||
|
|
||
| ns_emit syncing "$@" | ||
| exit $? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.