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.

package.go 8.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package services
  2. import (
  3. "fmt"
  4. "time"
  5. "gitlink.org.cn/cloudream/common/models"
  6. "gitlink.org.cn/cloudream/common/utils/serder"
  7. "gitlink.org.cn/cloudream/storage-client/internal/config"
  8. mytask "gitlink.org.cn/cloudream/storage-client/internal/task"
  9. "gitlink.org.cn/cloudream/storage-common/globals"
  10. agtcmd "gitlink.org.cn/cloudream/storage-common/pkgs/cmd"
  11. "gitlink.org.cn/cloudream/storage-common/pkgs/db/model"
  12. "gitlink.org.cn/cloudream/storage-common/pkgs/distlock/reqbuilder"
  13. "gitlink.org.cn/cloudream/storage-common/pkgs/iterator"
  14. coormq "gitlink.org.cn/cloudream/storage-common/pkgs/mq/coordinator"
  15. )
  16. type PackageService struct {
  17. *Service
  18. }
  19. func (svc *Service) PackageSvc() *PackageService {
  20. return &PackageService{Service: svc}
  21. }
  22. func (svc *PackageService) DownloadPackage(userID int64, packageID int64) (iterator.DownloadingObjectIterator, error) {
  23. coorCli, err := globals.CoordinatorMQPool.Acquire()
  24. if err != nil {
  25. return nil, fmt.Errorf("new coordinator client: %w", err)
  26. }
  27. defer coorCli.Close()
  28. mutex, err := reqbuilder.NewBuilder().
  29. // 用于判断用户是否有对象权限
  30. Metadata().UserBucket().ReadAny().
  31. // 用于查询可用的下载节点
  32. Node().ReadAny().
  33. // 用于读取包信息
  34. Package().ReadOne(packageID).
  35. // 用于读取包内的文件信息
  36. Object().ReadAny().
  37. // 用于查询Rep配置
  38. ObjectRep().ReadAny().
  39. // 用于查询Block配置
  40. ObjectBlock().ReadAny().
  41. // 用于查询包含了副本的节点
  42. Cache().ReadAny().
  43. MutexLock(svc.DistLock)
  44. if err != nil {
  45. return nil, fmt.Errorf("acquire locks failed, err: %w", err)
  46. }
  47. getPkgResp, err := coorCli.GetPackage(coormq.NewGetPackage(userID, packageID))
  48. if err != nil {
  49. return nil, fmt.Errorf("getting package: %w", err)
  50. }
  51. getObjsResp, err := coorCli.GetPackageObjects(coormq.NewGetPackageObjects(userID, packageID))
  52. if err != nil {
  53. return nil, fmt.Errorf("getting package objects: %w", err)
  54. }
  55. if getPkgResp.Redundancy.Type == models.RedundancyRep {
  56. iter, err := svc.downloadRepPackage(packageID, getObjsResp.Objects, coorCli)
  57. if err != nil {
  58. mutex.Unlock()
  59. return nil, err
  60. }
  61. iter.OnClosing = func() {
  62. mutex.Unlock()
  63. }
  64. return iter, nil
  65. } else {
  66. iter, err := svc.downloadECPackage(getPkgResp.Package, getObjsResp.Objects, coorCli)
  67. if err != nil {
  68. mutex.Unlock()
  69. return nil, err
  70. }
  71. iter.OnClosing = func() {
  72. mutex.Unlock()
  73. }
  74. return iter, nil
  75. }
  76. }
  77. func (svc *PackageService) downloadRepPackage(packageID int64, objects []model.Object, coorCli *coormq.PoolClient) (*iterator.RepObjectIterator, error) {
  78. getObjRepDataResp, err := coorCli.GetPackageObjectRepData(coormq.NewGetPackageObjectRepData(packageID))
  79. if err != nil {
  80. return nil, fmt.Errorf("getting package object rep data: %w", err)
  81. }
  82. iter := iterator.NewRepObjectIterator(objects, getObjRepDataResp.Data, &iterator.DownloadContext{
  83. Distlock: svc.DistLock,
  84. })
  85. return iter, nil
  86. }
  87. func (svc *PackageService) downloadECPackage(pkg model.Package, objects []model.Object, coorCli *coormq.PoolClient) (*iterator.ECObjectIterator, error) {
  88. getObjECDataResp, err := coorCli.GetPackageObjectECData(coormq.NewGetPackageObjectECData(pkg.PackageID))
  89. if err != nil {
  90. return nil, fmt.Errorf("getting package object ec data: %w", err)
  91. }
  92. var ecRed models.ECRedundancyInfo
  93. if err := serder.AnyToAny(pkg.Redundancy.Info, &ecRed); err != nil {
  94. return nil, fmt.Errorf("get ec redundancy info: %w", err)
  95. }
  96. getECResp, err := coorCli.GetECConfig(coormq.NewGetECConfig(ecRed.ECName))
  97. if err != nil {
  98. return nil, fmt.Errorf("getting ec: %w", err)
  99. }
  100. iter := iterator.NewECObjectIterator(objects, getObjECDataResp.Data, getECResp.Config, &iterator.ECDownloadContext{
  101. DownloadContext: &iterator.DownloadContext{
  102. Distlock: svc.DistLock,
  103. },
  104. ECPacketSize: config.Cfg().ECPacketSize,
  105. })
  106. return iter, nil
  107. }
  108. func (svc *PackageService) StartCreatingRepPackage(userID int64, bucketID int64, name string, objIter iterator.UploadingObjectIterator, repInfo models.RepRedundancyInfo) (string, error) {
  109. tsk := svc.TaskMgr.StartNew(mytask.NewCreateRepPackage(userID, bucketID, name, objIter, repInfo))
  110. return tsk.ID(), nil
  111. }
  112. func (svc *PackageService) WaitCreatingRepPackage(taskID string, waitTimeout time.Duration) (bool, *mytask.CreateRepPackageResult, error) {
  113. tsk := svc.TaskMgr.FindByID(taskID)
  114. if tsk.WaitTimeout(waitTimeout) {
  115. cteatePkgTask := tsk.Body().(*mytask.CreateRepPackage)
  116. return true, cteatePkgTask.Result, tsk.Error()
  117. }
  118. return false, nil, nil
  119. }
  120. func (svc *PackageService) StartUpdatingRepPackage(userID int64, packageID int64, objIter iterator.UploadingObjectIterator) (string, error) {
  121. tsk := svc.TaskMgr.StartNew(mytask.NewUpdateRepPackage(userID, packageID, objIter))
  122. return tsk.ID(), nil
  123. }
  124. func (svc *PackageService) WaitUpdatingRepPackage(taskID string, waitTimeout time.Duration) (bool, *agtcmd.UpdateRepPackageResult, error) {
  125. tsk := svc.TaskMgr.FindByID(taskID)
  126. if tsk.WaitTimeout(waitTimeout) {
  127. updatePkgTask := tsk.Body().(*mytask.UpdateRepPackage)
  128. return true, updatePkgTask.Result, tsk.Error()
  129. }
  130. return false, nil, nil
  131. }
  132. func (svc *PackageService) StartCreatingECPackage(userID int64, bucketID int64, name string, objIter iterator.UploadingObjectIterator, ecInfo models.ECRedundancyInfo) (string, error) {
  133. tsk := svc.TaskMgr.StartNew(mytask.NewCreateECPackage(userID, bucketID, name, objIter, ecInfo))
  134. return tsk.ID(), nil
  135. }
  136. func (svc *PackageService) WaitCreatingECPackage(taskID string, waitTimeout time.Duration) (bool, *agtcmd.CreateRepPackageResult, error) {
  137. tsk := svc.TaskMgr.FindByID(taskID)
  138. if tsk.WaitTimeout(waitTimeout) {
  139. cteatePkgTask := tsk.Body().(*mytask.CreateRepPackage)
  140. return true, cteatePkgTask.Result, tsk.Error()
  141. }
  142. return false, nil, nil
  143. }
  144. func (svc *PackageService) StartUpdatingECPackage(userID int64, packageID int64, objIter iterator.UploadingObjectIterator) (string, error) {
  145. tsk := svc.TaskMgr.StartNew(mytask.NewUpdateECPackage(userID, packageID, objIter))
  146. return tsk.ID(), nil
  147. }
  148. func (svc *PackageService) WaitUpdatingECPackage(taskID string, waitTimeout time.Duration) (bool, *agtcmd.UpdateECPackageResult, error) {
  149. tsk := svc.TaskMgr.FindByID(taskID)
  150. if tsk.WaitTimeout(waitTimeout) {
  151. updatePkgTask := tsk.Body().(*mytask.UpdateECPackage)
  152. return true, updatePkgTask.Result, tsk.Error()
  153. }
  154. return false, nil, nil
  155. }
  156. func (svc *PackageService) DeletePackage(userID int64, packageID int64) error {
  157. coorCli, err := globals.CoordinatorMQPool.Acquire()
  158. if err != nil {
  159. return fmt.Errorf("new coordinator client: %w", err)
  160. }
  161. defer coorCli.Close()
  162. mutex, err := reqbuilder.NewBuilder().
  163. Metadata().
  164. // 用于判断用户是否有对象的权限
  165. UserBucket().ReadAny().
  166. // 用于读取、修改包信息
  167. Package().WriteOne(packageID).
  168. // 用于删除包内的所有文件
  169. Object().WriteAny().
  170. // 用于删除Rep配置
  171. ObjectRep().WriteAny().
  172. // 用于删除Block配置
  173. ObjectBlock().WriteAny().
  174. // 用于修改Move此Object的记录的状态
  175. StoragePackage().WriteAny().
  176. MutexLock(svc.DistLock)
  177. if err != nil {
  178. return fmt.Errorf("acquire locks failed, err: %w", err)
  179. }
  180. defer mutex.Unlock()
  181. _, err = coorCli.DeletePackage(coormq.NewDeletePackage(userID, packageID))
  182. if err != nil {
  183. return fmt.Errorf("deleting package: %w", err)
  184. }
  185. return nil
  186. }
  187. func (svc *PackageService) GetCacheNodesByPackage(userID int64, packageID int64) ([]int64, string, error) {
  188. coorCli, err := globals.CoordinatorMQPool.Acquire()
  189. if err != nil {
  190. return nil, "", fmt.Errorf("new coordinator client: %w", err)
  191. }
  192. defer coorCli.Close()
  193. resp, err := coorCli.GetCacheNodesByPackage(coormq.NewGetCacheNodesByPackage(userID, packageID))
  194. if err != nil {
  195. return nil, "", fmt.Errorf("get node by package: %w", err)
  196. }
  197. return resp.NodeIDs, resp.RedundancyType, nil
  198. }
  199. func (svc *PackageService) GetStorageNodesByPackage(userID int64, packageID int64) ([]int64, error) {
  200. coorCli, err := globals.CoordinatorMQPool.Acquire()
  201. if err != nil {
  202. return nil, fmt.Errorf("new coordinator client: %w", err)
  203. }
  204. defer coorCli.Close()
  205. resp, err := coorCli.GetStorageNodesByPackage(coormq.NewGetStorageNodesByPackage(userID, packageID))
  206. if err != nil {
  207. return nil, fmt.Errorf("get node by package: %w", err)
  208. }
  209. return resp.NodeIDs, nil
  210. }

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