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.

137 lines
3.9 KiB

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. 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. localMountedDir, remoteStorageMountedLocation, remoteStorageConf, detectErr := detectMountInfo(commandEnv, writer, *dir)
  41. if detectErr != nil{
  42. return detectErr
  43. }
  44. // pull content from remote
  45. if err = c.cacheContentData(commandEnv, writer, util.FullPath(localMountedDir), remoteStorageMountedLocation, util.FullPath(*dir), fileFiler, remoteStorageConf); err != nil {
  46. return fmt.Errorf("cache content data: %v", err)
  47. }
  48. return nil
  49. }
  50. func recursivelyTraverseDirectory(filerClient filer_pb.FilerClient, dirPath util.FullPath, visitEntry func(dir util.FullPath, entry *filer_pb.Entry) bool) (err error) {
  51. err = filer_pb.ReadDirAllEntries(filerClient, dirPath, "", func(entry *filer_pb.Entry, isLast bool) error {
  52. if entry.IsDirectory {
  53. if !visitEntry(dirPath, entry) {
  54. return nil
  55. }
  56. subDir := dirPath.Child(entry.Name)
  57. if err := recursivelyTraverseDirectory(filerClient, subDir, visitEntry); err != nil {
  58. return err
  59. }
  60. } else {
  61. if !visitEntry(dirPath, entry) {
  62. return nil
  63. }
  64. }
  65. return nil
  66. })
  67. return
  68. }
  69. func shouldCacheToLocal(entry *filer_pb.Entry) bool {
  70. if entry.IsDirectory {
  71. return false
  72. }
  73. if entry.RemoteEntry == nil {
  74. return false
  75. }
  76. if entry.RemoteEntry.LastLocalSyncTsNs == 0 && entry.RemoteEntry.RemoteSize > 0 {
  77. return true
  78. }
  79. return false
  80. }
  81. func mayHaveCachedToLocal(entry *filer_pb.Entry) bool {
  82. if entry.IsDirectory {
  83. return false
  84. }
  85. if entry.RemoteEntry == nil {
  86. return false // should not uncache an entry that is not in remote
  87. }
  88. if entry.RemoteEntry.LastLocalSyncTsNs > 0 && len(entry.Chunks) > 0 {
  89. return true
  90. }
  91. return false
  92. }
  93. 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 {
  94. return recursivelyTraverseDirectory(commandEnv, dirToCache, func(dir util.FullPath, entry *filer_pb.Entry) bool {
  95. if !shouldCacheToLocal(entry) {
  96. return true // true means recursive traversal should continue
  97. }
  98. if fileFilter.matches(entry) {
  99. return true
  100. }
  101. println(dir, entry.Name)
  102. remoteLocation := filer.MapFullPathToRemoteStorageLocation(localMountedDir, remoteMountedLocation, dir.Child(entry.Name))
  103. if err := filer.DownloadToLocal(commandEnv, remoteConf, remoteLocation, dir, entry); err != nil {
  104. fmt.Fprintf(writer, "DownloadToLocal %+v: %v\n", remoteLocation, err)
  105. return false
  106. }
  107. return true
  108. })
  109. }