You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.9 KiB

3 years ago
3 years ago
3 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "github.com/chrislusf/seaweedfs/weed/operation"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/pb/remote_pb"
  9. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  10. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/golang/protobuf/proto"
  13. "strings"
  14. "sync"
  15. "time"
  16. )
  17. func (fs *FilerServer) DownloadToLocal(ctx context.Context, req *filer_pb.DownloadToLocalRequest) (*filer_pb.DownloadToLocalResponse, error) {
  18. // load all mappings
  19. mappingEntry, err := fs.filer.FindEntry(ctx, util.JoinPath(filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE))
  20. if err != nil {
  21. return nil, err
  22. }
  23. mappings, err := filer.UnmarshalRemoteStorageMappings(mappingEntry.Content)
  24. if err != nil {
  25. return nil, err
  26. }
  27. // find mapping
  28. var remoteStorageMountedLocation *remote_pb.RemoteStorageLocation
  29. var localMountedDir string
  30. for k, loc := range mappings.Mappings {
  31. if strings.HasPrefix(req.Directory, k) {
  32. localMountedDir, remoteStorageMountedLocation = k, loc
  33. }
  34. }
  35. if localMountedDir == "" {
  36. return nil, fmt.Errorf("%s is not mounted", req.Directory)
  37. }
  38. // find storage configuration
  39. storageConfEntry, err := fs.filer.FindEntry(ctx, util.JoinPath(filer.DirectoryEtcRemote, remoteStorageMountedLocation.Name+filer.REMOTE_STORAGE_CONF_SUFFIX))
  40. if err != nil {
  41. return nil, err
  42. }
  43. storageConf := &remote_pb.RemoteConf{}
  44. if unMarshalErr := proto.Unmarshal(storageConfEntry.Content, storageConf); unMarshalErr != nil {
  45. return nil, fmt.Errorf("unmarshal remote storage conf %s/%s: %v", filer.DirectoryEtcRemote, remoteStorageMountedLocation.Name+filer.REMOTE_STORAGE_CONF_SUFFIX, unMarshalErr)
  46. }
  47. // find the entry
  48. entry, err := fs.filer.FindEntry(ctx, util.JoinPath(req.Directory, req.Name))
  49. if err == filer_pb.ErrNotFound {
  50. return nil, err
  51. }
  52. resp := &filer_pb.DownloadToLocalResponse{}
  53. if entry.Remote == nil || entry.Remote.RemoteSize == 0 {
  54. return resp, nil
  55. }
  56. // detect storage option
  57. // replication level is set to "000" to ensure only need to ask one volume server to fetch the data.
  58. so, err := fs.detectStorageOption(req.Directory, "", "000", 0, "", "", "")
  59. if err != nil {
  60. return resp, err
  61. }
  62. assignRequest, altRequest := so.ToAssignRequests(1)
  63. // find a good chunk size
  64. chunkSize := int64(5 * 1024 * 1024)
  65. chunkCount := entry.Remote.RemoteSize/chunkSize + 1
  66. for chunkCount > 1000 && chunkSize < int64(fs.option.MaxMB)*1024*1024/2 {
  67. chunkSize *= 2
  68. chunkCount = entry.Remote.RemoteSize/chunkSize + 1
  69. }
  70. dest := util.FullPath(remoteStorageMountedLocation.Path).Child(string(util.FullPath(req.Directory).Child(req.Name))[len(localMountedDir):])
  71. var chunks []*filer_pb.FileChunk
  72. var fetchAndWriteErr error
  73. var wg sync.WaitGroup
  74. limitedConcurrentExecutor := util.NewLimitedConcurrentExecutor(8)
  75. for offset := int64(0); offset < entry.Remote.RemoteSize; offset += chunkSize {
  76. localOffset := offset
  77. wg.Add(1)
  78. limitedConcurrentExecutor.Execute(func() {
  79. defer wg.Done()
  80. size := chunkSize
  81. if localOffset+chunkSize > entry.Remote.RemoteSize {
  82. size = entry.Remote.RemoteSize - localOffset
  83. }
  84. // assign one volume server
  85. assignResult, err := operation.Assign(fs.filer.GetMaster, fs.grpcDialOption, assignRequest, altRequest)
  86. if err != nil {
  87. fetchAndWriteErr = err
  88. return
  89. }
  90. if assignResult.Error != "" {
  91. fetchAndWriteErr = fmt.Errorf("assign: %v", assignResult.Error)
  92. return
  93. }
  94. fileId, parseErr := needle.ParseFileIdFromString(assignResult.Fid)
  95. if assignResult.Error != "" {
  96. fetchAndWriteErr = fmt.Errorf("unrecognized file id %s: %v", assignResult.Fid, parseErr)
  97. return
  98. }
  99. var replicas []*volume_server_pb.FetchAndWriteNeedleRequest_Replica
  100. for _, r := range assignResult.Replicas {
  101. replicas = append(replicas, &volume_server_pb.FetchAndWriteNeedleRequest_Replica{
  102. Url: r.Url,
  103. PublicUrl: r.PublicUrl,
  104. })
  105. }
  106. // tell filer to tell volume server to download into needles
  107. err = operation.WithVolumeServerClient(assignResult.Url, fs.grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  108. _, fetchAndWriteErr := volumeServerClient.FetchAndWriteNeedle(context.Background(), &volume_server_pb.FetchAndWriteNeedleRequest{
  109. VolumeId: uint32(fileId.VolumeId),
  110. NeedleId: uint64(fileId.Key),
  111. Cookie: uint32(fileId.Cookie),
  112. Offset: localOffset,
  113. Size: size,
  114. Replicas: replicas,
  115. Auth: string(assignResult.Auth),
  116. RemoteConf: storageConf,
  117. RemoteLocation: &remote_pb.RemoteStorageLocation{
  118. Name: remoteStorageMountedLocation.Name,
  119. Bucket: remoteStorageMountedLocation.Bucket,
  120. Path: string(dest),
  121. },
  122. })
  123. if fetchAndWriteErr != nil {
  124. return fmt.Errorf("volume server %s fetchAndWrite %s: %v", assignResult.Url, dest, fetchAndWriteErr)
  125. }
  126. return nil
  127. })
  128. if err != nil && fetchAndWriteErr == nil {
  129. fetchAndWriteErr = err
  130. return
  131. }
  132. chunks = append(chunks, &filer_pb.FileChunk{
  133. FileId: assignResult.Fid,
  134. Offset: localOffset,
  135. Size: uint64(size),
  136. Mtime: time.Now().Unix(),
  137. Fid: &filer_pb.FileId{
  138. VolumeId: uint32(fileId.VolumeId),
  139. FileKey: uint64(fileId.Key),
  140. Cookie: uint32(fileId.Cookie),
  141. },
  142. })
  143. })
  144. }
  145. wg.Wait()
  146. if fetchAndWriteErr != nil {
  147. return nil, fetchAndWriteErr
  148. }
  149. garbage := entry.Chunks
  150. newEntry := entry.ShallowClone()
  151. newEntry.Chunks = chunks
  152. newEntry.Remote = proto.Clone(entry.Remote).(*filer_pb.RemoteEntry)
  153. newEntry.Remote.LastLocalSyncTsNs = time.Now().UnixNano()
  154. // this skips meta data log events
  155. if err := fs.filer.Store.UpdateEntry(context.Background(), newEntry); err != nil {
  156. return nil, err
  157. }
  158. fs.filer.DeleteChunks(garbage)
  159. fs.filer.NotifyUpdateEvent(ctx, entry, newEntry, true, false, nil)
  160. resp.Entry = newEntry.ToProtoEntry()
  161. return resp, nil
  162. }