From 9d0c5ffe6a0d155eeca3c47337c8bf9160f6063d Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 6 Dec 2026 00:53:11 -0800 Subject: [PATCH] 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 --- .../shell/command_fs_verify_restore_source.go | 16 +++++++--- .../command_fs_verify_restore_source_test.go | 32 +++++++++++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/weed/shell/command_fs_verify_restore_source.go b/weed/shell/command_fs_verify_restore_source.go index 76331dd558..70b38f5803 100644 --- a/weed/shell/command_fs_verify_restore_source.go +++ b/weed/shell/command_fs_verify_restore_source.go @@ -25,12 +25,16 @@ 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 { @@ -88,10 +92,6 @@ func openRestoreVolume(base string) (*restoreVolume, error) { 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 @@ -106,7 +106,7 @@ func openRestoreVolume(base string) (*restoreVolume, error) { 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 + 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 @@ -144,6 +144,12 @@ func (s *restoreSource) readNeedle(vid needle.VolumeId, key types.NeedleId, cook 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 diff --git a/weed/shell/command_fs_verify_restore_source_test.go b/weed/shell/command_fs_verify_restore_source_test.go index 9c05a8df67..af5834d5a4 100644 --- a/weed/shell/command_fs_verify_restore_source_test.go +++ b/weed/shell/command_fs_verify_restore_source_test.go @@ -91,8 +91,8 @@ func TestRestoreSourceReadsLastLiveRecord(t *testing.T) { if volume == nil || volume.collection != "pics" { t.Fatalf("volume 7 = %+v, want collection pics", volume) } - if len(volume.needles) != 4 { - t.Errorf("indexed %d needles, want 4", len(volume.needles)) + if volume.needles != nil { + t.Errorf("index loaded before the first lookup") } for _, c := range []struct { @@ -115,6 +115,10 @@ func TestRestoreSourceReadsLastLiveRecord(t *testing.T) { } } + if len(volume.needles) != 4 { + t.Errorf("indexed %d needles, want 4", len(volume.needles)) + } + // the earlier record of key 2 carried another cookie if _, err := source.readNeedle(7, 2, 0x22); err == nil || !strings.Contains(err.Error(), "cookie") { t.Errorf("stale cookie: got %v, want a cookie mismatch", err) @@ -171,6 +175,30 @@ func TestRestoreSourceOpen(t *testing.T) { t.Fatalf("got %v, want a duplicate volume error", err) } }) + t.Run("unreadable idx fails only its own lookups", func(t *testing.T) { + dir := t.TempDir() + v := newTestRestoreVolume(t, filepath.Join(dir, "7")) + v.write(1, 0x11, []byte("kept"), false) + v.close() + newTestRestoreVolume(t, filepath.Join(dir, "8")).close() + if err := os.Remove(filepath.Join(dir, "8.idx")); err != nil { + t.Fatal(err) + } + if err := os.Mkdir(filepath.Join(dir, "8.idx"), 0755); err != nil { + t.Fatal(err) + } + source, err := openRestoreSource(dir) + if err != nil { + t.Fatal(err) + } + defer source.Close() + if _, err := source.readNeedle(7, 1, 0x11); err != nil { + t.Errorf("volume 7: %v", err) + } + if _, err := source.readNeedle(8, 1, 0x11); err == nil || errors.Is(err, errNotInRestoreSource) { + t.Errorf("volume 8: got %v, want the index read error", err) + } + }) t.Run("idx without dat", func(t *testing.T) { dir := t.TempDir() newTestRestoreVolume(t, filepath.Join(dir, "7")).close()