Files
seaweedfs/weed/storage/store_blockvol.go
T
Ping QiuandClaude Opus 4.6 8b2b5f6f66 feat: Phase 6 CP6-3 -- failover + rebuild in Kubernetes, 126 tests
Wire low-level fencing primitives to master/VS control plane and CSI:

- Proto: replica/rebuild address fields on assignment/info/response messages
- Assignment queue: retain-until-confirmed (Peek+Confirm), stale epoch pruning
- VS assignment receiver: processes assignments from HeartbeatResponse
- BlockService replication: ProcessAssignments, deterministic ports (FNV hash)
- Registry replica tracking: SetReplica/ClearReplica/SwapPrimaryReplica
- CreateBlockVolume: primary + replica, enqueues assignments, single-copy mode
- Failover: lease-aware promotion, deferred timers with cancellation on reconnect
- ControllerPublish: returns fresh primary iSCSI address after failover
- Recovery: recoverBlockVolumes drains pendingRebuilds, enqueues Rebuilding
- Real integration tests on M02: failover address switch, rebuild data
  consistency, full lifecycle failover+rebuild (3 tests, all PASS)

Review fixes (12 findings, 5 High, 5 Medium, 2 Low):
- R1-1: AllocateBlockVolume returns replication ports
- R1-2: setupPrimaryReplication starts rebuild server
- R1-3: VS sends periodic block heartbeat for assignment confirmation
- R2-F1: LastLeaseGrant set before Register (no stale-lease race)
- R2-F2: Deferred promotion timers cancelled on VS reconnect
- R2-F3: SwapPrimaryReplica uses RoleToWire instead of uint32(1)
- R2-F4: DeleteBlockVolume deletes replica (best-effort)
- R2-F5: SwapPrimaryReplica computes epoch atomically under lock
- QA: SetReplica removes old replica from byServer index (BUG-QA-CP63-1)

126 CP6-3 tests (67 dev + 48 QA + 8 integration + 3 real).
Cumulative Phase 6: 352 tests. All PASS.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-05 00:52:05 -08:00

146 lines
4.5 KiB
Go

package storage
import (
"fmt"
"sync"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/storage/blockvol"
)
// BlockVolumeStore manages block volumes (iSCSI-backed).
// It is a standalone component held by VolumeServer, not embedded into Store,
// to keep the existing Store codebase unchanged.
type BlockVolumeStore struct {
mu sync.RWMutex
volumes map[string]*blockvol.BlockVol // keyed by volume file path
diskTypes map[string]string // path -> disk type (e.g. "ssd")
}
// NewBlockVolumeStore creates a new block volume manager.
func NewBlockVolumeStore() *BlockVolumeStore {
return &BlockVolumeStore{
volumes: make(map[string]*blockvol.BlockVol),
diskTypes: make(map[string]string),
}
}
// AddBlockVolume opens and registers a block volume.
// diskType is metadata for heartbeat reporting (e.g. "ssd", "hdd").
func (bs *BlockVolumeStore) AddBlockVolume(path, diskType string, cfgs ...blockvol.BlockVolConfig) (*blockvol.BlockVol, error) {
bs.mu.Lock()
defer bs.mu.Unlock()
if _, ok := bs.volumes[path]; ok {
return nil, fmt.Errorf("block volume already registered: %s", path)
}
vol, err := blockvol.OpenBlockVol(path, cfgs...)
if err != nil {
return nil, fmt.Errorf("open block volume %s: %w", path, err)
}
bs.volumes[path] = vol
bs.diskTypes[path] = diskType
glog.V(0).Infof("block volume registered: %s (disk=%s)", path, diskType)
return vol, nil
}
// RemoveBlockVolume closes and unregisters a block volume.
func (bs *BlockVolumeStore) RemoveBlockVolume(path string) error {
bs.mu.Lock()
defer bs.mu.Unlock()
vol, ok := bs.volumes[path]
if !ok {
return fmt.Errorf("block volume not found: %s", path)
}
if err := vol.Close(); err != nil {
glog.Warningf("error closing block volume %s: %v", path, err)
}
delete(bs.volumes, path)
delete(bs.diskTypes, path)
glog.V(0).Infof("block volume removed: %s", path)
return nil
}
// GetBlockVolume returns a registered block volume by path.
func (bs *BlockVolumeStore) GetBlockVolume(path string) (*blockvol.BlockVol, bool) {
bs.mu.RLock()
defer bs.mu.RUnlock()
vol, ok := bs.volumes[path]
return vol, ok
}
// ListBlockVolumes returns the paths of all registered block volumes.
func (bs *BlockVolumeStore) ListBlockVolumes() []string {
bs.mu.RLock()
defer bs.mu.RUnlock()
paths := make([]string, 0, len(bs.volumes))
for p := range bs.volumes {
paths = append(paths, p)
}
return paths
}
// CollectBlockVolumeHeartbeat returns status for all registered
// block volumes, suitable for inclusion in a heartbeat message.
func (bs *BlockVolumeStore) CollectBlockVolumeHeartbeat() []blockvol.BlockVolumeInfoMessage {
bs.mu.RLock()
defer bs.mu.RUnlock()
msgs := make([]blockvol.BlockVolumeInfoMessage, 0, len(bs.volumes))
for path, vol := range bs.volumes {
msgs = append(msgs, blockvol.ToBlockVolumeInfoMessage(path, bs.diskTypes[path], vol))
}
return msgs
}
// WithVolume looks up a volume by path and calls fn while holding RLock.
// This prevents RemoveBlockVolume from closing the volume while fn runs
// (BUG-CP4B3-1: TOCTOU between GetBlockVolume and HandleAssignment).
func (bs *BlockVolumeStore) WithVolume(path string, fn func(*blockvol.BlockVol) error) error {
bs.mu.RLock()
defer bs.mu.RUnlock()
vol, ok := bs.volumes[path]
if !ok {
return fmt.Errorf("block volume not found: %s", path)
}
return fn(vol)
}
// ProcessBlockVolumeAssignments applies a batch of assignments from master.
// Returns a slice of errors parallel to the input (nil = success).
// Unknown volumes and invalid transitions are logged and returned as errors,
// but do not stop processing of remaining assignments.
func (bs *BlockVolumeStore) ProcessBlockVolumeAssignments(
assignments []blockvol.BlockVolumeAssignment,
) []error {
errs := make([]error, len(assignments))
for i, a := range assignments {
role := blockvol.RoleFromWire(a.Role)
ttl := blockvol.LeaseTTLFromWire(a.LeaseTtlMs)
if err := bs.WithVolume(a.Path, func(vol *blockvol.BlockVol) error {
return vol.HandleAssignment(a.Epoch, role, ttl)
}); err != nil {
errs[i] = err
glog.Warningf("assignment: volume %s epoch=%d role=%s: %v",
a.Path, a.Epoch, role, err)
}
}
return errs
}
// Close closes all block volumes.
func (bs *BlockVolumeStore) Close() {
bs.mu.Lock()
defer bs.mu.Unlock()
for path, vol := range bs.volumes {
if err := vol.Close(); err != nil {
glog.Warningf("error closing block volume %s: %v", path, err)
}
delete(bs.volumes, path)
}
glog.V(0).Infof("all block volumes closed")
}