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.

179 lines
5.6 KiB

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. // tell filer to tell volume server to download into needles
  100. err = operation.WithVolumeServerClient(assignResult.Url, fs.grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  101. _, fetchAndWriteErr := volumeServerClient.FetchAndWriteNeedle(context.Background(), &volume_server_pb.FetchAndWriteNeedleRequest{
  102. VolumeId: uint32(fileId.VolumeId),
  103. NeedleId: uint64(fileId.Key),
  104. Cookie: uint32(fileId.Cookie),
  105. Offset: localOffset,
  106. Size: size,
  107. RemoteConf: storageConf,
  108. RemoteLocation: &remote_pb.RemoteStorageLocation{
  109. Name: remoteStorageMountedLocation.Name,
  110. Bucket: remoteStorageMountedLocation.Bucket,
  111. Path: string(dest),
  112. },
  113. })
  114. if fetchAndWriteErr != nil {
  115. return fmt.Errorf("volume server %s fetchAndWrite %s: %v", assignResult.Url, dest, fetchAndWriteErr)
  116. }
  117. return nil
  118. })
  119. if err != nil && fetchAndWriteErr == nil {
  120. fetchAndWriteErr = err
  121. return
  122. }
  123. chunks = append(chunks, &filer_pb.FileChunk{
  124. FileId: assignResult.Fid,
  125. Offset: localOffset,
  126. Size: uint64(size),
  127. Mtime: time.Now().Unix(),
  128. Fid: &filer_pb.FileId{
  129. VolumeId: uint32(fileId.VolumeId),
  130. FileKey: uint64(fileId.Key),
  131. Cookie: uint32(fileId.Cookie),
  132. },
  133. })
  134. })
  135. }
  136. wg.Wait()
  137. if fetchAndWriteErr != nil {
  138. return nil, fetchAndWriteErr
  139. }
  140. garbage := entry.Chunks
  141. newEntry := entry.ShallowClone()
  142. newEntry.Chunks = chunks
  143. newEntry.Remote = proto.Clone(entry.Remote).(*filer_pb.RemoteEntry)
  144. newEntry.Remote.LastLocalSyncTsNs = time.Now().UnixNano()
  145. // this skips meta data log events
  146. if err := fs.filer.Store.UpdateEntry(context.Background(), newEntry); err != nil {
  147. return nil, err
  148. }
  149. fs.filer.DeleteChunks(garbage)
  150. fs.filer.NotifyUpdateEvent(ctx, entry, newEntry, true, false, nil)
  151. resp.Entry = newEntry.ToProtoEntry()
  152. return resp, nil
  153. }