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.

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