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.

51 lines
1.5 KiB

  1. package replication
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/replication/source"
  7. "strings"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  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. return &Replicator{
  27. sink: sink,
  28. source: source,
  29. }
  30. }
  31. func (r *Replicator) Replicate(key string, message *filer_pb.EventNotification) error {
  32. if !strings.HasPrefix(key, r.source.Dir) {
  33. return nil
  34. }
  35. key = r.sink.GetDirectory() + key[len(r.source.Dir):]
  36. if message.OldEntry != nil && message.NewEntry == nil {
  37. return r.sink.DeleteEntry(key, message.OldEntry, message.DeleteChunks)
  38. }
  39. if message.OldEntry == nil && message.NewEntry != nil {
  40. return r.sink.CreateEntry(key, message.NewEntry)
  41. }
  42. return r.sink.UpdateEntry(key, message.OldEntry, message.NewEntry, message.DeleteChunks)
  43. }