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.

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