command_fs_meta_load.go 3.8 KB

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