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.

210 lines
6.6 KiB

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