Skip to content

perf(consensus): skip fsync for unsigned internal messages (block parts) (backport #5695) - #5828

Merged
mattac21 merged 2 commits into
v0.39.xfrom
mergify/bp/v0.39.x/pr-5695
May 4, 2026
Merged

mattac21 merged 2 commits into
v0.39.xfrom
mergify/bp/v0.39.x/pr-5695

Conversation

@mergify

@mergify mergify Bot commented May 4, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Skip fsync (WriteSync) for BlockPartMessage in the consensus WAL's receiveRoutine, using buffered Write instead
  • Only signed messages (VoteMessage, ProposalMessage) retain WriteSync for double-signing prevention
  • Adds explicit type-switch with default: panic to prevent future signed message types from silently bypassing fsync
  • Replaces stale TODO in replay_test.go with documentation of the selective-fsync approach

Safety analysis

Why signed messages MUST keep WriteSync:

  • Pre-sign FlushAndSync ensures WAL replay reaches the same deterministic state
  • Post-sign WriteSync ensures the signed message is durable before handleMsg processes/broadcasts it
  • Without this, crash-replay could re-sign a different vote → equivocation

Why BlockPartMessage can safely use Write:

  1. Block parts are unsigned data chunks derived from the proposal block
  2. On crash: proposal exists (fsynced), some block parts may be missing → round times out → consensus proceeds
  3. The periodic 2-second WAL flush (processFlushTicks) eventually flushes them
  4. Any subsequent WriteSync (vote) or FlushAndSync (pre-sign) also flushes buffered block parts
  5. EndHeightMessage uses WriteSync, which flushes ALL buffered writes before the end marker

Defensive measures:

  • Explicit case *BlockPartMessage (not default) ensures new message types panic rather than silently bypassing fsync
  • default: panic("unexpected internal message type") with SAFETY comment for future maintainers
  • WAL Write failure panics (matches WriteSync behavior) since write errors indicate disk failure

Benchmark results (Apple M1 Max SSD)

BenchmarkWALWrite-10          ~1μs/op     1680 B/op    13 allocs/op
BenchmarkWALWriteSync-10      ~4.4ms/op   1680 B/op    13 allocs/op

BenchmarkWALRoundSimulation/AllWriteSync-10       ~233ms/round
BenchmarkWALRoundSimulation/SelectiveFsync-10     ~9.3ms/round  (~25x faster)

PR checklist

…ts) (#5695)

## Summary

- Skip `fsync` (`WriteSync`) for `BlockPartMessage` in the consensus
WAL's `receiveRoutine`, using buffered `Write` instead
- Only signed messages (`VoteMessage`, `ProposalMessage`) retain
`WriteSync` for double-signing prevention
- Adds explicit type-switch with `default: panic` to prevent future
signed message types from silently bypassing fsync
- Replaces stale TODO in `replay_test.go` with documentation of the
selective-fsync approach

## Safety analysis

**Why signed messages MUST keep `WriteSync`:**
- Pre-sign `FlushAndSync` ensures WAL replay reaches the same
deterministic state
- Post-sign `WriteSync` ensures the signed message is durable before
`handleMsg` processes/broadcasts it
- Without this, crash-replay could re-sign a different vote →
equivocation

**Why `BlockPartMessage` can safely use `Write`:**
1. Block parts are unsigned data chunks derived from the proposal block
2. On crash: proposal exists (fsynced), some block parts may be missing
→ round times out → consensus proceeds
3. The periodic 2-second WAL flush (`processFlushTicks`) eventually
flushes them
4. Any subsequent `WriteSync` (vote) or `FlushAndSync` (pre-sign) also
flushes buffered block parts
5. `EndHeightMessage` uses `WriteSync`, which flushes ALL buffered
writes before the end marker

**Defensive measures:**
- Explicit `case *BlockPartMessage` (not `default`) ensures new message
types panic rather than silently bypassing fsync
- `default: panic("unexpected internal message type")` with SAFETY
comment for future maintainers
- WAL `Write` failure panics (matches `WriteSync` behavior) since write
errors indicate disk failure

## Benchmark results (Apple M1 Max SSD)

```
BenchmarkWALWrite-10          ~1μs/op     1680 B/op    13 allocs/op
BenchmarkWALWriteSync-10      ~4.4ms/op   1680 B/op    13 allocs/op

BenchmarkWALRoundSimulation/AllWriteSync-10       ~233ms/round
BenchmarkWALRoundSimulation/SelectiveFsync-10     ~9.3ms/round  (~25x faster)
```

---

#### PR checklist

- [x] Tests written/updated
- [x] Changelog entry added in `CHANGELOG.md`
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
Co-authored-by: Dmitry S <[email protected]>
(cherry picked from commit 62a15e7)
@greptile-apps

greptile-apps Bot commented May 4, 2026

Copy link
Copy Markdown
Contributor

PR author is not in the allowed authors list.

@github-actions

github-actions Bot commented May 4, 2026

Copy link
Copy Markdown

🔒 WARNING: unsigned commits detected

This pull request contains 1 commit(s) without a verified signature.

How to fix:

  1. Set up commit signing (GPG or SSH).
  2. Amend/rebase so every commit in this PR is verified-signed.
  3. Push the updated branch and open a new PR, or ask a maintainer to reopen once fixed.

Docs: https://docs.github.com/authentication/managing-commit-signature-verification

Unsigned commits:

@github-actions

github-actions Bot commented May 4, 2026

Copy link
Copy Markdown

🔒 WARNING: unsigned commits detected

This pull request contains 1 commit(s) without a verified signature.

How to fix:

  1. Set up commit signing (GPG or SSH).
  2. Amend/rebase so every commit in this PR is verified-signed.
  3. Push the updated branch and open a new PR, or ask a maintainer to reopen once fixed.

Docs: https://docs.github.com/authentication/managing-commit-signature-verification

Unsigned commits:

@mattac21
mattac21 merged commit 723a493 into v0.39.x May 4, 2026
34 checks passed
@mattac21
mattac21 deleted the mergify/bp/v0.39.x/pr-5695 branch May 4, 2026 20:42
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.

2 participants