2018-09-23 07:40:36 +00:00
|
|
|
package filersink
|
2018-09-21 08:54:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-04-06 03:31:58 +00:00
|
|
|
"google.golang.org/grpc"
|
2018-09-21 08:56:43 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-09-21 08:54:29 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2019-02-15 17:59:22 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/security"
|
2018-09-21 08:54:29 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
func (fs *FilerSink) replicateChunks(ctx context.Context, sourceChunks []*filer_pb.FileChunk) (replicatedChunks []*filer_pb.FileChunk, err error) {
|
2018-09-21 08:54:29 +00:00
|
|
|
if len(sourceChunks) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, sourceChunk := range sourceChunks {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(chunk *filer_pb.FileChunk) {
|
|
|
|
defer wg.Done()
|
2019-03-16 00:20:24 +00:00
|
|
|
replicatedChunk, e := fs.replicateOneChunk(ctx, chunk)
|
2018-09-21 08:54:29 +00:00
|
|
|
if e != nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
replicatedChunks = append(replicatedChunks, replicatedChunk)
|
|
|
|
}(sourceChunk)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
func (fs *FilerSink) replicateOneChunk(ctx context.Context, sourceChunk *filer_pb.FileChunk) (*filer_pb.FileChunk, error) {
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
fileId, err := fs.fetchAndWrite(ctx, sourceChunk)
|
2018-09-21 08:54:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("copy %s: %v", sourceChunk.FileId, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &filer_pb.FileChunk{
|
|
|
|
FileId: fileId,
|
|
|
|
Offset: sourceChunk.Offset,
|
|
|
|
Size: sourceChunk.Size,
|
|
|
|
Mtime: sourceChunk.Mtime,
|
|
|
|
ETag: sourceChunk.ETag,
|
|
|
|
SourceFileId: sourceChunk.FileId,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
func (fs *FilerSink) fetchAndWrite(ctx context.Context, sourceChunk *filer_pb.FileChunk) (fileId string, err error) {
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
filename, header, readCloser, err := fs.filerSource.ReadPart(ctx, sourceChunk.FileId)
|
2018-09-21 08:54:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("read part %s: %v", sourceChunk.FileId, err)
|
|
|
|
}
|
|
|
|
defer readCloser.Close()
|
|
|
|
|
|
|
|
var host string
|
2019-02-15 17:59:22 +00:00
|
|
|
var auth security.EncodedJwt
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
if err := fs.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
|
2018-09-21 08:54:29 +00:00
|
|
|
|
|
|
|
request := &filer_pb.AssignVolumeRequest{
|
|
|
|
Count: 1,
|
|
|
|
Replication: fs.replication,
|
|
|
|
Collection: fs.collection,
|
|
|
|
TtlSec: fs.ttlSec,
|
|
|
|
DataCenter: fs.dataCenter,
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
resp, err := client.AssignVolume(ctx, request)
|
2018-09-21 08:54:29 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("assign volume failure %v: %v", request, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-15 17:59:22 +00:00
|
|
|
fileId, host, auth = resp.FileId, resp.Url, security.EncodedJwt(resp.Auth)
|
2018-09-21 08:54:29 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return "", fmt.Errorf("filerGrpcAddress assign volume: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
|
|
|
|
|
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
|
|
|
|
|
|
|
uploadResult, err := operation.Upload(fileUrl, filename, readCloser,
|
2019-02-15 17:59:22 +00:00
|
|
|
"gzip" == header.Get("Content-Encoding"), header.Get("Content-Type"), nil, auth)
|
2018-09-21 08:54:29 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("upload data %v to %s: %v", filename, fileUrl, err)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
func (fs *FilerSink) withFilerClient(ctx context.Context, fn func(filer_pb.SeaweedFilerClient) error) error {
|
2018-09-21 08:54:29 +00:00
|
|
|
|
2019-04-06 03:31:58 +00:00
|
|
|
return util.WithCachedGrpcClient(ctx, func(grpcConnection *grpc.ClientConn) error {
|
|
|
|
client := filer_pb.NewSeaweedFilerClient(grpcConnection)
|
|
|
|
return fn(client)
|
|
|
|
}, fs.grpcAddress, fs.grpcDialOption)
|
2018-09-21 08:54:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func volumeId(fileId string) string {
|
|
|
|
lastCommaIndex := strings.LastIndex(fileId, ",")
|
|
|
|
if lastCommaIndex > 0 {
|
|
|
|
return fileId[:lastCommaIndex]
|
|
|
|
}
|
|
|
|
return fileId
|
|
|
|
}
|