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.

agent_check_cache.go 4.7 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package event
  2. import (
  3. "database/sql"
  4. "time"
  5. "github.com/jmoiron/sqlx"
  6. "github.com/samber/lo"
  7. "gitlink.org.cn/cloudream/common/pkgs/logger"
  8. "gitlink.org.cn/cloudream/common/pkgs/mq"
  9. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  10. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  11. agtmq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/agent"
  12. scevt "gitlink.org.cn/cloudream/storage/common/pkgs/mq/scanner/event"
  13. )
  14. type AgentCheckCache struct {
  15. *scevt.AgentCheckCache
  16. }
  17. func NewAgentCheckCache(evt *scevt.AgentCheckCache) *AgentCheckCache {
  18. return &AgentCheckCache{
  19. AgentCheckCache: evt,
  20. }
  21. }
  22. func (t *AgentCheckCache) TryMerge(other Event) bool {
  23. event, ok := other.(*AgentCheckCache)
  24. if !ok {
  25. return false
  26. }
  27. if event.NodeID != t.NodeID {
  28. return false
  29. }
  30. return true
  31. }
  32. func (t *AgentCheckCache) Execute(execCtx ExecuteContext) {
  33. log := logger.WithType[AgentCheckCache]("Event")
  34. log.Debugf("begin with %v", logger.FormatStruct(t.AgentCheckCache))
  35. defer log.Debugf("end")
  36. // TODO unavailable的节点需不需要发送任务?
  37. agtCli, err := stgglb.AgentMQPool.Acquire(t.NodeID)
  38. if err != nil {
  39. log.WithField("NodeID", t.NodeID).Warnf("create agent client failed, err: %s", err.Error())
  40. return
  41. }
  42. defer stgglb.AgentMQPool.Release(agtCli)
  43. checkResp, err := agtCli.CheckCache(agtmq.NewCheckCache(), mq.RequestOption{Timeout: time.Minute})
  44. if err != nil {
  45. log.WithField("NodeID", t.NodeID).Warnf("checking ipfs: %s", err.Error())
  46. return
  47. }
  48. realFileHashes := lo.SliceToMap(checkResp.FileHashes, func(hash string) (string, bool) { return hash, true })
  49. // 根据IPFS中实际文件情况修改元数据。修改过程中的失败均忽略。(但关联修改需要原子性)
  50. execCtx.Args.DB.DoTx(sql.LevelSerializable, func(tx *sqlx.Tx) error {
  51. t.checkCache(execCtx, tx, realFileHashes)
  52. t.checkPinnedObject(execCtx, tx, realFileHashes)
  53. t.checkObjectBlock(execCtx, tx, realFileHashes)
  54. return nil
  55. })
  56. }
  57. // 对比Cache表中的记录,多了增加,少了删除
  58. func (t *AgentCheckCache) checkCache(execCtx ExecuteContext, tx *sqlx.Tx, realFileHashes map[string]bool) {
  59. log := logger.WithType[AgentCheckCache]("Event")
  60. caches, err := execCtx.Args.DB.Cache().GetByNodeID(tx, t.NodeID)
  61. if err != nil {
  62. log.WithField("NodeID", t.NodeID).Warnf("getting caches by node id: %s", err.Error())
  63. return
  64. }
  65. realFileHashesCp := make(map[string]bool)
  66. for k, v := range realFileHashes {
  67. realFileHashesCp[k] = v
  68. }
  69. var rms []string
  70. for _, c := range caches {
  71. if realFileHashesCp[c.FileHash] {
  72. // Cache表使用FileHash和NodeID作为主键,
  73. // 所以通过同一个NodeID查询的结果不会存在两条相同FileHash的情况
  74. delete(realFileHashesCp, c.FileHash)
  75. continue
  76. }
  77. rms = append(rms, c.FileHash)
  78. }
  79. if len(rms) > 0 {
  80. err = execCtx.Args.DB.Cache().NodeBatchDelete(tx, t.NodeID, rms)
  81. if err != nil {
  82. log.Warnf("batch delete node caches: %w", err.Error())
  83. }
  84. }
  85. if len(realFileHashesCp) > 0 {
  86. err = execCtx.Args.DB.Cache().BatchCreateOnSameNode(tx, lo.Keys(realFileHashesCp), t.NodeID, 0)
  87. if err != nil {
  88. log.Warnf("batch create node caches: %w", err)
  89. return
  90. }
  91. }
  92. }
  93. // 对比PinnedObject表,多了不变,少了删除
  94. func (t *AgentCheckCache) checkPinnedObject(execCtx ExecuteContext, tx *sqlx.Tx, realFileHashes map[string]bool) {
  95. log := logger.WithType[AgentCheckCache]("Event")
  96. objs, err := execCtx.Args.DB.PinnedObject().GetObjectsByNodeID(tx, t.NodeID)
  97. if err != nil {
  98. log.WithField("NodeID", t.NodeID).Warnf("getting pinned objects by node id: %s", err.Error())
  99. return
  100. }
  101. var rms []cdssdk.ObjectID
  102. for _, c := range objs {
  103. if realFileHashes[c.FileHash] {
  104. continue
  105. }
  106. rms = append(rms, c.ObjectID)
  107. }
  108. if len(rms) > 0 {
  109. err = execCtx.Args.DB.PinnedObject().NodeBatchDelete(tx, t.NodeID, rms)
  110. if err != nil {
  111. log.Warnf("batch delete node pinned objects: %s", err.Error())
  112. }
  113. }
  114. }
  115. // 对比ObjectBlock表,多了不变,少了删除
  116. func (t *AgentCheckCache) checkObjectBlock(execCtx ExecuteContext, tx *sqlx.Tx, realFileHashes map[string]bool) {
  117. log := logger.WithType[AgentCheckCache]("Event")
  118. blocks, err := execCtx.Args.DB.ObjectBlock().GetByNodeID(tx, t.NodeID)
  119. if err != nil {
  120. log.WithField("NodeID", t.NodeID).Warnf("getting object blocks by node id: %s", err.Error())
  121. return
  122. }
  123. var rms []string
  124. for _, b := range blocks {
  125. if realFileHashes[b.FileHash] {
  126. continue
  127. }
  128. rms = append(rms, b.FileHash)
  129. }
  130. if len(rms) > 0 {
  131. err = execCtx.Args.DB.ObjectBlock().NodeBatchDelete(tx, t.NodeID, rms)
  132. if err != nil {
  133. log.Warnf("batch delete node object blocks: %s", err.Error())
  134. }
  135. }
  136. }
  137. func init() {
  138. RegisterMessageConvertor(NewAgentCheckCache)
  139. }

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