command_fs_verify.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/filer"
  7. "github.com/seaweedfs/seaweedfs/weed/operation"
  8. "github.com/seaweedfs/seaweedfs/weed/pb"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  12. "github.com/seaweedfs/seaweedfs/weed/storage"
  13. "github.com/seaweedfs/seaweedfs/weed/util"
  14. "go.uber.org/atomic"
  15. "golang.org/x/exp/slices"
  16. "io"
  17. "math"
  18. "strings"
  19. "sync"
  20. "time"
  21. )
  22. func init() {
  23. Commands = append(Commands, &commandFsVerify{})
  24. }
  25. type commandFsVerify struct {
  26. env *CommandEnv
  27. volumeServers []pb.ServerAddress
  28. volumeIds map[uint32][]pb.ServerAddress
  29. verbose *bool
  30. concurrency *int
  31. modifyTimeAgoAtSec int64
  32. writer io.Writer
  33. waitChan map[string]chan struct{}
  34. waitChanLock sync.RWMutex
  35. }
  36. func (c *commandFsVerify) Name() string {
  37. return "fs.verify"
  38. }
  39. func (c *commandFsVerify) Help() string {
  40. return `recursively verify all files under a directory
  41. fs.verify [-v] [-modifyTimeAgo 1h] /buckets/dir
  42. `
  43. }
  44. func (c *commandFsVerify) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  45. c.env = commandEnv
  46. c.writer = writer
  47. fsVerifyCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  48. c.verbose = fsVerifyCommand.Bool("v", false, "print out each processed files")
  49. modifyTimeAgo := fsVerifyCommand.Duration("modifyTimeAgo", 0, "only include files after this modify time to verify")
  50. c.concurrency = fsVerifyCommand.Int("concurrency", 0, "number of parallel verification per volume server")
  51. if err = fsVerifyCommand.Parse(args); err != nil {
  52. return err
  53. }
  54. path, parseErr := commandEnv.parseUrl(findInputDirectory(fsVerifyCommand.Args()))
  55. if parseErr != nil {
  56. return parseErr
  57. }
  58. c.modifyTimeAgoAtSec = int64(modifyTimeAgo.Seconds())
  59. c.volumeIds = make(map[uint32][]pb.ServerAddress)
  60. c.waitChan = make(map[string]chan struct{})
  61. c.volumeServers = []pb.ServerAddress{}
  62. defer func() {
  63. c.modifyTimeAgoAtSec = 0
  64. c.volumeIds = nil
  65. c.waitChan = nil
  66. c.volumeServers = nil
  67. }()
  68. if err := c.collectVolumeIds(); err != nil {
  69. return parseErr
  70. }
  71. if *c.concurrency > 0 {
  72. for _, volumeServer := range c.volumeServers {
  73. volumeServerStr := string(volumeServer)
  74. c.waitChan[volumeServerStr] = make(chan struct{}, *c.concurrency)
  75. defer close(c.waitChan[volumeServerStr])
  76. }
  77. }
  78. fCount, eConut, terr := c.verifyTraverseBfs(path)
  79. if terr == nil {
  80. fmt.Fprintf(writer, "verified %d files, error %d files \n", fCount, eConut)
  81. }
  82. return terr
  83. }
  84. func (c *commandFsVerify) collectVolumeIds() error {
  85. topologyInfo, _, err := collectTopologyInfo(c.env, 0)
  86. if err != nil {
  87. return err
  88. }
  89. eachDataNode(topologyInfo, func(dc string, rack RackId, nodeInfo *master_pb.DataNodeInfo) {
  90. for _, diskInfo := range nodeInfo.DiskInfos {
  91. for _, vi := range diskInfo.VolumeInfos {
  92. volumeServer := pb.NewServerAddressFromDataNode(nodeInfo)
  93. c.volumeIds[vi.Id] = append(c.volumeIds[vi.Id], volumeServer)
  94. if !slices.Contains(c.volumeServers, volumeServer) {
  95. c.volumeServers = append(c.volumeServers, volumeServer)
  96. }
  97. }
  98. }
  99. })
  100. return nil
  101. }
  102. func (c *commandFsVerify) verifyEntry(volumeServer pb.ServerAddress, fileId *filer_pb.FileId) error {
  103. err := operation.WithVolumeServerClient(false, volumeServer, c.env.option.GrpcDialOption,
  104. func(client volume_server_pb.VolumeServerClient) error {
  105. _, err := client.VolumeNeedleStatus(context.Background(),
  106. &volume_server_pb.VolumeNeedleStatusRequest{
  107. VolumeId: fileId.VolumeId,
  108. NeedleId: fileId.FileKey})
  109. return err
  110. },
  111. )
  112. if err != nil && !strings.Contains(err.Error(), storage.ErrorDeleted.Error()) {
  113. return err
  114. }
  115. return nil
  116. }
  117. type ItemEntry struct {
  118. chunks []*filer_pb.FileChunk
  119. path util.FullPath
  120. }
  121. func (c *commandFsVerify) verifyTraverseBfs(path string) (fileCount uint64, errCount uint64, err error) {
  122. timeNowAtSec := time.Now().Unix()
  123. return fileCount, errCount, doTraverseBfsAndSaving(c.env, c.writer, path, false,
  124. func(entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
  125. if c.modifyTimeAgoAtSec > 0 {
  126. if entry.Entry.Attributes != nil && c.modifyTimeAgoAtSec < timeNowAtSec-entry.Entry.Attributes.Mtime {
  127. return nil
  128. }
  129. }
  130. dataChunks, manifestChunks, resolveErr := filer.ResolveChunkManifest(filer.LookupFn(c.env), entry.Entry.GetChunks(), 0, math.MaxInt64)
  131. if resolveErr != nil {
  132. return fmt.Errorf("failed to ResolveChunkManifest: %+v", resolveErr)
  133. }
  134. dataChunks = append(dataChunks, manifestChunks...)
  135. if len(dataChunks) > 0 {
  136. outputChan <- &ItemEntry{
  137. chunks: dataChunks,
  138. path: util.NewFullPath(entry.Dir, entry.Entry.Name),
  139. }
  140. }
  141. return nil
  142. },
  143. func(outputChan chan interface{}) {
  144. var wg sync.WaitGroup
  145. itemErrCount := atomic.NewUint64(0)
  146. for itemEntry := range outputChan {
  147. i := itemEntry.(*ItemEntry)
  148. itemPath := string(i.path)
  149. fileMsg := fmt.Sprintf("file:%s", itemPath)
  150. itemIsVerifed := atomic.NewBool(true)
  151. for _, chunk := range i.chunks {
  152. if volumeIds, ok := c.volumeIds[chunk.Fid.VolumeId]; ok {
  153. for _, volumeServer := range volumeIds {
  154. if *c.concurrency == 0 {
  155. if err = c.verifyEntry(volumeServer, chunk.Fid); err != nil {
  156. fmt.Fprintf(c.writer, "%s failed verify fileId %s: %+v\n",
  157. fileMsg, chunk.GetFileIdString(), err)
  158. if itemIsVerifed.Load() {
  159. itemIsVerifed.Store(false)
  160. itemErrCount.Add(1)
  161. }
  162. }
  163. continue
  164. }
  165. c.waitChanLock.RLock()
  166. waitChan, ok := c.waitChan[string(volumeServer)]
  167. c.waitChanLock.RUnlock()
  168. if !ok {
  169. fmt.Fprintf(c.writer, "%s failed to get channel for %s fileId: %s: %+v\n",
  170. string(volumeServer), fileMsg, chunk.GetFileIdString(), err)
  171. if itemIsVerifed.Load() {
  172. itemIsVerifed.Store(false)
  173. itemErrCount.Add(1)
  174. }
  175. continue
  176. }
  177. wg.Add(1)
  178. waitChan <- struct{}{}
  179. go func(fChunk *filer_pb.FileChunk, path string, volumeServer pb.ServerAddress, msg string) {
  180. defer wg.Done()
  181. if err = c.verifyEntry(volumeServer, fChunk.Fid); err != nil {
  182. fmt.Fprintf(c.writer, "%s failed verify fileId %s: %+v\n",
  183. msg, fChunk.GetFileIdString(), err)
  184. if itemIsVerifed.Load() {
  185. itemIsVerifed.Store(false)
  186. itemErrCount.Add(1)
  187. }
  188. }
  189. <-waitChan
  190. }(chunk, itemPath, volumeServer, fileMsg)
  191. }
  192. } else {
  193. err = fmt.Errorf("volumeId %d not found", chunk.Fid.VolumeId)
  194. fmt.Fprintf(c.writer, "%s failed verify fileId %s: %+v\n",
  195. fileMsg, chunk.GetFileIdString(), err)
  196. if itemIsVerifed.Load() {
  197. itemIsVerifed.Store(false)
  198. itemErrCount.Add(1)
  199. }
  200. break
  201. }
  202. }
  203. if itemIsVerifed.Load() {
  204. if *c.verbose {
  205. fmt.Fprintf(c.writer, "%s needles:%d verifed\n", fileMsg, len(i.chunks))
  206. }
  207. fileCount++
  208. }
  209. }
  210. wg.Wait()
  211. errCount = itemErrCount.Load()
  212. })
  213. }