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

storage.go 7.2 kB

2 years ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "path"
  6. "time"
  7. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  8. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  9. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  10. stgmod "gitlink.org.cn/cloudream/storage/common/models"
  11. "gitlink.org.cn/cloudream/storage/common/pkgs/db2/model"
  12. "gitlink.org.cn/cloudream/storage/common/pkgs/distlock/reqbuilder"
  13. "gitlink.org.cn/cloudream/storage/common/pkgs/downloader/strategy"
  14. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch2"
  15. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch2/parser"
  16. agtmq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/agent"
  17. coormq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/coordinator"
  18. "gitlink.org.cn/cloudream/storage/common/pkgs/storage/factory"
  19. )
  20. type StorageService struct {
  21. *Service
  22. }
  23. func (svc *Service) StorageSvc() *StorageService {
  24. return &StorageService{Service: svc}
  25. }
  26. func (svc *StorageService) Get(userID cdssdk.UserID, storageID cdssdk.StorageID) (*model.Storage, error) {
  27. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  28. if err != nil {
  29. return nil, fmt.Errorf("new coordinator client: %w", err)
  30. }
  31. defer stgglb.CoordinatorMQPool.Release(coorCli)
  32. getResp, err := coorCli.GetStorage(coormq.ReqGetStorage(userID, storageID))
  33. if err != nil {
  34. return nil, fmt.Errorf("request to coordinator: %w", err)
  35. }
  36. return &getResp.Storage, nil
  37. }
  38. func (svc *StorageService) GetByName(userID cdssdk.UserID, name string) (*model.Storage, error) {
  39. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  40. if err != nil {
  41. return nil, fmt.Errorf("new coordinator client: %w", err)
  42. }
  43. defer stgglb.CoordinatorMQPool.Release(coorCli)
  44. getResp, err := coorCli.GetStorageByName(coormq.ReqGetStorageByName(userID, name))
  45. if err != nil {
  46. return nil, fmt.Errorf("request to coordinator: %w", err)
  47. }
  48. return &getResp.Storage, nil
  49. }
  50. func (svc *StorageService) GetDetails(stgIDs []cdssdk.StorageID) ([]*stgmod.StorageDetail, error) {
  51. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  52. if err != nil {
  53. return nil, fmt.Errorf("new coordinator client: %w", err)
  54. }
  55. defer stgglb.CoordinatorMQPool.Release(coorCli)
  56. getResp, err := coorCli.GetStorageDetails(coormq.ReqGetStorageDetails(stgIDs))
  57. if err != nil {
  58. return nil, fmt.Errorf("request to coordinator: %w", err)
  59. }
  60. return getResp.Storages, nil
  61. }
  62. func (svc *StorageService) LoadPackage(userID cdssdk.UserID, packageID cdssdk.PackageID, storageID cdssdk.StorageID, rootPath string) error {
  63. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  64. if err != nil {
  65. return fmt.Errorf("new coordinator client: %w", err)
  66. }
  67. defer stgglb.CoordinatorMQPool.Release(coorCli)
  68. destStg := svc.StorageMeta.Get(storageID)
  69. if destStg == nil {
  70. return fmt.Errorf("storage not found: %d", storageID)
  71. }
  72. if destStg.MasterHub == nil {
  73. return fmt.Errorf("storage %v has no master hub", storageID)
  74. }
  75. details, err := coorCli.GetPackageObjectDetails(coormq.ReqGetPackageObjectDetails(packageID))
  76. if err != nil {
  77. return err
  78. }
  79. var pinned []cdssdk.ObjectID
  80. plans := exec.NewPlanBuilder()
  81. for _, obj := range details.Objects {
  82. strg, err := svc.StrategySelector.Select(strategy.Request{
  83. Detail: obj,
  84. DestHub: destStg.MasterHub.HubID,
  85. })
  86. if err != nil {
  87. return fmt.Errorf("select download strategy: %w", err)
  88. }
  89. ft := ioswitch2.NewFromTo()
  90. switch strg := strg.(type) {
  91. case *strategy.DirectStrategy:
  92. ft.AddFrom(ioswitch2.NewFromShardstore(strg.Detail.Object.FileHash, *strg.Storage.MasterHub, strg.Storage, ioswitch2.RawStream()))
  93. case *strategy.ECReconstructStrategy:
  94. for i, b := range strg.Blocks {
  95. ft.AddFrom(ioswitch2.NewFromShardstore(b.FileHash, *strg.Storages[i].MasterHub, strg.Storages[i], ioswitch2.ECStream(b.Index)))
  96. ft.ECParam = &strg.Redundancy
  97. }
  98. default:
  99. return fmt.Errorf("unsupported download strategy: %T", strg)
  100. }
  101. ft.AddTo(ioswitch2.NewLoadToPublic(*destStg.MasterHub, *destStg, path.Join(rootPath, obj.Object.Path)))
  102. // 顺便保存到同存储服务的分片存储中
  103. if factory.GetBuilder(*destStg).ShardStoreDesc().Enabled() {
  104. ft.AddTo(ioswitch2.NewToShardStore(*destStg.MasterHub, *destStg, ioswitch2.RawStream(), ""))
  105. pinned = append(pinned, obj.Object.ObjectID)
  106. }
  107. err = parser.Parse(ft, plans)
  108. if err != nil {
  109. return fmt.Errorf("parse plan: %w", err)
  110. }
  111. }
  112. mutex, err := reqbuilder.NewBuilder().
  113. // 保护在storage目录中下载的文件
  114. Storage().Buzy(storageID).
  115. // 保护下载文件时同时保存到IPFS的文件
  116. Shard().Buzy(storageID).
  117. MutexLock(svc.DistLock)
  118. if err != nil {
  119. return fmt.Errorf("acquire locks failed, err: %w", err)
  120. }
  121. // 记录访问统计
  122. for _, obj := range details.Objects {
  123. svc.AccessStat.AddAccessCounter(obj.Object.ObjectID, packageID, storageID, 1)
  124. }
  125. defer mutex.Unlock()
  126. drv := plans.Execute(exec.NewExecContext())
  127. _, err = drv.Wait(context.Background())
  128. if err != nil {
  129. return err
  130. }
  131. // 失败也没关系
  132. coorCli.StoragePackageLoaded(coormq.ReqStoragePackageLoaded(userID, storageID, packageID, rootPath, pinned))
  133. return nil
  134. }
  135. // 请求节点启动从Storage中上传文件的任务。会返回节点ID和任务ID
  136. func (svc *StorageService) StartStorageCreatePackage(userID cdssdk.UserID, bucketID cdssdk.BucketID, name string, storageID cdssdk.StorageID, path string, storageAffinity cdssdk.StorageID) (cdssdk.HubID, string, error) {
  137. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  138. if err != nil {
  139. return 0, "", fmt.Errorf("new coordinator client: %w", err)
  140. }
  141. defer stgglb.CoordinatorMQPool.Release(coorCli)
  142. stgResp, err := coorCli.GetStorageDetails(coormq.ReqGetStorageDetails([]cdssdk.StorageID{storageID}))
  143. if err != nil {
  144. return 0, "", fmt.Errorf("getting storage info: %w", err)
  145. }
  146. if stgResp.Storages[0].Storage.ShardStore == nil {
  147. return 0, "", fmt.Errorf("shard storage is not enabled")
  148. }
  149. agentCli, err := stgglb.AgentMQPool.Acquire(stgResp.Storages[0].MasterHub.HubID)
  150. if err != nil {
  151. return 0, "", fmt.Errorf("new agent client: %w", err)
  152. }
  153. defer stgglb.AgentMQPool.Release(agentCli)
  154. startResp, err := agentCli.StartStorageCreatePackage(agtmq.NewStartStorageCreatePackage(userID, bucketID, name, storageID, path, storageAffinity))
  155. if err != nil {
  156. return 0, "", fmt.Errorf("start storage upload package: %w", err)
  157. }
  158. return stgResp.Storages[0].MasterHub.HubID, startResp.TaskID, nil
  159. }
  160. func (svc *StorageService) WaitStorageCreatePackage(hubID cdssdk.HubID, taskID string, waitTimeout time.Duration) (bool, cdssdk.PackageID, error) {
  161. agentCli, err := stgglb.AgentMQPool.Acquire(hubID)
  162. if err != nil {
  163. // TODO 失败是否要当做任务已经结束?
  164. return true, 0, fmt.Errorf("new agent client: %w", err)
  165. }
  166. defer stgglb.AgentMQPool.Release(agentCli)
  167. waitResp, err := agentCli.WaitStorageCreatePackage(agtmq.NewWaitStorageCreatePackage(taskID, waitTimeout.Milliseconds()))
  168. if err != nil {
  169. // TODO 请求失败是否要当做任务已经结束?
  170. return true, 0, fmt.Errorf("wait storage upload package: %w", err)
  171. }
  172. if !waitResp.IsComplete {
  173. return false, 0, nil
  174. }
  175. if waitResp.Error != "" {
  176. return true, 0, fmt.Errorf("%s", waitResp.Error)
  177. }
  178. return true, waitResp.PackageID, nil
  179. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。