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
3.6 KiB

6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package command
  2. import (
  3. "strings"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/replication"
  6. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  7. _ "github.com/chrislusf/seaweedfs/weed/replication/sink/azuresink"
  8. _ "github.com/chrislusf/seaweedfs/weed/replication/sink/b2sink"
  9. _ "github.com/chrislusf/seaweedfs/weed/replication/sink/filersink"
  10. _ "github.com/chrislusf/seaweedfs/weed/replication/sink/gcssink"
  11. _ "github.com/chrislusf/seaweedfs/weed/replication/sink/s3sink"
  12. "github.com/chrislusf/seaweedfs/weed/server"
  13. "github.com/spf13/viper"
  14. "github.com/chrislusf/seaweedfs/weed/replication/sub"
  15. )
  16. func init() {
  17. cmdFilerReplicate.Run = runFilerReplicate // break init cycle
  18. }
  19. var cmdFilerReplicate = &Command{
  20. UsageLine: "filer.replicate",
  21. Short: "replicate file changes to another destination",
  22. Long: `replicate file changes to another destination
  23. filer.replicate listens on filer notifications. If any file is updated, it will fetch the updated content,
  24. and write to the other destination.
  25. Run "weed scaffold -config replication" to generate a replication.toml file and customize the parameters.
  26. `,
  27. }
  28. func runFilerReplicate(cmd *Command, args []string) bool {
  29. weed_server.LoadConfiguration("replication", true)
  30. config := viper.GetViper()
  31. var notificationInput sub.NotificationInput
  32. for _, input := range sub.NotificationInputs {
  33. if config.GetBool("notification." + input.GetName() + ".enabled") {
  34. viperSub := config.Sub("notification." + input.GetName())
  35. if err := input.Initialize(viperSub); err != nil {
  36. glog.Fatalf("Failed to initialize notification input for %s: %+v",
  37. input.GetName(), err)
  38. }
  39. glog.V(0).Infof("Configure notification input to %s", input.GetName())
  40. notificationInput = input
  41. break
  42. }
  43. }
  44. if notificationInput == nil {
  45. println("Please follow 'weed scaffold -config=repliaction' to see example notification configurations.")
  46. return true
  47. }
  48. // avoid recursive replication
  49. if config.GetBool("notification.source.filer.enabled") && config.GetBool("notification.sink.filer.enabled") {
  50. sourceConfig, sinkConfig := config.Sub("source.filer"), config.Sub("sink.filer")
  51. if sourceConfig.GetString("grpcAddress") == sinkConfig.GetString("grpcAddress") {
  52. fromDir := sourceConfig.GetString("directory")
  53. toDir := sinkConfig.GetString("directory")
  54. if strings.HasPrefix(toDir, fromDir) {
  55. glog.Fatalf("recursive replication! source directory %s includes the sink directory %s", fromDir, toDir)
  56. }
  57. }
  58. }
  59. var dataSink sink.ReplicationSink
  60. for _, sk := range sink.Sinks {
  61. if config.GetBool("sink." + sk.GetName() + ".enabled") {
  62. viperSub := config.Sub("sink." + sk.GetName())
  63. if err := sk.Initialize(viperSub); err != nil {
  64. glog.Fatalf("Failed to initialize sink for %s: %+v",
  65. sk.GetName(), err)
  66. }
  67. glog.V(0).Infof("Configure sink to %s", sk.GetName())
  68. dataSink = sk
  69. break
  70. }
  71. }
  72. if dataSink == nil {
  73. println("no data sink configured:")
  74. for _, sk := range sink.Sinks {
  75. println(" " + sk.GetName())
  76. }
  77. return true
  78. }
  79. replicator := replication.NewReplicator(config.Sub("source.filer"), dataSink)
  80. for {
  81. key, m, err := notificationInput.ReceiveMessage()
  82. if err != nil {
  83. glog.Errorf("receive %s: %+v", key, err)
  84. continue
  85. }
  86. if key == "" {
  87. // long poll received no messages
  88. continue
  89. }
  90. if m.OldEntry != nil && m.NewEntry == nil {
  91. glog.V(1).Infof("delete: %s", key)
  92. } else if m.OldEntry == nil && m.NewEntry != nil {
  93. glog.V(1).Infof(" add: %s", key)
  94. } else {
  95. glog.V(1).Infof("modify: %s", key)
  96. }
  97. if err = replicator.Replicate(key, m); err != nil {
  98. glog.Errorf("replicate %s: %+v", key, err)
  99. }
  100. }
  101. return true
  102. }