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.

67 lines
1.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package replication
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  9. "github.com/chrislusf/seaweedfs/weed/replication/source"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. type Replicator struct {
  13. sink sink.ReplicationSink
  14. source *source.FilerSource
  15. }
  16. func NewReplicator(sourceConfig util.Configuration, configPrefix string, dataSink sink.ReplicationSink) *Replicator {
  17. source := &source.FilerSource{}
  18. source.Initialize(sourceConfig, configPrefix)
  19. dataSink.SetSourceFiler(source)
  20. return &Replicator{
  21. sink: dataSink,
  22. source: source,
  23. }
  24. }
  25. func (r *Replicator) Replicate(ctx context.Context, key string, message *filer_pb.EventNotification) error {
  26. if !strings.HasPrefix(key, r.source.Dir) {
  27. glog.V(4).Infof("skipping %v outside of %v", key, r.source.Dir)
  28. return nil
  29. }
  30. newKey := util.Join(r.sink.GetSinkToDirectory(), key[len(r.source.Dir):])
  31. glog.V(3).Infof("replicate %s => %s", key, newKey)
  32. key = newKey
  33. if message.OldEntry != nil && message.NewEntry == nil {
  34. glog.V(4).Infof("deleting %v", key)
  35. return r.sink.DeleteEntry(key, message.OldEntry.IsDirectory, message.DeleteChunks)
  36. }
  37. if message.OldEntry == nil && message.NewEntry != nil {
  38. glog.V(4).Infof("creating %v", key)
  39. return r.sink.CreateEntry(key, message.NewEntry)
  40. }
  41. if message.OldEntry == nil && message.NewEntry == nil {
  42. glog.V(0).Infof("weird message %+v", message)
  43. return nil
  44. }
  45. foundExisting, err := r.sink.UpdateEntry(key, message.OldEntry, message.NewParentPath, message.NewEntry, message.DeleteChunks)
  46. if foundExisting {
  47. glog.V(4).Infof("updated %v", key)
  48. return err
  49. }
  50. err = r.sink.DeleteEntry(key, message.OldEntry.IsDirectory, false)
  51. if err != nil {
  52. return fmt.Errorf("delete old entry %v: %v", key, err)
  53. }
  54. glog.V(4).Infof("creating missing %v", key)
  55. return r.sink.CreateEntry(key, message.NewEntry)
  56. }