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"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
2021-07-22 05:38:12 +08:00
|
|
|
"math"
|
2020-01-30 01:09:55 +08:00
|
|
|
|
2019-02-19 04:11:52 +08:00
|
|
|
"google.golang.org/grpc"
|
2018-09-21 16:54:29 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
2020-01-30 01:09:55 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/sink"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/source"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2018-09-17 15:27:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type FilerSink struct {
|
2021-01-24 16:01:44 +08:00
|
|
|
filerSource *source.FilerSource
|
|
|
|
grpcAddress string
|
|
|
|
dir string
|
|
|
|
replication string
|
|
|
|
collection string
|
|
|
|
ttlSec int32
|
2021-02-11 15:41:05 +08:00
|
|
|
diskType string
|
2021-01-24 16:01:44 +08:00
|
|
|
dataCenter string
|
|
|
|
grpcDialOption grpc.DialOption
|
|
|
|
address string
|
|
|
|
writeChunkByFiler bool
|
2021-03-01 12:34:14 +08:00
|
|
|
isIncremental bool
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-01 08:19:03 +08:00
|
|
|
func (fs *FilerSink) IsIncremental() bool {
|
|
|
|
return fs.isIncremental
|
|
|
|
}
|
|
|
|
|
2020-01-30 01:09:55 +08:00
|
|
|
func (fs *FilerSink) Initialize(configuration util.Configuration, prefix string) error {
|
2021-03-01 12:34:14 +08:00
|
|
|
fs.isIncremental = configuration.GetBool(prefix + "is_incremental")
|
2022-08-05 08:35:00 +08:00
|
|
|
fs.dataCenter = configuration.GetString(prefix + "dataCenter")
|
2020-09-10 02:21:23 +08:00
|
|
|
return fs.DoInitialize(
|
2021-01-24 16:01:44 +08:00
|
|
|
"",
|
2020-01-30 01:09:55 +08:00
|
|
|
configuration.GetString(prefix+"grpcAddress"),
|
|
|
|
configuration.GetString(prefix+"directory"),
|
|
|
|
configuration.GetString(prefix+"replication"),
|
|
|
|
configuration.GetString(prefix+"collection"),
|
|
|
|
configuration.GetInt(prefix+"ttlSec"),
|
2020-12-14 04:05:31 +08:00
|
|
|
configuration.GetString(prefix+"disk"),
|
2021-01-24 16:01:44 +08:00
|
|
|
security.LoadClientTLS(util.GetViper(), "grpc.client"),
|
|
|
|
false)
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-24 16:01:44 +08:00
|
|
|
func (fs *FilerSink) DoInitialize(address, grpcAddress string, dir string,
|
2021-02-10 03:37:07 +08:00
|
|
|
replication string, collection string, ttlSec int, diskType string, grpcDialOption grpc.DialOption, writeChunkByFiler bool) (err error) {
|
2021-01-24 16:01:44 +08:00
|
|
|
fs.address = address
|
|
|
|
if fs.address == "" {
|
|
|
|
fs.address = pb.GrpcAddressToServerAddress(grpcAddress)
|
|
|
|
}
|
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)
|
2020-12-14 03:59:32 +08:00
|
|
|
fs.diskType = diskType
|
2020-09-10 02:21:23 +08:00
|
|
|
fs.grpcDialOption = grpcDialOption
|
2021-01-24 16:01:44 +08:00
|
|
|
fs.writeChunkByFiler = writeChunkByFiler
|
2018-09-17 15:27:56 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-10 02:21:23 +08:00
|
|
|
func (fs *FilerSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2020-03-23 16:30:22 +08:00
|
|
|
dir, name := util.FullPath(key).DirAndName()
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2020-09-10 02:21:23 +08:00
|
|
|
glog.V(4).Infof("delete entry: %v", key)
|
|
|
|
err := filer_pb.Remove(fs, dir, name, deleteIncludeChunks, true, true, true, signatures)
|
2020-03-23 16:30:22 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("delete entry %s: %v", key, err)
|
|
|
|
return fmt.Errorf("delete entry %s: %v", key, err)
|
|
|
|
}
|
|
|
|
return nil
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
|
2020-09-10 02:21:23 +08:00
|
|
|
func (fs *FilerSink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2021-12-26 16:15:03 +08:00
|
|
|
return fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
dir, name := util.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)
|
2020-03-08 08:51:46 +08:00
|
|
|
if resp, err := filer_pb.LookupEntry(client, lookupRequest); err == nil {
|
2020-09-01 15:21:19 +08:00
|
|
|
if filer.ETag(resp.Entry) == filer.ETag(entry) {
|
2020-09-10 02:21:23 +08:00
|
|
|
glog.V(3).Infof("already replicated %s", key)
|
2018-09-23 02:14:04 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 16:54:29 +08:00
|
|
|
|
2020-10-26 06:46:29 +08:00
|
|
|
replicatedChunks, err := fs.replicateChunks(entry.Chunks, key)
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-23 02:14:04 +08:00
|
|
|
if err != nil {
|
2020-09-10 02:21:23 +08:00
|
|
|
// only warning here since the source chunk may have been deleted already
|
|
|
|
glog.Warningf("replicate entry chunks %s: %v", key, err)
|
2018-09-23 02:14:04 +08:00
|
|
|
}
|
|
|
|
|
2020-09-10 02:21:23 +08:00
|
|
|
glog.V(4).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,
|
2022-09-05 09:26:36 +08:00
|
|
|
Extended: entry.Extended,
|
2018-09-17 15:27:56 +08:00
|
|
|
Chunks: replicatedChunks,
|
2020-11-30 20:34:04 +08:00
|
|
|
Content: entry.Content,
|
2021-07-27 13:53:44 +08:00
|
|
|
RemoteEntry: entry.RemoteEntry,
|
2018-09-17 15:27:56 +08:00
|
|
|
},
|
2020-07-01 13:53:53 +08:00
|
|
|
IsFromOtherCluster: true,
|
2020-09-10 02:21:23 +08:00
|
|
|
Signatures: signatures,
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
|
2020-09-10 02:21:23 +08:00
|
|
|
glog.V(3).Infof("create: %v", request)
|
2020-02-26 13:50:12 +08:00
|
|
|
if err := filer_pb.CreateEntry(client, 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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-10 02:21:23 +08:00
|
|
|
func (fs *FilerSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
dir, name := util.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
|
2021-12-26 16:15:03 +08:00
|
|
|
err = fs.WithFilerClient(false, 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)
|
2020-03-08 08:51:46 +08:00
|
|
|
resp, err := filer_pb.LookupEntry(client, request)
|
2018-09-21 16:54:29 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-10 02:21:23 +08:00
|
|
|
glog.V(4).Infof("oldEntry %+v, newEntry %+v, existingEntry: %+v", oldEntry, newEntry, existingEntry)
|
2018-09-26 00:27:03 +08:00
|
|
|
|
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
|
2020-09-10 02:21:23 +08:00
|
|
|
glog.V(2).Infof("late updates %s", key)
|
2020-09-01 15:21:19 +08:00
|
|
|
} else if filer.ETag(newEntry) == filer.ETag(existingEntry) {
|
2018-09-23 02:14:04 +08:00
|
|
|
// skip if no change
|
|
|
|
// this usually happens when retrying the replication
|
2020-09-10 02:21:23 +08:00
|
|
|
glog.V(3).Infof("already replicated %s", key)
|
2018-09-23 02:14:04 +08:00
|
|
|
} else {
|
|
|
|
// find out what changed
|
2020-09-01 15:21:19 +08:00
|
|
|
deletedChunks, newChunks, err := compareChunks(filer.LookupFn(fs), oldEntry, newEntry)
|
2020-07-20 08:59:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return true, fmt.Errorf("replicte %s compare chunks error: %v", key, err)
|
|
|
|
}
|
2018-09-23 02:14:04 +08:00
|
|
|
|
|
|
|
// delete the chunks that are deleted from the source
|
|
|
|
if deleteIncludeChunks {
|
|
|
|
// remove the deleted chunks. Actual data deletion happens in filer UpdateEntry FindUnusedFileChunks
|
2022-02-07 19:46:28 +08:00
|
|
|
existingEntry.Chunks = filer.DoMinusChunksBySourceFileId(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
|
2020-10-26 06:46:29 +08:00
|
|
|
replicatedChunks, err := fs.replicateChunks(newChunks, key)
|
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...)
|
2022-05-06 18:54:12 +08:00
|
|
|
existingEntry.Attributes = newEntry.Attributes
|
|
|
|
existingEntry.Extended = newEntry.Extended
|
|
|
|
existingEntry.HardLinkId = newEntry.HardLinkId
|
|
|
|
existingEntry.HardLinkCounter = newEntry.HardLinkCounter
|
|
|
|
existingEntry.Content = newEntry.Content
|
|
|
|
existingEntry.RemoteEntry = newEntry.RemoteEntry
|
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
|
2021-12-26 16:15:03 +08:00
|
|
|
return true, fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2018-09-21 16:54:29 +08:00
|
|
|
|
|
|
|
request := &filer_pb.UpdateEntryRequest{
|
2020-07-01 13:53:53 +08:00
|
|
|
Directory: newParentPath,
|
|
|
|
Entry: existingEntry,
|
|
|
|
IsFromOtherCluster: true,
|
2020-09-10 02:21:23 +08:00
|
|
|
Signatures: signatures,
|
2018-09-21 16:54:29 +08:00
|
|
|
}
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
if _, err := client.UpdateEntry(context.Background(), 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
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2021-01-06 20:21:34 +08:00
|
|
|
func compareChunks(lookupFileIdFn wdclient.LookupFileIdFunctionType, oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk, err error) {
|
2021-07-20 14:07:22 +08:00
|
|
|
aData, aMeta, aErr := filer.ResolveChunkManifest(lookupFileIdFn, oldEntry.Chunks, 0, math.MaxInt64)
|
2020-07-20 08:59:43 +08:00
|
|
|
if aErr != nil {
|
|
|
|
return nil, nil, aErr
|
|
|
|
}
|
2021-07-20 14:07:22 +08:00
|
|
|
bData, bMeta, bErr := filer.ResolveChunkManifest(lookupFileIdFn, newEntry.Chunks, 0, math.MaxInt64)
|
2020-07-20 08:59:43 +08:00
|
|
|
if bErr != nil {
|
|
|
|
return nil, nil, bErr
|
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
deletedChunks = append(deletedChunks, filer.DoMinusChunks(aData, bData)...)
|
|
|
|
deletedChunks = append(deletedChunks, filer.DoMinusChunks(aMeta, bMeta)...)
|
2020-07-20 08:59:43 +08:00
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
newChunks = append(newChunks, filer.DoMinusChunks(bData, aData)...)
|
|
|
|
newChunks = append(newChunks, filer.DoMinusChunks(bMeta, aMeta)...)
|
2020-07-20 08:59:43 +08:00
|
|
|
|
2018-09-17 15:27:56 +08:00
|
|
|
return
|
|
|
|
}
|