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.

150 lines
4.5 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/localincrementalsink"
  13. _ "github.com/chrislusf/seaweedfs/weed/replication/sink/localsink"
  14. _ "github.com/chrislusf/seaweedfs/weed/replication/sink/s3sink"
  15. "github.com/chrislusf/seaweedfs/weed/replication/sub"
  16. "github.com/chrislusf/seaweedfs/weed/util"
  17. )
  18. func init() {
  19. cmdFilerReplicate.Run = runFilerReplicate // break init cycle
  20. }
  21. var cmdFilerReplicate = &Command{
  22. UsageLine: "filer.replicate",
  23. Short: "replicate file changes to another destination",
  24. Long: `replicate file changes to another destination
  25. filer.replicate listens on filer notifications. If any file is updated, it will fetch the updated content,
  26. and write to the other destination.
  27. Run "weed scaffold -config=replication" to generate a replication.toml file and customize the parameters.
  28. `,
  29. }
  30. func runFilerReplicate(cmd *Command, args []string) bool {
  31. util.LoadConfiguration("security", false)
  32. util.LoadConfiguration("replication", true)
  33. util.LoadConfiguration("notification", true)
  34. config := util.GetViper()
  35. var notificationInput sub.NotificationInput
  36. validateOneEnabledInput(config)
  37. for _, input := range sub.NotificationInputs {
  38. if config.GetBool("notification." + input.GetName() + ".enabled") {
  39. if err := input.Initialize(config, "notification."+input.GetName()+"."); 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. if config.GetString("source.filer.grpcAddress") == config.GetString("sink.filer.grpcAddress") {
  56. fromDir := config.GetString("source.filer.directory")
  57. toDir := config.GetString("sink.filer.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. if err := sk.Initialize(config, "sink."+sk.GetName()+"."); 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, "source.filer.", dataSink)
  83. for {
  84. key, m, onSuccessFn, onFailureFn, err := notificationInput.ReceiveMessage()
  85. if err != nil {
  86. glog.Errorf("receive %s: %+v", key, err)
  87. if onFailureFn != nil {
  88. onFailureFn()
  89. }
  90. continue
  91. }
  92. if key == "" {
  93. // long poll received no messages
  94. if onSuccessFn != nil {
  95. onSuccessFn()
  96. }
  97. continue
  98. }
  99. if m.OldEntry != nil && m.NewEntry == nil {
  100. glog.V(1).Infof("delete: %s", key)
  101. } else if m.OldEntry == nil && m.NewEntry != nil {
  102. glog.V(1).Infof(" add: %s", key)
  103. } else {
  104. glog.V(1).Infof("modify: %s", key)
  105. }
  106. if err = replicator.Replicate(context.Background(), key, m); err != nil {
  107. glog.Errorf("replicate %s: %+v", key, err)
  108. if onFailureFn != nil {
  109. onFailureFn()
  110. }
  111. } else {
  112. glog.V(1).Infof("replicated %s", key)
  113. if onSuccessFn != nil {
  114. onSuccessFn()
  115. }
  116. }
  117. }
  118. }
  119. func validateOneEnabledInput(config *util.ViperProxy) {
  120. enabledInput := ""
  121. for _, input := range sub.NotificationInputs {
  122. if config.GetBool("notification." + input.GetName() + ".enabled") {
  123. if enabledInput == "" {
  124. enabledInput = input.GetName()
  125. } else {
  126. glog.Fatalf("Notification input is enabled for both %s and %s", enabledInput, input.GetName())
  127. }
  128. }
  129. }
  130. }