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.

command_service_ec.go 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package services
  2. import (
  3. "database/sql"
  4. "errors"
  5. "github.com/jmoiron/sqlx"
  6. "gitlink.org.cn/cloudream/common/consts/errorcode"
  7. "gitlink.org.cn/cloudream/common/pkg/logger"
  8. ramsg "gitlink.org.cn/cloudream/rabbitmq/message"
  9. coormsg "gitlink.org.cn/cloudream/rabbitmq/message/coordinator"
  10. )
  11. func (svc *Service) PreUploadEcObject(msg *coormsg.PreUploadEcObject) (*coormsg.PreUploadEcResp, *ramsg.CodeMessage) {
  12. // 判断同名对象是否存在。等到UploadRepObject时再判断一次。
  13. // 此次的判断只作为参考,具体是否成功还是看UploadRepObject的结果
  14. isBucketAvai, err := svc.db.Bucket().IsAvailable(svc.db.SQLCtx(), msg.BucketID, msg.UserID)
  15. if err != nil {
  16. logger.WithField("BucketID", msg.BucketID).
  17. Warnf("check bucket available failed, err: %s", err.Error())
  18. return ramsg.ReplyFailed[coormsg.PreUploadEcResp](errorcode.OperationFailed, "check bucket available failed")
  19. }
  20. if !isBucketAvai {
  21. logger.WithField("BucketID", msg.BucketID).
  22. Warnf("bucket is not available to user")
  23. return ramsg.ReplyFailed[coormsg.PreUploadEcResp](errorcode.OperationFailed, "bucket is not available to user")
  24. }
  25. _, err = svc.db.Object().GetByName(svc.db.SQLCtx(), msg.BucketID, msg.ObjectName)
  26. if err == nil {
  27. logger.WithField("BucketID", msg.BucketID).
  28. WithField("ObjectName", msg.ObjectName).
  29. Warnf("object with given Name and BucketID already exists")
  30. return ramsg.ReplyFailed[coormsg.PreUploadEcResp](errorcode.OperationFailed, "object with given Name and BucketID already exists")
  31. }
  32. if !errors.Is(err, sql.ErrNoRows) {
  33. logger.WithField("BucketID", msg.BucketID).
  34. WithField("ObjectName", msg.ObjectName).
  35. Warnf("get object by name failed, err: %s", err.Error())
  36. return ramsg.ReplyFailed[coormsg.PreUploadEcResp](errorcode.OperationFailed, "get object by name failed")
  37. }
  38. //查询用户可用的节点IP
  39. nodes, err := svc.db.Node().GetUserNodes(svc.db.SQLCtx(), msg.UserID)
  40. if err != nil {
  41. logger.WithField("UserID", msg.UserID).
  42. Warnf("query user nodes failed, err: %s", err.Error())
  43. return ramsg.ReplyFailed[coormsg.PreUploadEcResp](errorcode.OperationFailed, "query user nodes failed")
  44. }
  45. // 查询客户端所属节点
  46. foundBelongNode := true
  47. belongNode, err := svc.db.Node().GetByExternalIP(svc.db.SQLCtx(), msg.ClientExternalIP)
  48. if err == sql.ErrNoRows {
  49. foundBelongNode = false
  50. } else if err != nil {
  51. logger.WithField("ClientExternalIP", msg.ClientExternalIP).
  52. Warnf("query client belong node failed, err: %s", err.Error())
  53. return ramsg.ReplyFailed[coormsg.PreUploadEcResp](errorcode.OperationFailed, "query client belong node failed")
  54. }
  55. var respNodes []ramsg.RespNode
  56. for _, node := range nodes {
  57. respNodes = append(respNodes, ramsg.NewRespNode(
  58. node.NodeID,
  59. node.ExternalIP,
  60. node.LocalIP,
  61. // LocationID 相同则认为是在同一个地域
  62. foundBelongNode && belongNode.LocationID == node.LocationID,
  63. ))
  64. }
  65. //查询纠删码参数
  66. ec, err := svc.db.Ec().GetEc(svc.db.SQLCtx(), msg.EcName)
  67. if err != nil {
  68. logger.WithField("Ec", msg.EcName).
  69. Warnf("check ec type failed, err: %s", err.Error())
  70. return ramsg.ReplyFailed[coormsg.PreUploadEcResp](errorcode.OperationFailed, "check bucket available failed")
  71. }
  72. ecc := ramsg.NewEc(ec.EcID, ec.Name, ec.EcK, ec.EcN)
  73. return ramsg.ReplyOK(coormsg.NewPreUploadEcResp(respNodes, ecc))
  74. }
  75. func (svc *Service) CreateEcObject(msg *coormsg.CreateEcObject) (*coormsg.CreateObjectResp, *ramsg.CodeMessage) {
  76. var objID int64
  77. err := svc.db.DoTx(sql.LevelDefault, func(tx *sqlx.Tx) error {
  78. var err error
  79. objID, err = svc.db.Object().CreateEcObject(tx, msg.BucketID, msg.ObjectName, msg.FileSize, msg.UserID, msg.NodeIDs, msg.Hashes, msg.EcName, msg.DirName)
  80. return err
  81. })
  82. if err != nil {
  83. logger.WithField("BucketName", msg.BucketID).
  84. WithField("ObjectName", msg.ObjectName).
  85. Warnf("create rep object failed, err: %s", err.Error())
  86. return ramsg.ReplyFailed[coormsg.CreateObjectResp](errorcode.OperationFailed, "create rep object failed")
  87. }
  88. return ramsg.ReplyOK(coormsg.NewCreateObjectResp(objID))
  89. }

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