mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
The scan of an entry stopped at the first chunk whose volume the cluster does not know, so a later lost chunk the restore source holds was never restored. Without -restoreFrom the scan still stops there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
642 lines
23 KiB
Go
642 lines
23 KiB
Go
package shell
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"slices"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/storage"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
|
"go.uber.org/atomic"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func init() {
|
|
Commands = append(Commands, &commandFsVerify{})
|
|
}
|
|
|
|
type commandFsVerify struct {
|
|
env *CommandEnv
|
|
volumeServers []pb.ServerAddress
|
|
volumeIds map[uint32][]pb.ServerAddress
|
|
verbose *bool
|
|
metadataFromLog *bool
|
|
pruneEntries *bool
|
|
restore *restoreSource
|
|
uploadRestored restoreUploadFn
|
|
concurrency *int
|
|
modifyTimeAgoAtSec int64
|
|
writer io.Writer
|
|
waitChan map[string]chan struct{}
|
|
waitChanLock sync.RWMutex
|
|
}
|
|
|
|
func (c *commandFsVerify) Name() string {
|
|
return "fs.verify"
|
|
}
|
|
|
|
func (c *commandFsVerify) Help() string {
|
|
return `recursively verify all files under a directory
|
|
|
|
fs.verify [-v] [-modifyTimeAgo 1h] [-pruneEntries] [-restoreFrom /dir] [-concurrency 4] /buckets/dir
|
|
|
|
-pruneEntries deletes filer entries whose needles are missing from every
|
|
volume location holding the volume (data lost, e.g. after a volume
|
|
truncation). The delete only applies when the entry is unchanged since
|
|
verification, so a re-uploaded file is never pruned.
|
|
|
|
-restoreFrom recovers needles missing from every location out of regular
|
|
volume files left on disk, <collection>_<vid>.dat/.idx or <vid>.dat/.idx in
|
|
the given directory, opened read-only. A needle is restorable when the last
|
|
live .idx record of its key holds the chunk's cookie, passes its CRC, covers
|
|
the chunk's size and, for an MD5-sized chunk ETag, matches it. A later
|
|
tombstone in the .idx does not prevent the restore: the filer still
|
|
references the needle, so its intact bytes are kept.
|
|
The bytes are uploaded in their stored form (a gzipped needle stays
|
|
gzipped) to a file id the filer assigns in the volume file's collection,
|
|
read back, and the entry's chunk is repointed at them, keeping its offset,
|
|
size, ETag and flags. All restorable chunks of an entry go in one update,
|
|
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, nor is an entry
|
|
without a TTL whose path rule sets one, since the update would take it.
|
|
An entry is never pruned while the restore source holds, or cannot be read
|
|
for, a missing needle of it.
|
|
|
|
`
|
|
}
|
|
|
|
func (c *commandFsVerify) HasTag(CommandTag) bool {
|
|
return false
|
|
}
|
|
|
|
func (c *commandFsVerify) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
|
c.env = commandEnv
|
|
c.writer = writer
|
|
fsVerifyCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
c.verbose = fsVerifyCommand.Bool("v", false, "print out each processed files")
|
|
modifyTimeAgo := fsVerifyCommand.Duration("modifyTimeAgo", 0, "only include files after this modify time to verify")
|
|
c.concurrency = fsVerifyCommand.Int("concurrency", 0, "number of parallel verification per volume server")
|
|
c.metadataFromLog = fsVerifyCommand.Bool("metadataFromLog", false, "Using filer log to get metadata")
|
|
c.pruneEntries = fsVerifyCommand.Bool("pruneEntries", false, "delete filer entries whose needles are missing from all volume locations (data lost); a changed entry is never deleted")
|
|
restoreFrom := fsVerifyCommand.String("restoreFrom", "", "directory of leftover <collection>_<vid>.dat/.idx files to restore needles missing from all volume locations")
|
|
if err = fsVerifyCommand.Parse(args); err != nil {
|
|
return err
|
|
}
|
|
|
|
if *restoreFrom != "" {
|
|
if c.restore, err = openRestoreSource(*restoreFrom); err != nil {
|
|
return fmt.Errorf("open restore source: %w", err)
|
|
}
|
|
c.uploadRestored = c.uploadRestoredBytes
|
|
defer func() {
|
|
c.restore.Close()
|
|
c.restore = nil
|
|
}()
|
|
}
|
|
|
|
path, parseErr := commandEnv.parseUrl(findInputDirectory(fsVerifyCommand.Args()))
|
|
if parseErr != nil {
|
|
return parseErr
|
|
}
|
|
|
|
c.modifyTimeAgoAtSec = int64(modifyTimeAgo.Seconds())
|
|
c.volumeIds = make(map[uint32][]pb.ServerAddress)
|
|
c.waitChan = make(map[string]chan struct{})
|
|
c.volumeServers = []pb.ServerAddress{}
|
|
defer func() {
|
|
c.modifyTimeAgoAtSec = 0
|
|
c.volumeIds = nil
|
|
c.waitChan = nil
|
|
c.volumeServers = nil
|
|
}()
|
|
|
|
if err := c.collectVolumeIds(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if *c.concurrency > 0 {
|
|
for _, volumeServer := range c.volumeServers {
|
|
volumeServerStr := string(volumeServer)
|
|
c.waitChan[volumeServerStr] = make(chan struct{}, *c.concurrency)
|
|
defer close(c.waitChan[volumeServerStr])
|
|
}
|
|
}
|
|
var counts fsVerifyCounts
|
|
if *c.metadataFromLog {
|
|
var wg sync.WaitGroup
|
|
counts, err = c.verifyProcessMetadata(path, &wg)
|
|
wg.Wait()
|
|
} else {
|
|
counts, err = c.verifyTraverseBfs(path)
|
|
}
|
|
if c.restore != nil {
|
|
fmt.Fprintf(writer, "verified %d files, error %d files, pruned %d entries, restored %d entries \n", counts.verified, counts.errors, counts.pruned, counts.restored)
|
|
} else {
|
|
fmt.Fprintf(writer, "verified %d files, error %d files, pruned %d entries \n", counts.verified, counts.errors, counts.pruned)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (c *commandFsVerify) collectVolumeIds() error {
|
|
topologyInfo, _, err := collectTopologyInfo(c.env, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
eachDataNode(topologyInfo, func(dc DataCenterId, rack RackId, nodeInfo *master_pb.DataNodeInfo) {
|
|
for _, diskInfo := range nodeInfo.DiskInfos {
|
|
for _, vi := range diskInfo.VolumeInfos {
|
|
volumeServer := pb.NewServerAddressFromDataNode(nodeInfo)
|
|
c.volumeIds[vi.Id] = append(c.volumeIds[vi.Id], volumeServer)
|
|
if !slices.Contains(c.volumeServers, volumeServer) {
|
|
c.volumeServers = append(c.volumeServers, volumeServer)
|
|
}
|
|
}
|
|
for _, vi := range diskInfo.EcShardInfos {
|
|
volumeServer := pb.NewServerAddressFromDataNode(nodeInfo)
|
|
c.volumeIds[vi.Id] = append(c.volumeIds[vi.Id], volumeServer)
|
|
if !slices.Contains(c.volumeServers, volumeServer) {
|
|
c.volumeServers = append(c.volumeServers, volumeServer)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (c *commandFsVerify) verifyChunk(volumeServer pb.ServerAddress, fileId *filer_pb.FileId) error {
|
|
err := operation.WithVolumeServerClient(false, volumeServer, c.env.option.GrpcDialOption,
|
|
func(client volume_server_pb.VolumeServerClient) error {
|
|
_, err := client.VolumeNeedleStatus(context.Background(),
|
|
&volume_server_pb.VolumeNeedleStatusRequest{
|
|
VolumeId: fileId.VolumeId,
|
|
NeedleId: fileId.FileKey})
|
|
return err
|
|
},
|
|
)
|
|
if err != nil && !strings.Contains(err.Error(), storage.ErrorDeleted.Error()) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type ItemEntry struct {
|
|
chunks []*filer_pb.FileChunk
|
|
rawChunks []*filer_pb.FileChunk
|
|
path util.FullPath
|
|
attr *filer_pb.FuseAttributes
|
|
// a manifest could not be resolved, so chunks are the readable ones
|
|
manifestUnresolved bool
|
|
}
|
|
|
|
type fsVerifyCounts struct {
|
|
verified, errors, pruned, restored uint64
|
|
}
|
|
|
|
// repairEntry restores an entry's missing needles when a restore source is
|
|
// set, and prunes it when none of them is restorable.
|
|
func (c *commandFsVerify) repairEntry(path util.FullPath, attr *filer_pb.FuseAttributes, rawChunks, missingChunks []*filer_pb.FileChunk, counts *fsVerifyCounts) {
|
|
if len(missingChunks) == 0 {
|
|
return
|
|
}
|
|
if c.restore != nil {
|
|
restored, restorable := c.restoreEntry(path, attr, rawChunks, missingChunks)
|
|
if restored {
|
|
counts.restored++
|
|
}
|
|
if restorable {
|
|
return
|
|
}
|
|
}
|
|
if !*c.pruneEntries {
|
|
return
|
|
}
|
|
pruned, pruneErr := c.pruneEntry(path, attr, rawChunks)
|
|
if pruneErr != nil {
|
|
fmt.Fprintf(c.writer, "prune %s failed: %v\n", path, pruneErr)
|
|
} else if pruned {
|
|
counts.pruned++
|
|
}
|
|
}
|
|
|
|
// resolveAndVerify expands chunk manifests, cut to their spans since chunks
|
|
// past a shortened manifest are never read, and verifies the entry's needles.
|
|
// An unresolvable manifest is an entry-level failure even when raw chunks are
|
|
// healthy; the readable chunks are still checked so a missing manifest needle
|
|
// can be pruned.
|
|
func (c *commandFsVerify) resolveAndVerify(entryPath string, chunks []*filer_pb.FileChunk, errorChunksCount *atomic.Uint64, wg *sync.WaitGroup) (verified bool, missingChunks []*filer_pb.FileChunk) {
|
|
dataChunks := chunks
|
|
manifestResolveFailed := false
|
|
if resolved, manifestChunks, resolveErr := filer.ResolveChunkManifest(context.Background(), filer.LookupFn(c.env), chunks, 0, math.MaxInt64, nil, true); resolveErr == nil {
|
|
dataChunks = append(resolved, manifestChunks...)
|
|
} else {
|
|
manifestResolveFailed = true
|
|
fmt.Fprintf(c.writer, "file: %s failed to resolve chunk manifest (%v), verifying readable chunks\n", entryPath, resolveErr)
|
|
dataChunks = readableChunks(filer.LookupFn(c.env), chunks, math.MaxInt64)
|
|
}
|
|
verified, missingChunks = c.verifyEntry(entryPath, dataChunks, errorChunksCount, wg)
|
|
if manifestResolveFailed && verified {
|
|
// an unreadable manifest is an entry-level failure even with healthy raw needles
|
|
verified = false
|
|
errorChunksCount.CompareAndSwap(0, 1)
|
|
}
|
|
return verified, missingChunks
|
|
}
|
|
|
|
func (c *commandFsVerify) verifyProcessMetadata(path string, wg *sync.WaitGroup) (counts fsVerifyCounts, err error) {
|
|
processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error {
|
|
message := resp.EventNotification
|
|
if resp.EventNotification.NewEntry == nil {
|
|
return nil
|
|
}
|
|
chunkCount := len(message.NewEntry.Chunks)
|
|
if chunkCount == 0 {
|
|
return nil
|
|
}
|
|
entryPath := fmt.Sprintf("%s/%s", message.NewParentPath, message.NewEntry.Name)
|
|
errorChunksCount := atomic.NewUint64(0)
|
|
verified, missingChunks := c.resolveAndVerify(entryPath, message.NewEntry.Chunks, errorChunksCount, wg)
|
|
if !verified {
|
|
if err = c.env.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
|
entryResp, errReq := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
|
|
Directory: message.NewParentPath,
|
|
Name: message.NewEntry.Name,
|
|
})
|
|
if errReq != nil {
|
|
if strings.HasSuffix(errReq.Error(), "no entry is found in filer store") {
|
|
return nil
|
|
}
|
|
return errReq
|
|
}
|
|
if entryResp.Entry.Attributes.Mtime == message.NewEntry.Attributes.Mtime &&
|
|
bytes.Equal(entryResp.Entry.Attributes.Md5, message.NewEntry.Attributes.Md5) {
|
|
fmt.Fprintf(c.writer, "file: %s needles:%d failed:%d\n", entryPath, chunkCount, errorChunksCount.Load())
|
|
counts.errors++
|
|
c.repairEntry(util.NewFullPath(message.NewParentPath, message.NewEntry.Name),
|
|
message.NewEntry.Attributes, message.NewEntry.Chunks, missingChunks, &counts)
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
if *c.verbose {
|
|
fmt.Fprintf(c.writer, "file: %s needles:%d verified\n", entryPath, chunkCount)
|
|
}
|
|
counts.verified++
|
|
return nil
|
|
}
|
|
metadataFollowOption := &pb.MetadataFollowOption{
|
|
ClientName: "shell_verify",
|
|
ClientId: util.RandomInt32(),
|
|
ClientEpoch: 0,
|
|
SelfSignature: 0,
|
|
PathPrefix: path,
|
|
AdditionalPathPrefixes: nil,
|
|
DirectoriesToWatch: nil,
|
|
StartTsNs: time.Now().Add(-1 * time.Second * time.Duration(c.modifyTimeAgoAtSec)).UnixNano(),
|
|
StopTsNs: time.Now().UnixNano(),
|
|
EventErrorType: pb.DontLogError,
|
|
FilerTokenSource: c.env.option.FilerTokenSource,
|
|
}
|
|
err = pb.FollowMetadata(c.env.FilerAddress(), c.env.option.GrpcDialOption, metadataFollowOption, processEventFn)
|
|
return counts, err
|
|
}
|
|
|
|
// readableChunks expands the manifests it can read, one level at a time and
|
|
// cut to their spans like ResolveChunkManifest, and keeps each one it cannot
|
|
// read as a chunk to verify, so a missing manifest at any depth is checked
|
|
// like any other needle. Chunks starting at or past end are never read.
|
|
func readableChunks(lookupFn wdclient.LookupFileIdFunctionType, chunks []*filer_pb.FileChunk, end int64) (readable []*filer_pb.FileChunk) {
|
|
for _, chunk := range chunks {
|
|
if chunk.Offset >= end {
|
|
continue
|
|
}
|
|
readable = append(readable, chunk)
|
|
if !chunk.IsChunkManifest {
|
|
continue
|
|
}
|
|
held, err := filer.ResolveOneChunkManifest(context.Background(), lookupFn, chunk, nil)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
readable = append(readable, readableChunks(lookupFn, held, min(end, chunk.Offset+int64(chunk.Size)))...)
|
|
}
|
|
return readable
|
|
}
|
|
|
|
// isNeedleMissingError reports whether a VolumeNeedleStatus error means the
|
|
// needle data is lost at that location. It must NOT match "volume not found".
|
|
// Newer Go servers and the Rust server return codes.NotFound; older Go
|
|
// servers return codes.Unknown with "needle not found <id>" or "EOF" (and EC
|
|
// volumes wrap it as "locate in local ec volume: ... needle not found").
|
|
// Legacy shapes are matched anchored so unrelated errors don't classify as missing.
|
|
func isNeedleMissingError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
msg := err.Error()
|
|
if st, ok := status.FromError(err); ok {
|
|
switch st.Code() {
|
|
case codes.NotFound:
|
|
return strings.HasPrefix(st.Message(), "needle not found ")
|
|
case codes.Unknown:
|
|
if st.Message() != "" {
|
|
msg = st.Message()
|
|
}
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
return strings.HasPrefix(msg, "needle not found ") || msg == "EOF" ||
|
|
strings.Contains(msg, "locate in local ec volume:") && strings.HasSuffix(msg, "needle not found")
|
|
}
|
|
|
|
// verifyEntry verifies all chunks of an entry. It returns verified=false when
|
|
// any chunk failed. missingChunks are the chunks whose needle is missing from
|
|
// every volume location holding its volume, i.e. the data is lost rather than
|
|
// a transient or routing error.
|
|
func (c *commandFsVerify) verifyEntry(path string, chunks []*filer_pb.FileChunk, errorCount *atomic.Uint64, wg *sync.WaitGroup) (verified bool, missingChunks []*filer_pb.FileChunk) {
|
|
fileMsg := fmt.Sprintf("file:%s", path)
|
|
itemIsVerifed := atomic.NewBool(true)
|
|
// tracks this entry's in-flight verifications so the missing-needle
|
|
// decision at the end of each chunk is deterministic
|
|
itemWg := &sync.WaitGroup{}
|
|
|
|
for _, chunk := range chunks {
|
|
if volumeIds, ok := c.volumeIds[chunk.Fid.VolumeId]; ok {
|
|
chunkMissingLocations := atomic.NewUint64(0)
|
|
for _, volumeServer := range volumeIds {
|
|
if *c.concurrency == 0 {
|
|
if err := c.verifyChunk(volumeServer, chunk.Fid); err != nil {
|
|
if isNeedleMissingError(err) {
|
|
chunkMissingLocations.Add(1)
|
|
}
|
|
if !(*c.metadataFromLog && strings.HasSuffix(err.Error(), "not found")) {
|
|
fmt.Fprintf(c.writer, "%s failed verify fileId %s: %+v, at volume server %v\n",
|
|
fileMsg, chunk.GetFileIdString(), err, volumeServer)
|
|
}
|
|
if itemIsVerifed.Load() {
|
|
itemIsVerifed.Store(false)
|
|
errorCount.Add(1)
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
c.waitChanLock.RLock()
|
|
waitChan, ok := c.waitChan[string(volumeServer)]
|
|
c.waitChanLock.RUnlock()
|
|
if !ok {
|
|
fmt.Fprintf(c.writer, "%s failed to get channel for %s fileId: %s\n",
|
|
string(volumeServer), fileMsg, chunk.GetFileIdString())
|
|
if itemIsVerifed.Load() {
|
|
itemIsVerifed.Store(false)
|
|
errorCount.Add(1)
|
|
}
|
|
continue
|
|
}
|
|
wg.Add(1)
|
|
itemWg.Add(1)
|
|
waitChan <- struct{}{}
|
|
go func(fChunk *filer_pb.FileChunk, path string, volumeServer pb.ServerAddress, msg string, chunkMissingLocations *atomic.Uint64) {
|
|
defer wg.Done()
|
|
defer itemWg.Done()
|
|
if err := c.verifyChunk(volumeServer, fChunk.Fid); err != nil {
|
|
if isNeedleMissingError(err) {
|
|
chunkMissingLocations.Add(1)
|
|
}
|
|
if !(*c.metadataFromLog && strings.HasSuffix(err.Error(), "not found")) {
|
|
fmt.Fprintf(c.writer, "%s failed verify fileId %s: %+v, at volume server %v\n",
|
|
msg, fChunk.GetFileIdString(), err, volumeServer)
|
|
}
|
|
if itemIsVerifed.Load() {
|
|
itemIsVerifed.Store(false)
|
|
errorCount.Add(1)
|
|
}
|
|
}
|
|
<-waitChan
|
|
}(chunk, path, volumeServer, fileMsg, chunkMissingLocations)
|
|
}
|
|
itemWg.Wait()
|
|
if chunkMissingLocations.Load() == uint64(len(volumeIds)) {
|
|
missingChunks = append(missingChunks, chunk)
|
|
}
|
|
} else {
|
|
if !*c.metadataFromLog {
|
|
err := fmt.Errorf("volumeId %d not found", chunk.Fid.VolumeId)
|
|
fmt.Fprintf(c.writer, "%s failed verify fileId %s: %+v\n",
|
|
fileMsg, chunk.GetFileIdString(), err)
|
|
}
|
|
if itemIsVerifed.Load() {
|
|
itemIsVerifed.Store(false)
|
|
errorCount.Add(1)
|
|
}
|
|
if c.restore == nil {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return itemIsVerifed.Load(), missingChunks
|
|
}
|
|
|
|
// deleteEntryCutoff splits an entry mtime into the conditional-delete fields:
|
|
// seconds keep older filers enforcing the guard, nanoseconds keep same-second
|
|
// precision where int64 can hold them. ok is false when neither field can
|
|
// express the cutoff and the delete must be skipped rather than sent unguarded.
|
|
func deleteEntryCutoff(mtimeSec int64, mtimeNsec int32) (sec, ns int64, ok bool) {
|
|
sec = mtimeSec
|
|
if sec <= 0 {
|
|
return 0, 0, false
|
|
}
|
|
if nsec := int64(mtimeNsec); nsec >= 0 && nsec < 1e9 && sec <= (math.MaxInt64-nsec)/1e9 {
|
|
ns = sec*1e9 + nsec
|
|
}
|
|
return sec, ns, true
|
|
}
|
|
|
|
// pruneEntry deletes an entry whose needles are gone, unless the entry
|
|
// changed since verification. Three guards protect a concurrent repair:
|
|
// re-lookup with mtime/md5 comparison, an identity check of the chunk set
|
|
// (an append or rewrite in the same second as the original mtime changes the
|
|
// chunks but not the timestamps), and IfNotModifiedAfter on the delete. The
|
|
// entry is only counted as pruned when a follow-up lookup confirms it is gone.
|
|
func (c *commandFsVerify) pruneEntry(path util.FullPath, attr *filer_pb.FuseAttributes, expectedChunks []*filer_pb.FileChunk) (pruned bool, err error) {
|
|
dir, name := path.DirAndName()
|
|
lookupErr := c.env.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
|
lookupResp, err := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
|
|
Directory: dir,
|
|
Name: name,
|
|
})
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "no entry is found in filer store") {
|
|
return nil // already gone
|
|
}
|
|
return err
|
|
}
|
|
if entryChangedSince(lookupResp.Entry, attr, expectedChunks) {
|
|
fmt.Fprintf(c.writer, "skip pruning %s: entry changed since verification\n", path)
|
|
return nil
|
|
}
|
|
sec, ns, ok := deleteEntryCutoff(attr.GetMtime(), attr.GetMtimeNs())
|
|
if !ok {
|
|
fmt.Fprintf(c.writer, "skip pruning %s: mtime cannot set a delete condition\n", path)
|
|
return nil
|
|
}
|
|
deleteResp, err := client.DeleteEntry(context.Background(), &filer_pb.DeleteEntryRequest{
|
|
Directory: dir,
|
|
Name: name,
|
|
IfNotModifiedAfter: sec,
|
|
IfNotModifiedAfterNs: ns,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if deleteResp.Error != "" {
|
|
return fmt.Errorf("delete entry %s: %s", path, deleteResp.Error)
|
|
}
|
|
// the filer silently skips the delete when the entry advanced past
|
|
// IfNotModifiedAfter between our lookup and the delete; confirm
|
|
confirm, err := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
|
|
Directory: dir,
|
|
Name: name,
|
|
})
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "no entry is found in filer store") {
|
|
pruned = true
|
|
fmt.Fprintf(c.writer, "pruned entry with missing needles: %s\n", path)
|
|
return nil
|
|
}
|
|
// the delete outcome is unknown — never count it as pruned
|
|
fmt.Fprintf(c.writer, "skip pruning %s: delete not confirmed (%v)\n", path, err)
|
|
return nil
|
|
}
|
|
if confirm.Entry != nil {
|
|
fmt.Fprintf(c.writer, "skip pruning %s: entry changed since verification\n", path)
|
|
return nil
|
|
}
|
|
pruned = true
|
|
fmt.Fprintf(c.writer, "pruned entry with missing needles: %s\n", path)
|
|
return nil
|
|
})
|
|
return pruned, lookupErr
|
|
}
|
|
|
|
// entryChangedSince reports whether the entry differs from the one verified,
|
|
// by mtime, md5, or chunk set (a same-second rewrite changes only the chunks).
|
|
func entryChangedSince(entry *filer_pb.Entry, attr *filer_pb.FuseAttributes, chunks []*filer_pb.FileChunk) bool {
|
|
att := entry.GetAttributes()
|
|
if att.GetMtime() != attr.GetMtime() || att.GetMtimeNs() != attr.GetMtimeNs() {
|
|
return true
|
|
}
|
|
if md5 := attr.GetMd5(); len(md5) > 0 && !bytes.Equal(att.GetMd5(), md5) {
|
|
return true
|
|
}
|
|
return !chunksEqual(chunkFingerprints(entry.GetChunks()), chunkFingerprints(chunks))
|
|
}
|
|
|
|
// chunkFingerprints maps the file id of every chunk to a set, so two chunk
|
|
// lists count as identical when they reference the same needles.
|
|
func chunkFingerprints(chunks []*filer_pb.FileChunk) map[string]struct{} {
|
|
ids := make(map[string]struct{}, len(chunks))
|
|
for _, chunk := range chunks {
|
|
if chunk == nil || chunk.Fid == nil {
|
|
continue
|
|
}
|
|
ids[chunk.GetFileIdString()] = struct{}{}
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func chunksEqual(a, b map[string]struct{}) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for id := range a {
|
|
if _, ok := b[id]; !ok {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (c *commandFsVerify) verifyTraverseBfs(path string) (counts fsVerifyCounts, err error) {
|
|
timeNowAtSec := time.Now().Unix()
|
|
err = doTraverseBfsAndSaving(c.env, c.writer, path, false, false,
|
|
func(ctx context.Context, entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
|
|
if c.modifyTimeAgoAtSec > 0 {
|
|
if entry.Entry.Attributes != nil && c.modifyTimeAgoAtSec < timeNowAtSec-entry.Entry.Attributes.Mtime {
|
|
return nil
|
|
}
|
|
}
|
|
entryPath := util.NewFullPath(entry.Dir, entry.Entry.Name)
|
|
manifestUnresolved := false
|
|
dataChunks, manifestChunks, resolveErr := filer.ResolveChunkManifest(context.Background(), filer.LookupFn(c.env), entry.Entry.GetChunks(), 0, math.MaxInt64, nil, true)
|
|
if resolveErr != nil {
|
|
if c.restore == nil {
|
|
return fmt.Errorf("failed to ResolveChunkManifest: %+v", resolveErr)
|
|
}
|
|
// a missing manifest needle may be restorable
|
|
fmt.Fprintf(c.writer, "file: %s failed to resolve chunk manifest (%v), verifying readable chunks\n", entryPath, resolveErr)
|
|
dataChunks, manifestChunks, manifestUnresolved = readableChunks(filer.LookupFn(c.env), entry.Entry.GetChunks(), math.MaxInt64), nil, true
|
|
}
|
|
dataChunks = append(dataChunks, manifestChunks...)
|
|
if len(dataChunks) > 0 {
|
|
select {
|
|
case outputChan <- &ItemEntry{
|
|
chunks: dataChunks,
|
|
rawChunks: entry.Entry.GetChunks(),
|
|
path: entryPath,
|
|
attr: entry.Entry.GetAttributes(),
|
|
manifestUnresolved: manifestUnresolved,
|
|
}:
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
return nil
|
|
},
|
|
func(outputChan chan interface{}) error {
|
|
var wg sync.WaitGroup
|
|
itemErrCount := atomic.NewUint64(0)
|
|
for itemEntry := range outputChan {
|
|
i := itemEntry.(*ItemEntry)
|
|
itemPath := string(i.path)
|
|
verified, missingChunks := c.verifyEntry(itemPath, i.chunks, itemErrCount, &wg)
|
|
if verified && i.manifestUnresolved {
|
|
verified = false
|
|
itemErrCount.Add(1)
|
|
}
|
|
if verified {
|
|
if *c.verbose {
|
|
fmt.Fprintf(c.writer, "file: %s needles:%d verified\n", itemPath, len(i.chunks))
|
|
}
|
|
counts.verified++
|
|
} else {
|
|
c.repairEntry(i.path, i.attr, i.rawChunks, missingChunks, &counts)
|
|
}
|
|
}
|
|
wg.Wait()
|
|
counts.errors = itemErrCount.Load()
|
|
return nil
|
|
})
|
|
return counts, err
|
|
}
|