2022-12-21 23:54:17 +00:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"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"
|
2023-03-20 01:38:57 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2022-12-21 23:54:17 +00:00
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
"strings"
|
2023-03-20 01:38:57 +00:00
|
|
|
"sync"
|
2022-12-21 23:54:17 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Commands = append(Commands, &commandFsVerify{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type commandFsVerify struct {
|
|
|
|
env *CommandEnv
|
2023-03-20 01:38:57 +00:00
|
|
|
volumeServers []pb.ServerAddress
|
2022-12-21 23:54:17 +00:00
|
|
|
volumeIds map[uint32][]pb.ServerAddress
|
|
|
|
verbose *bool
|
2023-03-20 01:38:57 +00:00
|
|
|
concurrency *int
|
2022-12-21 23:54:17 +00:00
|
|
|
modifyTimeAgoAtSec int64
|
|
|
|
writer io.Writer
|
2023-03-20 01:38:57 +00:00
|
|
|
waitChan map[string]chan struct{}
|
|
|
|
waitChanLock sync.RWMutex
|
2022-12-21 23:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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] /buckets/dir
|
|
|
|
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2023-03-20 01:38:57 +00:00
|
|
|
c.concurrency = fsVerifyCommand.Int("concurrency", 0, "number of parallel verification per volume server")
|
2022-12-21 23:54:17 +00:00
|
|
|
|
|
|
|
if err = fsVerifyCommand.Parse(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path, parseErr := commandEnv.parseUrl(findInputDirectory(fsVerifyCommand.Args()))
|
|
|
|
if parseErr != nil {
|
|
|
|
return parseErr
|
|
|
|
}
|
|
|
|
|
|
|
|
c.modifyTimeAgoAtSec = int64(modifyTimeAgo.Seconds())
|
2023-03-20 01:38:57 +00:00
|
|
|
c.volumeIds = make(map[uint32][]pb.ServerAddress)
|
|
|
|
c.waitChan = make(map[string]chan struct{})
|
|
|
|
c.volumeServers = []pb.ServerAddress{}
|
2023-03-20 01:48:40 +00:00
|
|
|
defer func() {
|
|
|
|
c.modifyTimeAgoAtSec = 0
|
|
|
|
c.volumeIds = nil
|
|
|
|
c.waitChan = nil
|
|
|
|
c.volumeServers = nil
|
|
|
|
}()
|
2022-12-21 23:54:17 +00:00
|
|
|
|
|
|
|
if err := c.collectVolumeIds(); err != nil {
|
|
|
|
return parseErr
|
|
|
|
}
|
|
|
|
|
2023-03-20 01:38:57 +00:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
2022-12-21 23:54:17 +00:00
|
|
|
|
2023-03-20 01:38:57 +00:00
|
|
|
fCount, eConut, terr := c.verifyTraverseBfs(path)
|
2022-12-21 23:54:17 +00:00
|
|
|
if terr == nil {
|
|
|
|
fmt.Fprintf(writer, "verified %d files, error %d files \n", fCount, eConut)
|
|
|
|
}
|
|
|
|
|
|
|
|
return terr
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsVerify) collectVolumeIds() error {
|
|
|
|
topologyInfo, _, err := collectTopologyInfo(c.env, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
eachDataNode(topologyInfo, func(dc string, rack RackId, nodeInfo *master_pb.DataNodeInfo) {
|
|
|
|
for _, diskInfo := range nodeInfo.DiskInfos {
|
|
|
|
for _, vi := range diskInfo.VolumeInfos {
|
2023-03-20 01:38:57 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-12-21 23:54:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-20 01:38:57 +00:00
|
|
|
func (c *commandFsVerify) verifyEntry(volumeServer pb.ServerAddress, fileId *filer_pb.FileId) error {
|
|
|
|
err := operation.WithVolumeServerClient(false, volumeServer, c.env.option.GrpcDialOption,
|
2022-12-21 23:54:17 +00:00
|
|
|
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
|
|
|
|
path util.FullPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsVerify) verifyTraverseBfs(path string) (fileCount int64, errCount int64, err error) {
|
|
|
|
timeNowAtSec := time.Now().Unix()
|
2023-01-03 07:20:45 +00:00
|
|
|
return fileCount, errCount, doTraverseBfsAndSaving(c.env, c.writer, path, false,
|
2022-12-21 23:54:17 +00:00
|
|
|
func(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dataChunks, manifestChunks, resolveErr := filer.ResolveChunkManifest(filer.LookupFn(c.env), entry.Entry.GetChunks(), 0, math.MaxInt64)
|
|
|
|
if resolveErr != nil {
|
|
|
|
return fmt.Errorf("failed to ResolveChunkManifest: %+v", resolveErr)
|
|
|
|
}
|
|
|
|
dataChunks = append(dataChunks, manifestChunks...)
|
|
|
|
if len(dataChunks) > 0 {
|
|
|
|
outputChan <- &ItemEntry{
|
|
|
|
chunks: dataChunks,
|
|
|
|
path: util.NewFullPath(entry.Dir, entry.Entry.Name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
func(outputChan chan interface{}) {
|
|
|
|
for itemEntry := range outputChan {
|
|
|
|
i := itemEntry.(*ItemEntry)
|
2023-03-20 01:38:57 +00:00
|
|
|
itemPath := string(i.path)
|
|
|
|
fileMsg := fmt.Sprintf("file:%s", itemPath)
|
|
|
|
errItem := make(map[string]error)
|
|
|
|
errItemLock := sync.RWMutex{}
|
2022-12-21 23:54:17 +00:00
|
|
|
for _, chunk := range i.chunks {
|
|
|
|
if volumeIds, ok := c.volumeIds[chunk.Fid.VolumeId]; ok {
|
|
|
|
for _, volumeServer := range volumeIds {
|
2023-03-20 01:38:57 +00:00
|
|
|
if *c.concurrency == 0 {
|
|
|
|
if err = c.verifyEntry(volumeServer, chunk.Fid); err != nil {
|
|
|
|
fmt.Fprintf(c.writer, "%s failed verify needle %d:%d: %+v\n",
|
|
|
|
fileMsg, chunk.Fid.VolumeId, chunk.Fid.FileKey, err)
|
|
|
|
}
|
|
|
|
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 chunk: %d:%d: %+v\n",
|
|
|
|
string(volumeServer), fileMsg, chunk.Fid.VolumeId, chunk.Fid.FileKey, err)
|
|
|
|
continue
|
2022-12-21 23:54:17 +00:00
|
|
|
}
|
2023-03-20 01:38:57 +00:00
|
|
|
waitChan <- struct{}{}
|
|
|
|
go func(fId *filer_pb.FileId, path string, volumeServer pb.ServerAddress, msg string) {
|
|
|
|
if err = c.verifyEntry(volumeServer, fId); err != nil {
|
|
|
|
errItemLock.Lock()
|
|
|
|
errItem[path] = err
|
|
|
|
fmt.Fprintf(c.writer, "%s failed verify needle %d:%d: %+v\n",
|
|
|
|
msg, fId.VolumeId, fId.FileKey, err)
|
|
|
|
errItemLock.Unlock()
|
|
|
|
}
|
|
|
|
<-waitChan
|
|
|
|
}(chunk.Fid, itemPath, volumeServer, fileMsg)
|
2022-12-21 23:54:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("volumeId %d not found", chunk.Fid.VolumeId)
|
2023-03-20 01:38:57 +00:00
|
|
|
fmt.Fprintf(c.writer, "%s %d:%d: %+v\n",
|
2022-12-21 23:54:17 +00:00
|
|
|
fileMsg, chunk.Fid.VolumeId, chunk.Fid.FileKey, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-03-20 01:38:57 +00:00
|
|
|
errItemLock.RLock()
|
|
|
|
err, _ = errItem[itemPath]
|
|
|
|
errItemLock.RUnlock()
|
2022-12-21 23:54:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errCount++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if *c.verbose {
|
2023-03-20 01:38:57 +00:00
|
|
|
fmt.Fprintf(c.writer, "%s needles:%d verifed\n", fileMsg, len(i.chunks))
|
2022-12-21 23:54:17 +00:00
|
|
|
}
|
|
|
|
fileCount++
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|