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.

136 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
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. )
  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. util.LoadConfiguration("security", false)
  30. util.LoadConfiguration("replication", true)
  31. util.LoadConfiguration("notification", true)
  32. config := util.GetViper()
  33. var notificationInput sub.NotificationInput
  34. validateOneEnabledInput(config)
  35. for _, input := range sub.NotificationInputs {
  36. if config.GetBool("notification." + input.GetName() + ".enabled") {
  37. if err := input.Initialize(config, "notification."+input.GetName()+"."); 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. if config.GetString("source.filer.grpcAddress") == config.GetString("sink.filer.grpcAddress") {
  54. fromDir := config.GetString("source.filer.directory")
  55. toDir := config.GetString("sink.filer.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. if err := sk.Initialize(config, "sink."+sk.GetName()+"."); err != nil {
  65. glog.Fatalf("Failed to initialize sink for %s: %+v",
  66. sk.GetName(), err)
  67. }
  68. glog.V(0).Infof("Configure sink to %s", sk.GetName())
  69. dataSink = sk
  70. break
  71. }
  72. }
  73. if dataSink == nil {
  74. println("no data sink configured in replication.toml:")
  75. for _, sk := range sink.Sinks {
  76. println(" " + sk.GetName())
  77. }
  78. return true
  79. }
  80. replicator := replication.NewReplicator(config, "source.filer.", dataSink)
  81. for {
  82. key, m, err := notificationInput.ReceiveMessage()
  83. if err != nil {
  84. glog.Errorf("receive %s: %+v", key, err)
  85. continue
  86. }
  87. if key == "" {
  88. // long poll received no messages
  89. continue
  90. }
  91. if m.OldEntry != nil && m.NewEntry == nil {
  92. glog.V(1).Infof("delete: %s", key)
  93. } else if m.OldEntry == nil && m.NewEntry != nil {
  94. glog.V(1).Infof(" add: %s", key)
  95. } else {
  96. glog.V(1).Infof("modify: %s", key)
  97. }
  98. if err = replicator.Replicate(context.Background(), key, m); err != nil {
  99. glog.Errorf("replicate %s: %+v", key, err)
  100. } else {
  101. glog.V(1).Infof("replicated %s", key)
  102. }
  103. }
  104. }
  105. func validateOneEnabledInput(config *util.ViperProxy) {
  106. enabledInput := ""
  107. for _, input := range sub.NotificationInputs {
  108. if config.GetBool("notification." + input.GetName() + ".enabled") {
  109. if enabledInput == "" {
  110. enabledInput = input.GetName()
  111. } else {
  112. glog.Fatalf("Notification input is enabled for both %s and %s", enabledInput, input.GetName())
  113. }
  114. }
  115. }
  116. }