Files
seaweedfs/weed/shell/command_fs_verify_restore_source.go
T
Chris LuandClaude Opus 5.5 9d0c5ffe6a shell: fs.verify -restoreFrom loads a volume's index on its first lookup
Pointed at a data directory, the restore source read every .idx into
memory before verification began, though missing chunks usually name
few of those volumes, and one unreadable .idx stopped the whole run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-06 00:53:11 -08:00

169 lines
5.2 KiB
Go

package shell
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/seaweedfs/seaweedfs/weed/storage/backend"
"github.com/seaweedfs/seaweedfs/weed/storage/idx"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
"github.com/seaweedfs/seaweedfs/weed/storage/types"
"github.com/seaweedfs/seaweedfs/weed/storage/volume_info"
)
// errNotInRestoreSource means the restore source holds no live record of the
// needle, as opposed to holding bytes that fail a check.
var errNotInRestoreSource = errors.New("not in restore source")
// restoreSource reads needles from regular volume files left on disk outside
// any volume server, <collection>_<vid>.dat/.idx or <vid>.dat/.idx.
type restoreSource struct {
volumes map[needle.VolumeId]*restoreVolume
}
// restoreVolume loads its .idx on the first lookup: a directory of many
// volumes usually holds few that missing chunks name.
type restoreVolume struct {
collection string
idxPath string
datPath string
dat backend.BackendStorageFile
version needle.Version
needles map[types.NeedleId]restoreNeedleLocation
indexErr error
}
type restoreNeedleLocation struct {
offset types.Offset
size types.Size
}
func openRestoreSource(dir string) (*restoreSource, error) {
idxFiles, err := filepath.Glob(filepath.Join(dir, "*.idx"))
if err != nil {
return nil, err
}
source := &restoreSource{volumes: make(map[needle.VolumeId]*restoreVolume)}
opened := false
defer func() {
if !opened {
source.Close()
}
}()
for _, idxPath := range idxFiles {
base := strings.TrimSuffix(idxPath, ".idx")
collection, name := "", filepath.Base(base)
if i := strings.LastIndex(name, "_"); i > 0 {
collection, name = name[:i], name[i+1:]
}
vid, parseErr := needle.NewVolumeId(name)
if parseErr != nil {
continue
}
if existing, found := source.volumes[vid]; found {
return nil, fmt.Errorf("volume %d is in both %s and %s", vid, existing.datPath, base+".dat")
}
volume, openErr := openRestoreVolume(base)
if openErr != nil {
return nil, fmt.Errorf("volume %d: %w", vid, openErr)
}
volume.collection = collection
source.volumes[vid] = volume
}
if len(source.volumes) == 0 {
return nil, fmt.Errorf("no <vid>.idx or <collection>_<vid>.idx volume files in %s", dir)
}
opened = true
return source, nil
}
func openRestoreVolume(base string) (*restoreVolume, error) {
info, hasRemoteFile, _, err := volume_info.MaybeLoadVolumeInfo(base + ".vif")
if err != nil {
return nil, err
}
if hasRemoteFile {
return nil, fmt.Errorf("%s.vif points at a remote .dat", base)
}
if info.BytesOffset != 0 && info.BytesOffset != types.OffsetSize {
return nil, fmt.Errorf("%s.vif records %d-byte offsets, this binary reads %d-byte offsets", base, info.BytesOffset, types.OffsetSize)
}
datFile, err := os.Open(base + ".dat")
if err != nil {
return nil, err
}
dat := backend.NewDiskFile(datFile)
superBlock, err := super_block.ReadSuperBlock(dat)
if err != nil {
dat.Close()
return nil, err
}
if !needle.IsSupportedVersion(superBlock.Version) {
dat.Close()
return nil, fmt.Errorf("%s.dat has unsupported needle version %d", base, superBlock.Version)
}
return &restoreVolume{idxPath: base + ".idx", datPath: base + ".dat", dat: dat, version: superBlock.Version}, nil
}
// loadRestoreIndex maps each key to its last live record. A later tombstone
// does not hide an earlier record: a needle deleted on the volume but still
// referenced by the filer is worth restoring when its bytes are intact.
func loadRestoreIndex(idxPath string) (map[types.NeedleId]restoreNeedleLocation, error) {
idxFile, err := os.Open(idxPath)
if err != nil {
return nil, err
}
defer idxFile.Close()
needles := make(map[types.NeedleId]restoreNeedleLocation)
err = idx.WalkIndexFile(idxFile, 0, func(key types.NeedleId, offset types.Offset, size types.Size) error {
if !offset.IsZero() && size.IsValid() {
needles[key] = restoreNeedleLocation{offset: offset, size: size}
}
return nil
})
if err != nil {
return nil, fmt.Errorf("read %s: %w", idxPath, err)
}
return needles, nil
}
func (s *restoreSource) Close() {
for _, v := range s.volumes {
v.dat.Close()
}
}
// readNeedle returns the needle a file id names, after checking that the
// record holds that key and cookie and that its data matches its CRC.
func (s *restoreSource) readNeedle(vid needle.VolumeId, key types.NeedleId, cookie types.Cookie) (*needle.Needle, error) {
v, found := s.volumes[vid]
if !found {
return nil, errNotInRestoreSource
}
if v.needles == nil && v.indexErr == nil {
v.needles, v.indexErr = loadRestoreIndex(v.idxPath)
}
if v.indexErr != nil {
return nil, v.indexErr
}
loc, found := v.needles[key]
if !found {
return nil, errNotInRestoreSource
}
n := &needle.Needle{}
if err := n.ReadData(v.dat, loc.offset.ToActualOffset(), loc.size, v.version); err != nil {
return nil, fmt.Errorf("read %s at %d: %w", v.datPath, loc.offset.ToActualOffset(), err)
}
if n.Id != key {
return nil, fmt.Errorf("%s at %d holds needle %s, want %s", v.datPath, loc.offset.ToActualOffset(), n.Id, key)
}
if n.Cookie != cookie {
return nil, fmt.Errorf("needle %s in %s has cookie %08x, want %08x", key, v.datPath, uint32(n.Cookie), uint32(cookie))
}
return n, nil
}