Repository navigation
feat(erigon): report the sync phase through the healthcheck #101
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,25 @@ | ||
| name: Node status test | ||
|
|
||
| on: | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| env: | ||
| NODE_STATUS_TOOLS: staker-package-scripts/scripts/node_status_tools.sh | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| repository: dappnode/staker-package-scripts | ||
| path: staker-package-scripts | ||
| - name: Test under dash | ||
| run: sh test/node_status_test.sh | ||
| - name: Test under Debian bookworm dash and jq 1.6 (the Erigon image base) | ||
| run: > | ||
| docker run --rm -e NODE_STATUS_TOOLS -v "$PWD:/work" -w /work debian:bookworm-slim sh -c | ||
| "apt-get update -qq && apt-get install -qq -y jq >/dev/null && sh test/node_status_test.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
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,169 @@ | ||
| #!/bin/sh | ||
|
|
||
| # Node status report of Erigon, printed by the Docker healthcheck | ||
| # | ||
| # Prints one JSON line (see ns_emit in node_status_tools.sh) telling which sync | ||
| # phase Erigon is in and how far along. Whether the node is synced is decided by | ||
| # the dappmanager, not here: "synced" only means eth_syncing returned false. | ||
| # | ||
| # Erigon 3 serves eth_syncing as {currentBlock, highestBlock, stages: [{stage_name, | ||
| # block_number}]} (rpc/jsonrpc/eth_system.go, node/shards/sync_state.go): | ||
| # - stages is empty while the snapshots are downloaded (OtterSync stage). Once | ||
| # the downloader reports, currentBlock / highestBlock is the share of bytes | ||
| # downloaded; before that both are 0. | ||
| # - stages lists every stage once the highest block is known; currentBlock is | ||
| # the progress of the Execution stage and highestBlock the head the | ||
| # consensus client sent. | ||
| # - startingBlock resets on every restart and is not used. | ||
| # The sync{stage=...} gauges of the metrics endpoint follow the stages as they | ||
| # run, ahead of eth_syncing, which only sees committed progress. | ||
| # | ||
| # Environment: | ||
| # NODE_STATUS_TOOLS: Path of node_status_tools.sh (default /etc/profile.d/node_status_tools.sh) | ||
| # NS_TIMEOUT: Timeout in seconds of each request (default 2, at most 4 requests run) | ||
| # EXTRA_OPTS: Erigon flags of the package, read for --http.port, --http.api and --metrics.port | ||
|
|
||
| # shellcheck source=/dev/null | ||
| . "${NODE_STATUS_TOOLS:-/etc/profile.d/node_status_tools.sh}" | ||
|
|
||
| # EXTRA_OPTS is split into words below and must not be globbed | ||
| set -f | ||
|
|
||
| NS_TIMEOUT=${NS_TIMEOUT:-2} | ||
| BACKGROUND="Finishing background sync tasks" | ||
|
|
||
| # Print the last value given to an Erigon flag in EXTRA_OPTS (--flag=value, | ||
| # --flag value, or with a single dash), nothing when it is not set | ||
| # | ||
| # Arguments: | ||
| # $1: Flag name without dashes | ||
| extra_opt() { | ||
| _value="" | ||
| _next="" | ||
| for _word in ${EXTRA_OPTS:-}; do | ||
| if [ -n "$_next" ]; then | ||
| _value=$_word | ||
| _next="" | ||
| continue | ||
| fi | ||
| case $_word in | ||
| --"$1"=* | -"$1"=*) _value=${_word#*=} ;; | ||
| --"$1" | -"$1") _next=1 ;; | ||
| esac | ||
| done | ||
| printf '%s' "$_value" | ||
| } | ||
|
|
||
| # Print the port given to an Erigon flag in EXTRA_OPTS, or the default | ||
| # | ||
| # Arguments: | ||
| # $1: Flag name without dashes | ||
| # $2: Default port | ||
| extra_port() { | ||
| _port=$(extra_opt "$1") | ||
| case $_port in | ||
| '' | *[!0-9]*) printf '%s' "$2" ;; | ||
| *) printf '%s' "$_port" ;; | ||
| esac | ||
| } | ||
|
|
||
| # Print an issue when the JSON-RPC namespaces the dashboard calls are not all | ||
| # enabled: Erigon serves only eth,erigon,engine without --http.api | ||
| api_issue() { | ||
| _apis=$(extra_opt http.api) | ||
| _missing="" | ||
| for _api in eth net web3; do | ||
| case ",${_apis:-eth,erigon,engine}," in | ||
| *",$_api,"*) ;; | ||
| *) _missing="${_missing:+$_missing, }$_api" ;; | ||
| esac | ||
| done | ||
| [ -z "$_missing" ] || printf '%s' "--http.api in EXTRA_OPTS lacks JSON-RPC namespaces the dashboard needs: $_missing" | ||
| } | ||
|
|
||
| NS_RPC_PORT=${NS_RPC_PORT:-$(extra_port http.port 8545)} | ||
| METRICS_URL="http://127.0.0.1:$(extra_port metrics.port 6060)/debug/metrics/prometheus" | ||
| ISSUE=$(api_issue) | ||
|
|
||
| # The JSON-RPC does not answer yet: Erigon is opening its database | ||
| if ! syncing=$(ns_rpc eth_syncing); then | ||
| ns_emit starting issue="$ISSUE" | ||
| exit $? | ||
| fi | ||
|
|
||
| # Stop calling the node once a call fails: each failure can take NS_TIMEOUT | ||
| if head=$(ns_block_head); then | ||
| peers=$(ns_json_get "$(ns_rpc net_peerCount)" .) | ||
| else | ||
| head="" | ||
| peers="" | ||
| fi | ||
|
|
||
| if [ "$syncing" = false ]; then | ||
| # shellcheck disable=SC2086 # $head is "height=<n> headTime=<n>" | ||
| ns_emit synced $head peers="$peers" issue="$ISSUE" | ||
| exit $? | ||
| fi | ||
|
|
||
| # currentBlock, highestBlock, number of stages and the OtterSync stage block | ||
| fields=$(ns_json_get "$syncing" \ | ||
| '"\(.currentBlock) \(.highestBlock) \(.stages // [] | length) \(first(.stages[]? | select(.stage_name == "OtterSync") | .block_number) // 0)"') | ||
| # shellcheck disable=SC2086 # four words | ||
| set -- $fields | ||
| current=$(ns_hex_to_dec "${1:-}") || current=0 | ||
| highest=$(ns_hex_to_dec "${2:-}") || highest=0 | ||
| stage_count=${3:-0} | ||
| otter_sync=$(ns_hex_to_dec "${4:-}") || otter_sync=0 | ||
|
|
||
| phase="" | ||
| progress="" | ||
| position="" | ||
| target="" | ||
|
|
||
| if [ "$stage_count" -gt 0 ]; then | ||
| if [ "$otter_sync" -eq 0 ] && [ "$current" -eq 0 ]; then | ||
| # The highest block is known (sent by a consensus client, or from the header-chain | ||
| # snapshots) but the snapshot download has not reported progress yet | ||
| phase="Preparing the snapshot download" | ||
| elif [ "$highest" -gt "$current" ]; then | ||
| phase="Executing blocks" | ||
| position=$current | ||
| target=$highest | ||
| fi | ||
| elif [ "$highest" -eq 0 ]; then | ||
| # Waiting for the snapshot list, or downloading the header-chain snapshots, which report no progress | ||
| phase="Preparing the snapshot download" | ||
| elif [ -n "$head" ] && metrics=$(ns_http_get "$METRICS_URL") && | ||
| otter_metric=$(printf '%s\n' "$metrics" | ns_metric sync stage=otter_sync) && | ||
| [ "$otter_metric" != 0 ]; then | ||
| # The OtterSync stage is past the download (or ran before a restart), and | ||
| # eth_syncing still shows its last numbers until the first commit | ||
| exec_metric=$(printf '%s\n' "$metrics" | ns_metric sync stage=execution) || exec_metric=0 | ||
| exec_block=$(ns_hex_to_dec "$exec_metric") || exec_block=0 | ||
| if [ "$exec_block" -eq 0 ]; then | ||
| # Filling the database from the snapshots before execution starts | ||
| phase="Indexing snapshots" | ||
| else | ||
| [ "$exec_block" -gt "$current" ] && current=$exec_block | ||
| if [ "$highest" -gt "$current" ]; then | ||
| phase="Executing blocks" | ||
| position=$current | ||
| target=$highest | ||
| fi | ||
| fi | ||
| else | ||
| # The snapshot download: currentBlock / highestBlock is the share of bytes downloaded | ||
| permille=$((current * 1000 / highest)) | ||
| if [ "$permille" -ge 990 ]; then | ||
| # Indexing after the download leaves the numbers near the end for a long time, with nothing to measure | ||
| phase="Indexing snapshots" | ||
| else | ||
| phase="Downloading snapshots" | ||
| progress="$((permille / 10)).$((permille % 10))" | ||
| fi | ||
| fi | ||
|
|
||
| # shellcheck disable=SC2086 # $head is "height=<n> headTime=<n>" | ||
| ns_emit syncing $head phase="$phase" progress="$progress" position="$position" target="$target" \ | ||
| peers="$peers" background="$BACKGROUND" issue="$ISSUE" | ||
| 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
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.