diff --git a/weed/shell/command_fs_verify.go b/weed/shell/command_fs_verify.go index 0a702c38ca..03a36a93e1 100644 --- a/weed/shell/command_fs_verify.go +++ b/weed/shell/command_fs_verify.go @@ -75,8 +75,9 @@ func (c *commandFsVerify) Help() string { which only applies when the entry is unchanged since verification. A missing chunk held in a chunk manifest is restored by uploading a rewritten manifest; the chunks held by a restored manifest are checked on - the next run. Chunks with a cipher key are not restored. An entry with a - restorable chunk is never pruned. + the next run. Chunks with a cipher key are not restored. An entry is never + pruned while the restore source holds, or cannot be read for, a missing + needle of it. ` } diff --git a/weed/shell/command_fs_verify_restore.go b/weed/shell/command_fs_verify_restore.go index 07593208a0..e889841f0c 100644 --- a/weed/shell/command_fs_verify_restore.go +++ b/weed/shell/command_fs_verify_restore.go @@ -44,9 +44,10 @@ type restorableChunk struct { // restoreEntry copies the restorable missing chunks of an entry from the // restore source into new needles and repoints the entry at them in one // update. restorable reports that at least one missing chunk has intact bytes -// in the restore source, whether or not the update landed. +// in the restore source, or may have, whether or not the update landed. func (c *commandFsVerify) restoreEntry(path util.FullPath, attr *filer_pb.FuseAttributes, rawChunks, missing []*filer_pb.FileChunk) (restored, restorable bool) { var ready []restorableChunk + mayHold := false seen := make(map[string]bool) for _, chunk := range missing { fileId := chunk.GetFileIdString() @@ -57,12 +58,13 @@ func (c *commandFsVerify) restoreEntry(path util.FullPath, attr *filer_pb.FuseAt n, collection, err := c.readRestorableNeedle(chunk) if err != nil { fmt.Fprintf(c.writer, "not restorable %s chunk %s: %v\n", path, fileId, err) + mayHold = mayHold || restoreSourceMayHold(err) continue } ready = append(ready, restorableChunk{fileId: fileId, needle: n, collection: collection, manifest: chunk.IsChunkManifest}) } if len(ready) == 0 { - return false, false + return false, mayHold } dir, name := path.DirAndName() @@ -178,16 +180,16 @@ func (c *commandFsVerify) restoreEntry(path util.FullPath, attr *filer_pb.FuseAt // readRestorableNeedle reads a missing chunk's needle from the restore source // and checks it is the content the chunk names. func (c *commandFsVerify) readRestorableNeedle(chunk *filer_pb.FileChunk) (*needle.Needle, string, error) { - if len(chunk.CipherKey) > 0 { - return nil, "", errors.New("encrypted chunk, skipped") - } vid := needle.VolumeId(chunk.Fid.GetVolumeId()) n, err := c.restore.readNeedle(vid, types.NeedleId(chunk.Fid.GetFileKey()), types.Cookie(chunk.Fid.GetCookie())) if err != nil { return nil, "", err } + if len(chunk.CipherKey) > 0 { + return nil, "", restoreSourceMayHoldError{errors.New("encrypted chunk, skipped")} + } if n.IsChunkedManifest() { - return nil, "", errors.New("needle is a volume-level chunk manifest") + return nil, "", restoreSourceMayHoldError{errors.New("needle is a volume-level chunk manifest")} } if err := checkRestoredContent(chunk, n); err != nil { return nil, "", err diff --git a/weed/shell/command_fs_verify_restore_source.go b/weed/shell/command_fs_verify_restore_source.go index 70b38f5803..efe14198c3 100644 --- a/weed/shell/command_fs_verify_restore_source.go +++ b/weed/shell/command_fs_verify_restore_source.go @@ -3,6 +3,7 @@ package shell import ( "errors" "fmt" + "io" "os" "path/filepath" "strings" @@ -19,6 +20,16 @@ import ( // needle, as opposed to holding bytes that fail a check. var errNotInRestoreSource = errors.New("not in restore source") +// restoreSourceMayHoldError marks a needle the restore source holds, or could +// not be read to tell, that is still not restored; its entry is not pruned. +type restoreSourceMayHoldError struct{ error } + +func (e restoreSourceMayHoldError) Unwrap() error { return e.error } + +func restoreSourceMayHold(err error) bool { + return errors.As(err, new(restoreSourceMayHoldError)) +} + // restoreSource reads needles from regular volume files left on disk outside // any volume server, _.dat/.idx or .dat/.idx. type restoreSource struct { @@ -148,7 +159,7 @@ func (s *restoreSource) readNeedle(vid needle.VolumeId, key types.NeedleId, cook v.needles, v.indexErr = loadRestoreIndex(v.idxPath) } if v.indexErr != nil { - return nil, v.indexErr + return nil, restoreSourceMayHoldError{v.indexErr} } loc, found := v.needles[key] if !found { @@ -156,7 +167,11 @@ func (s *restoreSource) readNeedle(vid needle.VolumeId, key types.NeedleId, cook } 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) + err = fmt.Errorf("read %s at %d: %w", v.datPath, loc.offset.ToActualOffset(), err) + if errors.Is(err, needle.ErrorCorrupted) || errors.Is(err, needle.ErrorSizeMismatch) || errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) { + return nil, err + } + return nil, restoreSourceMayHoldError{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) diff --git a/weed/shell/command_fs_verify_restore_test.go b/weed/shell/command_fs_verify_restore_test.go index 466f0ac6b4..4dce967cae 100644 --- a/weed/shell/command_fs_verify_restore_test.go +++ b/weed/shell/command_fs_verify_restore_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/base64" "encoding/hex" + "os" "path/filepath" "strings" "testing" @@ -13,6 +14,7 @@ import ( "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/storage/needle" + "github.com/seaweedfs/seaweedfs/weed/storage/types" "github.com/seaweedfs/seaweedfs/weed/util" "github.com/seaweedfs/seaweedfs/weed/wdclient" "google.golang.org/grpc" @@ -212,6 +214,59 @@ func TestFsVerifyRestoreEntry(t *testing.T) { }) } +func TestFsVerifyRestoreKeepsEntryTheSourceMayHold(t *testing.T) { + data := []byte("encrypted bytes") + dir := t.TempDir() + v := newTestRestoreVolume(t, filepath.Join(dir, "7")) + v.write(1, 0x11, data, false) + v.write(2, 0x22, data, false) + v.close() + newTestRestoreVolume(t, filepath.Join(dir, "8")).close() + if err := os.Truncate(filepath.Join(dir, "7.dat"), 8+int64(needle.GetActualSize(types.Size(len(data)), needle.GetCurrentVersion()))+10); err != nil { + t.Fatal(err) + } + 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) + } + + attr := &filer_pb.FuseAttributes{Mtime: time.Now().Add(-time.Hour).Unix()} + encrypted := testChunk(7, 1, 0x11, 0, len(data), "") + encrypted.CipherKey = []byte("key") + truncated := testChunk(7, 2, 0x22, 0, len(data), "") + unreadable := testChunk(8, 3, 0x33, 0, 10, "") + path := util.NewFullPath("/buckets/b", "file") + for _, c := range []struct { + name string + chunk *filer_pb.FileChunk + kept bool + }{ + {"encrypted chunk held by the source", encrypted, true}, + {"unreadable index", unreadable, true}, + {"needle cut off the .dat", truncated, false}, + } { + t.Run(c.name, func(t *testing.T) { + chunks := []*filer_pb.FileChunk{c.chunk} + entry := &filer_pb.Entry{Name: "file", Attributes: proto.Clone(attr).(*filer_pb.FuseAttributes), Chunks: chunks} + filer := &restoreTestFiler{stubFilerServer: stubFilerServer{entries: map[string]*filer_pb.Entry{string(path): entry}}} + cmd, output, uploads := newRestoreTestCommand(t, filer, dir) + prune := true + cmd.pruneEntries = &prune + + var counts fsVerifyCounts + cmd.repairEntry(path, attr, chunks, chunks, &counts) + if len(*uploads) != 0 || len(filer.updates) != 0 { + t.Fatalf("uploads=%d updates=%d, want none\n%s", len(*uploads), len(filer.updates), output) + } + if kept := len(filer.deleted) == 0; kept != c.kept { + t.Fatalf("kept=%v, want %v\n%s", kept, c.kept, output) + } + }) + } +} + // A missing chunk held in a manifest is restored by uploading a rewritten // manifest that points at the new needle and keeps every other chunk. func TestFsVerifyRestoreChunkInsideManifest(t *testing.T) {