replicator.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package replication
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "google.golang.org/grpc"
  7. "strings"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/replication/sink"
  12. "github.com/seaweedfs/seaweedfs/weed/replication/source"
  13. "github.com/seaweedfs/seaweedfs/weed/util"
  14. )
  15. type Replicator struct {
  16. sink sink.ReplicationSink
  17. source *source.FilerSource
  18. excludeDirs []string
  19. }
  20. func NewReplicator(sourceConfig util.Configuration, configPrefix string, dataSink sink.ReplicationSink) *Replicator {
  21. source := &source.FilerSource{}
  22. source.Initialize(sourceConfig, configPrefix)
  23. dataSink.SetSourceFiler(source)
  24. return &Replicator{
  25. sink: dataSink,
  26. source: source,
  27. excludeDirs: sourceConfig.GetStringSlice(configPrefix + "excludeDirectories"),
  28. }
  29. }
  30. func (r *Replicator) Replicate(ctx context.Context, key string, message *filer_pb.EventNotification) error {
  31. if message.IsFromOtherCluster && r.sink.GetName() == "filer" {
  32. return nil
  33. }
  34. if !strings.HasPrefix(key, r.source.Dir) {
  35. glog.V(4).Infof("skipping %v outside of %v", key, r.source.Dir)
  36. return nil
  37. }
  38. for _, excludeDir := range r.excludeDirs {
  39. if strings.HasPrefix(key, excludeDir) {
  40. glog.V(4).Infof("skipping %v of exclude dir %v", key, excludeDir)
  41. return nil
  42. }
  43. }
  44. var dateKey string
  45. if r.sink.IsIncremental() {
  46. var mTime int64
  47. if message.NewEntry != nil {
  48. mTime = message.NewEntry.Attributes.Mtime
  49. } else if message.OldEntry != nil {
  50. mTime = message.OldEntry.Attributes.Mtime
  51. }
  52. dateKey = time.Unix(mTime, 0).Format("2006-01-02")
  53. }
  54. newKey := util.Join(r.sink.GetSinkToDirectory(), dateKey, key[len(r.source.Dir):])
  55. glog.V(3).Infof("replicate %s => %s", key, newKey)
  56. key = newKey
  57. if message.OldEntry != nil && message.NewEntry == nil {
  58. glog.V(4).Infof("deleting %v", key)
  59. return r.sink.DeleteEntry(key, message.OldEntry.IsDirectory, message.DeleteChunks, message.Signatures)
  60. }
  61. if message.OldEntry == nil && message.NewEntry != nil {
  62. glog.V(4).Infof("creating %v", key)
  63. return r.sink.CreateEntry(key, message.NewEntry, message.Signatures)
  64. }
  65. if message.OldEntry == nil && message.NewEntry == nil {
  66. glog.V(0).Infof("weird message %+v", message)
  67. return nil
  68. }
  69. foundExisting, err := r.sink.UpdateEntry(key, message.OldEntry, message.NewParentPath, message.NewEntry, message.DeleteChunks, message.Signatures)
  70. if foundExisting {
  71. glog.V(4).Infof("updated %v", key)
  72. return err
  73. }
  74. err = r.sink.DeleteEntry(key, message.OldEntry.IsDirectory, false, message.Signatures)
  75. if err != nil {
  76. return fmt.Errorf("delete old entry %v: %v", key, err)
  77. }
  78. glog.V(4).Infof("creating missing %v", key)
  79. return r.sink.CreateEntry(key, message.NewEntry, message.Signatures)
  80. }
  81. func ReadFilerSignature(grpcDialOption grpc.DialOption, filer pb.ServerAddress) (filerSignature int32, readErr error) {
  82. if readErr = pb.WithFilerClient(false, 0, filer, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  83. if resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{}); err != nil {
  84. return fmt.Errorf("GetFilerConfiguration %s: %v", filer, err)
  85. } else {
  86. filerSignature = resp.Signature
  87. }
  88. return nil
  89. }); readErr != nil {
  90. return 0, readErr
  91. }
  92. return filerSignature, nil
  93. }