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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package task
  2. import (
  3. "fmt"
  4. "time"
  5. "gitlink.org.cn/cloudream/client/internal/config"
  6. "gitlink.org.cn/cloudream/common/pkg/distlock/reqbuilder"
  7. agtcli "gitlink.org.cn/cloudream/rabbitmq/client/agent"
  8. agtmsg "gitlink.org.cn/cloudream/rabbitmq/message/agent"
  9. coormsg "gitlink.org.cn/cloudream/rabbitmq/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. // 先向协调端请求文件相关的元数据
  53. preMoveResp, err := ctx.Coordinator.PreMoveObjectToStorage(coormsg.NewPreMoveObjectToStorage(t.objectID, t.storageID, t.userID))
  54. if err != nil {
  55. return fmt.Errorf("pre move object to storage: %w", err)
  56. }
  57. // 然后向代理端发送移动文件的请求
  58. agentClient, err := agtcli.NewClient(preMoveResp.NodeID, &config.Cfg().RabbitMQ)
  59. if err != nil {
  60. return fmt.Errorf("create agent client to %d failed, err: %w", preMoveResp.NodeID, err)
  61. }
  62. defer agentClient.Close()
  63. agentMoveResp, err := agentClient.StartStorageMoveObject(
  64. agtmsg.NewStartStorageMoveObject(preMoveResp.Directory,
  65. t.objectID,
  66. t.userID,
  67. preMoveResp.FileSize,
  68. preMoveResp.Redundancy,
  69. ))
  70. if err != nil {
  71. return fmt.Errorf("start moving object to storage: %w", err)
  72. }
  73. for {
  74. waitResp, err := agentClient.WaitStorageMoveObject(agtmsg.NewWaitStorageMoveObject(agentMoveResp.TaskID, int64(time.Second)*5))
  75. if err != nil {
  76. return fmt.Errorf("wait moving object: %w", err)
  77. }
  78. if waitResp.IsComplete {
  79. if waitResp.Error != "" {
  80. return fmt.Errorf("agent moving object: %s", waitResp.Error)
  81. }
  82. break
  83. }
  84. }
  85. _, err = ctx.Coordinator.MoveObjectToStorage(coormsg.NewMoveObjectToStorage(t.objectID, t.storageID, t.userID))
  86. if err != nil {
  87. return fmt.Errorf("moving object to storage: %w", err)
  88. }
  89. return nil
  90. }

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