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.

124 lines
3.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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/replication/sub"
  13. "github.com/chrislusf/seaweedfs/weed/server"
  14. "github.com/spf13/viper"
  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. weed_server.LoadConfiguration("notification", true)
  31. config := viper.GetViper()
  32. var notificationInput sub.NotificationInput
  33. for _, input := range sub.NotificationInputs {
  34. if config.GetBool("notification." + input.GetName() + ".enabled") {
  35. viperSub := config.Sub("notification." + input.GetName())
  36. if err := input.Initialize(viperSub); err != nil {
  37. glog.Fatalf("Failed to initialize notification input for %s: %+v",
  38. input.GetName(), err)
  39. }
  40. glog.V(0).Infof("Configure notification input to %s", input.GetName())
  41. notificationInput = input
  42. break
  43. }
  44. }
  45. if notificationInput == nil {
  46. println("No notification is defined in notification.toml file.")
  47. println("Please follow 'weed scaffold -config=notification' to see example notification configurations.")
  48. return true
  49. }
  50. // avoid recursive replication
  51. if config.GetBool("notification.source.filer.enabled") && config.GetBool("notification.sink.filer.enabled") {
  52. sourceConfig, sinkConfig := config.Sub("source.filer"), config.Sub("sink.filer")
  53. if sourceConfig.GetString("grpcAddress") == sinkConfig.GetString("grpcAddress") {
  54. fromDir := sourceConfig.GetString("directory")
  55. toDir := sinkConfig.GetString("directory")
  56. if strings.HasPrefix(toDir, fromDir) {
  57. glog.Fatalf("recursive replication! source directory %s includes the sink directory %s", fromDir, toDir)
  58. }
  59. }
  60. }
  61. var dataSink sink.ReplicationSink
  62. for _, sk := range sink.Sinks {
  63. if config.GetBool("sink." + sk.GetName() + ".enabled") {
  64. viperSub := config.Sub("sink." + sk.GetName())
  65. if err := sk.Initialize(viperSub); err != nil {
  66. glog.Fatalf("Failed to initialize sink for %s: %+v",
  67. sk.GetName(), err)
  68. }
  69. glog.V(0).Infof("Configure sink to %s", sk.GetName())
  70. dataSink = sk
  71. break
  72. }
  73. }
  74. if dataSink == nil {
  75. println("no data sink configured in replication.toml:")
  76. for _, sk := range sink.Sinks {
  77. println(" " + sk.GetName())
  78. }
  79. return true
  80. }
  81. replicator := replication.NewReplicator(config.Sub("source.filer"), dataSink)
  82. for {
  83. key, m, err := notificationInput.ReceiveMessage()
  84. if err != nil {
  85. glog.Errorf("receive %s: %+v", key, err)
  86. continue
  87. }
  88. if key == "" {
  89. // long poll received no messages
  90. continue
  91. }
  92. if m.OldEntry != nil && m.NewEntry == nil {
  93. glog.V(1).Infof("delete: %s", key)
  94. } else if m.OldEntry == nil && m.NewEntry != nil {
  95. glog.V(1).Infof(" add: %s", key)
  96. } else {
  97. glog.V(1).Infof("modify: %s", key)
  98. }
  99. if err = replicator.Replicate(key, m); err != nil {
  100. glog.Errorf("replicate %s: %+v", key, err)
  101. } else {
  102. glog.V(1).Infof("replicated %s", key)
  103. }
  104. }
  105. return true
  106. }