command_fs_meta_load.go 3.7 KB

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