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 4.6 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package services
  2. import (
  3. "fmt"
  4. "time"
  5. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  6. mytask "gitlink.org.cn/cloudream/storage/client/internal/task"
  7. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  8. agtcmd "gitlink.org.cn/cloudream/storage/common/pkgs/cmd"
  9. "gitlink.org.cn/cloudream/storage/common/pkgs/db/model"
  10. "gitlink.org.cn/cloudream/storage/common/pkgs/iterator"
  11. coormq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/coordinator"
  12. )
  13. type PackageService struct {
  14. *Service
  15. }
  16. func (svc *Service) PackageSvc() *PackageService {
  17. return &PackageService{Service: svc}
  18. }
  19. func (svc *PackageService) Get(userID cdssdk.UserID, packageID cdssdk.PackageID) (*model.Package, error) {
  20. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  21. if err != nil {
  22. return nil, fmt.Errorf("new coordinator client: %w", err)
  23. }
  24. defer stgglb.CoordinatorMQPool.Release(coorCli)
  25. getResp, err := coorCli.GetPackage(coormq.NewGetPackage(userID, packageID))
  26. if err != nil {
  27. return nil, fmt.Errorf("requsting to coodinator: %w", err)
  28. }
  29. return &getResp.Package, nil
  30. }
  31. func (svc *PackageService) DownloadPackage(userID cdssdk.UserID, packageID cdssdk.PackageID) (iterator.DownloadingObjectIterator, error) {
  32. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  33. if err != nil {
  34. return nil, fmt.Errorf("new coordinator client: %w", err)
  35. }
  36. defer stgglb.CoordinatorMQPool.Release(coorCli)
  37. getObjsResp, err := coorCli.GetPackageObjectDetails(coormq.NewGetPackageObjectDetails(packageID))
  38. if err != nil {
  39. return nil, fmt.Errorf("getting package object details: %w", err)
  40. }
  41. iter := iterator.NewDownloadObjectIterator(getObjsResp.Objects, &iterator.DownloadContext{
  42. Distlock: svc.DistLock,
  43. })
  44. return iter, nil
  45. }
  46. func (svc *PackageService) StartCreatingPackage(userID cdssdk.UserID, bucketID cdssdk.BucketID, name string, objIter iterator.UploadingObjectIterator, nodeAffinity *cdssdk.NodeID) (string, error) {
  47. tsk := svc.TaskMgr.StartNew(mytask.NewCreatePackage(userID, bucketID, name, objIter, nodeAffinity))
  48. return tsk.ID(), nil
  49. }
  50. func (svc *PackageService) WaitCreatingPackage(taskID string, waitTimeout time.Duration) (bool, *agtcmd.CreatePackageResult, error) {
  51. tsk := svc.TaskMgr.FindByID(taskID)
  52. if tsk.WaitTimeout(waitTimeout) {
  53. cteatePkgTask := tsk.Body().(*mytask.CreatePackage)
  54. return true, cteatePkgTask.Result, tsk.Error()
  55. }
  56. return false, nil, nil
  57. }
  58. func (svc *PackageService) StartUpdatingPackage(userID cdssdk.UserID, packageID cdssdk.PackageID, objIter iterator.UploadingObjectIterator) (string, error) {
  59. tsk := svc.TaskMgr.StartNew(mytask.NewUpdatePackage(userID, packageID, objIter))
  60. return tsk.ID(), nil
  61. }
  62. func (svc *PackageService) WaitUpdatingPackage(taskID string, waitTimeout time.Duration) (bool, *agtcmd.UpdatePackageResult, error) {
  63. tsk := svc.TaskMgr.FindByID(taskID)
  64. if tsk.WaitTimeout(waitTimeout) {
  65. updatePkgTask := tsk.Body().(*mytask.UpdatePackage)
  66. return true, updatePkgTask.Result, tsk.Error()
  67. }
  68. return false, nil, nil
  69. }
  70. func (svc *PackageService) DeletePackage(userID cdssdk.UserID, packageID cdssdk.PackageID) error {
  71. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  72. if err != nil {
  73. return fmt.Errorf("new coordinator client: %w", err)
  74. }
  75. defer stgglb.CoordinatorMQPool.Release(coorCli)
  76. _, err = coorCli.DeletePackage(coormq.NewDeletePackage(userID, packageID))
  77. if err != nil {
  78. return fmt.Errorf("deleting package: %w", err)
  79. }
  80. return nil
  81. }
  82. func (svc *PackageService) GetCachedNodes(userID cdssdk.UserID, packageID cdssdk.PackageID) (cdssdk.PackageCachingInfo, error) {
  83. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  84. if err != nil {
  85. return cdssdk.PackageCachingInfo{}, fmt.Errorf("new coordinator client: %w", err)
  86. }
  87. defer stgglb.CoordinatorMQPool.Release(coorCli)
  88. resp, err := coorCli.GetPackageCachedNodes(coormq.NewGetPackageCachedNodes(userID, packageID))
  89. if err != nil {
  90. return cdssdk.PackageCachingInfo{}, fmt.Errorf("get package cached nodes: %w", err)
  91. }
  92. tmp := cdssdk.PackageCachingInfo{
  93. NodeInfos: resp.NodeInfos,
  94. PackageSize: resp.PackageSize,
  95. }
  96. return tmp, nil
  97. }
  98. func (svc *PackageService) GetLoadedNodes(userID cdssdk.UserID, packageID cdssdk.PackageID) ([]cdssdk.NodeID, error) {
  99. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  100. if err != nil {
  101. return nil, fmt.Errorf("new coordinator client: %w", err)
  102. }
  103. defer stgglb.CoordinatorMQPool.Release(coorCli)
  104. resp, err := coorCli.GetPackageLoadedNodes(coormq.NewGetPackageLoadedNodes(userID, packageID))
  105. if err != nil {
  106. return nil, fmt.Errorf("get package loaded nodes: %w", err)
  107. }
  108. return resp.NodeIDs, nil
  109. }

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