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.

219 lines
6.9 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. // find configuration for remote storage
  49. remoteConf, err := filer.ReadRemoteStorageConf(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress, remote_storage.ParseLocationName(*remote))
  50. if err != nil {
  51. return fmt.Errorf("find configuration for %s: %v", *remote, err)
  52. }
  53. remoteStorageLocation, err := remote_storage.ParseRemoteLocation(remoteConf.Type, *remote)
  54. if err != nil {
  55. return err
  56. }
  57. // sync metadata from remote
  58. if err = c.syncMetadata(commandEnv, writer, *dir, *nonEmpty, remoteConf, remoteStorageLocation); err != nil {
  59. return fmt.Errorf("pull metadata: %v", err)
  60. }
  61. // store a mount configuration in filer
  62. if err = c.saveMountMapping(commandEnv, writer, *dir, remoteStorageLocation); err != nil {
  63. return fmt.Errorf("save mount mapping: %v", err)
  64. }
  65. return nil
  66. }
  67. func listExistingRemoteStorageMounts(commandEnv *CommandEnv, writer io.Writer) (mappings *remote_pb.RemoteStorageMapping, err error) {
  68. // read current mapping
  69. mappings, err = filer.ReadMountMappings(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress)
  70. if err != nil {
  71. return mappings, err
  72. }
  73. jsonPrintln(writer, mappings)
  74. return
  75. }
  76. func jsonPrintln(writer io.Writer, message proto.Message) error {
  77. if message == nil {
  78. return nil
  79. }
  80. m := jsonpb.Marshaler{
  81. EmitDefaults: false,
  82. Indent: " ",
  83. }
  84. err := m.Marshal(writer, message)
  85. fmt.Fprintln(writer)
  86. return err
  87. }
  88. func (c *commandRemoteMount) findRemoteStorageConfiguration(commandEnv *CommandEnv, writer io.Writer, remote *remote_pb.RemoteStorageLocation) (conf *remote_pb.RemoteConf, err error) {
  89. return filer.ReadRemoteStorageConf(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress, remote.Name)
  90. }
  91. func (c *commandRemoteMount) syncMetadata(commandEnv *CommandEnv, writer io.Writer, dir string, nonEmpty bool, remoteConf *remote_pb.RemoteConf, remote *remote_pb.RemoteStorageLocation) error {
  92. // find existing directory, and ensure the directory is empty
  93. err := commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  94. parent, name := util.FullPath(dir).DirAndName()
  95. _, lookupErr := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
  96. Directory: parent,
  97. Name: name,
  98. })
  99. if lookupErr != nil {
  100. if !strings.Contains(lookupErr.Error(), filer_pb.ErrNotFound.Error()) {
  101. return fmt.Errorf("lookup %s: %v", dir, lookupErr)
  102. }
  103. }
  104. mountToDirIsEmpty := true
  105. listErr := filer_pb.SeaweedList(client, dir, "", func(entry *filer_pb.Entry, isLast bool) error {
  106. mountToDirIsEmpty = false
  107. return nil
  108. }, "", false, 1)
  109. if listErr != nil {
  110. return fmt.Errorf("list %s: %v", dir, listErr)
  111. }
  112. if !mountToDirIsEmpty {
  113. if !nonEmpty {
  114. return fmt.Errorf("dir %s is not empty", dir)
  115. }
  116. }
  117. return nil
  118. })
  119. if err != nil {
  120. return err
  121. }
  122. // pull metadata from remote
  123. if err = pullMetadata(commandEnv, writer, util.FullPath(dir), remote, util.FullPath(dir), remoteConf); err != nil {
  124. return fmt.Errorf("cache content data: %v", err)
  125. }
  126. return nil
  127. }
  128. func (c *commandRemoteMount) saveMountMapping(commandEnv *CommandEnv, writer io.Writer, dir string, remoteStorageLocation *remote_pb.RemoteStorageLocation) (err error) {
  129. // read current mapping
  130. var oldContent, newContent []byte
  131. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  132. oldContent, err = filer.ReadInsideFiler(client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE)
  133. return err
  134. })
  135. if err != nil {
  136. if err != filer_pb.ErrNotFound {
  137. return fmt.Errorf("read existing mapping: %v", err)
  138. }
  139. }
  140. // add new mapping
  141. newContent, err = filer.AddRemoteStorageMapping(oldContent, dir, remoteStorageLocation)
  142. if err != nil {
  143. return fmt.Errorf("add mapping %s~%s: %v", dir, remoteStorageLocation, err)
  144. }
  145. // save back
  146. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  147. return filer.SaveInsideFiler(client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE, newContent)
  148. })
  149. if err != nil {
  150. return fmt.Errorf("save mapping: %v", err)
  151. }
  152. return nil
  153. }
  154. // if an entry has synchronized metadata but has not synchronized content
  155. // entry.Attributes.FileSize == entry.RemoteEntry.RemoteSize
  156. // entry.Attributes.Mtime == entry.RemoteEntry.RemoteMtime
  157. // entry.RemoteEntry.LastLocalSyncTsNs == 0
  158. // if an entry has synchronized metadata but has synchronized content before
  159. // entry.Attributes.FileSize == entry.RemoteEntry.RemoteSize
  160. // entry.Attributes.Mtime == entry.RemoteEntry.RemoteMtime
  161. // entry.RemoteEntry.LastLocalSyncTsNs > 0
  162. // if an entry has synchronized metadata but has new updates
  163. // entry.Attributes.Mtime * 1,000,000,000 > entry.RemoteEntry.LastLocalSyncTsNs
  164. func doSaveRemoteEntry(client filer_pb.SeaweedFilerClient, localDir string, existingEntry *filer_pb.Entry, remoteEntry *filer_pb.RemoteEntry) error {
  165. existingEntry.RemoteEntry = remoteEntry
  166. existingEntry.Attributes.FileSize = uint64(remoteEntry.RemoteSize)
  167. existingEntry.Attributes.Mtime = remoteEntry.RemoteMtime
  168. _, updateErr := client.UpdateEntry(context.Background(), &filer_pb.UpdateEntryRequest{
  169. Directory: localDir,
  170. Entry: existingEntry,
  171. })
  172. if updateErr != nil {
  173. return updateErr
  174. }
  175. return nil
  176. }