You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
7.0 KiB

more solid weed mount (#4089) * compare chunks by timestamp * fix slab clearing error * fix test compilation * move oldest chunk to sealed, instead of by fullness * lock on fh.entryViewCache * remove verbose logs * revert slat clearing * less logs * less logs * track write and read by timestamp * remove useless logic * add entry lock on file handle release * use mem chunk only, swap file chunk has problems * comment out code that maybe used later * add debug mode to compare data read and write * more efficient readResolvedChunks with linked list * small optimization * fix test compilation * minor fix on writer * add SeparateGarbageChunks * group chunks into sections * turn off debug mode * fix tests * fix tests * tmp enable swap file chunk * Revert "tmp enable swap file chunk" This reverts commit 985137ec472924e4815f258189f6ca9f2168a0a7. * simple refactoring * simple refactoring * do not re-use swap file chunk. Sealed chunks should not be re-used. * comment out debugging facilities * either mem chunk or swap file chunk is fine now * remove orderedMutex as *semaphore.Weighted not found impactful * optimize size calculation for changing large files * optimize performance to avoid going through the long list of chunks * still problems with swap file chunk * rename * tiny optimization * swap file chunk save only successfully read data * fix * enable both mem and swap file chunk * resolve chunks with range * rename * fix chunk interval list * also change file handle chunk group when adding chunks * pick in-active chunk with time-decayed counter * fix compilation * avoid nil with empty fh.entry * refactoring * rename * rename * refactor visible intervals to *list.List * refactor chunkViews to *list.List * add IntervalList for generic interval list * change visible interval to use IntervalList in generics * cahnge chunkViews to *IntervalList[*ChunkView] * use NewFileChunkSection to create * rename variables * refactor * fix renaming leftover * renaming * renaming * add insert interval * interval list adds lock * incrementally add chunks to readers Fixes: 1. set start and stop offset for the value object 2. clone the value object 3. use pointer instead of copy-by-value when passing to interval.Value 4. use insert interval since adding chunk could be out of order * fix tests compilation * fix tests compilation
2 years ago
  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. }