2018-09-23 15:40:36 +08:00
|
|
|
package filersink
|
2018-09-17 15:27:56 +08:00
|
|
|
|
|
|
|
import (
|
2018-09-21 16:54:29 +08:00
|
|
|
"context"
|
2018-09-21 16:56:43 +08:00
|
|
|
"fmt"
|
2019-02-19 04:11:52 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/security"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"google.golang.org/grpc"
|
2018-09-21 16:54:29 +08:00
|
|
|
|
2018-09-17 15:27:56 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-09-21 16:56:43 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-10-11 15:08:13 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication/sink"
|
2018-09-21 16:54:29 +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 FilerSink struct {
|
2019-02-19 04:11:52 +08:00
|
|
|
filerSource *source.FilerSource
|
|
|
|
grpcAddress string
|
|
|
|
dir string
|
|
|
|
replication string
|
|
|
|
collection string
|
|
|
|
ttlSec int32
|
|
|
|
dataCenter string
|
|
|
|
grpcDialOption grpc.DialOption
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
|
2018-10-11 15:08:13 +08:00
|
|
|
func init() {
|
2018-10-04 14:36:52 +08:00
|
|
|
sink.Sinks = append(sink.Sinks, &FilerSink{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FilerSink) GetName() string {
|
|
|
|
return "filer"
|
|
|
|
}
|
|
|
|
|
2018-09-22 15:53:52 +08:00
|
|
|
func (fs *FilerSink) GetSinkToDirectory() string {
|
2018-09-17 16:37:24 +08:00
|
|
|
return fs.dir
|
|
|
|
}
|
|
|
|
|
2018-09-17 15:27:56 +08:00
|
|
|
func (fs *FilerSink) Initialize(configuration util.Configuration) error {
|
|
|
|
return fs.initialize(
|
|
|
|
configuration.GetString("grpcAddress"),
|
|
|
|
configuration.GetString("directory"),
|
2018-09-22 15:53:52 +08:00
|
|
|
configuration.GetString("replication"),
|
|
|
|
configuration.GetString("collection"),
|
|
|
|
configuration.GetInt("ttlSec"),
|
2018-09-17 15:27:56 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:54:29 +08:00
|
|
|
func (fs *FilerSink) SetSourceFiler(s *source.FilerSource) {
|
|
|
|
fs.filerSource = s
|
|
|
|
}
|
|
|
|
|
2018-09-22 15:53:52 +08:00
|
|
|
func (fs *FilerSink) initialize(grpcAddress string, dir string,
|
|
|
|
replication string, collection string, ttlSec int) (err error) {
|
2018-09-17 15:27:56 +08:00
|
|
|
fs.grpcAddress = grpcAddress
|
|
|
|
fs.dir = dir
|
2018-09-22 15:53:52 +08:00
|
|
|
fs.replication = replication
|
|
|
|
fs.collection = collection
|
|
|
|
fs.ttlSec = int32(ttlSec)
|
2019-02-19 04:11:52 +08:00
|
|
|
fs.grpcDialOption = security.LoadClientTLS(viper.Sub("grpc"), "client")
|
2018-09-17 15:27:56 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-16 08:20:24 +08:00
|
|
|
func (fs *FilerSink) DeleteEntry(ctx context.Context, key string, isDirectory, deleteIncludeChunks bool) error {
|
|
|
|
return fs.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-17 16:37:24 +08:00
|
|
|
dir, name := filer2.FullPath(key).DirAndName()
|
2018-09-17 15:27:56 +08:00
|
|
|
|
|
|
|
request := &filer_pb.DeleteEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
|
|
|
IsDeleteData: deleteIncludeChunks,
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("delete entry: %v", request)
|
2019-03-16 08:20:24 +08:00
|
|
|
_, err := client.DeleteEntry(ctx, request)
|
2018-09-17 15:27:56 +08:00
|
|
|
if err != nil {
|
2018-09-17 16:37:24 +08:00
|
|
|
glog.V(0).Infof("delete entry %s: %v", key, err)
|
|
|
|
return fmt.Errorf("delete entry %s: %v", key, err)
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-16 08:20:24 +08:00
|
|
|
func (fs *FilerSink) CreateEntry(ctx context.Context, key string, entry *filer_pb.Entry) error {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2019-03-16 08:20:24 +08:00
|
|
|
return fs.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-23 02:14:04 +08:00
|
|
|
dir, name := filer2.FullPath(key).DirAndName()
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-23 02:14:04 +08:00
|
|
|
// look up existing entry
|
|
|
|
lookupRequest := &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
glog.V(1).Infof("lookup: %v", lookupRequest)
|
|
|
|
if resp, err := client.LookupDirectoryEntry(ctx, lookupRequest); err == nil {
|
|
|
|
if filer2.ETag(resp.Entry.Chunks) == filer2.ETag(entry.Chunks) {
|
|
|
|
glog.V(0).Infof("already replicated %s", key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 16:54:29 +08:00
|
|
|
|
2019-03-16 08:20:24 +08:00
|
|
|
replicatedChunks, err := fs.replicateChunks(ctx, entry.Chunks)
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-23 02:14:04 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("replicate entry chunks %s: %v", key, err)
|
|
|
|
return fmt.Errorf("replicate entry chunks %s: %v", key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(0).Infof("replicated %s %+v ===> %+v", key, entry.Chunks, replicatedChunks)
|
2018-09-17 15:27:56 +08:00
|
|
|
|
|
|
|
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)
|
2018-09-23 02:14:04 +08:00
|
|
|
if _, err := client.CreateEntry(ctx, request); err != nil {
|
2018-09-17 16:37:24 +08:00
|
|
|
glog.V(0).Infof("create entry %s: %v", key, err)
|
|
|
|
return fmt.Errorf("create entry %s: %v", key, err)
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:44:31 +08:00
|
|
|
func (fs *FilerSink) UpdateEntry(ctx context.Context, key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-21 16:54:29 +08:00
|
|
|
dir, name := filer2.FullPath(key).DirAndName()
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-21 16:54:29 +08:00
|
|
|
// read existing entry
|
2018-10-04 14:36:52 +08:00
|
|
|
var existingEntry *filer_pb.Entry
|
2019-03-16 08:20:24 +08:00
|
|
|
err = fs.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-21 16:54:29 +08:00
|
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
|
|
|
}
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-26 00:27:03 +08:00
|
|
|
glog.V(4).Infof("lookup entry: %v", request)
|
2018-09-21 16:54:29 +08:00
|
|
|
resp, err := client.LookupDirectoryEntry(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("lookup %s: %v", key, err)
|
|
|
|
return err
|
|
|
|
}
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-10-04 14:36:52 +08:00
|
|
|
existingEntry = resp.Entry
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-21 16:54:29 +08:00
|
|
|
return nil
|
|
|
|
})
|
2018-09-17 15:27:56 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-04 14:36:52 +08:00
|
|
|
return false, fmt.Errorf("lookup %s: %v", key, err)
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 00:27:03 +08:00
|
|
|
glog.V(0).Infof("oldEntry %+v, newEntry %+v, existingEntry: %+v", oldEntry, newEntry, existingEntry)
|
|
|
|
|
2018-11-01 12:48:05 +08:00
|
|
|
if existingEntry.Attributes.Mtime > newEntry.Attributes.Mtime {
|
|
|
|
// skip if already changed
|
|
|
|
// this usually happens when the messages are not ordered
|
|
|
|
glog.V(0).Infof("late updates %s", key)
|
|
|
|
} else if filer2.ETag(newEntry.Chunks) == filer2.ETag(existingEntry.Chunks) {
|
2018-09-23 02:14:04 +08:00
|
|
|
// skip if no change
|
|
|
|
// this usually happens when retrying the replication
|
|
|
|
glog.V(0).Infof("already replicated %s", key)
|
|
|
|
} else {
|
|
|
|
// find out what changed
|
|
|
|
deletedChunks, newChunks := compareChunks(oldEntry, newEntry)
|
|
|
|
|
|
|
|
// delete the chunks that are deleted from the source
|
|
|
|
if deleteIncludeChunks {
|
|
|
|
// remove the deleted chunks. Actual data deletion happens in filer UpdateEntry FindUnusedFileChunks
|
2019-06-23 11:04:56 +08:00
|
|
|
existingEntry.Chunks = filer2.MinusChunks(existingEntry.Chunks, deletedChunks)
|
2018-09-23 02:14:04 +08:00
|
|
|
}
|
2018-09-21 16:54:29 +08:00
|
|
|
|
2018-09-23 02:14:04 +08:00
|
|
|
// replicate the chunks that are new in the source
|
2019-03-16 08:20:24 +08:00
|
|
|
replicatedChunks, err := fs.replicateChunks(ctx, newChunks)
|
2018-09-23 02:14:04 +08:00
|
|
|
if err != nil {
|
2018-10-04 14:36:52 +08:00
|
|
|
return true, fmt.Errorf("replicte %s chunks error: %v", key, err)
|
2018-09-23 02:14:04 +08:00
|
|
|
}
|
2018-09-26 00:27:03 +08:00
|
|
|
existingEntry.Chunks = append(existingEntry.Chunks, replicatedChunks...)
|
2018-09-23 02:14:04 +08:00
|
|
|
}
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-21 16:54:29 +08:00
|
|
|
// save updated meta data
|
2019-03-16 08:20:24 +08:00
|
|
|
return true, fs.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
|
2018-09-21 16:54:29 +08:00
|
|
|
|
|
|
|
request := &filer_pb.UpdateEntryRequest{
|
2019-04-16 15:44:31 +08:00
|
|
|
Directory: newParentPath,
|
2018-09-26 00:27:03 +08:00
|
|
|
Entry: existingEntry,
|
2018-09-21 16:54:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := client.UpdateEntry(ctx, request); err != nil {
|
2018-09-26 00:27:03 +08:00
|
|
|
return fmt.Errorf("update existingEntry %s: %v", key, err)
|
2018-09-21 16:54:29 +08:00
|
|
|
}
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-21 16:54:29 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
func compareChunks(oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk) {
|
2019-06-23 11:04:56 +08:00
|
|
|
deletedChunks = filer2.MinusChunks(oldEntry.Chunks, newEntry.Chunks)
|
|
|
|
newChunks = filer2.MinusChunks(newEntry.Chunks, oldEntry.Chunks)
|
2018-09-17 15:27:56 +08:00
|
|
|
return
|
|
|
|
}
|