Repository navigation
feat(reth): report the sync phase through the healthcheck #103
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,21 @@ | ||
| name: Node status test | ||
|
|
||
| on: | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| node-status: | ||
| 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 with dash | ||
| run: NODE_STATUS_TOOLS=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
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
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,181 @@ | ||
| #!/bin/sh | ||
|
|
||
| # Node status report of the Reth package, run as the Docker healthcheck. | ||
| # Prints one JSON line (see node_status_tools.sh in dappnode/staker-package-scripts): | ||
| # what Reth is doing, from its own JSON-RPC, metrics and the snapshot download | ||
| # marker the entrypoint leaves. Whether the node is synced is decided by the | ||
| # dappmanager, not here. | ||
| # | ||
| # Environment: | ||
| # NODE_STATUS_TOOLS: Helper library to source (default /etc/profile.d/node_status_tools.sh) | ||
| # NODE_STATUS_DIR: Directory of the snapshot download marker (default /tmp/node-status) | ||
|
|
||
| # Every call gets 2 s, so even a node that stops answering midway stays well inside the 10 s timeout | ||
| NS_TIMEOUT=${NS_TIMEOUT:-2} | ||
|
|
||
| # shellcheck source=/dev/null | ||
| . "${NODE_STATUS_TOOLS:-/etc/profile.d/node_status_tools.sh}" || exit 1 | ||
|
|
||
| SNAPSHOT_MARKER="${NODE_STATUS_DIR:-/tmp/node-status}/snapshot-download" | ||
| METRICS_URL=http://127.0.0.1:6060/metrics | ||
|
|
||
| # Blocks a stage may trail the pipeline target and still count as done. Reth only runs the | ||
| # pipeline for larger gaps; smaller ones are filled block by block by the live sync. | ||
| STAGE_TOLERANCE_BLOCKS=32 | ||
|
|
||
| # The JSON-RPC does not answer: Reth is opening its database, or the entrypoint is still | ||
| # downloading a snapshot. The download marker counts only while the process it names runs, so | ||
| # a marker left by a killed download never makes a starting node look like it downloads. | ||
| report_starting() { | ||
| pid="" | ||
| phase="" | ||
| progress="" | ||
| if [ -f "$SNAPSHOT_MARKER" ]; then | ||
| while IFS='=' read -r key value; do | ||
| case $key in | ||
| pid) pid=$value ;; | ||
| phase) phase=$value ;; | ||
| progress) progress=$value ;; | ||
| esac | ||
| done <"$SNAPSHOT_MARKER" | ||
| case $pid in | ||
| '' | *[!0-9]*) phase="" ;; | ||
| *) grep -qa download "/proc/$pid/cmdline" 2>/dev/null || phase="" ;; | ||
| esac | ||
| fi | ||
| if [ -n "$phase" ]; then | ||
| ns_emit starting phase="$phase" progress="$progress" | ||
| else | ||
| ns_emit starting | ||
| fi | ||
| } | ||
|
|
||
| # Reads the eth_syncing object on stdin and prints ns_emit arguments, one per line. | ||
| # | ||
| # Reth's currentBlock and highestBlock are always the same block: the latest block only moves | ||
| # when the whole staged pipeline finishes. Progress comes from `stages`, the checkpoint of each | ||
| # pipeline stage, weighted by the stage's estimated share of the sync time. The pipeline target | ||
| # is not reported, so the highest checkpoint (normally the Headers stage) stands for it. | ||
| # | ||
| # Prints "awaiting=true" when every stage is at the latest block: the pipeline has nothing to | ||
| # do, so either no consensus client drives the node, or it just restarted, or the Headers stage | ||
| # is downloading (it saves its checkpoint only once all headers are in). | ||
| analyze_stages() { | ||
| jq -r --argjson tolerance "$STAGE_TOLERANCE_BLOCKS" ' | ||
| def num: if type == "number" then floor | ||
| elif type == "string" and test("^0[xX][0-9a-fA-F]+$") then .[2:] | ascii_downcase | explode | ||
| | reduce .[] as $c (0; . * 16 + (if $c >= 97 then $c - 87 else $c - 48 end)) | ||
| elif type == "string" and test("^[0-9]+$") then tonumber | ||
| else null end; | ||
| def commas: tostring | if length > 3 then (.[:-3] | commas) + "," + .[-3:] else . end; | ||
| def near($a; $b): ($a - $b) as $d | $d <= $tolerance and $d >= -$tolerance; | ||
| # Pipeline stages in run order (crates/stages/stages/src/sets.rs at the pinned Reth), | ||
| # with their estimated share of the sync time. Stages not listed are ignored. | ||
| [ | ||
| {name: "Era", weight: 0, label: "Importing history files"}, | ||
| {name: "Headers", weight: 5, label: "Downloading block headers"}, | ||
| {name: "Bodies", weight: 10, label: "Downloading blocks"}, | ||
| {name: "SenderRecovery", weight: 5, label: "Recovering transaction senders"}, | ||
| {name: "Execution", weight: 60, label: "Executing blocks"}, | ||
| {name: "PruneSenderRecovery", weight: 0, label: "Pruning"}, | ||
| {name: "MerkleUnwind", weight: 0, label: "Building the state trie"}, | ||
| {name: "AccountHashing", weight: 3, label: "Building the state trie"}, | ||
| {name: "StorageHashing", weight: 3, label: "Building the state trie"}, | ||
| {name: "MerkleExecute", weight: 4, label: "Building the state trie"}, | ||
| {name: "TransactionLookup", weight: 4, label: "Indexing transactions"}, | ||
| {name: "IndexStorageHistory", weight: 3, label: "Indexing state history"}, | ||
| {name: "IndexAccountHistory", weight: 3, label: "Indexing state history"}, | ||
| {name: "Prune", weight: 0, label: "Pruning"}, | ||
| {name: "Finish", weight: 0, label: "Finishing"} | ||
| ] as $table | ||
| | ((.currentBlock | num) // 0) as $current | ||
| | ((.startingBlock | num) // 0) as $start | ||
| | (if (.stages | type) == "array" then .stages else [] end | ||
| | map(select(type == "object") | ||
| | {name: (.name // .stage_name), block: ((.block // .block_number) | num)} | ||
| | select((.name | type) == "string" and .block != null)) | ||
| | reduce .[] as $s ({}; .[$s.name] = $s.block)) as $blocks | ||
| | [$table[] | select($blocks[.name] != null) | .block = $blocks[.name]] as $known | ||
| | ([$known[] | select(.name != "Era") | .block] | max) as $highest | ||
| | ([$known[] | select(.name == "Era") | .block] | first) as $era | ||
| | ([$known[] | select(.name == "MerkleUnwind") | .block] | first) as $unwind | ||
| # Optional stages that are not part of this pipeline keep an old checkpoint (genesis when | ||
| # they never ran): Era runs only with --era.enable, ahead of every other stage, and | ||
| # PruneSenderRecovery only with sender recovery pruning, ahead of MerkleUnwind | ||
| | [$known[] | select(.name != "Era") | ||
| | select(.name != "PruneSenderRecovery" or $unwind == null or .block >= $unwind)] as $stages | ||
| | if $stages == [] then empty | ||
| elif $era != null and $era > $highest then "phase=Importing history files" | ||
| elif all($stages[]; near(.block; $current)) then "awaiting=true" | ||
| else | ||
| # Stages rebuilt from scratch (the indices after a snapshot download) start below the | ||
| # block this run started from | ||
| reduce $stages[] as $s ({done: 0, total: 0, stage: null}; | ||
| (if $s.block >= $start then $start else 0 end) as $base | ||
| | (if $highest > $base then ([1, ([0, ($s.block - $base) / ($highest - $base)] | max)] | min) | ||
| else 1 end) as $fraction | ||
| | .done += $s.weight * $fraction | ||
| | .total += $s.weight | ||
| | if .stage == null and ($highest - $s.block) > $tolerance then .stage = $s else . end) | ||
| | "phase=\(.stage.label // "Finishing")", | ||
| (if .total > 0 then "progress=\(.done / .total * 100)" else empty end), | ||
| (if .stage != null | ||
| then "detail=Pipeline stage:\(.stage.name), block \(.stage.block | commas) of \($highest | commas)" | ||
| else empty end) | ||
| end | ||
| ' 2>/dev/null | ||
| } | ||
|
|
||
| # The Headers stage downloads from the target down to the local head and saves its checkpoint only | ||
| # at the end, so while it runs every stage still sits at the latest block. Its downloader has | ||
| # requests in flight only during that download. | ||
| headers_downloading() { | ||
| metrics=$(ns_http_get "$METRICS_URL") || return 1 | ||
| in_flight=$(printf '%s\n' "$metrics" | ns_metric reth_downloaders_headers_in_flight_requests) || return 1 | ||
| case $in_flight in | ||
| '' | 0 | -*) return 1 ;; | ||
| esac | ||
| return 0 | ||
| } | ||
|
|
||
| if ! syncing=$(ns_rpc eth_syncing); then | ||
| report_starting | ||
| exit $? | ||
| fi | ||
|
|
||
| # Stop at the first call that fails: each one can take the full timeout | ||
| head="" | ||
| peers="" | ||
| rpc_ok=false | ||
| if head=$(ns_block_head); then | ||
| peers=$(ns_json_get "$(ns_rpc net_peerCount)" .) && [ -n "$peers" ] && rpc_ok=true | ||
| fi | ||
|
|
||
| if [ "$syncing" = "false" ]; then | ||
| # shellcheck disable=SC2086 # $head holds two key=value words | ||
| ns_emit synced $head peers="$peers" | ||
| exit $? | ||
| fi | ||
|
|
||
| set -- | ||
| awaiting=false | ||
| while IFS= read -r arg; do | ||
| case $arg in | ||
| awaiting=true) awaiting=true ;; | ||
| ?*) set -- "$@" "$arg" ;; | ||
| esac | ||
| done <<EOF | ||
| $(printf '%s\n' "$syncing" | analyze_stages) | ||
| EOF | ||
|
|
||
| if [ "$awaiting" = true ]; then | ||
| if [ "$rpc_ok" = true ] && headers_downloading; then | ||
| set -- "$@" phase="Downloading block headers" | ||
| else | ||
| set -- "$@" awaitingTarget=true | ||
| fi | ||
| fi | ||
|
|
||
| # shellcheck disable=SC2086 # $head holds two key=value words | ||
| ns_emit syncing $head peers="$peers" "$@" | ||
| exit $? |
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.