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.

190 lines
4.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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. `
  32. }
  33. func (c *commandFsMetaSave) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  34. fsMetaSaveCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  35. verbose := fsMetaSaveCommand.Bool("v", false, "print out each processed files")
  36. outputFileName := fsMetaSaveCommand.String("o", "", "output the meta data to this file")
  37. // chunksFileName := fsMetaSaveCommand.String("chunks", "", "output all the chunks to this file")
  38. if err = fsMetaSaveCommand.Parse(args); err != nil {
  39. return nil
  40. }
  41. path, parseErr := commandEnv.parseUrl(findInputDirectory(fsMetaSaveCommand.Args()))
  42. if parseErr != nil {
  43. return parseErr
  44. }
  45. fileName := *outputFileName
  46. if fileName == "" {
  47. t := time.Now()
  48. fileName = fmt.Sprintf("%s-%d-%4d%02d%02d-%02d%02d%02d.meta",
  49. commandEnv.option.FilerHost, commandEnv.option.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. return doTraverseBfsAndSaving(commandEnv, writer, path, *verbose, func(outputChan chan interface{}) {
  57. sizeBuf := make([]byte, 4)
  58. for item := range outputChan {
  59. b := item.([]byte)
  60. util.Uint32toBytes(sizeBuf, uint32(len(b)))
  61. dst.Write(sizeBuf)
  62. dst.Write(b)
  63. }
  64. }, func(entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
  65. bytes, err := proto.Marshal(entry)
  66. if err != nil {
  67. fmt.Fprintf(writer, "marshall error: %v\n", err)
  68. return
  69. }
  70. outputChan <- bytes
  71. return nil
  72. })
  73. }
  74. func doTraverseBfsAndSaving(commandEnv *CommandEnv, writer io.Writer, path string, verbose bool, saveFn func(outputChan chan interface{}), genFn func(entry *filer_pb.FullEntry, outputChan chan interface{}) error) error {
  75. var wg sync.WaitGroup
  76. wg.Add(1)
  77. outputChan := make(chan interface{}, 1024)
  78. go func() {
  79. saveFn(outputChan)
  80. wg.Done()
  81. }()
  82. var dirCount, fileCount uint64
  83. err := doTraverseBfs(writer, commandEnv, util.FullPath(path), func(parentPath util.FullPath, entry *filer_pb.Entry) {
  84. protoMessage := &filer_pb.FullEntry{
  85. Dir: string(parentPath),
  86. Entry: entry,
  87. }
  88. if err := genFn(protoMessage, outputChan); err != nil {
  89. fmt.Fprintf(writer, "marshall error: %v\n", err)
  90. return
  91. }
  92. if entry.IsDirectory {
  93. atomic.AddUint64(&dirCount, 1)
  94. } else {
  95. atomic.AddUint64(&fileCount, 1)
  96. }
  97. if verbose {
  98. println(parentPath.Child(entry.Name))
  99. }
  100. })
  101. close(outputChan)
  102. wg.Wait()
  103. if err == nil && writer != nil {
  104. fmt.Fprintf(writer, "total %d directories, %d files\n", dirCount, fileCount)
  105. }
  106. return err
  107. }
  108. func doTraverseBfs(writer io.Writer, filerClient filer_pb.FilerClient, parentPath util.FullPath, fn func(parentPath util.FullPath, entry *filer_pb.Entry)) (err error) {
  109. K := 5
  110. var jobQueueWg sync.WaitGroup
  111. queue := util.NewQueue()
  112. jobQueueWg.Add(1)
  113. queue.Enqueue(parentPath)
  114. var isTerminating bool
  115. for i := 0; i < K; i++ {
  116. go func() {
  117. for {
  118. if isTerminating {
  119. break
  120. }
  121. t := queue.Dequeue()
  122. if t == nil {
  123. time.Sleep(329 * time.Millisecond)
  124. continue
  125. }
  126. dir := t.(util.FullPath)
  127. processErr := processOneDirectory(writer, filerClient, dir, queue, &jobQueueWg, fn)
  128. if processErr != nil {
  129. err = processErr
  130. }
  131. jobQueueWg.Done()
  132. }
  133. }()
  134. }
  135. jobQueueWg.Wait()
  136. isTerminating = true
  137. return
  138. }
  139. 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) {
  140. return filer_pb.ReadDirAllEntries(filerClient, parentPath, "", func(entry *filer_pb.Entry, isLast bool) {
  141. fn(parentPath, entry)
  142. if entry.IsDirectory {
  143. subDir := fmt.Sprintf("%s/%s", parentPath, entry.Name)
  144. if parentPath == "/" {
  145. subDir = "/" + entry.Name
  146. }
  147. jobQueueWg.Add(1)
  148. queue.Enqueue(util.FullPath(subDir))
  149. }
  150. })
  151. }