From 788acdf5275dfb7610afe9144c17d9128d1737f6 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 17 Sep 2018 00:27:56 -0700 Subject: [PATCH] add WIP filer.replicate --- weed/command/command.go | 1 + weed/command/filer_replication.go | 61 ++++++++ weed/command/scaffold.go | 32 +++- weed/filer2/filer.go | 6 +- weed/filer2/filer_notify.go | 8 +- weed/pb/filer.proto | 6 +- weed/pb/filer_pb/filer.pb.go | 188 +++++++++++++----------- weed/replication/notification_kafka.go | 77 ++++++++++ weed/replication/notifications.go | 18 +++ weed/replication/replicator.go | 31 ++++ weed/replication/sink/filer_sink.go | 163 ++++++++++++++++++++ weed/replication/source/filer_source.go | 97 ++++++++++++ weed/server/filer_grpc_server.go | 2 +- 13 files changed, 594 insertions(+), 96 deletions(-) create mode 100644 weed/command/filer_replication.go create mode 100644 weed/replication/notification_kafka.go create mode 100644 weed/replication/notifications.go create mode 100644 weed/replication/replicator.go create mode 100644 weed/replication/sink/filer_sink.go create mode 100644 weed/replication/source/filer_source.go diff --git a/weed/command/command.go b/weed/command/command.go index 61f13c5e2..91b9bf3fc 100644 --- a/weed/command/command.go +++ b/weed/command/command.go @@ -14,6 +14,7 @@ var Commands = []*Command{ cmdCopy, cmdFix, cmdFilerExport, + cmdFilerReplicate, cmdServer, cmdMaster, cmdFiler, diff --git a/weed/command/filer_replication.go b/weed/command/filer_replication.go new file mode 100644 index 000000000..18f549edc --- /dev/null +++ b/weed/command/filer_replication.go @@ -0,0 +1,61 @@ +package command + +import ( + "github.com/chrislusf/seaweedfs/weed/glog" + "github.com/chrislusf/seaweedfs/weed/server" + "github.com/spf13/viper" + "github.com/chrislusf/seaweedfs/weed/replication" +) + +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 + } + } + + replicator := replication.NewReplicator(config.Sub("sink.filer")) + + for { + key, m, err := notificationInput.ReceiveMessage() + if err != nil { + glog.Errorf("receive %s: +v", key, err) + continue + } + if err = replicator.Replicate(key, m); err != nil { + glog.Errorf("replicate %s: +v", key, err) + } + } + + return true +} diff --git a/weed/command/scaffold.go b/weed/command/scaffold.go index a5df2d18d..17d8b1884 100644 --- a/weed/command/scaffold.go +++ b/weed/command/scaffold.go @@ -19,7 +19,7 @@ var cmdScaffold = &Command{ var ( outputPath = cmdScaffold.Flag.String("output", "", "if not empty, save the configuration file to this directory") - config = cmdScaffold.Flag.String("config", "filer", "the configuration file to generate") + config = cmdScaffold.Flag.String("config", "filer", "[filer|replication] the configuration file to generate") ) func runScaffold(cmd *Command, args []string) bool { @@ -28,6 +28,8 @@ func runScaffold(cmd *Command, args []string) bool { switch *config { case "filer": content = FILER_TOML_EXAMPLE + case "replication": + content = REPLICATION_TOML_EXAMPLE } if content == "" { println("need a valid -config option") @@ -139,5 +141,33 @@ hosts = [ ] topic = "seaweedfs_filer" +` + REPLICATION_TOML_EXAMPLE = ` +# A sample TOML config file for replicating SeaweedFS filer store + + +[source.filer] +enabled = true +grpcAddress = "localhost:18888" +# id is to identify the notification source, avoid reprocessing the same events +id = "filer1" +# all files under this directory tree and not from this source.filer.id is replicated +directory = "/" + +[notification.kafka] +enabled = true +hosts = [ + "localhost:9092" +] +topic = "seaweedfs_filer" + +[sink.filer] +enabled = true +grpcAddress = "localhost:18888" +# id is to identify the notification source, avoid reprocessing the same events +id = "filer2" +# all files under this directory tree and not from this source.filer.id is replicated +directory = "/" + ` ) diff --git a/weed/filer2/filer.go b/weed/filer2/filer.go index 84cac5c27..a652e6480 100644 --- a/weed/filer2/filer.go +++ b/weed/filer2/filer.go @@ -91,7 +91,7 @@ func (f *Filer) CreateEntry(entry *Entry) error { return fmt.Errorf("mkdir %s: %v", dirPath, mkdirErr) } - f.NotifyUpdateEvent(nil, dirEntry) + f.NotifyUpdateEvent(nil, dirEntry, false) } else if !dirEntry.IsDirectory() { return fmt.Errorf("%s is a file", dirPath) @@ -125,7 +125,7 @@ func (f *Filer) CreateEntry(entry *Entry) error { return fmt.Errorf("insert entry %s: %v", entry.FullPath, err) } - f.NotifyUpdateEvent(oldEntry, entry) + f.NotifyUpdateEvent(oldEntry, entry, true) f.deleteChunksIfNotNew(oldEntry, entry) @@ -176,7 +176,7 @@ func (f *Filer) DeleteEntryMetaAndData(p FullPath, isRecursive bool, shouldDelet } glog.V(3).Infof("deleting entry %v", p) - f.NotifyUpdateEvent(entry, nil) + f.NotifyUpdateEvent(entry, nil, shouldDeleteChunks) return f.store.DeleteEntry(p) } diff --git a/weed/filer2/filer_notify.go b/weed/filer2/filer_notify.go index 7fc64a1be..b8fe1dbfb 100644 --- a/weed/filer2/filer_notify.go +++ b/weed/filer2/filer_notify.go @@ -6,7 +6,7 @@ import ( "github.com/chrislusf/seaweedfs/weed/glog" ) -func (f *Filer) NotifyUpdateEvent(oldEntry, newEntry *Entry) { +func (f *Filer) NotifyUpdateEvent(oldEntry, newEntry *Entry, deleteChunks bool) { var key string if oldEntry != nil { key = string(oldEntry.FullPath) @@ -23,13 +23,13 @@ func (f *Filer) NotifyUpdateEvent(oldEntry, newEntry *Entry) { notification.Queue.SendMessage( key, &filer_pb.EventNotification{ - OldEntry: toProtoEntry(oldEntry), - NewEntry: toProtoEntry(newEntry), + OldEntry: toProtoEntry(oldEntry), + NewEntry: toProtoEntry(newEntry), + DeleteChunks: deleteChunks, }, ) } - } func toProtoEntry(entry *Entry) *filer_pb.Entry { diff --git a/weed/pb/filer.proto b/weed/pb/filer.proto index e2b175c11..c4cebb8ba 100644 --- a/weed/pb/filer.proto +++ b/weed/pb/filer.proto @@ -67,8 +67,9 @@ message Entry { } message EventNotification { - Entry old_entry = 2; - Entry new_entry = 3; + Entry old_entry = 1; + Entry new_entry = 2; + bool delete_chunks = 3; } message FileChunk { @@ -77,6 +78,7 @@ message FileChunk { uint64 size = 3; int64 mtime = 4; string e_tag = 5; + string source_file_id = 6; } message FuseAttributes { diff --git a/weed/pb/filer_pb/filer.pb.go b/weed/pb/filer_pb/filer.pb.go index 7ee8a7dd2..66b92bbcc 100644 --- a/weed/pb/filer_pb/filer.pb.go +++ b/weed/pb/filer_pb/filer.pb.go @@ -211,8 +211,9 @@ func (m *Entry) GetExtended() map[string][]byte { } type EventNotification struct { - OldEntry *Entry `protobuf:"bytes,2,opt,name=old_entry,json=oldEntry" json:"old_entry,omitempty"` - NewEntry *Entry `protobuf:"bytes,3,opt,name=new_entry,json=newEntry" json:"new_entry,omitempty"` + OldEntry *Entry `protobuf:"bytes,1,opt,name=old_entry,json=oldEntry" json:"old_entry,omitempty"` + NewEntry *Entry `protobuf:"bytes,2,opt,name=new_entry,json=newEntry" json:"new_entry,omitempty"` + DeleteChunks bool `protobuf:"varint,3,opt,name=delete_chunks,json=deleteChunks" json:"delete_chunks,omitempty"` } func (m *EventNotification) Reset() { *m = EventNotification{} } @@ -234,12 +235,20 @@ func (m *EventNotification) GetNewEntry() *Entry { return nil } +func (m *EventNotification) GetDeleteChunks() bool { + if m != nil { + return m.DeleteChunks + } + return false +} + type FileChunk struct { - FileId string `protobuf:"bytes,1,opt,name=file_id,json=fileId" json:"file_id,omitempty"` - Offset int64 `protobuf:"varint,2,opt,name=offset" json:"offset,omitempty"` - Size uint64 `protobuf:"varint,3,opt,name=size" json:"size,omitempty"` - Mtime int64 `protobuf:"varint,4,opt,name=mtime" json:"mtime,omitempty"` - ETag string `protobuf:"bytes,5,opt,name=e_tag,json=eTag" json:"e_tag,omitempty"` + FileId string `protobuf:"bytes,1,opt,name=file_id,json=fileId" json:"file_id,omitempty"` + Offset int64 `protobuf:"varint,2,opt,name=offset" json:"offset,omitempty"` + Size uint64 `protobuf:"varint,3,opt,name=size" json:"size,omitempty"` + Mtime int64 `protobuf:"varint,4,opt,name=mtime" json:"mtime,omitempty"` + ETag string `protobuf:"bytes,5,opt,name=e_tag,json=eTag" json:"e_tag,omitempty"` + SourceFileId string `protobuf:"bytes,6,opt,name=source_file_id,json=sourceFileId" json:"source_file_id,omitempty"` } func (m *FileChunk) Reset() { *m = FileChunk{} } @@ -282,6 +291,13 @@ func (m *FileChunk) GetETag() string { return "" } +func (m *FileChunk) GetSourceFileId() string { + if m != nil { + return m.SourceFileId + } + return "" +} + type FuseAttributes struct { FileSize uint64 `protobuf:"varint,1,opt,name=file_size,json=fileSize" json:"file_size,omitempty"` Mtime int64 `protobuf:"varint,2,opt,name=mtime" json:"mtime,omitempty"` @@ -1138,82 +1154,84 @@ var _SeaweedFiler_serviceDesc = grpc.ServiceDesc{ func init() { proto.RegisterFile("filer.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 1218 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xb4, 0x57, 0xcd, 0x6e, 0xdb, 0xc6, - 0x13, 0x37, 0xa9, 0x0f, 0x8b, 0x23, 0x39, 0xff, 0x78, 0xe5, 0x24, 0xfc, 0x2b, 0x76, 0xaa, 0xb0, - 0x49, 0xe1, 0xa0, 0x86, 0x61, 0xb8, 0x3d, 0x24, 0x0d, 0x0a, 0x34, 0xb0, 0x9d, 0x20, 0x80, 0xe3, - 0x00, 0x74, 0x5c, 0xa0, 0xe8, 0x41, 0xa0, 0xc9, 0xb1, 0xba, 0x30, 0x45, 0xaa, 0xe4, 0xd2, 0x8e, -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xc8, 0x71, 0xf0, 0x60, 0x91, 0x5a, 0x77, 0xa8, - 0x8f, 0x92, 0xee, 0xb0, 0x86, 0x4c, 0x74, 0x87, 0x75, 0x13, 0xe8, 0x2c, 0x91, 0x1f, 0xe1, 0xf6, - 0x7c, 0x4b, 0x93, 0x87, 0xf3, 0x69, 0x55, 0x26, 0x65, 0xe0, 0x5c, 0x67, 0x92, 0x3b, 0x3f, 0x6d, - 0x8b, 0xdf, 0xcb, 0xaf, 0xfe, 0x0b, 0x00, 0x00, 0xff, 0xff, 0xbf, 0xe3, 0x2a, 0x44, 0x6d, 0x0e, - 0x00, 0x00, + // 1251 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xb4, 0x57, 0x5f, 0x6f, 0xd4, 0x46, + 0x10, 0xc7, 0xbe, 0x3f, 0x39, 0xcf, 0xdd, 0x51, 0xd8, 0x0b, 0xe0, 0x1e, 0x84, 0x1e, 0x06, 0xaa, + 0xa0, 0xa2, 0x08, 0xa5, 0x7d, 0x80, 0xa2, 0x4a, 0x45, 0x49, 0x40, 0x48, 0x01, 0x24, 0x07, 0x2a, + 0x55, 0x7d, 0xb0, 0x1c, 0x7b, 0x72, 0x5d, 0xc5, 0x67, 0x5f, 0xed, 0x75, 0x42, 0xfa, 0x15, 0xfa, + 0xd2, 0x4a, 0x7d, 0xed, 0x43, 0x9f, 0xfa, 0xde, 0x0f, 0xd0, 0x97, 0x7e, 0xb1, 0x6a, 0xff, 0xd8, + 0xb7, 0x3e, 0xfb, 0x02, 0xa8, 0xe2, 0xcd, 0x3b, 0x33, 0x3b, 0xf3, 0x9b, 0xd9, 0x99, 0xdf, 0xae, + 0xa1, 0x7f, 0x44, 0x23, 0x4c, 0xb7, 0xe6, 0x69, 0xc2, 0x12, 0xd2, 0x13, 0x0b, 0x6f, 0x7e, 0xe8, + 0xbc, 0x82, 0xeb, 0xfb, 0x49, 0x72, 0x9c, 0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 0x20, 0x3f, 0xc0, + 0xa5, 0xe5, 0x96, 0x26, 0xb7, 0x96, 0xd3, 0xaa, 0x4d, 0xca, 0xd8, 0x39, 0xcf, 0xa4, 0x70, 0x7e, + 0xd8, 0x15, 0xff, 0xaa, 0x5f, 0xfe, 0x17, 0x00, 0x00, 0xff, 0xff, 0x08, 0x7d, 0x7a, 0x7c, 0xba, + 0x0e, 0x00, 0x00, } diff --git a/weed/replication/notification_kafka.go b/weed/replication/notification_kafka.go new file mode 100644 index 000000000..ce3b86ae9 --- /dev/null +++ b/weed/replication/notification_kafka.go @@ -0,0 +1,77 @@ +package replication + +import ( + "github.com/Shopify/sarama" + "github.com/chrislusf/seaweedfs/weed/util" + "github.com/golang/protobuf/proto" + "github.com/chrislusf/seaweedfs/weed/glog" + "fmt" + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" +) + +func init() { + NotificationInputs = append(NotificationInputs, &KafkaInput{ + }) +} + +type KafkaInput struct { + topic string + consumer sarama.Consumer + messageChan chan *sarama.ConsumerMessage +} + +func (k *KafkaInput) GetName() string { + return "kafka" +} + +func (k *KafkaInput) Initialize(configuration util.Configuration) error { + glog.V(0).Infof("replication.notification.kafka.hosts: %v\n", configuration.GetStringSlice("hosts")) + glog.V(0).Infof("replication.notification.kafka.topic: %v\n", configuration.GetString("topic")) + return k.initialize( + configuration.GetStringSlice("hosts"), + configuration.GetString("topic"), + ) +} + +func (k *KafkaInput) initialize(hosts []string, topic string) (err error) { + config := sarama.NewConfig() + config.Consumer.Return.Errors = true + k.consumer, err = sarama.NewConsumer(hosts, config) + k.topic = topic + k.messageChan = make(chan *sarama.ConsumerMessage, 1) + + partitions, err := k.consumer.Partitions(topic) + if err != nil { + panic(err) + } + + for _, partition := range partitions { + partitionConsumer, err := k.consumer.ConsumePartition(topic, partition, sarama.OffsetNewest) + if err != nil { + panic(err) + } + go func() { + for { + select { + case err := <-partitionConsumer.Errors(): + fmt.Println(err) + case msg := <-partitionConsumer.Messages(): + k.messageChan <- msg + } + } + }() + } + + return nil +} + +func (k *KafkaInput) ReceiveMessage() (key string, message *filer_pb.EventNotification, err error) { + + msg := <-k.messageChan + + key = string(msg.Key) + message = &filer_pb.EventNotification{} + err = proto.Unmarshal(msg.Value, message) + + return +} diff --git a/weed/replication/notifications.go b/weed/replication/notifications.go new file mode 100644 index 000000000..ff40c3aad --- /dev/null +++ b/weed/replication/notifications.go @@ -0,0 +1,18 @@ +package replication + +import ( + "github.com/chrislusf/seaweedfs/weed/util" + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" +) + +type NotificationInput interface { + // GetName gets the name to locate the configuration in sync.toml file + GetName() string + // Initialize initializes the file store + Initialize(configuration util.Configuration) error + ReceiveMessage() (key string, message *filer_pb.EventNotification, err error) +} + +var ( + NotificationInputs []NotificationInput +) diff --git a/weed/replication/replicator.go b/weed/replication/replicator.go new file mode 100644 index 000000000..c223021c2 --- /dev/null +++ b/weed/replication/replicator.go @@ -0,0 +1,31 @@ +package replication + +import ( + "github.com/chrislusf/seaweedfs/weed/replication/sink" + "github.com/chrislusf/seaweedfs/weed/util" + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" +) + +type Replicator struct { + sink *sink.FilerSink +} + +func NewReplicator(config util.Configuration) *Replicator { + + sink := &sink.FilerSink{} + sink.Initialize(config) + + return &Replicator{ + sink: sink, + } +} + +func (r *Replicator) Replicate(key string, message *filer_pb.EventNotification) error { + if message.OldEntry != nil && message.NewEntry == nil { + return r.sink.DeleteEntry(message.OldEntry, message.DeleteChunks) + } + if message.OldEntry == nil && message.NewEntry != nil { + return r.sink.CreateEntry(message.NewEntry) + } + return r.sink.UpdateEntry(message.OldEntry, message.NewEntry, message.DeleteChunks) +} diff --git a/weed/replication/sink/filer_sink.go b/weed/replication/sink/filer_sink.go new file mode 100644 index 000000000..57ebeddff --- /dev/null +++ b/weed/replication/sink/filer_sink.go @@ -0,0 +1,163 @@ +package sink + +import ( + "github.com/chrislusf/seaweedfs/weed/util" + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" + "fmt" + "strings" + "github.com/chrislusf/seaweedfs/weed/filer2" + "github.com/chrislusf/seaweedfs/weed/glog" + "context" + "sync" +) + +type ReplicationSink interface { + DeleteEntry(entry *filer_pb.Entry, deleteIncludeChunks bool) error + CreateEntry(entry *filer_pb.Entry) error + UpdateEntry(oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) error +} + +type FilerSink struct { + grpcAddress string + id string + dir string +} + +func (fs *FilerSink) Initialize(configuration util.Configuration) error { + return fs.initialize( + configuration.GetString("grpcAddress"), + configuration.GetString("id"), + configuration.GetString("directory"), + ) +} + +func (fs *FilerSink) initialize(grpcAddress string, id string, dir string) (err error) { + fs.grpcAddress = grpcAddress + fs.id = id + fs.dir = dir + return nil +} + +func (fs *FilerSink) DeleteEntry(entry *filer_pb.Entry, deleteIncludeChunks bool) error { + return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error { + + dir, name := filer2.FullPath(entry.Name).DirAndName() + + request := &filer_pb.DeleteEntryRequest{ + Directory: dir, + Name: name, + IsDirectory: entry.IsDirectory, + IsDeleteData: deleteIncludeChunks, + } + + glog.V(1).Infof("delete entry: %v", request) + _, err := client.DeleteEntry(context.Background(), request) + if err != nil { + glog.V(0).Infof("delete entry %s: %v", entry.Name, err) + return fmt.Errorf("delete entry %s: %v", entry.Name, err) + } + + return nil + }) +} + +func (fs *FilerSink) CreateEntry(entry *filer_pb.Entry) error { + + replicatedChunks, err := replicateChunks(entry.Chunks) + + if err != nil { + glog.V(0).Infof("replicate entry chunks %s: %v", entry.Name, err) + return fmt.Errorf("replicate entry chunks %s: %v", entry.Name, err) + } + + return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error { + + dir, name := filer2.FullPath(entry.Name).DirAndName() + + request := &filer_pb.CreateEntryRequest{ + Directory: dir, + Entry: &filer_pb.Entry{ + Name: name, + IsDirectory: entry.IsDirectory, + Attributes: entry.Attributes, + Chunks: replicatedChunks, + }, + } + + glog.V(1).Infof("create: %v", request) + if _, err := client.CreateEntry(context.Background(), request); err != nil { + glog.V(0).Infof("create entry %s: %v", entry.Name, err) + return fmt.Errorf("create entry %s: %v", entry.Name, err) + } + + return nil + }) +} + +func (fs *FilerSink) UpdateEntry(oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) error { + return nil +} + +func (fs *FilerSink) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error { + + grpcConnection, err := util.GrpcDial(fs.grpcAddress) + if err != nil { + return fmt.Errorf("fail to dial %s: %v", fs.grpcAddress, err) + } + defer grpcConnection.Close() + + client := filer_pb.NewSeaweedFilerClient(grpcConnection) + + return fn(client) +} + +func volumeId(fileId string) string { + lastCommaIndex := strings.LastIndex(fileId, ",") + if lastCommaIndex > 0 { + return fileId[:lastCommaIndex] + } + return fileId +} + +func replicateChunks(sourceChunks []*filer_pb.FileChunk) (replicatedChunks []*filer_pb.FileChunk, err error) { + if len(sourceChunks) == 0 { + return + } + var wg sync.WaitGroup + for _, s := range sourceChunks { + wg.Add(1) + go func(chunk *filer_pb.FileChunk) { + defer wg.Done() + replicatedChunk, e := replicateOneChunk(chunk) + if e != nil { + err = e + } + replicatedChunks = append(replicatedChunks, replicatedChunk) + }(s) + } + wg.Wait() + + return +} + +func replicateOneChunk(sourceChunk *filer_pb.FileChunk) (*filer_pb.FileChunk, error) { + + fileId, err := fetchAndWrite(sourceChunk) + if err != nil { + return nil, fmt.Errorf("copy %s: %v", sourceChunk.FileId, err) + } + + return &filer_pb.FileChunk{ + FileId: fileId, + Offset: sourceChunk.Offset, + Size: sourceChunk.Size, + Mtime: sourceChunk.Mtime, + ETag: sourceChunk.ETag, + SourceFileId: sourceChunk.FileId, + }, nil +} + +func fetchAndWrite(sourceChunk *filer_pb.FileChunk) (fileId string, err error) { + + return +} diff --git a/weed/replication/source/filer_source.go b/weed/replication/source/filer_source.go new file mode 100644 index 000000000..49c623815 --- /dev/null +++ b/weed/replication/source/filer_source.go @@ -0,0 +1,97 @@ +package source + +import ( + "io" + "github.com/chrislusf/seaweedfs/weed/util" + "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" + "fmt" + "github.com/chrislusf/seaweedfs/weed/glog" + "strings" + "context" +) + +type ReplicationSource interface { + ReadPart(part string) io.ReadCloser +} + +type FilerSource struct { + grpcAddress string + id string + dir string +} + +func (fs *FilerSource) Initialize(configuration util.Configuration) error { + return fs.initialize( + configuration.GetString("grpcAddress"), + configuration.GetString("id"), + configuration.GetString("directory"), + ) +} + +func (fs *FilerSource) initialize(grpcAddress string, id string, dir string) (err error) { + fs.grpcAddress = grpcAddress + fs.id = id + fs.dir = dir + return nil +} + +func (fs *FilerSource) ReadPart(part string) (readCloser io.ReadCloser, err error) { + + vid2Locations := make(map[string]*filer_pb.Locations) + + vid := volumeId(part) + + err = fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error { + + glog.V(4).Infof("read lookup volume id locations: %v", vid) + resp, err := client.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{ + VolumeIds: []string{vid}, + }) + if err != nil { + return err + } + + vid2Locations = resp.LocationsMap + + return nil + }) + + if err != nil { + glog.V(1).Infof("replication lookup volume id: %v", vid, err) + return nil, fmt.Errorf("replicationlookup volume id %v: %v", vid, err) + } + + locations := vid2Locations[vid] + + if locations == nil || len(locations.Locations) == 0 { + glog.V(1).Infof("replication locate volume id: %v", vid, err) + return nil, fmt.Errorf("replication locate volume id %v: %v", vid, err) + } + + fileUrl := fmt.Sprintf("http://%s/%s", locations.Locations[0].Url, part) + + _, readCloser, err = util.DownloadUrl(fileUrl) + + return readCloser, err +} + +func (fs *FilerSource) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error { + + grpcConnection, err := util.GrpcDial(fs.grpcAddress) + if err != nil { + return fmt.Errorf("fail to dial %s: %v", fs.grpcAddress, err) + } + defer grpcConnection.Close() + + client := filer_pb.NewSeaweedFilerClient(grpcConnection) + + return fn(client) +} + +func volumeId(fileId string) string { + lastCommaIndex := strings.LastIndex(fileId, ",") + if lastCommaIndex > 0 { + return fileId[:lastCommaIndex] + } + return fileId +} diff --git a/weed/server/filer_grpc_server.go b/weed/server/filer_grpc_server.go index 98e8699b0..6c3390a26 100644 --- a/weed/server/filer_grpc_server.go +++ b/weed/server/filer_grpc_server.go @@ -188,7 +188,7 @@ func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntr } } - fs.filer.NotifyUpdateEvent(entry, newEntry) + fs.filer.NotifyUpdateEvent(entry, newEntry, true) return &filer_pb.UpdateEntryResponse{}, err }