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.

217 lines
6.8 KiB

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