2018-09-17 15:27:56 +08:00
|
|
|
package replication
|
|
|
|
|
|
|
|
import (
|
2019-03-16 08:20:24 +08:00
|
|
|
"context"
|
2019-04-16 15:44:31 +08:00
|
|
|
"fmt"
|
2018-09-23 16:46:50 +08:00
|
|
|
"path/filepath"
|
2018-09-17 16:37:24 +08:00
|
|
|
"strings"
|
2018-09-21 16:56:43 +08:00
|
|
|
|
2018-10-11 15:08:13 +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"
|
2018-10-11 15:08:13 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication/source"
|
2018-09-21 16:56:43 +08:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2020-01-30 01:09:55 +08:00
|
|
|
func NewReplicator(sourceConfig util.Configuration, configPrefix string, dataSink sink.ReplicationSink) *Replicator {
|
2018-09-17 16:37:24 +08:00
|
|
|
|
|
|
|
source := &source.FilerSource{}
|
2020-01-30 01:09:55 +08:00
|
|
|
source.Initialize(sourceConfig, configPrefix)
|
2018-09-17 16:37:24 +08:00
|
|
|
|
2018-10-04 14:36:52 +08:00
|
|
|
dataSink.SetSourceFiler(source)
|
2018-09-21 16:54:29 +08:00
|
|
|
|
2018-09-17 15:27:56 +08:00
|
|
|
return &Replicator{
|
2018-10-04 14:36:52 +08:00
|
|
|
sink: dataSink,
|
2018-09-17 16:37:24 +08:00
|
|
|
source: source,
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 08:20:24 +08:00
|
|
|
func (r *Replicator) Replicate(ctx context.Context, key string, message *filer_pb.EventNotification) error {
|
2018-09-17 16:37:24 +08:00
|
|
|
if !strings.HasPrefix(key, r.source.Dir) {
|
2018-10-07 04:01:29 +08:00
|
|
|
glog.V(4).Infof("skipping %v outside of %v", key, r.source.Dir)
|
2018-09-17 16:37:24 +08:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-05 05:00:08 +08:00
|
|
|
newKey := filepath.ToSlash(filepath.Join(r.sink.GetSinkToDirectory(), key[len(r.source.Dir):]))
|
2018-11-05 03:58:59 +08:00
|
|
|
glog.V(3).Infof("replicate %s => %s", key, newKey)
|
|
|
|
key = newKey
|
2018-09-17 15:27:56 +08:00
|
|
|
if message.OldEntry != nil && message.NewEntry == nil {
|
2018-10-07 04:00:33 +08:00
|
|
|
glog.V(4).Infof("deleting %v", key)
|
2019-03-16 08:20:24 +08:00
|
|
|
return r.sink.DeleteEntry(ctx, key, message.OldEntry.IsDirectory, message.DeleteChunks)
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
if message.OldEntry == nil && message.NewEntry != nil {
|
2018-10-07 04:00:33 +08:00
|
|
|
glog.V(4).Infof("creating %v", key)
|
2019-03-16 08:20:24 +08:00
|
|
|
return r.sink.CreateEntry(ctx, key, message.NewEntry)
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
2018-09-26 00:27:03 +08:00
|
|
|
if message.OldEntry == nil && message.NewEntry == nil {
|
2018-10-04 14:36:52 +08:00
|
|
|
glog.V(0).Infof("weird message %+v", message)
|
2018-09-26 00:27:03 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:44:31 +08:00
|
|
|
foundExisting, err := r.sink.UpdateEntry(ctx, key, message.OldEntry, message.NewParentPath, message.NewEntry, message.DeleteChunks)
|
2018-10-04 14:36:52 +08:00
|
|
|
if foundExisting {
|
2018-10-07 04:00:33 +08:00
|
|
|
glog.V(4).Infof("updated %v", key)
|
2018-10-04 14:36:52 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:44:31 +08:00
|
|
|
err = r.sink.DeleteEntry(ctx, key, message.OldEntry.IsDirectory, false)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete old entry %v: %v", key, err)
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:00:33 +08:00
|
|
|
glog.V(4).Infof("creating missing %v", key)
|
2019-03-16 08:20:24 +08:00
|
|
|
return r.sink.CreateEntry(ctx, key, message.NewEntry)
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|