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.

138 lines
3.9 KiB

3 years ago
3 years ago
3 years ago
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. "io"
  9. )
  10. func init() {
  11. Commands = append(Commands, &commandRemoteCache{})
  12. }
  13. type commandRemoteCache struct {
  14. }
  15. func (c *commandRemoteCache) Name() string {
  16. return "remote.cache"
  17. }
  18. func (c *commandRemoteCache) Help() string {
  19. return `cache the file content for mounted directories or files
  20. # assume a remote storage is configured to name "cloud1"
  21. remote.configure -name=cloud1 -type=s3 -access_key=xxx -secret_key=yyy
  22. # mount and pull one bucket
  23. remote.mount -dir=/xxx -remote=cloud1/bucket
  24. # after mount, run one of these command to cache the content of the files
  25. remote.cache -dir=/xxx
  26. remote.cache -dir=/xxx/some/sub/dir
  27. remote.cache -dir=/xxx/some/sub/dir -include=*.pdf
  28. This is designed to run regularly. So you can add it to some cronjob.
  29. If a file is already synchronized with the remote copy, the file will be skipped to avoid unnecessary copy.
  30. The actual data copying goes through volume severs.
  31. `
  32. }
  33. func (c *commandRemoteCache) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  34. remoteMountCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  35. dir := remoteMountCommand.String("dir", "", "a directory in filer")
  36. fileFiler := newFileFilter(remoteMountCommand)
  37. if err = remoteMountCommand.Parse(args); err != nil {
  38. return nil
  39. }
  40. mappings, localMountedDir, remoteStorageMountedLocation, remoteStorageConf, detectErr := detectMountInfo(commandEnv, writer, *dir)
  41. if detectErr != nil{
  42. jsonPrintln(writer, mappings)
  43. return detectErr
  44. }
  45. // pull content from remote
  46. if err = c.cacheContentData(commandEnv, writer, util.FullPath(localMountedDir), remoteStorageMountedLocation, util.FullPath(*dir), fileFiler, remoteStorageConf); err != nil {
  47. return fmt.Errorf("cache content data: %v", err)
  48. }
  49. return nil
  50. }
  51. func recursivelyTraverseDirectory(filerClient filer_pb.FilerClient, dirPath util.FullPath, visitEntry func(dir util.FullPath, entry *filer_pb.Entry) bool) (err error) {
  52. err = filer_pb.ReadDirAllEntries(filerClient, dirPath, "", func(entry *filer_pb.Entry, isLast bool) error {
  53. if entry.IsDirectory {
  54. if !visitEntry(dirPath, entry) {
  55. return nil
  56. }
  57. subDir := dirPath.Child(entry.Name)
  58. if err := recursivelyTraverseDirectory(filerClient, subDir, visitEntry); err != nil {
  59. return err
  60. }
  61. } else {
  62. if !visitEntry(dirPath, entry) {
  63. return nil
  64. }
  65. }
  66. return nil
  67. })
  68. return
  69. }
  70. func shouldCacheToLocal(entry *filer_pb.Entry) bool {
  71. if entry.IsDirectory {
  72. return false
  73. }
  74. if entry.RemoteEntry == nil {
  75. return false
  76. }
  77. if entry.RemoteEntry.LastLocalSyncTsNs == 0 && entry.RemoteEntry.RemoteSize > 0 {
  78. return true
  79. }
  80. return false
  81. }
  82. func mayHaveCachedToLocal(entry *filer_pb.Entry) bool {
  83. if entry.IsDirectory {
  84. return false
  85. }
  86. if entry.RemoteEntry == nil {
  87. return false // should not uncache an entry that is not in remote
  88. }
  89. if entry.RemoteEntry.LastLocalSyncTsNs > 0 && len(entry.Chunks) > 0 {
  90. return true
  91. }
  92. return false
  93. }
  94. func (c *commandRemoteCache) cacheContentData(commandEnv *CommandEnv, writer io.Writer, localMountedDir util.FullPath, remoteMountedLocation *filer_pb.RemoteStorageLocation, dirToCache util.FullPath, fileFilter *FileFilter, remoteConf *filer_pb.RemoteConf) error {
  95. return recursivelyTraverseDirectory(commandEnv, dirToCache, func(dir util.FullPath, entry *filer_pb.Entry) bool {
  96. if !shouldCacheToLocal(entry) {
  97. return true // true means recursive traversal should continue
  98. }
  99. if fileFilter.matches(entry) {
  100. return true
  101. }
  102. println(dir, entry.Name)
  103. remoteLocation := filer.MapFullPathToRemoteStorageLocation(localMountedDir, remoteMountedLocation, dir.Child(entry.Name))
  104. if err := filer.DownloadToLocal(commandEnv, remoteConf, remoteLocation, dir, entry); err != nil {
  105. fmt.Fprintf(writer, "DownloadToLocal %+v: %v\n", remoteLocation, err)
  106. return false
  107. }
  108. return true
  109. })
  110. }