Repository navigation
feat(geth): report the sync phase through the healthcheck #105
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
Open
3alpha
wants to merge
2
commits into
main
Choose a base branch
from
feat/node-status-report
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 | ||
| 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 | ||
| env: | ||
| NODE_STATUS_TOOLS: ${{ github.workspace }}/staker-package-scripts/scripts/node_status_tools.sh | ||
| - name: Test under busybox ash (Alpine, like the Geth image) | ||
| run: > | ||
| docker run --rm -v "$PWD:/work" -w /work alpine:3 sh -c | ||
| "apk add -q jq && NODE_STATUS_TOOLS=/work/staker-package-scripts/scripts/node_status_tools.sh 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| # DAppNode package Geth | ||
|
|
||
| ## Node status report | ||
|
|
||
| The package reports Geth's sync phase (header, block and state download, healing, transaction indexing) through its Docker healthcheck, which the dappmanager reads to show the node's status. The healthcheck script is [`geth/node-status.sh`](geth/node-status.sh), installed in the image as `/usr/local/bin/node-status`; it uses the shared helpers from `dappnode/staker-package-scripts`. Its test is [`test/node_status_test.sh`](test/node_status_test.sh): run `sh test/node_status_test.sh` with a clone of `staker-package-scripts` next to this repository, or point `NODE_STATUS_TOOLS` at its `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
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,146 @@ | ||
| #!/bin/sh | ||
|
|
||
| # Node status report of the Geth package, run as the Docker healthcheck. | ||
| # Prints one JSON line (see the node status contract) telling which sync phase | ||
| # Geth is in and how far along. Whether the node is synced is decided by the | ||
| # dappmanager, not here. | ||
| # | ||
| # Geth snap sync, as of v1.17: | ||
| # - Before a consensus client drives it, eth_syncing is an object with | ||
| # currentBlock and highestBlock at 0: nothing to sync towards yet. | ||
| # - The latest block stays at genesis until the state is complete. Meanwhile | ||
| # the chain (headers, bodies, receipts) and the state download run together. | ||
| # eth_syncing gives no total for the state; the snap progress metric does | ||
| # (v1.17.6+). Healing has no total at all. | ||
| # - Snap/2 (only after the Amsterdam fork) replaces healing with a BAL | ||
| # catch-up and a trie generation, both measurable from eth_syncing. | ||
| # - eth_syncing stays an object after reaching the head until transactions | ||
| # (and state history, if enabled) are indexed: background work. | ||
| # | ||
| # Environment: | ||
| # NODE_STATUS_TOOLS: Path of the shared helpers (default /etc/profile.d/node_status_tools.sh) | ||
| # GETH_METRICS_PORT: Port of the metrics server, also read by Geth itself (default 6060) | ||
| # NS_RPC_PORT, NS_TIMEOUT: See node_status_tools.sh | ||
|
|
||
| # At most four calls, each with its own timeout, and none after the JSON-RPC | ||
| # fails, so the script ends well inside the healthcheck timeout even when Geth hangs | ||
| NS_TIMEOUT=${NS_TIMEOUT:-2} | ||
|
|
||
| # shellcheck source=/dev/null | ||
| . "${NODE_STATUS_TOOLS:-/etc/profile.d/node_status_tools.sh}" || exit 1 | ||
|
|
||
| METRICS_URL="http://127.0.0.1:${GETH_METRICS_PORT:-6060}/debug/metrics/prometheus" | ||
|
|
||
| # Decimal value of a quantity field of the eth_syncing object, 0 when absent | ||
| sync_field() { | ||
| ns_hex_to_dec "$(ns_json_get "$syncing" ".$1")" 2>/dev/null || echo 0 | ||
| } | ||
|
|
||
| # Percentage of $1 over $2 with one decimal, nothing when $2 is not positive | ||
| percent() { | ||
| awk -v done="$1" -v total="$2" 'BEGIN { if (total > 0) printf "%.1f\n", done * 100 / total }' | ||
| } | ||
|
|
||
| # The JSON-RPC does not answer yet: Geth is starting (opening its database) | ||
| if ! syncing=$(ns_rpc eth_syncing); then | ||
| ns_emit starting | ||
| exit $? | ||
| fi | ||
|
|
||
| # Latest block and peers, skipped after the first call that fails | ||
| head="" | ||
| height="" | ||
| peers="" | ||
| if head=$(ns_block_head); then | ||
| height=${head#height=} | ||
| height=${height%% *} | ||
| peers=$(ns_json_get "$(ns_rpc net_peerCount)" .) || peers="" | ||
| else | ||
| head="" | ||
| fi | ||
|
|
||
| if [ "$syncing" = "false" ]; then | ||
| # shellcheck disable=SC2086 # $head holds height=<n> headTime=<n> | ||
| ns_emit synced $head peers="$peers" | ||
| exit $? | ||
| fi | ||
|
|
||
| # Anything but an object tells nothing more | ||
| case $syncing in | ||
| "{"*) ;; | ||
| *) | ||
| # shellcheck disable=SC2086 # $head holds height=<n> headTime=<n> | ||
| ns_emit syncing $head peers="$peers" | ||
| exit $? | ||
| ;; | ||
| esac | ||
|
|
||
| phase="" | ||
| progress="" | ||
| awaiting="" | ||
| background="" | ||
| detail="" | ||
|
|
||
| current=$(sync_field currentBlock) | ||
| highest=$(sync_field highestBlock) | ||
|
|
||
| if [ "$current" -eq 0 ] && [ "$highest" -eq 0 ]; then | ||
| # Nothing downloaded and no target: waits for the consensus client, or | ||
| # fetches the headers it was just given | ||
| phase="Downloading block headers" | ||
| awaiting=true | ||
| elif [ "$(($(sync_field healingTrienodes) + $(sync_field healingBytecode)))" -gt 0 ]; then | ||
| phase="Healing state" | ||
| pending=$(sync_field healingTrienodes) | ||
| [ "$pending" -gt 0 ] && detail="Trie nodes pending:$pending" | ||
| elif [ "$height" = "0" ]; then | ||
| # Snap sync before the state is complete: the latest block is genesis | ||
| synced_lists=$(sync_field syncedAccessLists) | ||
| total_lists=$(sync_field totalAccessLists) | ||
| trie_gen=$(sync_field trieGenProgress) | ||
| accounts=$(sync_field syncedAccounts) | ||
| if [ "$total_lists" -gt 0 ] && [ "$synced_lists" -lt "$total_lists" ]; then | ||
| phase="Catching up state" | ||
| progress=$(percent "$synced_lists" "$total_lists") | ||
| elif [ "$trie_gen" -gt 0 ] && [ "$trie_gen" -lt 100 ]; then | ||
| phase="Generating state trie" | ||
| progress=$trie_gen | ||
| else | ||
| # Only these two gauges are needed from the (large) metrics page | ||
| metrics=$(ns_http_get "$METRICS_URL" | | ||
| grep -E '^eth_(protocols_snap_sync_progress|downloader_chain_progress)[ {]') | ||
| # Fractions 0-1; both gauges exist from start-up at 0, so 0 means not measured yet | ||
| state_fraction=$(printf '%s\n' "$metrics" | ns_metric eth_protocols_snap_sync_progress) | ||
| chain_fraction=$(printf '%s\n' "$metrics" | ns_metric eth_downloader_chain_progress) | ||
| if awk -v f="${state_fraction:-0}" 'BEGIN { exit !(f > 0) }'; then | ||
| phase="Downloading state" | ||
| progress=$(percent "$state_fraction" 1) | ||
| detail="Accounts downloaded:$accounts" | ||
| elif [ "$accounts" -gt 0 ]; then | ||
| phase="Downloading state" | ||
| detail="Accounts downloaded:$accounts" | ||
| elif awk -v f="${chain_fraction:-0}" 'BEGIN { exit !(f > 0) }'; then | ||
| phase="Downloading blocks" | ||
| progress=$(percent "$chain_fraction" 1) | ||
| else | ||
| phase="Downloading blocks" | ||
| progress=$(percent "$current" "$highest") | ||
| fi | ||
| fi | ||
| fi | ||
|
|
||
| # Work that goes on once the node follows the head | ||
| if [ -z "$phase" ]; then | ||
| tx_remaining=$(sync_field txIndexRemainingBlocks) | ||
| if [ "$tx_remaining" -gt 0 ]; then | ||
| background="Indexing transactions" | ||
| detail="Blocks left to index:$tx_remaining" | ||
| elif [ "$(($(sync_field stateIndexRemaining) + $(sync_field trienodeIndexRemaining)))" -gt 0 ]; then | ||
| background="Indexing state history" | ||
| fi | ||
| fi | ||
|
|
||
| # shellcheck disable=SC2086 # $head holds height=<n> headTime=<n> | ||
| ns_emit syncing $head peers="$peers" phase="$phase" progress="$progress" \ | ||
| awaitingTarget="$awaiting" background="$background" detail="$detail" | ||
| 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.