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.

120 lines
4.6 KiB

3 years ago
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/replication/source"
  10. "github.com/seaweedfs/seaweedfs/weed/security"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. "google.golang.org/grpc"
  13. "os"
  14. "time"
  15. )
  16. type RemoteGatewayOptions struct {
  17. filerAddress *string
  18. grpcDialOption grpc.DialOption
  19. readChunkFromFiler *bool
  20. timeAgo *time.Duration
  21. createBucketAt *string
  22. createBucketRandomSuffix *bool
  23. include *string
  24. exclude *string
  25. mappings *remote_pb.RemoteStorageMapping
  26. remoteConfs map[string]*remote_pb.RemoteConf
  27. bucketsDir string
  28. clientId int32
  29. clientEpoch int32
  30. }
  31. var _ = filer_pb.FilerClient(&RemoteGatewayOptions{})
  32. func (option *RemoteGatewayOptions) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
  33. return pb.WithFilerClient(streamingMode, pb.ServerAddress(*option.filerAddress), option.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  34. return fn(client)
  35. })
  36. }
  37. func (option *RemoteGatewayOptions) AdjustedUrl(location *filer_pb.Location) string {
  38. return location.Url
  39. }
  40. var (
  41. remoteGatewayOptions RemoteGatewayOptions
  42. )
  43. func init() {
  44. cmdFilerRemoteGateway.Run = runFilerRemoteGateway // break init cycle
  45. remoteGatewayOptions.filerAddress = cmdFilerRemoteGateway.Flag.String("filer", "localhost:8888", "filer of the SeaweedFS cluster")
  46. remoteGatewayOptions.createBucketAt = cmdFilerRemoteGateway.Flag.String("createBucketAt", "", "one remote storage name to create new buckets in")
  47. remoteGatewayOptions.createBucketRandomSuffix = cmdFilerRemoteGateway.Flag.Bool("createBucketWithRandomSuffix", true, "add randomized suffix to bucket name to avoid conflicts")
  48. remoteGatewayOptions.readChunkFromFiler = cmdFilerRemoteGateway.Flag.Bool("filerProxy", false, "read file chunks from filer instead of volume servers")
  49. remoteGatewayOptions.timeAgo = cmdFilerRemoteGateway.Flag.Duration("timeAgo", 0, "start time before now. \"300ms\", \"1.5h\" or \"2h45m\". Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"")
  50. remoteGatewayOptions.include = cmdFilerRemoteGateway.Flag.String("include", "", "pattens of new bucket names, e.g., s3*")
  51. remoteGatewayOptions.exclude = cmdFilerRemoteGateway.Flag.String("exclude", "", "pattens of new bucket names, e.g., local*")
  52. remoteGatewayOptions.clientId = util.RandomInt32()
  53. }
  54. var cmdFilerRemoteGateway = &Command{
  55. UsageLine: "filer.remote.gateway",
  56. Short: "resumable continuously write back bucket creation, deletion, and other local updates to remote object store",
  57. Long: `resumable continuously write back bucket creation, deletion, and other local updates to remote object store
  58. filer.remote.gateway listens on filer local buckets update events.
  59. If any bucket is created, deleted, or updated, it will mirror the changes to remote object store.
  60. weed filer.remote.sync -createBucketAt=cloud1
  61. `,
  62. }
  63. func runFilerRemoteGateway(cmd *Command, args []string) bool {
  64. util.LoadConfiguration("security", false)
  65. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  66. remoteGatewayOptions.grpcDialOption = grpcDialOption
  67. filerAddress := pb.ServerAddress(*remoteGatewayOptions.filerAddress)
  68. filerSource := &source.FilerSource{}
  69. filerSource.DoInitialize(
  70. filerAddress.ToHttpAddress(),
  71. filerAddress.ToGrpcAddress(),
  72. "/", // does not matter
  73. *remoteGatewayOptions.readChunkFromFiler,
  74. )
  75. remoteGatewayOptions.bucketsDir = "/buckets"
  76. // check buckets again
  77. remoteGatewayOptions.WithFilerClient(false, func(filerClient filer_pb.SeaweedFilerClient) error {
  78. resp, err := filerClient.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  79. if err != nil {
  80. return err
  81. }
  82. remoteGatewayOptions.bucketsDir = resp.DirBuckets
  83. return nil
  84. })
  85. // read filer remote storage mount mappings
  86. if detectErr := remoteGatewayOptions.collectRemoteStorageConf(); detectErr != nil {
  87. fmt.Fprintf(os.Stderr, "read mount info: %v\n", detectErr)
  88. return true
  89. }
  90. // synchronize /buckets folder
  91. fmt.Printf("synchronize buckets in %s ...\n", remoteGatewayOptions.bucketsDir)
  92. util.RetryForever("filer.remote.sync buckets", func() error {
  93. return remoteGatewayOptions.followBucketUpdatesAndUploadToRemote(filerSource)
  94. }, func(err error) bool {
  95. if err != nil {
  96. glog.Errorf("synchronize %s: %v", remoteGatewayOptions.bucketsDir, err)
  97. }
  98. return true
  99. })
  100. return true
  101. }