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.

178 lines
4.4 KiB

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