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.

86 lines
2.7 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. "github.com/chrislusf/seaweedfs/weed/pb"
  6. "google.golang.org/grpc"
  7. "strings"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  11. "github.com/chrislusf/seaweedfs/weed/replication/source"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. type Replicator struct {
  15. sink sink.ReplicationSink
  16. source *source.FilerSource
  17. }
  18. func NewReplicator(sourceConfig util.Configuration, configPrefix string, dataSink sink.ReplicationSink) *Replicator {
  19. source := &source.FilerSource{}
  20. source.Initialize(sourceConfig, configPrefix)
  21. dataSink.SetSourceFiler(source)
  22. return &Replicator{
  23. sink: dataSink,
  24. source: source,
  25. }
  26. }
  27. func (r *Replicator) Replicate(ctx context.Context, key string, message *filer_pb.EventNotification) error {
  28. if message.IsFromOtherCluster && r.sink.GetName() == "filer" {
  29. return nil
  30. }
  31. if !strings.HasPrefix(key, r.source.Dir) {
  32. glog.V(4).Infof("skipping %v outside of %v", key, r.source.Dir)
  33. return nil
  34. }
  35. newKey := util.Join(r.sink.GetSinkToDirectory(), key[len(r.source.Dir):])
  36. glog.V(3).Infof("replicate %s => %s", key, newKey)
  37. key = newKey
  38. if message.OldEntry != nil && message.NewEntry == nil {
  39. glog.V(4).Infof("deleting %v", key)
  40. return r.sink.DeleteEntry(key, message.OldEntry.IsDirectory, message.DeleteChunks, message.Signatures)
  41. }
  42. if message.OldEntry == nil && message.NewEntry != nil {
  43. glog.V(4).Infof("creating %v", key)
  44. return r.sink.CreateEntry(key, message.NewEntry, message.Signatures)
  45. }
  46. if message.OldEntry == nil && message.NewEntry == nil {
  47. glog.V(0).Infof("weird message %+v", message)
  48. return nil
  49. }
  50. foundExisting, err := r.sink.UpdateEntry(key, message.OldEntry, message.NewParentPath, message.NewEntry, message.DeleteChunks, message.Signatures)
  51. if foundExisting {
  52. glog.V(4).Infof("updated %v", key)
  53. return err
  54. }
  55. err = r.sink.DeleteEntry(key, message.OldEntry.IsDirectory, false, message.Signatures)
  56. if err != nil {
  57. return fmt.Errorf("delete old entry %v: %v", key, err)
  58. }
  59. glog.V(4).Infof("creating missing %v", key)
  60. return r.sink.CreateEntry(key, message.NewEntry, message.Signatures)
  61. }
  62. func ReadFilerSignature(grpcDialOption grpc.DialOption, filer string) (filerSignature int32, readErr error) {
  63. if readErr = pb.WithFilerClient(filer, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  64. if resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{}); err != nil {
  65. return fmt.Errorf("GetFilerConfiguration %s: %v", filer, err)
  66. } else {
  67. filerSignature = resp.Signature
  68. }
  69. return nil
  70. }); readErr != nil {
  71. return 0, readErr
  72. }
  73. return filerSignature, nil
  74. }