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