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.

61 lines
1.7 KiB

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