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.

storage.go 2.3 kB

2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package rpc
  2. import (
  3. "context"
  4. "gitlink.org.cn/cloudream/common/consts/errorcode"
  5. "gitlink.org.cn/cloudream/common/pkgs/logger"
  6. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc"
  7. corrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/coordinator"
  8. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  9. "gitlink.org.cn/cloudream/jcs-pub/coordinator/internal/db"
  10. )
  11. func (svc *Service) SelectStorageHub(ctx context.Context, msg *corrpc.SelectStorageHub) (*corrpc.SelectStorageHubResp, *rpc.CodeError) {
  12. d := svc.db
  13. resp, err := db.DoTx01(d, func(tx db.SQLContext) ([]*jcstypes.Hub, error) {
  14. allLoc, err := d.HubLocation().GetAll(tx)
  15. if err != nil {
  16. return nil, err
  17. }
  18. stgHubIDs := make([]jcstypes.HubID, 0, len(msg.Storages))
  19. for _, stg := range msg.Storages {
  20. var matchedHubID jcstypes.HubID
  21. switch stg := stg.(type) {
  22. case *jcstypes.PubShardsType:
  23. matchedHubID = stg.MasterHub
  24. default:
  25. stgLoc := stg.GetLocation()
  26. var matchedScore int
  27. for _, loc := range allLoc {
  28. sc := matchLocation(stgLoc, loc)
  29. if sc > matchedScore {
  30. matchedScore = sc
  31. matchedHubID = loc.HubID
  32. }
  33. }
  34. }
  35. stgHubIDs = append(stgHubIDs, matchedHubID)
  36. }
  37. hubs, err := d.Hub().BatchGetByID(tx, stgHubIDs)
  38. if err != nil {
  39. return nil, err
  40. }
  41. hubMap := make(map[jcstypes.HubID]*jcstypes.Hub)
  42. for _, hub := range hubs {
  43. h := hub
  44. hubMap[hub.HubID] = &h
  45. }
  46. resp := make([]*jcstypes.Hub, len(msg.Storages))
  47. for i := range msg.Storages {
  48. resp[i] = hubMap[stgHubIDs[i]]
  49. }
  50. return resp, nil
  51. })
  52. if err != nil {
  53. logger.Warnf("select storage hubs: %s", err.Error())
  54. return nil, rpc.Failed(errorcode.OperationFailed, "%v", err)
  55. }
  56. return &corrpc.SelectStorageHubResp{
  57. Hubs: resp,
  58. }, nil
  59. }
  60. // 匹配规则:
  61. // 1. 按照StorageName、Location顺序检查StorageLocation和HubLocation
  62. // 2. "*"代表通配符,匹配任意值,如果匹配到了通配,那么就直接结束匹配
  63. // 3. 匹配越精确,分数越高
  64. func matchLocation(loc jcstypes.Location, hubLoc jcstypes.HubLocation) int {
  65. if hubLoc.StorageName == "*" {
  66. return 1
  67. }
  68. if hubLoc.StorageName != loc.StorageName {
  69. return 0
  70. }
  71. if hubLoc.Location == "*" {
  72. return 2
  73. }
  74. if hubLoc.Location == loc.Location {
  75. return 3
  76. }
  77. return 0
  78. }

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