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.

54 lines
1.5 KiB

6 years ago
6 years ago
  1. package replication
  2. import (
  3. "strings"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  7. "github.com/chrislusf/seaweedfs/weed/replication/source"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. type Replicator struct {
  11. sink sink.ReplicationSink
  12. source *source.FilerSource
  13. }
  14. func NewReplicator(sourceConfig, sinkConfig util.Configuration) *Replicator {
  15. sink := &sink.FilerSink{}
  16. sink.Initialize(sinkConfig)
  17. source := &source.FilerSource{}
  18. source.Initialize(sourceConfig)
  19. if sourceConfig.GetString("grpcAddress") == sinkConfig.GetString("grpcAddress") {
  20. fromDir := sourceConfig.GetString("directory")
  21. toDir := sinkConfig.GetString("directory")
  22. if strings.HasPrefix(toDir, fromDir) {
  23. glog.Fatalf("recursive replication! source directory %s includes the sink directory %s", fromDir, toDir)
  24. }
  25. }
  26. sink.SetSourceFiler(source)
  27. return &Replicator{
  28. sink: sink,
  29. source: source,
  30. }
  31. }
  32. func (r *Replicator) Replicate(key string, message *filer_pb.EventNotification) error {
  33. if !strings.HasPrefix(key, r.source.Dir) {
  34. return nil
  35. }
  36. key = r.sink.GetSinkToDirectory() + key[len(r.source.Dir):]
  37. if message.OldEntry != nil && message.NewEntry == nil {
  38. return r.sink.DeleteEntry(key, message.OldEntry, message.DeleteChunks)
  39. }
  40. if message.OldEntry == nil && message.NewEntry != nil {
  41. return r.sink.CreateEntry(key, message.NewEntry)
  42. }
  43. return r.sink.UpdateEntry(key, message.OldEntry, message.NewEntry, message.DeleteChunks)
  44. }