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.

100 lines
3.3 KiB

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