mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-09-26 18:04:33 +00:00
Local fencing primitives for block volumes. Every write path validates role + epoch + lease before accepting data. RoleNone (default) skips all checks for Phase 3 backward compatibility. New files: epoch.go, lease.go, role.go, write_gate.go Modified: superblock.go (Epoch field), blockvol.go (fencing fields, writeGate in WriteLBA/Trim), group_commit.go (PostSyncCheck/Gotcha A), dirty_map.go (P3-BUG-9 power-of-2 panic) Bug fixes: BUG-4A-1 (atomic epoch), BUG-4A-2 (CAS SetRole), BUG-4A-3 (mutex SetEpoch), BUG-4A-4 (single role.Load), BUG-4A-6 (safeCallback recover) 837 tests (557 engine + 280 iSCSI), all passing. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
39 lines
969 B
Go
39 lines
969 B
Go
package blockvol
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// Epoch returns the current epoch of this volume.
|
|
func (v *BlockVol) Epoch() uint64 {
|
|
return v.epoch.Load()
|
|
}
|
|
|
|
// SetEpoch persists a new epoch to the superblock and fsyncs.
|
|
// Must be durable before writes are accepted at the new epoch.
|
|
func (v *BlockVol) SetEpoch(epoch uint64) error {
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
|
|
v.super.Epoch = epoch
|
|
v.epoch.Store(epoch)
|
|
|
|
if _, err := v.fd.Seek(0, os.SEEK_SET); err != nil {
|
|
return fmt.Errorf("blockvol: seek superblock: %w", err)
|
|
}
|
|
if _, err := v.super.WriteTo(v.fd); err != nil {
|
|
return fmt.Errorf("blockvol: write superblock: %w", err)
|
|
}
|
|
if err := v.fd.Sync(); err != nil {
|
|
return fmt.Errorf("blockvol: sync superblock: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetMasterEpoch sets the expected epoch from the master.
|
|
// Writes are rejected if v.epoch != v.masterEpoch (when role != RoleNone).
|
|
func (v *BlockVol) SetMasterEpoch(epoch uint64) {
|
|
v.masterEpoch.Store(epoch)
|
|
}
|