diff --git a/weed/command/fix.go b/weed/command/fix.go index cbb1e0aef1..45dad91799 100644 --- a/weed/command/fix.go +++ b/weed/command/fix.go @@ -229,6 +229,18 @@ func SaveToIdx(scaner *VolumeFileScanner4Fix, idxName string) (ret error) { return nil } +// localDatSize is the length of the .dat the scan reads, or 0 when the volume +// is read from a remote tier. +func localDatSize(basepath, baseFileName string) int64 { + if _, hasRemoteFile, _, _ := volume_info.MaybeLoadVolumeInfo(path.Join(basepath, baseFileName+".vif")); hasRemoteFile { + return 0 + } + if info, err := os.Stat(path.Join(basepath, baseFileName+".dat")); err == nil { + return info.Size() + } + return 0 +} + func doFixOneVolume(basepath string, baseFileName string, collection string, volumeId int64, fixIncludeDeleted bool) { indexFileName := path.Join(basepath, baseFileName+".idx") @@ -256,9 +268,7 @@ func doFixOneVolume(basepath string, baseFileName string, collection string, vol nmDeleted: nmDeleted, includeDeleted: fixIncludeDeleted, } - if info, err := os.Stat(path.Join(basepath, baseFileName+".dat")); err == nil { - scanner.datSize = info.Size() - } + scanner.datSize = localDatSize(basepath, baseFileName) if err := storage.ScanVolumeFile(basepath, collection, vid, storage.NeedleMapInMemory, scanner); err != nil { err := fmt.Errorf("scan .dat File: %w", err) diff --git a/weed/command/fix_test.go b/weed/command/fix_test.go index 274846c926..bdd227e1b7 100644 --- a/weed/command/fix_test.go +++ b/weed/command/fix_test.go @@ -7,12 +7,14 @@ import ( "slices" "testing" + "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb" "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/needle_map" "github.com/seaweedfs/seaweedfs/weed/storage/super_block" "github.com/seaweedfs/seaweedfs/weed/storage/types" + "github.com/seaweedfs/seaweedfs/weed/storage/volume_info" ) // TestSaveToIdxWritesOffsetOrder guards that weed fix rebuilds the .idx as an @@ -200,3 +202,22 @@ func TestFixIgnoreErrorReportsIncompleteScan(t *testing.T) { } }) } + +func TestLocalDatSizeIgnoresRemoteTier(t *testing.T) { + dir := t.TempDir() + base, _ := writeFixTestVolume(t, dir, 2) + info, err := os.Stat(base + ".dat") + if err != nil { + t.Fatal(err) + } + if got := localDatSize(dir, "fix_7"); got != info.Size() { + t.Fatalf("localDatSize = %d, want %d", got, info.Size()) + } + vi := &volume_server_pb.VolumeInfo{Files: []*volume_server_pb.RemoteFile{{BackendType: "s3", Key: "fix_7.dat", FileSize: uint64(info.Size()) * 2}}} + if err := volume_info.SaveVolumeInfo(base+".vif", vi); err != nil { + t.Fatal(err) + } + if got := localDatSize(dir, "fix_7"); got != 0 { + t.Errorf("localDatSize with a remote .dat = %d, want 0", got) + } +}