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.

200 lines
6.0 KiB

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