Skip to content

feat(statesync): implement statesync spec for the new approach - #663

Merged
shotonoff merged 22 commits into
v0.13-devfrom
feat/statesync-implement-spec-for-new-approach
Jul 19, 2023
Merged

shotonoff merged 22 commits into
v0.13-devfrom
feat/statesync-implement-spec-for-new-approach

Conversation

@shotonoff

@shotonoff shotonoff commented Jul 19, 2023 •

Copy link
Copy Markdown
Collaborator

Issue being fixed or feature implemented

To be able to perform state synchronization in Dash Platform, Tenderdash must be compatible with some restriction of GroveDB.
This PR contains the changes of a new approach state synchonization, that is described in specification

What was done?

  • Replaced chunk index on chunk ID. Chunk Index is the number of the piece of data of a snapshot. Chunk ID is the path to the root node of subtree.
  • Replaced format field on version
  • Added the new apply chunk result: complete, to be able to get a final status of a snapshot

How Has This Been Tested?

Unit Tests / E2E tests

Breaking Changes

The original way of replicating state synchronization is obsolete and has been replaced by another approach

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

@shotonoff shotonoff changed the title Feat/statesync implement spec for new approach feat(statesync): implement statesync spec for the new approach Jul 19, 2023
@QuantumExplorer

Copy link
Copy Markdown
Member

Was told this was already reviewed, so I'm accepting it.

@shotonoff
shotonoff merged commit 51b15af into v0.13-dev Jul 19, 2023
@lklimek
lklimek deleted the feat/statesync-implement-spec-for-new-approach branch February 1, 2024 13:36
PastaPastaPasta added a commit that referenced this pull request Sep 2, 2026
…omics

SnapshotChunksTotal was left on the Metricer interface when the snapshot_chunks_total RPC field was removed in #663; nothing consumes it and chunks are discovered incrementally, so it is not a usable progress denominator. Remove it along with the chunkQueue.TotalChunksCount and snapshotPool.Len plumbing that only existed to serve it.

Switch the syncer's lock-free counters to atomic.Int64, matching the evidence pool and p2p rate limiter, so the alignment guarantee is structural rather than by comment. Drop the stale snapshots != nil guard in TotalSnapshots, and make tests read the metrics through the accessors instead of raw fields.
lklimek added a commit that referenced this pull request Sep 11, 2026
…tus (#1426)

* fix(statesync): implement missing Metricer methods and keep metrics after sync

The state-sync counters surfaced by the /status RPC were never usable: *statesync.Reactor did not implement SnapshotChunksCount()/SnapshotChunksTotal(), so it silently failed to satisfy the Metricer interface, and the remaining getters read through r.syncer, which syncComplete() nils the moment a sync ends - so even a wired-up reactor would report zeros for the rest of the process lifetime.

- Implement SnapshotChunksCount()/SnapshotChunksTotal(), sourced from the syncer's chunk queue while a sync is active (chunks are content-addressed and discovered incrementally, so the total is the number of distinct chunks seen so far), and add a compile-time var _ Metricer assertion so the interface can no longer drift.
- Snapshot the sync's final values (total snapshots, avg chunk time, snapshot height, chunk counts) into Reactor fields under r.mtx before syncComplete() drops the syncer; getters read the live syncer during a sync and the snapshot afterwards. The syncer likewise records the queue's final chunk counts before dropping it at the end of Sync().
- Fix data races: backfilledBlocks/backfillBlockTotal were written without r.mtx while the Metricer getters read them under RLock; the writes now take the lock. The syncer's avgChunkTime/lastSyncedSnapshotHeight were similarly read across goroutines; they are now accessed atomically, and TotalSnapshots() now uses a locked pool.Len() instead of reading the pool map directly.

Persisting the completed-sync record across process restart is deliberately out of scope: it needs a state-store change, which is a design decision left to maintainers.

Co-Authored-By: Claude Fable 5 <[email protected]>

* fix(node): wire the statesync reactor into the RPC env as StateSyncMetricer

The statesync reactor was constructed and dropped straight into node.services with no reference kept, so env.StateSyncMetricer was never assigned anywhere and /status unconditionally skipped the state-sync counters. Keep a reference and expose it on rpcEnv, mirroring how ConsensusReactor and BlockSyncReactor are wired.

Co-Authored-By: Claude Fable 5 <[email protected]>

* docs(rpc): remove phantom snapshot_chunks_total field from openapi

The SyncInfo Go struct has no snapshot_chunks_total field; the openapi schema documented one that /status never returns. Remove it to match the actual response.

Co-Authored-By: Claude Fable 5 <[email protected]>

* test(statesync): cover the Metricer surface and /status metrics wiring

- TestReactorMetricer exercises every Metricer getter across the sync lifecycle: zeros before any sync, live syncer values during a sync (snapshot pool, chunk queue counts), and persistence of the snapshotted values after releaseChunkQueue and syncComplete drop the syncing objects.
- TestReactor_Sync now asserts that a completed end-to-end sync leaves the final metrics readable (snapshot height, snapshot/chunk counts, avg chunk time) after the syncer is gone.
- TestStatusStateSyncMetrics verifies /status copies the metrics from a wired StateSyncMetricer into sync_info.
All run under go test -race.

Co-Authored-By: Claude Fable 5 <[email protected]>

* docs(statesync): clarify metric accessor invariants from review

Note that syncer.snapshots is set once at construction (why TotalSnapshots reads it without s.mtx), and that the test stub's SnapshotChunksTotal only satisfies the interface since SyncInfo has no such field.

Co-Authored-By: Claude Fable 5 <[email protected]>

* fix(statesync): count discovered snapshots cumulatively for /status

TotalSnapshots previously returned the snapshot pool size, which shrinks on rejections and peer disconnects; a peer leaving between the restore and syncComplete (e.g. during backfill) zeroed the metric this PR exists to fix. Back it with an atomic counter incremented when AddSnapshot accepts a new snapshot, and pin in the metrics test that the value survives emptying the pool.

Co-Authored-By: Claude Fable 5 <[email protected]>

* test(node): assert the statesync reactor is wired as the RPC StateSyncMetricer

Removing the rpcEnv.StateSyncMetricer wiring kept every existing test green; TestNodeStartStop now fails if the /status metrics source is unplugged.

Co-Authored-By: Claude Fable 5 <[email protected]>

* refactor(statesync): drop unused SnapshotChunksTotal and use typed atomics

SnapshotChunksTotal was left on the Metricer interface when the snapshot_chunks_total RPC field was removed in #663; nothing consumes it and chunks are discovered incrementally, so it is not a usable progress denominator. Remove it along with the chunkQueue.TotalChunksCount and snapshotPool.Len plumbing that only existed to serve it.

Switch the syncer's lock-free counters to atomic.Int64, matching the evidence pool and p2p rate limiter, so the alignment guarantee is structural rather than by comment. Drop the stale snapshots != nil guard in TotalSnapshots, and make tests read the metrics through the accessors instead of raw fields.

* docs(rpc): describe the state sync fields in sync_info

These fields carry real values now, and chunk_process_avg_time is a nanosecond duration serialized as a string, which is not guessable from the schema.

---------

Co-authored-by: Claude Fable 5 <[email protected]>
Co-authored-by: lklimek <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants