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.

148 lines
4.4 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, onSuccessFn, onFailureFn, err := notificationInput.ReceiveMessage()
  83. if err != nil {
  84. glog.Errorf("receive %s: %+v", key, err)
  85. if onFailureFn != nil {
  86. onFailureFn()
  87. }
  88. continue
  89. }
  90. if key == "" {
  91. // long poll received no messages
  92. if onSuccessFn != nil {
  93. onSuccessFn()
  94. }
  95. continue
  96. }
  97. if m.OldEntry != nil && m.NewEntry == nil {
  98. glog.V(1).Infof("delete: %s", key)
  99. } else if m.OldEntry == nil && m.NewEntry != nil {
  100. glog.V(1).Infof(" add: %s", key)
  101. } else {
  102. glog.V(1).Infof("modify: %s", key)
  103. }
  104. if err = replicator.Replicate(context.Background(), key, m); err != nil {
  105. glog.Errorf("replicate %s: %+v", key, err)
  106. if onFailureFn != nil {
  107. onFailureFn()
  108. }
  109. } else {
  110. glog.V(1).Infof("replicated %s", key)
  111. if onSuccessFn != nil {
  112. onSuccessFn()
  113. }
  114. }
  115. }
  116. }
  117. func validateOneEnabledInput(config *util.ViperProxy) {
  118. enabledInput := ""
  119. for _, input := range sub.NotificationInputs {
  120. if config.GetBool("notification." + input.GetName() + ".enabled") {
  121. if enabledInput == "" {
  122. enabledInput = input.GetName()
  123. } else {
  124. glog.Fatalf("Notification input is enabled for both %s and %s", enabledInput, input.GetName())
  125. }
  126. }
  127. }
  128. }