2018-09-23 07:40:36 +00:00
|
|
|
package filersink
|
2018-09-21 08:54:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2018-09-21 08:56:43 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-02-25 06:28:45 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
2018-09-21 08:54:29 +00:00
|
|
|
)
|
|
|
|
|
2020-10-25 22:46:29 +00:00
|
|
|
func (fs *FilerSink) replicateChunks(sourceChunks []*filer_pb.FileChunk, path string) (replicatedChunks []*filer_pb.FileChunk, err error) {
|
2018-09-21 08:54:29 +00:00
|
|
|
if len(sourceChunks) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-03-11 06:37:14 +00:00
|
|
|
|
2020-03-17 17:01:55 +00:00
|
|
|
replicatedChunks = make([]*filer_pb.FileChunk, len(sourceChunks))
|
2020-03-11 06:37:14 +00:00
|
|
|
|
2018-09-21 08:54:29 +00:00
|
|
|
var wg sync.WaitGroup
|
2020-03-11 06:37:14 +00:00
|
|
|
for chunkIndex, sourceChunk := range sourceChunks {
|
2018-09-21 08:54:29 +00:00
|
|
|
wg.Add(1)
|
2020-03-11 06:37:14 +00:00
|
|
|
go func(chunk *filer_pb.FileChunk, index int) {
|
2018-09-21 08:54:29 +00:00
|
|
|
defer wg.Done()
|
2020-10-25 22:46:29 +00:00
|
|
|
replicatedChunk, e := fs.replicateOneChunk(chunk, path)
|
2018-09-21 08:54:29 +00:00
|
|
|
if e != nil {
|
|
|
|
err = e
|
2021-01-24 08:01:44 +00:00
|
|
|
return
|
2018-09-21 08:54:29 +00:00
|
|
|
}
|
2020-03-11 06:37:14 +00:00
|
|
|
replicatedChunks[index] = replicatedChunk
|
|
|
|
}(sourceChunk, chunkIndex)
|
2018-09-21 08:54:29 +00:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-25 22:46:29 +00:00
|
|
|
func (fs *FilerSink) replicateOneChunk(sourceChunk *filer_pb.FileChunk, path string) (*filer_pb.FileChunk, error) {
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2020-10-25 22:46:29 +00:00
|
|
|
fileId, err := fs.fetchAndWrite(sourceChunk, path)
|
2018-09-21 08:54:29 +00:00
|
|
|
if err != nil {
|
2019-06-23 03:04:56 +00:00
|
|
|
return nil, fmt.Errorf("copy %s: %v", sourceChunk.GetFileIdString(), err)
|
2018-09-21 08:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &filer_pb.FileChunk{
|
|
|
|
FileId: fileId,
|
|
|
|
Offset: sourceChunk.Offset,
|
|
|
|
Size: sourceChunk.Size,
|
|
|
|
Mtime: sourceChunk.Mtime,
|
|
|
|
ETag: sourceChunk.ETag,
|
2019-06-23 03:04:56 +00:00
|
|
|
SourceFileId: sourceChunk.GetFileIdString(),
|
2020-03-06 08:49:47 +00:00
|
|
|
CipherKey: sourceChunk.CipherKey,
|
2020-06-20 15:15:49 +00:00
|
|
|
IsCompressed: sourceChunk.IsCompressed,
|
2018-09-21 08:54:29 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-25 22:46:29 +00:00
|
|
|
func (fs *FilerSink) fetchAndWrite(sourceChunk *filer_pb.FileChunk, path string) (fileId string, err error) {
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2020-09-09 10:53:09 +00:00
|
|
|
filename, header, resp, err := fs.filerSource.ReadPart(sourceChunk.GetFileIdString())
|
2018-09-21 08:54:29 +00:00
|
|
|
if err != nil {
|
2019-06-23 03:04:56 +00:00
|
|
|
return "", fmt.Errorf("read part %s: %v", sourceChunk.GetFileIdString(), err)
|
2018-09-21 08:54:29 +00:00
|
|
|
}
|
2020-09-09 10:53:09 +00:00
|
|
|
defer util.CloseResponse(resp)
|
2018-09-21 08:54:29 +00:00
|
|
|
|
|
|
|
var host string
|
2019-02-15 17:59:22 +00:00
|
|
|
var auth security.EncodedJwt
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2021-12-26 08:15:03 +00:00
|
|
|
if err := fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2021-05-07 14:29:26 +00:00
|
|
|
return util.Retry("assignVolume", func() error {
|
|
|
|
request := &filer_pb.AssignVolumeRequest{
|
|
|
|
Count: 1,
|
|
|
|
Replication: fs.replication,
|
|
|
|
Collection: fs.collection,
|
|
|
|
TtlSec: fs.ttlSec,
|
|
|
|
DataCenter: fs.dataCenter,
|
|
|
|
DiskType: fs.diskType,
|
|
|
|
Path: path,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.AssignVolume(context.Background(), request)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("assign volume failure %v: %v", request, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.Error != "" {
|
|
|
|
return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
|
|
|
|
}
|
|
|
|
|
2021-09-13 05:47:52 +00:00
|
|
|
fileId, host, auth = resp.FileId, resp.Location.Url, security.EncodedJwt(resp.Auth)
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2021-05-07 14:29:26 +00:00
|
|
|
return nil
|
|
|
|
})
|
2018-09-21 08:54:29 +00:00
|
|
|
}); err != nil {
|
|
|
|
return "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
|
2021-01-24 08:01:44 +00:00
|
|
|
if fs.writeChunkByFiler {
|
|
|
|
fileUrl = fmt.Sprintf("http://%s/?proxyChunkId=%s", fs.address, fileId)
|
|
|
|
}
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2018-09-23 05:11:49 +00:00
|
|
|
glog.V(4).Infof("replicating %s to %s header:%+v", filename, fileUrl, header)
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2020-03-06 08:49:47 +00:00
|
|
|
// fetch data as is, regardless whether it is encrypted or not
|
2021-09-06 23:20:49 +00:00
|
|
|
uploadOption := &operation.UploadOption{
|
|
|
|
UploadUrl: fileUrl,
|
|
|
|
Filename: filename,
|
|
|
|
Cipher: false,
|
|
|
|
IsInputCompressed: "gzip" == header.Get("Content-Encoding"),
|
|
|
|
MimeType: header.Get("Content-Type"),
|
|
|
|
PairMap: nil,
|
|
|
|
Jwt: auth,
|
|
|
|
}
|
|
|
|
uploadResult, err, _ := operation.Upload(resp.Body, uploadOption)
|
2018-09-21 08:54:29 +00:00
|
|
|
if err != nil {
|
2020-09-09 10:53:09 +00:00
|
|
|
glog.V(0).Infof("upload source data %v to %s: %v", sourceChunk.GetFileIdString(), fileUrl, err)
|
2018-09-21 08:54:29 +00:00
|
|
|
return "", fmt.Errorf("upload data: %v", err)
|
|
|
|
}
|
|
|
|
if uploadResult.Error != "" {
|
|
|
|
glog.V(0).Infof("upload failure %v to %s: %v", filename, fileUrl, err)
|
|
|
|
return "", fmt.Errorf("upload result: %v", uploadResult.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-29 20:26:02 +00:00
|
|
|
var _ = filer_pb.FilerClient(&FilerSink{})
|
|
|
|
|
2021-12-26 08:15:03 +00:00
|
|
|
func (fs *FilerSink) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2021-12-26 08:15:03 +00:00
|
|
|
return pb.WithGrpcClient(streamingMode, func(grpcConnection *grpc.ClientConn) error {
|
2019-04-06 03:31:58 +00:00
|
|
|
client := filer_pb.NewSeaweedFilerClient(grpcConnection)
|
2020-02-26 05:50:12 +00:00
|
|
|
return fn(client)
|
2019-04-06 03:31:58 +00:00
|
|
|
}, fs.grpcAddress, fs.grpcDialOption)
|
2018-09-21 08:54:29 +00:00
|
|
|
|
|
|
|
}
|
2020-10-12 03:15:10 +00:00
|
|
|
func (fs *FilerSink) AdjustedUrl(location *filer_pb.Location) string {
|
|
|
|
return location.Url
|
2018-09-21 08:54:29 +00:00
|
|
|
}
|