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.

141 lines
4.1 KiB

3 years ago
3 years ago
4 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. remote.cache -dir=/xxx/some/sub/dir -exclude=*.txt
  29. remote.cache -maxSize=1024000 # cache files smaller than 100K
  30. remote.cache -maxAge=3600 # cache files less than 1 hour old
  31. This is designed to run regularly. So you can add it to some cronjob.
  32. If a file is already synchronized with the remote copy, the file will be skipped to avoid unnecessary copy.
  33. The actual data copying goes through volume severs in parallel.
  34. `
  35. }
  36. func (c *commandRemoteCache) 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. fileFiler := newFileFilter(remoteMountCommand)
  40. if err = remoteMountCommand.Parse(args); err != nil {
  41. return nil
  42. }
  43. mappings, localMountedDir, remoteStorageMountedLocation, remoteStorageConf, detectErr := detectMountInfo(commandEnv, writer, *dir)
  44. if detectErr != nil{
  45. jsonPrintln(writer, mappings)
  46. return detectErr
  47. }
  48. // pull content from remote
  49. if err = c.cacheContentData(commandEnv, writer, util.FullPath(localMountedDir), remoteStorageMountedLocation, util.FullPath(*dir), fileFiler, remoteStorageConf); err != nil {
  50. return fmt.Errorf("cache content data: %v", err)
  51. }
  52. return nil
  53. }
  54. func recursivelyTraverseDirectory(filerClient filer_pb.FilerClient, dirPath util.FullPath, visitEntry func(dir util.FullPath, entry *filer_pb.Entry) bool) (err error) {
  55. err = filer_pb.ReadDirAllEntries(filerClient, dirPath, "", func(entry *filer_pb.Entry, isLast bool) error {
  56. if entry.IsDirectory {
  57. if !visitEntry(dirPath, entry) {
  58. return nil
  59. }
  60. subDir := dirPath.Child(entry.Name)
  61. if err := recursivelyTraverseDirectory(filerClient, subDir, visitEntry); err != nil {
  62. return err
  63. }
  64. } else {
  65. if !visitEntry(dirPath, entry) {
  66. return nil
  67. }
  68. }
  69. return nil
  70. })
  71. return
  72. }
  73. func shouldCacheToLocal(entry *filer_pb.Entry) bool {
  74. if entry.IsDirectory {
  75. return false
  76. }
  77. if entry.RemoteEntry == nil {
  78. return false
  79. }
  80. if entry.RemoteEntry.LastLocalSyncTsNs == 0 && entry.RemoteEntry.RemoteSize > 0 {
  81. return true
  82. }
  83. return false
  84. }
  85. func mayHaveCachedToLocal(entry *filer_pb.Entry) bool {
  86. if entry.IsDirectory {
  87. return false
  88. }
  89. if entry.RemoteEntry == nil {
  90. return false // should not uncache an entry that is not in remote
  91. }
  92. if entry.RemoteEntry.LastLocalSyncTsNs > 0 && len(entry.Chunks) > 0 {
  93. return true
  94. }
  95. return false
  96. }
  97. 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 {
  98. return recursivelyTraverseDirectory(commandEnv, dirToCache, func(dir util.FullPath, entry *filer_pb.Entry) bool {
  99. if !shouldCacheToLocal(entry) {
  100. return true // true means recursive traversal should continue
  101. }
  102. if fileFilter.matches(entry) {
  103. return true
  104. }
  105. println(dir, entry.Name)
  106. remoteLocation := filer.MapFullPathToRemoteStorageLocation(localMountedDir, remoteMountedLocation, dir.Child(entry.Name))
  107. if err := filer.DownloadToLocal(commandEnv, remoteConf, remoteLocation, dir, entry); err != nil {
  108. fmt.Fprintf(writer, "DownloadToLocal %+v: %v\n", remoteLocation, err)
  109. return false
  110. }
  111. return true
  112. })
  113. }