2018-09-17 15:27:56 +08:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2018-10-07 04:01:38 +08:00
|
|
|
"strings"
|
|
|
|
|
2018-09-17 15:27:56 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-09-21 16:56:43 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication"
|
2018-10-04 14:36:52 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication/sink"
|
2018-10-07 04:01:38 +08:00
|
|
|
_ "github.com/chrislusf/seaweedfs/weed/replication/sink/filersink"
|
|
|
|
_ "github.com/chrislusf/seaweedfs/weed/replication/sink/gcssink"
|
2018-10-11 15:08:13 +08:00
|
|
|
_ "github.com/chrislusf/seaweedfs/weed/replication/sink/s3sink"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/server"
|
|
|
|
"github.com/spf13/viper"
|
2018-09-17 15:27:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdFilerReplicate.Run = runFilerReplicate // break init cycle
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmdFilerReplicate = &Command{
|
|
|
|
UsageLine: "filer.replicate",
|
|
|
|
Short: "replicate file changes to another destination",
|
|
|
|
Long: `replicate file changes to another destination
|
|
|
|
|
|
|
|
filer.replicate listens on filer notifications. If any file is updated, it will fetch the updated content,
|
|
|
|
and write to the other destination.
|
|
|
|
|
|
|
|
Run "weed scaffold -config replication" to generate a replication.toml file and customize the parameters.
|
|
|
|
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runFilerReplicate(cmd *Command, args []string) bool {
|
|
|
|
|
|
|
|
weed_server.LoadConfiguration("replication", true)
|
|
|
|
config := viper.GetViper()
|
|
|
|
|
|
|
|
var notificationInput replication.NotificationInput
|
|
|
|
|
|
|
|
for _, input := range replication.NotificationInputs {
|
|
|
|
if config.GetBool("notification." + input.GetName() + ".enabled") {
|
|
|
|
viperSub := config.Sub("notification." + input.GetName())
|
|
|
|
if err := input.Initialize(viperSub); err != nil {
|
|
|
|
glog.Fatalf("Failed to initialize notification input for %s: %+v",
|
|
|
|
input.GetName(), err)
|
|
|
|
}
|
|
|
|
glog.V(0).Infof("Configure notification input to %s", input.GetName())
|
|
|
|
notificationInput = input
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 15:40:36 +08:00
|
|
|
// avoid recursive replication
|
|
|
|
if config.GetBool("notification.source.filer.enabled") && config.GetBool("notification.sink.filer.enabled") {
|
|
|
|
sourceConfig, sinkConfig := config.Sub("source.filer"), config.Sub("sink.filer")
|
|
|
|
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-10-04 14:36:52 +08:00
|
|
|
var dataSink sink.ReplicationSink
|
|
|
|
for _, sk := range sink.Sinks {
|
|
|
|
if config.GetBool("sink." + sk.GetName() + ".enabled") {
|
|
|
|
viperSub := config.Sub("sink." + sk.GetName())
|
|
|
|
if err := sk.Initialize(viperSub); err != nil {
|
|
|
|
glog.Fatalf("Failed to initialize sink for %s: %+v",
|
|
|
|
sk.GetName(), err)
|
|
|
|
}
|
|
|
|
glog.V(0).Infof("Configure sink to %s", sk.GetName())
|
|
|
|
dataSink = sk
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:01:38 +08:00
|
|
|
if dataSink == nil {
|
2018-10-07 08:10:15 +08:00
|
|
|
println("no data sink configured:")
|
2018-10-07 04:01:38 +08:00
|
|
|
for _, sk := range sink.Sinks {
|
|
|
|
println(" " + sk.GetName())
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-10-04 14:36:52 +08:00
|
|
|
replicator := replication.NewReplicator(config.Sub("source.filer"), dataSink)
|
2018-09-17 15:27:56 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
key, m, err := notificationInput.ReceiveMessage()
|
|
|
|
if err != nil {
|
2018-09-17 17:23:21 +08:00
|
|
|
glog.Errorf("receive %s: %+v", key, err)
|
2018-09-17 15:27:56 +08:00
|
|
|
continue
|
|
|
|
}
|
2018-09-22 15:12:10 +08:00
|
|
|
if m.OldEntry != nil && m.NewEntry == nil {
|
2018-09-22 15:11:46 +08:00
|
|
|
glog.V(1).Infof("delete: %s", key)
|
2018-09-22 15:12:10 +08:00
|
|
|
} else if m.OldEntry == nil && m.NewEntry != nil {
|
2018-09-22 15:11:46 +08:00
|
|
|
glog.V(1).Infof(" add: %s", key)
|
2018-09-22 15:12:10 +08:00
|
|
|
} else {
|
2018-09-22 15:11:46 +08:00
|
|
|
glog.V(1).Infof("modify: %s", key)
|
|
|
|
}
|
2018-09-17 15:27:56 +08:00
|
|
|
if err = replicator.Replicate(key, m); err != nil {
|
2018-09-17 17:23:21 +08:00
|
|
|
glog.Errorf("replicate %s: %+v", key, err)
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|