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.

62 lines
1.8 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. "context"
  4. "path/filepath"
  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, dataSink sink.ReplicationSink) *Replicator {
  17. source := &source.FilerSource{}
  18. source.Initialize(sourceConfig)
  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 := filepath.ToSlash(filepath.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(ctx, 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(ctx, 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(ctx, key, message.OldEntry, message.NewEntry, message.DeleteChunks)
  46. if foundExisting {
  47. glog.V(4).Infof("updated %v", key)
  48. return err
  49. }
  50. glog.V(4).Infof("creating missing %v", key)
  51. return r.sink.CreateEntry(ctx, key, message.NewEntry)
  52. }