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.

196 lines
5.1 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. err = 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. if err == nil {
  74. fmt.Fprintf(writer, "meta data for http://%s:%d%s is saved to %s\n", commandEnv.option.FilerHost, commandEnv.option.FilerPort, path, fileName)
  75. }
  76. return err
  77. }
  78. 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 {
  79. var wg sync.WaitGroup
  80. wg.Add(1)
  81. outputChan := make(chan interface{}, 1024)
  82. go func() {
  83. saveFn(outputChan)
  84. wg.Done()
  85. }()
  86. var dirCount, fileCount uint64
  87. err := doTraverseBfs(writer, commandEnv, util.FullPath(path), func(parentPath util.FullPath, entry *filer_pb.Entry) {
  88. protoMessage := &filer_pb.FullEntry{
  89. Dir: string(parentPath),
  90. Entry: entry,
  91. }
  92. if err := genFn(protoMessage, outputChan); err != nil {
  93. fmt.Fprintf(writer, "marshall error: %v\n", err)
  94. return
  95. }
  96. if entry.IsDirectory {
  97. atomic.AddUint64(&dirCount, 1)
  98. } else {
  99. atomic.AddUint64(&fileCount, 1)
  100. }
  101. if verbose {
  102. println(parentPath.Child(entry.Name))
  103. }
  104. })
  105. close(outputChan)
  106. wg.Wait()
  107. if err == nil && writer != nil {
  108. fmt.Fprintf(writer, "total %d directories, %d files\n", dirCount, fileCount)
  109. }
  110. return err
  111. }
  112. func doTraverseBfs(writer io.Writer, filerClient filer_pb.FilerClient, parentPath util.FullPath, fn func(parentPath util.FullPath, entry *filer_pb.Entry)) (err error) {
  113. K := 5
  114. var jobQueueWg sync.WaitGroup
  115. queue := util.NewQueue()
  116. jobQueueWg.Add(1)
  117. queue.Enqueue(parentPath)
  118. var isTerminating bool
  119. for i := 0; i < K; i++ {
  120. go func() {
  121. for {
  122. if isTerminating {
  123. break
  124. }
  125. t := queue.Dequeue()
  126. if t == nil {
  127. time.Sleep(329 * time.Millisecond)
  128. continue
  129. }
  130. dir := t.(util.FullPath)
  131. processErr := processOneDirectory(writer, filerClient, dir, queue, &jobQueueWg, fn)
  132. if processErr != nil {
  133. err = processErr
  134. }
  135. jobQueueWg.Done()
  136. }
  137. }()
  138. }
  139. jobQueueWg.Wait()
  140. isTerminating = true
  141. return
  142. }
  143. 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) {
  144. return filer_pb.ReadDirAllEntries(filerClient, parentPath, "", func(entry *filer_pb.Entry, isLast bool) {
  145. fn(parentPath, entry)
  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(util.FullPath(subDir))
  153. }
  154. })
  155. }