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) } }) } }