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.

99 lines
2.4 KiB

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/util"
  9. "io"
  10. "strings"
  11. )
  12. func init() {
  13. Commands = append(Commands, &commandRemoteUncache{})
  14. }
  15. type commandRemoteUncache struct {
  16. }
  17. func (c *commandRemoteUncache) Name() string {
  18. return "remote.uncache"
  19. }
  20. func (c *commandRemoteUncache) Help() string {
  21. return `keep the metadata but remote cache the file content for mounted directories or files
  22. remote.uncache -dir=xxx
  23. remote.uncache -dir=xxx/some/sub/dir
  24. `
  25. }
  26. func (c *commandRemoteUncache) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. remoteMountCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  28. dir := remoteMountCommand.String("dir", "", "a directory in filer")
  29. if err = remoteMountCommand.Parse(args); err != nil {
  30. return nil
  31. }
  32. mappings, listErr := filer.ReadMountMappings(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress)
  33. if listErr != nil {
  34. return listErr
  35. }
  36. if *dir == "" {
  37. jsonPrintln(writer, mappings)
  38. fmt.Fprintln(writer, "need to specify '-dir' option")
  39. return nil
  40. }
  41. var localMountedDir string
  42. for k := range mappings.Mappings {
  43. if strings.HasPrefix(*dir, k) {
  44. localMountedDir = k
  45. }
  46. }
  47. if localMountedDir == "" {
  48. jsonPrintln(writer, mappings)
  49. fmt.Fprintf(writer, "%s is not mounted\n", *dir)
  50. return nil
  51. }
  52. // pull content from remote
  53. if err = c.uncacheContentData(commandEnv, writer, util.FullPath(*dir)); err != nil {
  54. return fmt.Errorf("cache content data: %v", err)
  55. }
  56. return nil
  57. }
  58. func (c *commandRemoteUncache) uncacheContentData(commandEnv *CommandEnv, writer io.Writer, dirToCache util.FullPath) error {
  59. return recursivelyTraverseDirectory(commandEnv, dirToCache, func(dir util.FullPath, entry *filer_pb.Entry) bool {
  60. if !mayHaveCachedToLocal(entry) {
  61. return true // true means recursive traversal should continue
  62. }
  63. entry.RemoteEntry.LocalMtime = 0
  64. entry.Chunks = nil
  65. println(dir, entry.Name)
  66. err := commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  67. _, updateErr := client.UpdateEntry(context.Background(), &filer_pb.UpdateEntryRequest{
  68. Directory: string(dir),
  69. Entry: entry,
  70. })
  71. return updateErr
  72. })
  73. if err != nil {
  74. fmt.Fprintf(writer, "uncache %+v: %v\n", dir.Child(entry.Name), err)
  75. return false
  76. }
  77. return true
  78. })
  79. }