2018-09-17 07:27:56 +00:00
|
|
|
package sink
|
|
|
|
|
|
|
|
import (
|
2018-09-21 08:54:29 +00:00
|
|
|
"context"
|
2018-09-21 08:56:43 +00:00
|
|
|
"fmt"
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2018-09-17 07:27:56 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-09-21 08:56:43 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-09-21 08:54:29 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication/source"
|
2018-09-21 08:56:43 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-09-17 07:27:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ReplicationSink interface {
|
2018-09-17 08:37:24 +00:00
|
|
|
DeleteEntry(key string, entry *filer_pb.Entry, deleteIncludeChunks bool) error
|
|
|
|
CreateEntry(key string, entry *filer_pb.Entry) error
|
|
|
|
UpdateEntry(key string, oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) error
|
2018-09-22 07:53:52 +00:00
|
|
|
GetSinkToDirectory() string
|
2018-09-21 08:54:29 +00:00
|
|
|
SetSourceFiler(s *source.FilerSource)
|
2018-09-17 07:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FilerSink struct {
|
2018-09-22 07:53:52 +00:00
|
|
|
filerSource *source.FilerSource
|
2018-09-17 07:27:56 +00:00
|
|
|
grpcAddress string
|
|
|
|
dir string
|
2018-09-21 08:54:29 +00:00
|
|
|
replication string
|
|
|
|
collection string
|
|
|
|
ttlSec int32
|
|
|
|
dataCenter string
|
2018-09-17 07:27:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 07:53:52 +00:00
|
|
|
func (fs *FilerSink) GetSinkToDirectory() string {
|
2018-09-17 08:37:24 +00:00
|
|
|
return fs.dir
|
|
|
|
}
|
|
|
|
|
2018-09-17 07:27:56 +00:00
|
|
|
func (fs *FilerSink) Initialize(configuration util.Configuration) error {
|
|
|
|
return fs.initialize(
|
|
|
|
configuration.GetString("grpcAddress"),
|
|
|
|
configuration.GetString("directory"),
|
2018-09-22 07:53:52 +00:00
|
|
|
configuration.GetString("replication"),
|
|
|
|
configuration.GetString("collection"),
|
|
|
|
configuration.GetInt("ttlSec"),
|
2018-09-17 07:27:56 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-09-21 08:54:29 +00:00
|
|
|
func (fs *FilerSink) SetSourceFiler(s *source.FilerSource) {
|
|
|
|
fs.filerSource = s
|
|
|
|
}
|
|
|
|
|
2018-09-22 07:53:52 +00:00
|
|
|
func (fs *FilerSink) initialize(grpcAddress string, dir string,
|
|
|
|
replication string, collection string, ttlSec int) (err error) {
|
2018-09-17 07:27:56 +00:00
|
|
|
fs.grpcAddress = grpcAddress
|
|
|
|
fs.dir = dir
|
2018-09-22 07:53:52 +00:00
|
|
|
fs.replication = replication
|
|
|
|
fs.collection = collection
|
|
|
|
fs.ttlSec = int32(ttlSec)
|
2018-09-17 07:27:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-17 08:37:24 +00:00
|
|
|
func (fs *FilerSink) DeleteEntry(key string, entry *filer_pb.Entry, deleteIncludeChunks bool) error {
|
2018-09-17 07:27:56 +00:00
|
|
|
return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
|
2018-09-17 08:37:24 +00:00
|
|
|
dir, name := filer2.FullPath(key).DirAndName()
|
2018-09-17 07:27:56 +00:00
|
|
|
|
|
|
|
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 {
|
2018-09-17 08:37:24 +00:00
|
|
|
glog.V(0).Infof("delete entry %s: %v", key, err)
|
|
|
|
return fmt.Errorf("delete entry %s: %v", key, err)
|
2018-09-17 07:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-17 08:37:24 +00:00
|
|
|
func (fs *FilerSink) CreateEntry(key string, entry *filer_pb.Entry) error {
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-22 18:14:04 +00:00
|
|
|
return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-22 18:14:04 +00:00
|
|
|
dir, name := filer2.FullPath(key).DirAndName()
|
|
|
|
ctx := context.Background()
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-22 18:14:04 +00: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 08:54:29 +00:00
|
|
|
|
2018-09-22 18:14:04 +00:00
|
|
|
replicatedChunks, err := fs.replicateChunks(entry.Chunks)
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-22 18:14:04 +00: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 07:27:56 +00: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-22 18:14:04 +00:00
|
|
|
if _, err := client.CreateEntry(ctx, request); err != nil {
|
2018-09-17 08:37:24 +00:00
|
|
|
glog.V(0).Infof("create entry %s: %v", key, err)
|
|
|
|
return fmt.Errorf("create entry %s: %v", key, err)
|
2018-09-17 07:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-21 08:54:29 +00:00
|
|
|
func (fs *FilerSink) UpdateEntry(key string, oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (err error) {
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-21 08:54:29 +00:00
|
|
|
ctx := context.Background()
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-21 08:54:29 +00:00
|
|
|
dir, name := filer2.FullPath(key).DirAndName()
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-21 08:54:29 +00:00
|
|
|
// read existing entry
|
|
|
|
var entry *filer_pb.Entry
|
|
|
|
err = fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-21 08:54:29 +00:00
|
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
|
|
|
}
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-21 08:54:29 +00:00
|
|
|
glog.V(4).Infof("lookup directory entry: %v", request)
|
|
|
|
resp, err := client.LookupDirectoryEntry(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("lookup %s: %v", key, err)
|
|
|
|
return err
|
|
|
|
}
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-21 08:54:29 +00:00
|
|
|
entry = resp.Entry
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-21 08:54:29 +00:00
|
|
|
return nil
|
|
|
|
})
|
2018-09-17 07:27:56 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-09-22 18:14:04 +00:00
|
|
|
return fmt.Errorf("lookup when updating %s: %v", key, err)
|
2018-09-17 07:27:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 18:14:04 +00:00
|
|
|
if filer2.ETag(newEntry.Chunks) == filer2.ETag(entry.Chunks) {
|
|
|
|
// 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
|
|
|
|
entry.Chunks = minusChunks(entry.Chunks, deletedChunks)
|
|
|
|
}
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2018-09-22 18:14:04 +00:00
|
|
|
// replicate the chunks that are new in the source
|
|
|
|
replicatedChunks, err := fs.replicateChunks(newChunks)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("replicte %s chunks error: %v", key, err)
|
|
|
|
}
|
|
|
|
entry.Chunks = append(entry.Chunks, replicatedChunks...)
|
|
|
|
}
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-21 08:54:29 +00:00
|
|
|
// save updated meta data
|
|
|
|
return fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
|
|
|
|
request := &filer_pb.UpdateEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Entry: entry,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := client.UpdateEntry(ctx, request); err != nil {
|
|
|
|
return fmt.Errorf("update entry %s: %v", key, err)
|
|
|
|
}
|
2018-09-17 07:27:56 +00:00
|
|
|
|
2018-09-21 08:54:29 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
func compareChunks(oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk) {
|
|
|
|
deletedChunks = minusChunks(oldEntry.Chunks, newEntry.Chunks)
|
|
|
|
newChunks = minusChunks(newEntry.Chunks, oldEntry.Chunks)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func minusChunks(as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk) {
|
|
|
|
for _, a := range as {
|
|
|
|
found := false
|
|
|
|
for _, b := range bs {
|
|
|
|
if a.FileId == b.FileId {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
delta = append(delta, a)
|
|
|
|
}
|
|
|
|
}
|
2018-09-17 07:27:56 +00:00
|
|
|
return
|
|
|
|
}
|