command_remote_mount.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/pb/remote_pb"
  9. "github.com/chrislusf/seaweedfs/weed/remote_storage"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "github.com/golang/protobuf/jsonpb"
  12. "github.com/golang/protobuf/proto"
  13. "io"
  14. "os"
  15. "strings"
  16. "time"
  17. )
  18. func init() {
  19. Commands = append(Commands, &commandRemoteMount{})
  20. }
  21. type commandRemoteMount struct {
  22. }
  23. func (c *commandRemoteMount) Name() string {
  24. return "remote.mount"
  25. }
  26. func (c *commandRemoteMount) Help() string {
  27. return `mount remote storage and pull its metadata
  28. # assume a remote storage is configured to name "cloud1"
  29. remote.configure -name=cloud1 -type=s3 -s3.access_key=xxx -s3.secret_key=yyy
  30. # mount and pull one bucket
  31. remote.mount -dir=/xxx -remote=cloud1/bucket
  32. # mount and pull one directory in the bucket
  33. remote.mount -dir=/xxx -remote=cloud1/bucket/dir1
  34. # after mount, start a separate process to write updates to remote storage
  35. weed filer.remote.sync -filer=<filerHost>:<filerPort> -dir=/xxx
  36. `
  37. }
  38. func (c *commandRemoteMount) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  39. remoteMountCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  40. dir := remoteMountCommand.String("dir", "", "a directory in filer")
  41. nonEmpty := remoteMountCommand.Bool("nonempty", false, "allows the mounting over a non-empty directory")
  42. remote := remoteMountCommand.String("remote", "", "a directory in remote storage, ex. <storageName>/<bucket>/path/to/dir")
  43. if err = remoteMountCommand.Parse(args); err != nil {
  44. return nil
  45. }
  46. if *dir == "" {
  47. _, err = listExistingRemoteStorageMounts(commandEnv, writer)
  48. return err
  49. }
  50. // find configuration for remote storage
  51. remoteConf, err := filer.ReadRemoteStorageConf(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress, remote_storage.ParseLocationName(*remote))
  52. if err != nil {
  53. return fmt.Errorf("find configuration for %s: %v", *remote, err)
  54. }
  55. remoteStorageLocation, err := remote_storage.ParseRemoteLocation(remoteConf.Type, *remote)
  56. if err != nil {
  57. return err
  58. }
  59. // sync metadata from remote
  60. if err = syncMetadata(commandEnv, writer, *dir, *nonEmpty, remoteConf, remoteStorageLocation); err != nil {
  61. return fmt.Errorf("pull metadata: %v", err)
  62. }
  63. // store a mount configuration in filer
  64. if err = filer.InsertMountMapping(commandEnv, *dir, remoteStorageLocation); err != nil {
  65. return fmt.Errorf("save mount mapping: %v", err)
  66. }
  67. return nil
  68. }
  69. func listExistingRemoteStorageMounts(commandEnv *CommandEnv, writer io.Writer) (mappings *remote_pb.RemoteStorageMapping, err error) {
  70. // read current mapping
  71. mappings, err = filer.ReadMountMappings(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress)
  72. if err != nil {
  73. return mappings, err
  74. }
  75. jsonPrintln(writer, mappings)
  76. return
  77. }
  78. func jsonPrintln(writer io.Writer, message proto.Message) error {
  79. if message == nil {
  80. return nil
  81. }
  82. m := jsonpb.Marshaler{
  83. EmitDefaults: false,
  84. Indent: " ",
  85. }
  86. err := m.Marshal(writer, message)
  87. fmt.Fprintln(writer)
  88. return err
  89. }
  90. func syncMetadata(commandEnv *CommandEnv, writer io.Writer, dir string, nonEmpty bool, remoteConf *remote_pb.RemoteConf, remote *remote_pb.RemoteStorageLocation) error {
  91. // find existing directory, and ensure the directory is empty
  92. err := commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  93. parent, name := util.FullPath(dir).DirAndName()
  94. _, lookupErr := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
  95. Directory: parent,
  96. Name: name,
  97. })
  98. if lookupErr != nil {
  99. if strings.Contains(lookupErr.Error(), filer_pb.ErrNotFound.Error()) {
  100. _, createErr := client.CreateEntry(context.Background(), &filer_pb.CreateEntryRequest{
  101. Directory: parent,
  102. Entry: &filer_pb.Entry{
  103. Name: name,
  104. IsDirectory: true,
  105. Attributes: &filer_pb.FuseAttributes{
  106. Mtime: time.Now().Unix(),
  107. Crtime: time.Now().Unix(),
  108. FileMode: uint32(0644 | os.ModeDir),
  109. },
  110. RemoteEntry: &filer_pb.RemoteEntry{
  111. StorageName: remoteConf.Name,
  112. },
  113. },
  114. })
  115. return createErr
  116. }
  117. }
  118. mountToDirIsEmpty := true
  119. listErr := filer_pb.SeaweedList(client, dir, "", func(entry *filer_pb.Entry, isLast bool) error {
  120. mountToDirIsEmpty = false
  121. return nil
  122. }, "", false, 1)
  123. if listErr != nil {
  124. return fmt.Errorf("list %s: %v", dir, listErr)
  125. }
  126. if !mountToDirIsEmpty {
  127. if !nonEmpty {
  128. return fmt.Errorf("dir %s is not empty", dir)
  129. }
  130. }
  131. return nil
  132. })
  133. if err != nil {
  134. return err
  135. }
  136. // pull metadata from remote
  137. if err = pullMetadata(commandEnv, writer, util.FullPath(dir), remote, util.FullPath(dir), remoteConf); err != nil {
  138. return fmt.Errorf("cache metadata: %v", err)
  139. }
  140. return nil
  141. }
  142. // if an entry has synchronized metadata but has not synchronized content
  143. // entry.Attributes.FileSize == entry.RemoteEntry.RemoteSize
  144. // entry.Attributes.Mtime == entry.RemoteEntry.RemoteMtime
  145. // entry.RemoteEntry.LastLocalSyncTsNs == 0
  146. // if an entry has synchronized metadata but has synchronized content before
  147. // entry.Attributes.FileSize == entry.RemoteEntry.RemoteSize
  148. // entry.Attributes.Mtime == entry.RemoteEntry.RemoteMtime
  149. // entry.RemoteEntry.LastLocalSyncTsNs > 0
  150. // if an entry has synchronized metadata but has new updates
  151. // entry.Attributes.Mtime * 1,000,000,000 > entry.RemoteEntry.LastLocalSyncTsNs
  152. func doSaveRemoteEntry(client filer_pb.SeaweedFilerClient, localDir string, existingEntry *filer_pb.Entry, remoteEntry *filer_pb.RemoteEntry) error {
  153. existingEntry.RemoteEntry = remoteEntry
  154. existingEntry.Attributes.FileSize = uint64(remoteEntry.RemoteSize)
  155. existingEntry.Attributes.Mtime = remoteEntry.RemoteMtime
  156. _, updateErr := client.UpdateEntry(context.Background(), &filer_pb.UpdateEntryRequest{
  157. Directory: localDir,
  158. Entry: existingEntry,
  159. })
  160. if updateErr != nil {
  161. return updateErr
  162. }
  163. return nil
  164. }