2021-08-10 05:35:18 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-11-29 16:28:26 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-08-10 05:35:18 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
2021-09-13 13:47:52 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
2021-08-10 05:35:18 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2021-08-27 06:18:34 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/remote_pb"
|
2021-08-10 05:35:18 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
2021-10-31 10:27:25 +08:00
|
|
|
func (fs *FilerServer) CacheRemoteObjectToLocalCluster(ctx context.Context, req *filer_pb.CacheRemoteObjectToLocalClusterRequest) (*filer_pb.CacheRemoteObjectToLocalClusterResponse, error) {
|
2021-08-10 05:35:18 +08:00
|
|
|
|
|
|
|
// load all mappings
|
|
|
|
mappingEntry, err := fs.filer.FindEntry(ctx, util.JoinPath(filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mappings, err := filer.UnmarshalRemoteStorageMappings(mappingEntry.Content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// find mapping
|
2021-08-27 06:18:34 +08:00
|
|
|
var remoteStorageMountedLocation *remote_pb.RemoteStorageLocation
|
2021-08-10 05:35:18 +08:00
|
|
|
var localMountedDir string
|
|
|
|
for k, loc := range mappings.Mappings {
|
|
|
|
if strings.HasPrefix(req.Directory, k) {
|
|
|
|
localMountedDir, remoteStorageMountedLocation = k, loc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if localMountedDir == "" {
|
|
|
|
return nil, fmt.Errorf("%s is not mounted", req.Directory)
|
|
|
|
}
|
|
|
|
|
|
|
|
// find storage configuration
|
|
|
|
storageConfEntry, err := fs.filer.FindEntry(ctx, util.JoinPath(filer.DirectoryEtcRemote, remoteStorageMountedLocation.Name+filer.REMOTE_STORAGE_CONF_SUFFIX))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-27 06:18:34 +08:00
|
|
|
storageConf := &remote_pb.RemoteConf{}
|
2021-08-10 05:35:18 +08:00
|
|
|
if unMarshalErr := proto.Unmarshal(storageConfEntry.Content, storageConf); unMarshalErr != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal remote storage conf %s/%s: %v", filer.DirectoryEtcRemote, remoteStorageMountedLocation.Name+filer.REMOTE_STORAGE_CONF_SUFFIX, unMarshalErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the entry
|
|
|
|
entry, err := fs.filer.FindEntry(ctx, util.JoinPath(req.Directory, req.Name))
|
|
|
|
if err == filer_pb.ErrNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-31 10:27:25 +08:00
|
|
|
resp := &filer_pb.CacheRemoteObjectToLocalClusterResponse{}
|
2021-08-10 05:35:18 +08:00
|
|
|
if entry.Remote == nil || entry.Remote.RemoteSize == 0 {
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// detect storage option
|
2021-12-22 21:57:26 +08:00
|
|
|
so, err := fs.detectStorageOption(req.Directory, "", "", 0, "", "", "", "")
|
2021-08-10 05:35:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
assignRequest, altRequest := so.ToAssignRequests(1)
|
|
|
|
|
|
|
|
// find a good chunk size
|
|
|
|
chunkSize := int64(5 * 1024 * 1024)
|
|
|
|
chunkCount := entry.Remote.RemoteSize/chunkSize + 1
|
2021-08-10 06:08:53 +08:00
|
|
|
for chunkCount > 1000 && chunkSize < int64(fs.option.MaxMB)*1024*1024/2 {
|
2021-08-10 05:35:18 +08:00
|
|
|
chunkSize *= 2
|
|
|
|
chunkCount = entry.Remote.RemoteSize/chunkSize + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
dest := util.FullPath(remoteStorageMountedLocation.Path).Child(string(util.FullPath(req.Directory).Child(req.Name))[len(localMountedDir):])
|
|
|
|
|
|
|
|
var chunks []*filer_pb.FileChunk
|
2021-08-15 06:41:13 +08:00
|
|
|
var fetchAndWriteErr error
|
2021-08-27 07:16:26 +08:00
|
|
|
var wg sync.WaitGroup
|
2021-08-10 05:35:18 +08:00
|
|
|
|
2021-08-15 06:41:13 +08:00
|
|
|
limitedConcurrentExecutor := util.NewLimitedConcurrentExecutor(8)
|
2021-08-10 05:35:18 +08:00
|
|
|
for offset := int64(0); offset < entry.Remote.RemoteSize; offset += chunkSize {
|
2021-08-15 06:41:13 +08:00
|
|
|
localOffset := offset
|
2021-08-10 05:35:18 +08:00
|
|
|
|
2021-08-27 07:16:26 +08:00
|
|
|
wg.Add(1)
|
2021-08-15 06:41:13 +08:00
|
|
|
limitedConcurrentExecutor.Execute(func() {
|
2021-08-27 07:16:26 +08:00
|
|
|
defer wg.Done()
|
2021-08-15 06:41:13 +08:00
|
|
|
size := chunkSize
|
|
|
|
if localOffset+chunkSize > entry.Remote.RemoteSize {
|
|
|
|
size = entry.Remote.RemoteSize - localOffset
|
|
|
|
}
|
2021-08-10 05:35:18 +08:00
|
|
|
|
2021-08-15 06:41:13 +08:00
|
|
|
// assign one volume server
|
|
|
|
assignResult, err := operation.Assign(fs.filer.GetMaster, fs.grpcDialOption, assignRequest, altRequest)
|
|
|
|
if err != nil {
|
|
|
|
fetchAndWriteErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if assignResult.Error != "" {
|
|
|
|
fetchAndWriteErr = fmt.Errorf("assign: %v", assignResult.Error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fileId, parseErr := needle.ParseFileIdFromString(assignResult.Fid)
|
|
|
|
if assignResult.Error != "" {
|
|
|
|
fetchAndWriteErr = fmt.Errorf("unrecognized file id %s: %v", assignResult.Fid, parseErr)
|
|
|
|
return
|
2021-08-10 05:35:18 +08:00
|
|
|
}
|
|
|
|
|
2021-09-07 09:30:44 +08:00
|
|
|
var replicas []*volume_server_pb.FetchAndWriteNeedleRequest_Replica
|
|
|
|
for _, r := range assignResult.Replicas {
|
|
|
|
replicas = append(replicas, &volume_server_pb.FetchAndWriteNeedleRequest_Replica{
|
|
|
|
Url: r.Url,
|
|
|
|
PublicUrl: r.PublicUrl,
|
2021-09-15 01:37:06 +08:00
|
|
|
GrpcPort: int32(r.GrpcPort),
|
2021-09-07 09:30:44 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-15 06:41:13 +08:00
|
|
|
// tell filer to tell volume server to download into needles
|
2021-09-13 13:47:52 +08:00
|
|
|
assignedServerAddress := pb.NewServerAddressWithGrpcPort(assignResult.Url, assignResult.GrpcPort)
|
2021-12-26 16:15:03 +08:00
|
|
|
err = operation.WithVolumeServerClient(false, assignedServerAddress, fs.grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
|
2021-08-15 06:41:13 +08:00
|
|
|
_, fetchAndWriteErr := volumeServerClient.FetchAndWriteNeedle(context.Background(), &volume_server_pb.FetchAndWriteNeedleRequest{
|
2021-09-01 17:45:42 +08:00
|
|
|
VolumeId: uint32(fileId.VolumeId),
|
|
|
|
NeedleId: uint64(fileId.Key),
|
|
|
|
Cookie: uint32(fileId.Cookie),
|
|
|
|
Offset: localOffset,
|
|
|
|
Size: size,
|
2021-09-07 09:30:44 +08:00
|
|
|
Replicas: replicas,
|
|
|
|
Auth: string(assignResult.Auth),
|
2021-08-27 06:18:34 +08:00
|
|
|
RemoteConf: storageConf,
|
|
|
|
RemoteLocation: &remote_pb.RemoteStorageLocation{
|
|
|
|
Name: remoteStorageMountedLocation.Name,
|
|
|
|
Bucket: remoteStorageMountedLocation.Bucket,
|
|
|
|
Path: string(dest),
|
|
|
|
},
|
2021-08-15 06:41:13 +08:00
|
|
|
})
|
|
|
|
if fetchAndWriteErr != nil {
|
|
|
|
return fmt.Errorf("volume server %s fetchAndWrite %s: %v", assignResult.Url, dest, fetchAndWriteErr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-08-10 05:35:18 +08:00
|
|
|
|
2021-08-27 06:18:34 +08:00
|
|
|
if err != nil && fetchAndWriteErr == nil {
|
2021-08-15 06:41:13 +08:00
|
|
|
fetchAndWriteErr = err
|
|
|
|
return
|
|
|
|
}
|
2021-08-10 05:35:18 +08:00
|
|
|
|
2021-08-15 06:41:13 +08:00
|
|
|
chunks = append(chunks, &filer_pb.FileChunk{
|
|
|
|
FileId: assignResult.Fid,
|
|
|
|
Offset: localOffset,
|
|
|
|
Size: uint64(size),
|
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Fid: &filer_pb.FileId{
|
|
|
|
VolumeId: uint32(fileId.VolumeId),
|
|
|
|
FileKey: uint64(fileId.Key),
|
|
|
|
Cookie: uint32(fileId.Cookie),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2021-08-10 05:35:18 +08:00
|
|
|
}
|
|
|
|
|
2021-08-27 07:16:26 +08:00
|
|
|
wg.Wait()
|
2021-08-16 14:20:46 +08:00
|
|
|
if fetchAndWriteErr != nil {
|
|
|
|
return nil, fetchAndWriteErr
|
|
|
|
}
|
|
|
|
|
2021-08-10 05:35:18 +08:00
|
|
|
garbage := entry.Chunks
|
|
|
|
|
|
|
|
newEntry := entry.ShallowClone()
|
|
|
|
newEntry.Chunks = chunks
|
|
|
|
newEntry.Remote = proto.Clone(entry.Remote).(*filer_pb.RemoteEntry)
|
2021-08-15 12:46:34 +08:00
|
|
|
newEntry.Remote.LastLocalSyncTsNs = time.Now().UnixNano()
|
2021-08-10 05:35:18 +08:00
|
|
|
|
|
|
|
// this skips meta data log events
|
|
|
|
|
|
|
|
if err := fs.filer.Store.UpdateEntry(context.Background(), newEntry); err != nil {
|
2021-11-29 16:28:26 +08:00
|
|
|
fs.filer.DeleteChunks(chunks)
|
2021-08-10 05:35:18 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fs.filer.DeleteChunks(garbage)
|
|
|
|
|
|
|
|
fs.filer.NotifyUpdateEvent(ctx, entry, newEntry, true, false, nil)
|
|
|
|
|
2021-08-10 13:11:57 +08:00
|
|
|
resp.Entry = newEntry.ToProtoEntry()
|
2021-08-10 05:35:18 +08:00
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|