seaweedfs/weed/replication/replicator.go

62 lines
1.7 KiB
Go
Raw Normal View History

2018-09-17 07:27:56 +00:00
package replication
import (
2018-09-23 08:46:50 +00:00
"path/filepath"
2018-09-17 08:37:24 +00:00
"strings"
2018-09-21 08:56:43 +00:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/replication/sink"
2018-09-23 07:40:36 +00:00
"github.com/chrislusf/seaweedfs/weed/replication/sink/filersink"
2018-09-21 08:56:43 +00:00
"github.com/chrislusf/seaweedfs/weed/replication/source"
"github.com/chrislusf/seaweedfs/weed/util"
"github.com/chrislusf/seaweedfs/weed/glog"
2018-09-17 07:27:56 +00:00
)
type Replicator struct {
2018-09-17 08:37:24 +00:00
sink sink.ReplicationSink
source *source.FilerSource
2018-09-17 07:27:56 +00:00
}
2018-09-17 08:37:24 +00:00
func NewReplicator(sourceConfig, sinkConfig util.Configuration) *Replicator {
2018-09-17 07:27:56 +00:00
2018-09-23 07:40:36 +00:00
sink := &filersink.FilerSink{}
2018-09-17 08:37:24 +00:00
sink.Initialize(sinkConfig)
source := &source.FilerSource{}
source.Initialize(sourceConfig)
2018-09-21 08:54:29 +00:00
sink.SetSourceFiler(source)
2018-09-17 07:27:56 +00:00
return &Replicator{
2018-09-17 08:37:24 +00:00
sink: sink,
source: source,
2018-09-17 07:27:56 +00:00
}
}
func (r *Replicator) Replicate(key string, message *filer_pb.EventNotification) error {
2018-09-17 08:37:24 +00:00
if !strings.HasPrefix(key, r.source.Dir) {
return nil
}
2018-09-23 08:46:50 +00:00
key = filepath.Join(r.sink.GetSinkToDirectory(), key[len(r.source.Dir):])
2018-09-17 07:27:56 +00:00
if message.OldEntry != nil && message.NewEntry == nil {
2018-09-17 08:37:24 +00:00
return r.sink.DeleteEntry(key, message.OldEntry, message.DeleteChunks)
2018-09-17 07:27:56 +00:00
}
if message.OldEntry == nil && message.NewEntry != nil {
2018-09-17 08:37:24 +00:00
return r.sink.CreateEntry(key, message.NewEntry)
2018-09-17 07:27:56 +00:00
}
if existingEntry, err := r.sink.LookupEntry(key); err == nil {
if message.OldEntry == nil && message.NewEntry == nil {
glog.V(0).Infof("message %+v existingEntry: %+v", message, existingEntry)
return r.sink.DeleteEntry(key, existingEntry, true)
}
return r.sink.UpdateEntry(key, message.OldEntry, message.NewEntry, existingEntry, message.DeleteChunks)
}
glog.V(0).Infof("key:%s, message %+v", key, message)
if message.OldEntry == nil && message.NewEntry == nil {
return nil
}
return r.sink.CreateEntry(key, message.NewEntry)
2018-09-17 07:27:56 +00:00
}