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.

202 lines
4.9 KiB

  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "os"
  8. "sync"
  9. "sync/atomic"
  10. "time"
  11. "github.com/golang/protobuf/proto"
  12. "github.com/chrislusf/seaweedfs/weed/filer2"
  13. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. func init() {
  17. Commands = append(Commands, &commandFsMetaSave{})
  18. }
  19. type commandFsMetaSave struct {
  20. }
  21. func (c *commandFsMetaSave) Name() string {
  22. return "fs.meta.save"
  23. }
  24. func (c *commandFsMetaSave) Help() string {
  25. return `save all directory and file meta data to a local file for metadata backup.
  26. fs.meta.save / # save from the root
  27. fs.meta.save -v -o t.meta / # save from the root, output to t.meta file.
  28. fs.meta.save /path/to/save # save from the directory /path/to/save
  29. fs.meta.save . # save from current directory
  30. fs.meta.save # save from current directory
  31. The meta data will be saved into a local <filer_host>-<port>-<time>.meta file.
  32. These meta data can be later loaded by fs.meta.load command,
  33. This assumes there are no deletions, so this is different from taking a snapshot.
  34. `
  35. }
  36. func (c *commandFsMetaSave) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  37. fsMetaSaveCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  38. verbose := fsMetaSaveCommand.Bool("v", false, "print out each processed files")
  39. outputFileName := fsMetaSaveCommand.String("o", "", "output the meta data to this file")
  40. if err = fsMetaSaveCommand.Parse(args); err != nil {
  41. return nil
  42. }
  43. filerServer, filerPort, path, err := commandEnv.parseUrl(findInputDirectory(fsMetaSaveCommand.Args()))
  44. if err != nil {
  45. return err
  46. }
  47. ctx := context.Background()
  48. return commandEnv.withFilerClient(ctx, filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  49. t := time.Now()
  50. fileName := *outputFileName
  51. if fileName == "" {
  52. fileName = fmt.Sprintf("%s-%d-%4d%02d%02d-%02d%02d%02d.meta",
  53. filerServer, filerPort, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
  54. }
  55. dst, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  56. if err != nil {
  57. return nil
  58. }
  59. defer dst.Close()
  60. var dirCount, fileCount uint64
  61. err = doTraverseBFS(ctx, writer, client, filer2.FullPath(path), func(parentPath filer2.FullPath, entry *filer_pb.Entry) error {
  62. protoMessage := &filer_pb.FullEntry{
  63. Dir: string(parentPath),
  64. Entry: entry,
  65. }
  66. bytes, err := proto.Marshal(protoMessage)
  67. if err != nil {
  68. return fmt.Errorf("marshall error: %v", err)
  69. }
  70. sizeBuf := make([]byte, 4)
  71. util.Uint32toBytes(sizeBuf, uint32(len(bytes)))
  72. dst.Write(sizeBuf)
  73. dst.Write(bytes)
  74. if entry.IsDirectory {
  75. atomic.AddUint64(&dirCount, 1)
  76. } else {
  77. atomic.AddUint64(&fileCount, 1)
  78. }
  79. if *verbose {
  80. println(parentPath.Child(entry.Name))
  81. }
  82. return nil
  83. })
  84. if err == nil {
  85. fmt.Fprintf(writer, "\ntotal %d directories, %d files", dirCount, fileCount)
  86. fmt.Fprintf(writer, "\nmeta data for http://%s:%d%s is saved to %s\n", filerServer, filerPort, path, fileName)
  87. }
  88. return err
  89. })
  90. }
  91. func doTraverseBFS(ctx context.Context, writer io.Writer, client filer_pb.SeaweedFilerClient,
  92. parentPath filer2.FullPath, fn func(parentPath filer2.FullPath, entry *filer_pb.Entry) error) (err error) {
  93. K := 5
  94. var jobQueueWg sync.WaitGroup
  95. queue := util.NewQueue()
  96. jobQueueWg.Add(1)
  97. queue.Enqueue(parentPath)
  98. var isTerminating bool
  99. for i := 0; i < K; i++ {
  100. go func() {
  101. for {
  102. if isTerminating {
  103. break
  104. }
  105. t := queue.Dequeue()
  106. if t == nil {
  107. time.Sleep(329 * time.Millisecond)
  108. continue
  109. }
  110. dir := t.(filer2.FullPath)
  111. processErr := processOneDirectory(ctx, writer, client, dir, queue, &jobQueueWg, fn)
  112. if processErr != nil {
  113. err = processErr
  114. }
  115. jobQueueWg.Done()
  116. }
  117. }()
  118. }
  119. jobQueueWg.Wait()
  120. isTerminating = true
  121. return
  122. }
  123. func processOneDirectory(ctx context.Context, writer io.Writer, client filer_pb.SeaweedFilerClient,
  124. parentPath filer2.FullPath, queue *util.Queue, jobQueueWg *sync.WaitGroup,
  125. fn func(parentPath filer2.FullPath, entry *filer_pb.Entry) error) (err error) {
  126. paginatedCount := -1
  127. startFromFileName := ""
  128. paginateSize := 1000
  129. for paginatedCount == -1 || paginatedCount == paginateSize {
  130. resp, listErr := client.ListEntries(ctx, &filer_pb.ListEntriesRequest{
  131. Directory: string(parentPath),
  132. Prefix: "",
  133. StartFromFileName: startFromFileName,
  134. InclusiveStartFrom: false,
  135. Limit: uint32(paginateSize),
  136. })
  137. if listErr != nil {
  138. err = listErr
  139. return
  140. }
  141. paginatedCount = len(resp.Entries)
  142. for _, entry := range resp.Entries {
  143. if err = fn(parentPath, entry); err != nil {
  144. return err
  145. }
  146. if entry.IsDirectory {
  147. subDir := fmt.Sprintf("%s/%s", parentPath, entry.Name)
  148. if parentPath == "/" {
  149. subDir = "/" + entry.Name
  150. }
  151. jobQueueWg.Add(1)
  152. queue.Enqueue(filer2.FullPath(subDir))
  153. }
  154. startFromFileName = entry.Name
  155. }
  156. }
  157. return
  158. }