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.

59 lines
1.6 KiB

6 years ago
6 years ago
6 years ago
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/source"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. )
  11. type Replicator struct {
  12. sink sink.ReplicationSink
  13. source *source.FilerSource
  14. }
  15. func NewReplicator(sourceConfig util.Configuration, dataSink sink.ReplicationSink) *Replicator {
  16. source := &source.FilerSource{}
  17. source.Initialize(sourceConfig)
  18. dataSink.SetSourceFiler(source)
  19. return &Replicator{
  20. sink: dataSink,
  21. source: source,
  22. }
  23. }
  24. func (r *Replicator) Replicate(key string, message *filer_pb.EventNotification) error {
  25. if !strings.HasPrefix(key, r.source.Dir) {
  26. glog.V(4).Infof("skipping %v outside of %v", key, r.source.Dir)
  27. return nil
  28. }
  29. key = filepath.Join(r.sink.GetSinkToDirectory(), key[len(r.source.Dir):])
  30. if message.OldEntry != nil && message.NewEntry == nil {
  31. glog.V(4).Infof("deleting %v", key)
  32. return r.sink.DeleteEntry(key, message.OldEntry.IsDirectory, message.DeleteChunks)
  33. }
  34. if message.OldEntry == nil && message.NewEntry != nil {
  35. glog.V(4).Infof("creating %v", key)
  36. return r.sink.CreateEntry(key, message.NewEntry)
  37. }
  38. if message.OldEntry == nil && message.NewEntry == nil {
  39. glog.V(0).Infof("weird message %+v", message)
  40. return nil
  41. }
  42. foundExisting, err := r.sink.UpdateEntry(key, message.OldEntry, message.NewEntry, message.DeleteChunks)
  43. if foundExisting {
  44. glog.V(4).Infof("updated %v", key)
  45. return err
  46. }
  47. glog.V(4).Infof("creating missing %v", key)
  48. return r.sink.CreateEntry(key, message.NewEntry)
  49. }