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.

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