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
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/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  8. "github.com/chrislusf/seaweedfs/weed/replication/source"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  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. newKey := filepath.Join(r.sink.GetSinkToDirectory(), key[len(r.source.Dir):])
  30. glog.V(3).Infof("replicate %s => %s", key, newKey)
  31. key = newKey
  32. if message.OldEntry != nil && message.NewEntry == nil {
  33. glog.V(4).Infof("deleting %v", key)
  34. return r.sink.DeleteEntry(key, message.OldEntry.IsDirectory, message.DeleteChunks)
  35. }
  36. if message.OldEntry == nil && message.NewEntry != nil {
  37. glog.V(4).Infof("creating %v", key)
  38. return r.sink.CreateEntry(key, message.NewEntry)
  39. }
  40. if message.OldEntry == nil && message.NewEntry == nil {
  41. glog.V(0).Infof("weird message %+v", message)
  42. return nil
  43. }
  44. foundExisting, err := r.sink.UpdateEntry(key, message.OldEntry, message.NewEntry, message.DeleteChunks)
  45. if foundExisting {
  46. glog.V(4).Infof("updated %v", key)
  47. return err
  48. }
  49. glog.V(4).Infof("creating missing %v", key)
  50. return r.sink.CreateEntry(key, message.NewEntry)
  51. }