command_fs_verify.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package shell
  2. import (
  3. "bytes"
  4. "context"
  5. "flag"
  6. "fmt"
  7. "github.com/seaweedfs/seaweedfs/weed/filer"
  8. "github.com/seaweedfs/seaweedfs/weed/operation"
  9. "github.com/seaweedfs/seaweedfs/weed/pb"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  13. "github.com/seaweedfs/seaweedfs/weed/storage"
  14. "github.com/seaweedfs/seaweedfs/weed/util"
  15. "go.uber.org/atomic"
  16. "golang.org/x/exp/slices"
  17. "io"
  18. "math"
  19. "strings"
  20. "sync"
  21. "time"
  22. )
  23. func init() {
  24. Commands = append(Commands, &commandFsVerify{})
  25. }
  26. type commandFsVerify struct {
  27. env *CommandEnv
  28. volumeServers []pb.ServerAddress
  29. volumeIds map[uint32][]pb.ServerAddress
  30. verbose *bool
  31. metadataFromLog *bool
  32. concurrency *int
  33. modifyTimeAgoAtSec int64
  34. writer io.Writer
  35. waitChan map[string]chan struct{}
  36. waitChanLock sync.RWMutex
  37. }
  38. func (c *commandFsVerify) Name() string {
  39. return "fs.verify"
  40. }
  41. func (c *commandFsVerify) Help() string {
  42. return `recursively verify all files under a directory
  43. fs.verify [-v] [-modifyTimeAgo 1h] /buckets/dir
  44. `
  45. }
  46. func (c *commandFsVerify) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  47. c.env = commandEnv
  48. c.writer = writer
  49. fsVerifyCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  50. c.verbose = fsVerifyCommand.Bool("v", false, "print out each processed files")
  51. modifyTimeAgo := fsVerifyCommand.Duration("modifyTimeAgo", 0, "only include files after this modify time to verify")
  52. c.concurrency = fsVerifyCommand.Int("concurrency", 0, "number of parallel verification per volume server")
  53. c.metadataFromLog = fsVerifyCommand.Bool("metadataFromLog", false, "Using filer log to get metadata")
  54. if err = fsVerifyCommand.Parse(args); err != nil {
  55. return err
  56. }
  57. path, parseErr := commandEnv.parseUrl(findInputDirectory(fsVerifyCommand.Args()))
  58. if parseErr != nil {
  59. return parseErr
  60. }
  61. c.modifyTimeAgoAtSec = int64(modifyTimeAgo.Seconds())
  62. c.volumeIds = make(map[uint32][]pb.ServerAddress)
  63. c.waitChan = make(map[string]chan struct{})
  64. c.volumeServers = []pb.ServerAddress{}
  65. defer func() {
  66. c.modifyTimeAgoAtSec = 0
  67. c.volumeIds = nil
  68. c.waitChan = nil
  69. c.volumeServers = nil
  70. }()
  71. if err := c.collectVolumeIds(); err != nil {
  72. return parseErr
  73. }
  74. if *c.concurrency > 0 {
  75. for _, volumeServer := range c.volumeServers {
  76. volumeServerStr := string(volumeServer)
  77. c.waitChan[volumeServerStr] = make(chan struct{}, *c.concurrency)
  78. defer close(c.waitChan[volumeServerStr])
  79. }
  80. }
  81. var fCount, eCount uint64
  82. if *c.metadataFromLog {
  83. var wg sync.WaitGroup
  84. fCount, eCount, err = c.verifyProcessMetadata(path, &wg)
  85. wg.Wait()
  86. if err != nil {
  87. return err
  88. }
  89. } else {
  90. fCount, eCount, err = c.verifyTraverseBfs(path)
  91. }
  92. fmt.Fprintf(writer, "verified %d files, error %d files \n", fCount, eCount)
  93. return err
  94. }
  95. func (c *commandFsVerify) collectVolumeIds() error {
  96. topologyInfo, _, err := collectTopologyInfo(c.env, 0)
  97. if err != nil {
  98. return err
  99. }
  100. eachDataNode(topologyInfo, func(dc string, rack RackId, nodeInfo *master_pb.DataNodeInfo) {
  101. for _, diskInfo := range nodeInfo.DiskInfos {
  102. for _, vi := range diskInfo.VolumeInfos {
  103. volumeServer := pb.NewServerAddressFromDataNode(nodeInfo)
  104. c.volumeIds[vi.Id] = append(c.volumeIds[vi.Id], volumeServer)
  105. if !slices.Contains(c.volumeServers, volumeServer) {
  106. c.volumeServers = append(c.volumeServers, volumeServer)
  107. }
  108. }
  109. }
  110. })
  111. return nil
  112. }
  113. func (c *commandFsVerify) verifyChunk(volumeServer pb.ServerAddress, fileId *filer_pb.FileId) error {
  114. err := operation.WithVolumeServerClient(false, volumeServer, c.env.option.GrpcDialOption,
  115. func(client volume_server_pb.VolumeServerClient) error {
  116. _, err := client.VolumeNeedleStatus(context.Background(),
  117. &volume_server_pb.VolumeNeedleStatusRequest{
  118. VolumeId: fileId.VolumeId,
  119. NeedleId: fileId.FileKey})
  120. return err
  121. },
  122. )
  123. if err != nil && !strings.Contains(err.Error(), storage.ErrorDeleted.Error()) {
  124. return err
  125. }
  126. return nil
  127. }
  128. type ItemEntry struct {
  129. chunks []*filer_pb.FileChunk
  130. path util.FullPath
  131. }
  132. func (c *commandFsVerify) verifyProcessMetadata(path string, wg *sync.WaitGroup) (fileCount uint64, errCount uint64, err error) {
  133. processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error {
  134. message := resp.EventNotification
  135. if resp.EventNotification.NewEntry == nil {
  136. return nil
  137. }
  138. chunkCount := len(message.NewEntry.Chunks)
  139. if chunkCount == 0 {
  140. return nil
  141. }
  142. entryPath := fmt.Sprintf("%s/%s", message.NewParentPath, message.NewEntry.Name)
  143. errorChunksCount := atomic.NewUint64(0)
  144. if !c.verifyEntry(entryPath, message.NewEntry.Chunks, errorChunksCount, wg) {
  145. if err = c.env.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  146. entryResp, errReq := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
  147. Directory: message.NewParentPath,
  148. Name: message.NewEntry.Name,
  149. })
  150. if errReq != nil {
  151. if strings.HasSuffix(errReq.Error(), "no entry is found in filer store") {
  152. return nil
  153. }
  154. return errReq
  155. }
  156. if entryResp.Entry.Attributes.Mtime == message.NewEntry.Attributes.Mtime &&
  157. bytes.Equal(entryResp.Entry.Attributes.Md5, message.NewEntry.Attributes.Md5) {
  158. fmt.Fprintf(c.writer, "file: %s needles:%d failed:%d\n", entryPath, chunkCount, errorChunksCount.Load())
  159. errCount++
  160. }
  161. return nil
  162. }); err != nil {
  163. return err
  164. }
  165. return nil
  166. }
  167. if *c.verbose {
  168. fmt.Fprintf(c.writer, "file: %s needles:%d verifed\n", entryPath, chunkCount)
  169. }
  170. fileCount++
  171. return nil
  172. }
  173. metadataFollowOption := &pb.MetadataFollowOption{
  174. ClientName: "shell_verify",
  175. ClientId: util.RandomInt32(),
  176. ClientEpoch: 0,
  177. SelfSignature: 0,
  178. PathPrefix: path,
  179. AdditionalPathPrefixes: nil,
  180. DirectoriesToWatch: nil,
  181. StartTsNs: time.Now().Add(-1 * time.Second * time.Duration(c.modifyTimeAgoAtSec)).UnixNano(),
  182. StopTsNs: time.Now().UnixNano(),
  183. EventErrorType: pb.DontLogError,
  184. }
  185. return fileCount, errCount, pb.FollowMetadata(c.env.option.FilerAddress, c.env.option.GrpcDialOption, metadataFollowOption, processEventFn)
  186. }
  187. func (c *commandFsVerify) verifyEntry(path string, chunks []*filer_pb.FileChunk, errorCount *atomic.Uint64, wg *sync.WaitGroup) bool {
  188. fileMsg := fmt.Sprintf("file:%s", path)
  189. itemIsVerifed := atomic.NewBool(true)
  190. for _, chunk := range chunks {
  191. if volumeIds, ok := c.volumeIds[chunk.Fid.VolumeId]; ok {
  192. for _, volumeServer := range volumeIds {
  193. if *c.concurrency == 0 {
  194. if err := c.verifyChunk(volumeServer, chunk.Fid); err != nil {
  195. if !(*c.metadataFromLog && strings.HasSuffix(err.Error(), "not found")) {
  196. fmt.Fprintf(c.writer, "%s failed verify fileId %s: %+v\n",
  197. fileMsg, chunk.GetFileIdString(), err)
  198. }
  199. if itemIsVerifed.Load() {
  200. itemIsVerifed.Store(false)
  201. errorCount.Add(1)
  202. }
  203. }
  204. continue
  205. }
  206. c.waitChanLock.RLock()
  207. waitChan, ok := c.waitChan[string(volumeServer)]
  208. c.waitChanLock.RUnlock()
  209. if !ok {
  210. fmt.Fprintf(c.writer, "%s failed to get channel for %s fileId: %s\n",
  211. string(volumeServer), fileMsg, chunk.GetFileIdString())
  212. if itemIsVerifed.Load() {
  213. itemIsVerifed.Store(false)
  214. errorCount.Add(1)
  215. }
  216. continue
  217. }
  218. wg.Add(1)
  219. waitChan <- struct{}{}
  220. go func(fChunk *filer_pb.FileChunk, path string, volumeServer pb.ServerAddress, msg string) {
  221. defer wg.Done()
  222. if err := c.verifyChunk(volumeServer, fChunk.Fid); err != nil {
  223. if !(*c.metadataFromLog && strings.HasSuffix(err.Error(), "not found")) {
  224. fmt.Fprintf(c.writer, "%s failed verify fileId %s: %+v\n",
  225. msg, fChunk.GetFileIdString(), err)
  226. }
  227. if itemIsVerifed.Load() {
  228. itemIsVerifed.Store(false)
  229. errorCount.Add(1)
  230. }
  231. }
  232. <-waitChan
  233. }(chunk, path, volumeServer, fileMsg)
  234. }
  235. } else {
  236. if !*c.metadataFromLog {
  237. err := fmt.Errorf("volumeId %d not found", chunk.Fid.VolumeId)
  238. fmt.Fprintf(c.writer, "%s failed verify fileId %s: %+v\n",
  239. fileMsg, chunk.GetFileIdString(), err)
  240. }
  241. if itemIsVerifed.Load() {
  242. itemIsVerifed.Store(false)
  243. errorCount.Add(1)
  244. }
  245. break
  246. }
  247. }
  248. return itemIsVerifed.Load()
  249. }
  250. func (c *commandFsVerify) verifyTraverseBfs(path string) (fileCount uint64, errCount uint64, err error) {
  251. timeNowAtSec := time.Now().Unix()
  252. return fileCount, errCount, doTraverseBfsAndSaving(c.env, c.writer, path, false,
  253. func(entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
  254. if c.modifyTimeAgoAtSec > 0 {
  255. if entry.Entry.Attributes != nil && c.modifyTimeAgoAtSec < timeNowAtSec-entry.Entry.Attributes.Mtime {
  256. return nil
  257. }
  258. }
  259. dataChunks, manifestChunks, resolveErr := filer.ResolveChunkManifest(filer.LookupFn(c.env), entry.Entry.GetChunks(), 0, math.MaxInt64)
  260. if resolveErr != nil {
  261. return fmt.Errorf("failed to ResolveChunkManifest: %+v", resolveErr)
  262. }
  263. dataChunks = append(dataChunks, manifestChunks...)
  264. if len(dataChunks) > 0 {
  265. outputChan <- &ItemEntry{
  266. chunks: dataChunks,
  267. path: util.NewFullPath(entry.Dir, entry.Entry.Name),
  268. }
  269. }
  270. return nil
  271. },
  272. func(outputChan chan interface{}) {
  273. var wg sync.WaitGroup
  274. itemErrCount := atomic.NewUint64(0)
  275. for itemEntry := range outputChan {
  276. i := itemEntry.(*ItemEntry)
  277. itemPath := string(i.path)
  278. if c.verifyEntry(itemPath, i.chunks, itemErrCount, &wg) {
  279. if *c.verbose {
  280. fmt.Fprintf(c.writer, "file: %s needles:%d verifed\n", itemPath, len(i.chunks))
  281. }
  282. fileCount++
  283. }
  284. }
  285. wg.Wait()
  286. errCount = itemErrCount.Load()
  287. })
  288. }