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.

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