Files
seaweedfs/weed/shell/command_volume_fsck_cutoff_test.go
T
5038508fd9 shell: volume.fsck says how many chunks its cutoff left out of the missing-chunk check (#13318)
* shell: volume.fsck says how many chunks its cutoff left out of the missing-chunk check

volume.fsck -findMissingChunksInFiler leaves out every filer chunk written
within -cutoffTimeAgo, five hours by default, and did not say so. Run after
volume servers lost power, it found nothing missing for exactly the files
at risk: the ones written last.

The check now counts the chunks it leaves out for the cutoff and ends with
one line that gives the number and points at -cutoffTimeAgo. The default
cutoff stays, and the orphan check is not touched.

* shell: volume.fsck counts only the chunks its run would have checked

The walk reads the whole filer, so a recent chunk on a volume that
-volumeId or -collection left out of the run was counted too, and the
report said a shorter cutoff would check it. Count a chunk only when its
volume is one of the run's, or one the master does not list and -volumeId
does not leave out, which is what the check itself looks at.

* shell: volume.fsck applies -modifyTimeAgo before counting chunks the cutoff left out

A chunk older than -modifyTimeAgo is not checked at any cutoff, so it
does not belong in the count a shorter -cutoffTimeAgo would check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 14:21:42 -08:00

86 lines
3.5 KiB
Go

package shell
import (
"bytes"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
)
// The missing-chunk check leaves out the chunks written within the cutoff.
// It has to say so: finding nothing missing says nothing about those, and
// they are the ones a crash takes.
func TestVolumeFsckReportsChunksNewerThanCutoff(t *testing.T) {
now := time.Now()
chunk := func(volumeId uint32, fileKey uint64, age time.Duration) *filer_pb.FileChunk {
return &filer_pb.FileChunk{Size: 1, ModifiedTsNs: now.Add(-age).UnixNano(), Fid: &filer_pb.FileId{VolumeId: volumeId, FileKey: fileKey, Cookie: 1}}
}
directory := &filer_pb.Entry{IsDirectory: true}
stub := &stubFilerServer{entries: map[string]*filer_pb.Entry{
"/": directory,
"/old": {Chunks: []*filer_pb.FileChunk{chunk(7, 11, 10*time.Hour)}},
"/recent": {Chunks: []*filer_pb.FileChunk{chunk(7, 22, time.Minute), chunk(7, 33, 2*time.Minute)}},
// volume 8 is listed by the master but not part of the run, volume 9 is
// not listed at all
"/elsewhere": {Chunks: []*filer_pb.FileChunk{chunk(8, 44, time.Minute)}},
"/unlisted": {Chunks: []*filer_pb.FileChunk{chunk(9, 55, time.Minute)}},
}}
env := startStubFiler(t, stub)
const cutoffTimeAgo = 5 * time.Hour
for _, tc := range []struct {
name string
volumeIds map[uint32]bool
modifyFromAtNs int64
cutoffFromAtNs int64
wantSkipped int64
wantReport string
}{
// the two of volume 7, and the one whose volume would be reported as not found
{name: "the default cutoff", cutoffFromAtNs: now.Add(-cutoffTimeAgo).UnixNano(), wantSkipped: 3,
wantReport: "3 chunks written in the last 5h0m0s were not checked"},
{name: "the default cutoff with -volumeId", volumeIds: map[uint32]bool{7: true}, cutoffFromAtNs: now.Add(-cutoffTimeAgo).UnixNano(), wantSkipped: 2,
wantReport: "2 chunks written in the last 5h0m0s were not checked"},
// -modifyTimeAgo leaves out the two-minute-old chunk of volume 7 at any cutoff
{name: "the default cutoff with -modifyTimeAgo", modifyFromAtNs: now.Add(-90 * time.Second).UnixNano(), cutoffFromAtNs: now.Add(-cutoffTimeAgo).UnixNano(), wantSkipped: 2,
wantReport: "2 chunks written in the last 5h0m0s were not checked"},
{name: "no cutoff", cutoffFromAtNs: 0, wantSkipped: 0},
} {
t.Run(tc.name, func(t *testing.T) {
verbose, findMissing, verify, force := false, true, false, false
var out bytes.Buffer
c := &commandVolumeFsck{
tempFolder: t.TempDir(),
verbose: &verbose,
findMissingChunksInFiler: &findMissing,
verifyNeedle: &verify,
forcePurging: &force,
writer: &out,
bucketsPath: "/buckets",
env: env,
volumeIds: tc.volumeIds,
knownVolumeIds: map[uint32]bool{7: true, 8: true},
}
vinfo := map[string]map[uint32]VInfo{"dn1": {7: {}}}
if err := c.collectFilerFileIdAndPaths(vinfo, false, nil, tc.modifyFromAtNs, tc.cutoffFromAtNs); err != nil {
t.Fatalf("collect filer file ids: %v", err)
}
if got := c.chunksNewerThanCutoff.Load(); got != tc.wantSkipped {
t.Fatalf("%d chunks counted as left out, want %d", got, tc.wantSkipped)
}
out.Reset()
c.reportChunksNewerThanCutoff(cutoffTimeAgo)
if tc.wantReport == "" {
if out.Len() != 0 {
t.Fatalf("nothing was left out, yet the check reports %q", out.String())
}
} else if !strings.Contains(out.String(), tc.wantReport) {
t.Fatalf("report %q does not say %q", out.String(), tc.wantReport)
}
})
}
}