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.

213 lines
6.7 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
  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. if message == nil {
  74. return nil
  75. }
  76. m := jsonpb.Marshaler{
  77. EmitDefaults: false,
  78. Indent: " ",
  79. }
  80. err := m.Marshal(writer, message)
  81. fmt.Fprintln(writer)
  82. return err
  83. }
  84. func (c *commandRemoteMount) findRemoteStorageConfiguration(commandEnv *CommandEnv, writer io.Writer, remote *filer_pb.RemoteStorageLocation) (conf *filer_pb.RemoteConf, err error) {
  85. return filer.ReadRemoteStorageConf(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress, remote.Name)
  86. }
  87. func (c *commandRemoteMount) syncMetadata(commandEnv *CommandEnv, writer io.Writer, dir string, nonEmpty bool, remoteConf *filer_pb.RemoteConf, remote *filer_pb.RemoteStorageLocation) error {
  88. // find existing directory, and ensure the directory is empty
  89. err := commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  90. parent, name := util.FullPath(dir).DirAndName()
  91. _, lookupErr := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
  92. Directory: parent,
  93. Name: name,
  94. })
  95. if lookupErr != nil {
  96. return fmt.Errorf("lookup %s: %v", dir, lookupErr)
  97. }
  98. mountToDirIsEmpty := true
  99. listErr := filer_pb.SeaweedList(client, dir, "", func(entry *filer_pb.Entry, isLast bool) error {
  100. mountToDirIsEmpty = false
  101. return nil
  102. }, "", false, 1)
  103. if listErr != nil {
  104. return fmt.Errorf("list %s: %v", dir, listErr)
  105. }
  106. if !mountToDirIsEmpty {
  107. if !nonEmpty {
  108. return fmt.Errorf("dir %s is not empty", dir)
  109. }
  110. }
  111. return nil
  112. })
  113. if err != nil {
  114. return err
  115. }
  116. // pull metadata from remote
  117. if err = pullMetadata(commandEnv, writer, util.FullPath(dir), remote, util.FullPath(dir), remoteConf); err != nil {
  118. return fmt.Errorf("cache content data: %v", err)
  119. }
  120. return nil
  121. }
  122. func (c *commandRemoteMount) saveMountMapping(commandEnv *CommandEnv, writer io.Writer, dir string, remoteStorageLocation *filer_pb.RemoteStorageLocation) (err error) {
  123. // read current mapping
  124. var oldContent, newContent []byte
  125. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  126. oldContent, err = filer.ReadInsideFiler(client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE)
  127. return err
  128. })
  129. if err != nil {
  130. if err != filer_pb.ErrNotFound {
  131. return fmt.Errorf("read existing mapping: %v", err)
  132. }
  133. }
  134. // add new mapping
  135. newContent, err = filer.AddRemoteStorageMapping(oldContent, dir, remoteStorageLocation)
  136. if err != nil {
  137. return fmt.Errorf("add mapping %s~%s: %v", dir, remoteStorageLocation, err)
  138. }
  139. // save back
  140. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  141. return filer.SaveInsideFiler(client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE, newContent)
  142. })
  143. if err != nil {
  144. return fmt.Errorf("save mapping: %v", err)
  145. }
  146. return nil
  147. }
  148. // if an entry has synchronized metadata but has not synchronized content
  149. // entry.Attributes.FileSize == entry.RemoteEntry.RemoteSize
  150. // entry.Attributes.Mtime == entry.RemoteEntry.RemoteMtime
  151. // entry.RemoteEntry.LastLocalSyncTsNs == 0
  152. // if an entry has synchronized metadata but has synchronized content before
  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 new updates
  157. // entry.Attributes.Mtime * 1,000,000,000 > entry.RemoteEntry.LastLocalSyncTsNs
  158. func doSaveRemoteEntry(client filer_pb.SeaweedFilerClient, localDir string, existingEntry *filer_pb.Entry, remoteEntry *filer_pb.RemoteEntry) error {
  159. existingEntry.RemoteEntry = remoteEntry
  160. existingEntry.Attributes.FileSize = uint64(remoteEntry.RemoteSize)
  161. existingEntry.Attributes.Mtime = remoteEntry.RemoteMtime
  162. _, updateErr := client.UpdateEntry(context.Background(), &filer_pb.UpdateEntryRequest{
  163. Directory: localDir,
  164. Entry: existingEntry,
  165. })
  166. if updateErr != nil {
  167. return updateErr
  168. }
  169. return nil
  170. }