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.

97 lines
3.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. package command
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/pb"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/replication/source"
  8. "github.com/chrislusf/seaweedfs/weed/security"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "google.golang.org/grpc"
  11. "time"
  12. )
  13. type RemoteSyncOptions struct {
  14. filerAddress *string
  15. grpcDialOption grpc.DialOption
  16. readChunkFromFiler *bool
  17. timeAgo *time.Duration
  18. dir *string
  19. }
  20. var _ = filer_pb.FilerClient(&RemoteSyncOptions{})
  21. func (option *RemoteSyncOptions) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  22. return pb.WithFilerClient(pb.ServerAddress(*option.filerAddress), option.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  23. return fn(client)
  24. })
  25. }
  26. func (option *RemoteSyncOptions) AdjustedUrl(location *filer_pb.Location) string {
  27. return location.Url
  28. }
  29. var (
  30. remoteSyncOptions RemoteSyncOptions
  31. )
  32. func init() {
  33. cmdFilerRemoteSynchronize.Run = runFilerRemoteSynchronize // break init cycle
  34. remoteSyncOptions.filerAddress = cmdFilerRemoteSynchronize.Flag.String("filer", "localhost:8888", "filer of the SeaweedFS cluster")
  35. remoteSyncOptions.dir = cmdFilerRemoteSynchronize.Flag.String("dir", "", "a mounted directory on filer")
  36. remoteSyncOptions.readChunkFromFiler = cmdFilerRemoteSynchronize.Flag.Bool("filerProxy", false, "read file chunks from filer instead of volume servers")
  37. remoteSyncOptions.timeAgo = cmdFilerRemoteSynchronize.Flag.Duration("timeAgo", 0, "start time before now, skipping previous metadata changes. \"300ms\", \"1.5h\" or \"2h45m\". Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"")
  38. }
  39. var cmdFilerRemoteSynchronize = &Command{
  40. UsageLine: "filer.remote.sync",
  41. Short: "resumable continuously write back updates to remote storage",
  42. Long: `resumable continuously write back updates to remote storage
  43. filer.remote.sync listens on filer update events.
  44. If any mounted remote file is updated, it will fetch the updated content,
  45. and write to the remote storage.
  46. weed filer.remote.sync -dir=/mount/s3_on_cloud
  47. The metadata sync starting time is determined with the following priority order:
  48. 1. specified by timeAgo
  49. 2. last sync timestamp for this directory
  50. 3. directory creation time
  51. `,
  52. }
  53. func runFilerRemoteSynchronize(cmd *Command, args []string) bool {
  54. util.LoadConfiguration("security", false)
  55. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  56. remoteSyncOptions.grpcDialOption = grpcDialOption
  57. dir := *remoteSyncOptions.dir
  58. filerAddress := pb.ServerAddress(*remoteSyncOptions.filerAddress)
  59. filerSource := &source.FilerSource{}
  60. filerSource.DoInitialize(
  61. filerAddress.ToHttpAddress(),
  62. filerAddress.ToGrpcAddress(),
  63. "/", // does not matter
  64. *remoteSyncOptions.readChunkFromFiler,
  65. )
  66. if dir != "" {
  67. fmt.Printf("synchronize %s to remote storage...\n", dir)
  68. util.RetryForever("filer.remote.sync "+dir, func() error {
  69. return followUpdatesAndUploadToRemote(&remoteSyncOptions, filerSource, dir)
  70. }, func(err error) bool {
  71. if err != nil {
  72. glog.Errorf("synchronize %s: %v", dir, err)
  73. }
  74. return true
  75. })
  76. return true
  77. }
  78. return true
  79. }