Skip to content

feat(snapshot):完善快照逻辑;#33

Merged
NeverENG merged 1 commit into
mainfrom
feature/snapshot
May 12, 2026
Merged

feat(snapshot):完善快照逻辑;#33
NeverENG merged 1 commit into
mainfrom
feature/snapshot

Conversation

@NeverENG
Copy link
Copy Markdown
Owner

@NeverENG NeverENG commented May 12, 2026

Summary by CodeRabbit

  • New Features

    • Added snapshot replay functionality to efficiently handle snapshot entries during log application.
  • Bug Fixes

    • Improved snapshot-aware log handling for election votes, log replication, and commit advancement.
    • Fixed snapshot fallback behavior for followers when indices fall within snapshot ranges.
  • Chores

    • Updated capacity configuration settings (MaxConn, work pool, and message channel sizes increased to 100).
    • Refactored snapshot creation API to simplify the interface.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 12, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 85dc1ef0-ca6b-468a-a5fa-91b27036c977

📥 Commits

Reviewing files that changed from the base of the PR and between ab48a70 and 6eb8862.

⛔ Files ignored due to path filters (3)
  • Raft/raft_data/raft_log.dat is excluded by !**/*.dat
  • Server/raft_data/raft_log.dat is excluded by !**/*.dat
  • Server/raft_data/raft_state.dat is excluded by !**/*.dat
📒 Files selected for processing (9)
  • Raft/raft.go
  • Raft/raft_test.go
  • Raft/rpc.go
  • config/config.json
  • service/fsm.go
  • service/router.go
  • storage/engine.go
  • storage/istorage/IMemTable.go
  • storage/zstorage/memtable.go

📝 Walkthrough

Walkthrough

Raft log/index handling is refactored to translate between absolute indices and snapshot-compacted in-memory log slices. Leaders initialize replication state using snapshot metadata and fallback to snapshot install when followers lag; followers validate and apply logs relative to snapshot boundaries. Snapshot creation internally serializes log entries, and the FSM replays snapshot contents to storage via a new FlushToSSTable path. Configuration capacity settings are increased and debug logging is removed.

Changes

Snapshot-aware Raft implementation

Layer / File(s) Summary
Index translation helpers and election
Raft/raft.go
Binary import added; getTermAt and getLastLogIndex helpers translate between absolute indices and in-memory log positions using snapshot metadata; election vote computation now uses snapshot metadata when logs are compacted; single-node self-vote early-return removed.
Leader initialization and heartbeats
Raft/raft.go
Leader initializes nextIndex/matchIndex using snapshot-aware absolute indices; heartbeats check if follower's prevLogIndex falls within snapshot range and send InstallSnapshot RPC; on success, match state is updated via getLastLogIndex().
Log replication with snapshot fallback
Raft/raft.go
Log replication computes prevLogTerm using snapshot helpers, sends InstallSnapshot when follower lags behind snapshot, slices entries using snapshot-relative offsets, and updates leader state on success.
Commit advancement and log application
Raft/raft.go
Commit index advancement iterates by absolute log indices; committed log application translates absolute lastApplied to relative indices before sending entries to ApplyCh.
Follower RPC snapshot-aware validation and installation
Raft/rpc.go
Vote eligibility uses snapshot metadata as "last index/term"; AppendEntries validates and applies entries relative to snapshot boundaries; InstallSnapshot rejects stale snapshots, trims logs using snapshot-relative offsets, and updates lastSnapshotIndex.
Snapshot creation, serialization, and persistence
Raft/raft.go
TakeSnapshot signature changed to accept only index; builds snapshot entries from in-memory log using relative bounds, internally serializes via SerializeLogEntries, truncates WAL, and renumbers in-memory log; exports SerializeLogEntries and DeserializeLogEntries helpers for big-endian entry encoding/decoding; FSM snapshot entries are logged before application.
Raft test updates
Raft/raft_test.go
TestSnapshotCreation and TestSnapshotPersistence updated to call r.TakeSnapshot(index) with single argument.

Storage layer snapshot replay

Layer / File(s) Summary
FSM snapshot detection and replay
service/fsm.go
FSM detects entry.IsSnapshot, asynchronously calls replaySnapshot, and returns early; replaySnapshot imports istorage, deserializes entries, unmarshals commands, converts to istorage.LogEntry, and flushes batch to SSTable.
Storage FlushToSSTable implementation
storage/engine.go, storage/istorage/IMemTable.go, storage/zstorage/memtable.go
Engine delegates FlushToSSTable to MemTable; IMemTable interface declares the method; MemTable builds temporary skiplist, flushes ordered result to SSTable, signals compaction, logs completion; SSTable "found" logging removed from Get.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • NeverENG/BanDB#30: Exports SSTable flush methods and hardens I/O; this PR adds snapshot replay flushing via the same storage interface.

Poem

🐰 A rabbit hops through snapshot lands,
Where logs once split by time's own hands,
Now absolute indices guide the way,
Through snapshots old and entries new today,
Storage speaks, and FSM listens true,
All in sync—a Raft born anew! 🚀

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/snapshot

Comment @coderabbitai help to get the list of available commands and usage tips.

@NeverENG NeverENG merged commit 2df4f40 into main May 12, 2026
1 of 2 checks passed
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.

1 participant