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.

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