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.

149 lines
3.5 KiB

6 years ago
6 years ago
6 years ago
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. "strings"
  8. "sync"
  9. "time"
  10. "google.golang.org/protobuf/proto"
  11. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  12. "github.com/seaweedfs/seaweedfs/weed/util"
  13. )
  14. func init() {
  15. Commands = append(Commands, &commandFsMetaLoad{})
  16. }
  17. type commandFsMetaLoad struct {
  18. dirPrefix *string
  19. }
  20. func (c *commandFsMetaLoad) Name() string {
  21. return "fs.meta.load"
  22. }
  23. func (c *commandFsMetaLoad) Help() string {
  24. return `load saved filer meta data to restore the directory and file structure
  25. fs.meta.load <filer_host>-<port>-<time>.meta
  26. fs.meta.load -v=false <filer_host>-<port>-<time>.meta // skip printing out the verbose output
  27. fs.meta.load -dirPrefix=/buckets/important* <filer_host>.meta // load any dirs with prefix "important"
  28. `
  29. }
  30. func (c *commandFsMetaLoad) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  31. if len(args) == 0 {
  32. fmt.Fprintf(writer, "missing a metadata file\n")
  33. return nil
  34. }
  35. fileName := args[len(args)-1]
  36. metaLoadCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  37. c.dirPrefix = metaLoadCommand.String("dirPrefix", "", "load entries only with directories matching prefix")
  38. concurrency := metaLoadCommand.Int("concurrency", 1, "number of parallel meta load to filer")
  39. verbose := metaLoadCommand.Bool("v", true, "verbose mode")
  40. if err = metaLoadCommand.Parse(args[0 : len(args)-1]); err != nil {
  41. return nil
  42. }
  43. dst, err := os.OpenFile(fileName, os.O_RDONLY, 0644)
  44. if err != nil {
  45. return nil
  46. }
  47. defer dst.Close()
  48. var dirCount, fileCount uint64
  49. lastLogTime := time.Now()
  50. err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  51. sizeBuf := make([]byte, 4)
  52. waitChan := make(chan struct{}, *concurrency)
  53. defer close(waitChan)
  54. var wg sync.WaitGroup
  55. for {
  56. if n, err := dst.Read(sizeBuf); n != 4 {
  57. if err == io.EOF {
  58. return nil
  59. }
  60. return err
  61. }
  62. size := util.BytesToUint32(sizeBuf)
  63. data := make([]byte, int(size))
  64. if n, err := dst.Read(data); n != len(data) {
  65. return err
  66. }
  67. fullEntry := &filer_pb.FullEntry{}
  68. if err = proto.Unmarshal(data, fullEntry); err != nil {
  69. return err
  70. }
  71. // check collection name pattern
  72. entryFullName := string(util.FullPath(fullEntry.Dir).Child(fullEntry.Entry.Name))
  73. if *c.dirPrefix != "" {
  74. if !strings.HasPrefix(fullEntry.Dir, *c.dirPrefix) {
  75. if *verbose {
  76. fmt.Fprintf(writer, "not match dir prefix %s\n", entryFullName)
  77. }
  78. continue
  79. }
  80. }
  81. if *verbose || lastLogTime.Add(time.Second).Before(time.Now()) {
  82. if !*verbose {
  83. lastLogTime = time.Now()
  84. }
  85. fmt.Fprintf(writer, "load %s\n", entryFullName)
  86. }
  87. fullEntry.Entry.Name = strings.ReplaceAll(fullEntry.Entry.Name, "/", "x")
  88. if fullEntry.Entry.IsDirectory {
  89. wg.Wait()
  90. if errEntry := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  91. Directory: fullEntry.Dir,
  92. Entry: fullEntry.Entry,
  93. }); errEntry != nil {
  94. return errEntry
  95. }
  96. dirCount++
  97. } else {
  98. wg.Add(1)
  99. waitChan <- struct{}{}
  100. go func(entry *filer_pb.FullEntry) {
  101. if errEntry := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  102. Directory: entry.Dir,
  103. Entry: entry.Entry,
  104. }); errEntry != nil {
  105. err = errEntry
  106. }
  107. defer wg.Done()
  108. <-waitChan
  109. }(fullEntry)
  110. if err != nil {
  111. return err
  112. }
  113. fileCount++
  114. }
  115. }
  116. })
  117. if err == nil {
  118. fmt.Fprintf(writer, "\ntotal %d directories, %d files", dirCount, fileCount)
  119. fmt.Fprintf(writer, "\n%s is loaded.\n", fileName)
  120. }
  121. return err
  122. }