2018-09-17 15:27:56 +08:00
|
|
|
package replication
|
|
|
|
|
|
|
|
import (
|
2018-09-17 16:37:24 +08:00
|
|
|
"strings"
|
2018-09-21 16:56:43 +08:00
|
|
|
|
2018-09-17 16:37:24 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-09-21 16:56:43 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication/sink"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication/source"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-09-17 15:27:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Replicator struct {
|
2018-09-17 16:37:24 +08:00
|
|
|
sink sink.ReplicationSink
|
|
|
|
source *source.FilerSource
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
|
2018-09-17 16:37:24 +08:00
|
|
|
func NewReplicator(sourceConfig, sinkConfig util.Configuration) *Replicator {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
|
|
|
sink := &sink.FilerSink{}
|
2018-09-17 16:37:24 +08:00
|
|
|
sink.Initialize(sinkConfig)
|
|
|
|
|
|
|
|
source := &source.FilerSource{}
|
|
|
|
source.Initialize(sourceConfig)
|
|
|
|
|
|
|
|
if sourceConfig.GetString("grpcAddress") == sinkConfig.GetString("grpcAddress") {
|
|
|
|
fromDir := sourceConfig.GetString("directory")
|
|
|
|
toDir := sinkConfig.GetString("directory")
|
|
|
|
if strings.HasPrefix(toDir, fromDir) {
|
|
|
|
glog.Fatalf("recursive replication! source directory %s includes the sink directory %s", fromDir, toDir)
|
|
|
|
}
|
|
|
|
}
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-21 16:54:29 +08:00
|
|
|
sink.SetSourceFiler(source)
|
|
|
|
|
2018-09-17 15:27:56 +08:00
|
|
|
return &Replicator{
|
2018-09-17 16:37:24 +08:00
|
|
|
sink: sink,
|
|
|
|
source: source,
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Replicator) Replicate(key string, message *filer_pb.EventNotification) error {
|
2018-09-17 16:37:24 +08:00
|
|
|
if !strings.HasPrefix(key, r.source.Dir) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
key = r.sink.GetDirectory() + key[len(r.source.Dir):]
|
2018-09-17 15:27:56 +08:00
|
|
|
if message.OldEntry != nil && message.NewEntry == nil {
|
2018-09-17 16:37:24 +08:00
|
|
|
return r.sink.DeleteEntry(key, message.OldEntry, message.DeleteChunks)
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
if message.OldEntry == nil && message.NewEntry != nil {
|
2018-09-17 16:37:24 +08:00
|
|
|
return r.sink.CreateEntry(key, message.NewEntry)
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
2018-09-17 16:37:24 +08:00
|
|
|
return r.sink.UpdateEntry(key, message.OldEntry, message.NewEntry, message.DeleteChunks)
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|