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.

108 lines
3.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
  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/filersink"
  8. _ "github.com/chrislusf/seaweedfs/weed/replication/sink/gcssink"
  9. _ "github.com/chrislusf/seaweedfs/weed/replication/sink/s3sink"
  10. "github.com/chrislusf/seaweedfs/weed/server"
  11. "github.com/spf13/viper"
  12. )
  13. func init() {
  14. cmdFilerReplicate.Run = runFilerReplicate // break init cycle
  15. }
  16. var cmdFilerReplicate = &Command{
  17. UsageLine: "filer.replicate",
  18. Short: "replicate file changes to another destination",
  19. Long: `replicate file changes to another destination
  20. filer.replicate listens on filer notifications. If any file is updated, it will fetch the updated content,
  21. and write to the other destination.
  22. Run "weed scaffold -config replication" to generate a replication.toml file and customize the parameters.
  23. `,
  24. }
  25. func runFilerReplicate(cmd *Command, args []string) bool {
  26. weed_server.LoadConfiguration("replication", true)
  27. config := viper.GetViper()
  28. var notificationInput replication.NotificationInput
  29. for _, input := range replication.NotificationInputs {
  30. if config.GetBool("notification." + input.GetName() + ".enabled") {
  31. viperSub := config.Sub("notification." + input.GetName())
  32. if err := input.Initialize(viperSub); err != nil {
  33. glog.Fatalf("Failed to initialize notification input for %s: %+v",
  34. input.GetName(), err)
  35. }
  36. glog.V(0).Infof("Configure notification input to %s", input.GetName())
  37. notificationInput = input
  38. break
  39. }
  40. }
  41. // avoid recursive replication
  42. if config.GetBool("notification.source.filer.enabled") && config.GetBool("notification.sink.filer.enabled") {
  43. sourceConfig, sinkConfig := config.Sub("source.filer"), config.Sub("sink.filer")
  44. if sourceConfig.GetString("grpcAddress") == sinkConfig.GetString("grpcAddress") {
  45. fromDir := sourceConfig.GetString("directory")
  46. toDir := sinkConfig.GetString("directory")
  47. if strings.HasPrefix(toDir, fromDir) {
  48. glog.Fatalf("recursive replication! source directory %s includes the sink directory %s", fromDir, toDir)
  49. }
  50. }
  51. }
  52. var dataSink sink.ReplicationSink
  53. for _, sk := range sink.Sinks {
  54. if config.GetBool("sink." + sk.GetName() + ".enabled") {
  55. viperSub := config.Sub("sink." + sk.GetName())
  56. if err := sk.Initialize(viperSub); err != nil {
  57. glog.Fatalf("Failed to initialize sink for %s: %+v",
  58. sk.GetName(), err)
  59. }
  60. glog.V(0).Infof("Configure sink to %s", sk.GetName())
  61. dataSink = sk
  62. break
  63. }
  64. }
  65. if dataSink == nil {
  66. println("no data sink configured:")
  67. for _, sk := range sink.Sinks {
  68. println(" " + sk.GetName())
  69. }
  70. return true
  71. }
  72. replicator := replication.NewReplicator(config.Sub("source.filer"), dataSink)
  73. for {
  74. key, m, err := notificationInput.ReceiveMessage()
  75. if err != nil {
  76. glog.Errorf("receive %s: %+v", key, err)
  77. continue
  78. }
  79. if m.OldEntry != nil && m.NewEntry == nil {
  80. glog.V(1).Infof("delete: %s", key)
  81. } else if m.OldEntry == nil && m.NewEntry != nil {
  82. glog.V(1).Infof(" add: %s", key)
  83. } else {
  84. glog.V(1).Infof("modify: %s", key)
  85. }
  86. if err = replicator.Replicate(key, m); err != nil {
  87. glog.Errorf("replicate %s: %+v", key, err)
  88. }
  89. }
  90. return true
  91. }