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.

move_object_to_storage.go 3.2 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package task
  2. import (
  3. "fmt"
  4. "time"
  5. "gitlink.org.cn/cloudream/common/pkgs/distlock/reqbuilder"
  6. "gitlink.org.cn/cloudream/storage-client/internal/config"
  7. agtcli "gitlink.org.cn/cloudream/storage-common/pkgs/mq/client/agent"
  8. agtmsg "gitlink.org.cn/cloudream/storage-common/pkgs/mq/message/agent"
  9. coormsg "gitlink.org.cn/cloudream/storage-common/pkgs/mq/message/coordinator"
  10. )
  11. type MoveObjectToStorage struct {
  12. userID int64
  13. objectID int64
  14. storageID int64
  15. }
  16. func NewMoveObjectToStorage(userID int64, objectID int64, storageID int64) *MoveObjectToStorage {
  17. return &MoveObjectToStorage{
  18. userID: userID,
  19. objectID: objectID,
  20. storageID: storageID,
  21. }
  22. }
  23. func (t *MoveObjectToStorage) Execute(ctx TaskContext, complete CompleteFn) {
  24. err := t.do(ctx)
  25. complete(err, CompleteOption{
  26. RemovingDelay: time.Minute,
  27. })
  28. }
  29. func (t *MoveObjectToStorage) do(ctx TaskContext) error {
  30. mutex, err := reqbuilder.NewBuilder().
  31. Metadata().
  32. // 用于判断用户是否有Storage权限
  33. UserStorage().ReadOne(t.objectID, t.storageID).
  34. // 用于判断用户是否有对象权限
  35. UserBucket().ReadAny().
  36. // 用于读取对象信息
  37. Object().ReadOne(t.objectID).
  38. // 用于查询Rep配置
  39. ObjectRep().ReadOne(t.objectID).
  40. // 用于查询Block配置
  41. ObjectBlock().ReadAny().
  42. // 用于创建Move记录
  43. StorageObject().CreateOne(t.storageID, t.userID, t.objectID).
  44. Storage().
  45. // 用于创建对象文件
  46. CreateOneObject(t.storageID, t.userID, t.objectID).
  47. MutexLock(ctx.DistLock)
  48. if err != nil {
  49. return fmt.Errorf("acquire locks failed, err: %w", err)
  50. }
  51. defer mutex.Unlock()
  52. err = moveSingleObjectToStorage(ctx, t.userID, t.objectID, t.storageID)
  53. return err
  54. }
  55. func moveSingleObjectToStorage(ctx TaskContext, userID int64, objectID int64, storageID int64) error {
  56. // 先向协调端请求文件相关的元数据
  57. preMoveResp, err := ctx.Coordinator.PreMoveObjectToStorage(coormsg.NewPreMoveObjectToStorage(objectID, storageID, userID))
  58. if err != nil {
  59. return fmt.Errorf("pre move object to storage: %w", err)
  60. }
  61. // 然后向代理端发送移动文件的请求
  62. agentClient, err := agtcli.NewClient(preMoveResp.NodeID, &config.Cfg().RabbitMQ)
  63. if err != nil {
  64. return fmt.Errorf("create agent client to %d failed, err: %w", preMoveResp.NodeID, err)
  65. }
  66. defer agentClient.Close()
  67. agentMoveResp, err := agentClient.StartStorageMoveObject(
  68. agtmsg.NewStartStorageMoveObject(preMoveResp.Directory,
  69. objectID,
  70. preMoveResp.Object.Name,
  71. userID,
  72. preMoveResp.Object.FileSize,
  73. preMoveResp.Redundancy,
  74. ))
  75. if err != nil {
  76. return fmt.Errorf("start moving object to storage: %w", err)
  77. }
  78. for {
  79. waitResp, err := agentClient.WaitStorageMoveObject(agtmsg.NewWaitStorageMoveObject(agentMoveResp.TaskID, int64(time.Second)*5))
  80. if err != nil {
  81. return fmt.Errorf("wait moving object: %w", err)
  82. }
  83. if waitResp.IsComplete {
  84. if waitResp.Error != "" {
  85. return fmt.Errorf("agent moving object: %s", waitResp.Error)
  86. }
  87. break
  88. }
  89. }
  90. _, err = ctx.Coordinator.MoveObjectToStorage(coormsg.NewMoveObjectToStorage(objectID, storageID, userID))
  91. if err != nil {
  92. return fmt.Errorf("moving object to storage: %w", err)
  93. }
  94. return nil
  95. }

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