mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
A restore source opens <collection>_<vid>.dat/.idx or <vid>.dat/.idx files read-only and maps each key to its last live .idx record. A later tombstone does not hide an earlier record, since a filer may still reference the needle. Reads check the record's key, the file id's cookie and the CRC.
163 lines
5.0 KiB
Go
163 lines
5.0 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
|
|
}
|
|
|
|
type restoreVolume struct {
|
|
collection string
|
|
datPath string
|
|
dat backend.BackendStorageFile
|
|
version needle.Version
|
|
needles map[types.NeedleId]restoreNeedleLocation
|
|
}
|
|
|
|
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)
|
|
}
|
|
needles, err := loadRestoreIndex(base + ".idx")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
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{datPath: base + ".dat", dat: dat, version: superBlock.Version, needles: needles}, 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
|
|
}
|
|
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
|
|
}
|