fix(agent): bound concurrent beacon state and bad block fetches - #64
Merged
Conversation
Beacon states and execution bad blocks are both read fully into memory before being compressed and stored. In `single` mode one process runs an agent per node and every agent reacts to the same block event, so nothing limited how many of those reads ran at once - peak memory scaled with the node count rather than with any configured bound. On a glamsterdam devnet with ~100MB states, 47 agents sawtoothed between 2.3GB and 5.67GB against a 6GB limit and were OOM killed every 35-64s. Trimming the agent list only moved the deadline (20 agents lasted ~101s), since the peak is a function of concurrent fetches. Add a process-wide semaphore for each path. The slot is held across the fetch, the compression and the upload, because both the raw and compressed copies are live for that whole window; for bad blocks it is held across the indexing pass, since the decoded slice is retained until it completes. Also drop the raw state reference once compressed so only one copy survives the upload. Measured with 47 agents against a mock beacon node serving 100MB states, 6GB / 2 CPU, same config otherwise: before peak 5.67GB (94% of limit), 2.3-5.67GB sawtooth limit 4 peak 1.09GB (18%), 679 states indexed limit 10 peak 1.44GB (24%), 752 states indexed Bounding concurrency also made state archival work: previously the fetches contended badly enough that a production deployment persisted 20 states in 10 days. The limits are process-wide and fixed by whichever agent starts first, as agents are constructed independently. Threading a limiter through the single config would avoid that at the cost of changing agent.New. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Bad block responses are roughly an order of magnitude smaller than beacon states, so the same limit costs far less memory on that path. The slot is also held across the whole indexing pass, which is slow enough that a lower limit throttles throughput without meaningfully lowering the peak. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Clears the goconst failures blocking CI. No behaviour change - every replacement is a literal swapped for a constant holding the same value. Reuses the existing indexer Key* constants where the literals duplicated them, and adds constants for the remaining repeated logrus field keys, prometheus labels, gorm order clauses and test fixtures. Test-only values live in _test.go files so they are not compiled into the binary. The four lock tests in pkg/server/persistence fail on master as well and are untouched by this change. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Beacon states and execution bad blocks are both read fully into memory (
io.ReadAll) before being compressed and stored. Insinglemode one process runs an agent per node, and every agent reacts to the same block event, so nothing limited how many of those reads ran concurrently — peak memory scaled with the node count rather than with any configured bound.On a glamsterdam devnet with ~100MB states, 47 agents sawtoothed between 2.3GB and 5.67GB against a 6GB limit and were OOM killed every 35–64s. Trimming the agent list only moved the deadline (20 agents lasted ~101s), which is what you'd expect if the peak is a function of concurrent fetches rather than of agent count directly.
Heap profile from the failing pod, 28s apart:
FetchRawBeaconStateethrpc.Provider.Do→GetBadBlocksio.ReadAll(flat)Change
A process-wide semaphore for each path. The slot is held across the fetch, the compression and the upload, because both the raw and compressed copies are live for that whole window. For bad blocks it is held across the indexing pass, since the decoded slice is retained until that completes — releasing after the fetch would not bound anything.
Also drops the raw state reference once compressed, so only one copy survives the upload.
Two new config knobs, both defaulted to 10:
Results
47 agents against a mock beacon node serving 100MB states, 6GB / 2 CPU, config otherwise unchanged:
5.2x lower peak with all 47 agents running, and the sawtooth is replaced by a narrow band with GC comfortably ahead.
Bounding concurrency also made state archival actually work — the production deployment this came from had persisted 20 states in 10 days, because the fetches were contending badly enough to die before persisting.
Notes for review
agent.New. Threading a limiter through the single config would avoid the first-writer-wins wart at the cost of changing that signature — happy to do that instead if preferred.io.ReadAlland decode but produced no validBadBlockrecords, so the indexing half of that loop did not run. Its limit of 10 is reasoned from the relative payload sizes (~18MB vs ~130MB per item) rather than benchmarked.FetchRawBeaconStatereturns[]bytefromethpandaops/beaconwith no streaming variant, so that is a two-repo change. This bounds the damage in the meantime.golang.org/x/syncwas already a direct dependency; nogo.modchange.🤖 Generated with Claude Code