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_storage.go 6.3 kB

2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package event
  2. import (
  3. "database/sql"
  4. "time"
  5. "github.com/samber/lo"
  6. "gitlink.org.cn/cloudream/common/pkgs/distlock/reqbuilder"
  7. "gitlink.org.cn/cloudream/common/pkgs/logger"
  8. "gitlink.org.cn/cloudream/common/pkgs/mq"
  9. "gitlink.org.cn/cloudream/storage-common/consts"
  10. "gitlink.org.cn/cloudream/storage-common/pkgs/db/model"
  11. agtcli "gitlink.org.cn/cloudream/storage-common/pkgs/mq/client/agent"
  12. agtmsg "gitlink.org.cn/cloudream/storage-common/pkgs/mq/message/agent"
  13. scevt "gitlink.org.cn/cloudream/storage-common/pkgs/mq/message/scanner/event"
  14. "gitlink.org.cn/cloudream/storage-scanner/internal/config"
  15. )
  16. type AgentCheckStorage struct {
  17. scevt.AgentCheckStorage
  18. }
  19. func NewAgentCheckStorage(storageID int64, objectIDs []int64) *AgentCheckStorage {
  20. return &AgentCheckStorage{
  21. AgentCheckStorage: scevt.NewAgentCheckStorage(storageID, objectIDs),
  22. }
  23. }
  24. func (t *AgentCheckStorage) TryMerge(other Event) bool {
  25. event, ok := other.(*AgentCheckStorage)
  26. if !ok {
  27. return false
  28. }
  29. if t.StorageID != event.StorageID {
  30. return false
  31. }
  32. // ObjectIDs为nil时代表全量检查
  33. if event.ObjectIDs == nil {
  34. t.ObjectIDs = nil
  35. } else if t.ObjectIDs != nil {
  36. t.ObjectIDs = lo.Union(t.ObjectIDs, event.ObjectIDs)
  37. }
  38. return true
  39. }
  40. func (t *AgentCheckStorage) Execute(execCtx ExecuteContext) {
  41. log := logger.WithType[AgentCheckStorage]("Event")
  42. log.Debugf("begin with %v", logger.FormatStruct(t))
  43. defer log.Debugf("end")
  44. // 读取数据的地方就不加锁了,因为check任务会反复执行,单次失败问题不大
  45. stg, err := execCtx.Args.DB.Storage().GetByID(execCtx.Args.DB.SQLCtx(), t.StorageID)
  46. if err != nil {
  47. if err != sql.ErrNoRows {
  48. log.WithField("StorageID", t.StorageID).Warnf("get storage failed, err: %s", err.Error())
  49. }
  50. return
  51. }
  52. node, err := execCtx.Args.DB.Node().GetByID(execCtx.Args.DB.SQLCtx(), stg.NodeID)
  53. if err != nil {
  54. if err != sql.ErrNoRows {
  55. log.WithField("StorageID", t.StorageID).Warnf("get storage node failed, err: %s", err.Error())
  56. }
  57. return
  58. }
  59. // TODO unavailable的节点需不需要发送任务?
  60. if node.State != consts.NodeStateNormal {
  61. return
  62. }
  63. if t.ObjectIDs == nil {
  64. t.checkComplete(execCtx, stg)
  65. } else {
  66. t.checkIncrement(execCtx, stg)
  67. }
  68. }
  69. func (t *AgentCheckStorage) checkComplete(execCtx ExecuteContext, stg model.Storage) {
  70. log := logger.WithType[AgentCheckStorage]("Event")
  71. mutex, err := reqbuilder.NewBuilder().
  72. Metadata().
  73. // 全量模式下查询、修改Move记录
  74. StorageObject().WriteAny().
  75. Storage().
  76. // 全量模式下删除对象文件
  77. WriteAnyObject(t.StorageID).
  78. MutexLock(execCtx.Args.DistLock)
  79. if err != nil {
  80. log.Warnf("acquire locks failed, err: %s", err.Error())
  81. return
  82. }
  83. defer mutex.Unlock()
  84. objects, err := execCtx.Args.DB.StorageObject().GetAllByStorageID(execCtx.Args.DB.SQLCtx(), t.StorageID)
  85. if err != nil {
  86. log.WithField("StorageID", t.StorageID).Warnf("get storage objects failed, err: %s", err.Error())
  87. return
  88. }
  89. t.startCheck(execCtx, stg, true, objects)
  90. }
  91. func (t *AgentCheckStorage) checkIncrement(execCtx ExecuteContext, stg model.Storage) {
  92. log := logger.WithType[AgentCheckStorage]("Event")
  93. mutex, err := reqbuilder.NewBuilder().
  94. Metadata().
  95. // 全量模式下查询、修改Move记录。因为可能有多个User Move相同的文件,所以只能用集合Write锁
  96. StorageObject().WriteAny().
  97. Storage().
  98. // 全量模式下删除对象文件。因为可能有多个User Move相同的文件,所以只能用集合Write锁
  99. WriteAnyObject(t.StorageID).
  100. MutexLock(execCtx.Args.DistLock)
  101. if err != nil {
  102. log.Warnf("acquire locks failed, err: %s", err.Error())
  103. return
  104. }
  105. defer mutex.Unlock()
  106. var objects []model.StorageObject
  107. for _, objID := range t.ObjectIDs {
  108. objs, err := execCtx.Args.DB.StorageObject().GetAllByStorageAndObjectID(execCtx.Args.DB.SQLCtx(), t.StorageID, objID)
  109. if err != nil {
  110. log.WithField("StorageID", t.StorageID).
  111. WithField("ObjectID", objID).
  112. Warnf("get storage object failed, err: %s", err.Error())
  113. return
  114. }
  115. objects = append(objects, objs...)
  116. }
  117. t.startCheck(execCtx, stg, false, objects)
  118. }
  119. func (t *AgentCheckStorage) startCheck(execCtx ExecuteContext, stg model.Storage, isComplete bool, objects []model.StorageObject) {
  120. log := logger.WithType[AgentCheckStorage]("Event")
  121. // 投递任务
  122. agentClient, err := agtcli.NewClient(stg.NodeID, &config.Cfg().RabbitMQ)
  123. if err != nil {
  124. log.WithField("NodeID", stg.NodeID).Warnf("create agent client failed, err: %s", err.Error())
  125. return
  126. }
  127. defer agentClient.Close()
  128. checkResp, err := agentClient.StorageCheck(agtmsg.NewStorageCheck(stg.StorageID, stg.Directory, isComplete, objects), mq.RequestOption{Timeout: time.Minute})
  129. if err != nil {
  130. log.WithField("NodeID", stg.NodeID).Warnf("checking storage: %s", err.Error())
  131. return
  132. }
  133. // 根据返回结果修改数据库
  134. var chkObjIDs []int64
  135. for _, entry := range checkResp.Entries {
  136. switch entry.Operation {
  137. case agtmsg.CHECK_STORAGE_RESP_OP_DELETE:
  138. err := execCtx.Args.DB.StorageObject().Delete(execCtx.Args.DB.SQLCtx(), t.StorageID, entry.ObjectID, entry.UserID)
  139. if err != nil {
  140. log.WithField("StorageID", t.StorageID).
  141. WithField("ObjectID", entry.ObjectID).
  142. Warnf("delete storage object failed, err: %s", err.Error())
  143. }
  144. chkObjIDs = append(chkObjIDs, entry.ObjectID)
  145. log.WithField("StorageID", t.StorageID).
  146. WithField("ObjectID", entry.ObjectID).
  147. WithField("UserID", entry.UserID).
  148. Debugf("delete storage object")
  149. case agtmsg.CHECK_STORAGE_RESP_OP_SET_NORMAL:
  150. err := execCtx.Args.DB.StorageObject().SetStateNormal(execCtx.Args.DB.SQLCtx(), t.StorageID, entry.ObjectID, entry.UserID)
  151. if err != nil {
  152. log.WithField("StorageID", t.StorageID).
  153. WithField("ObjectID", entry.ObjectID).
  154. Warnf("change storage object state failed, err: %s", err.Error())
  155. }
  156. log.WithField("StorageID", t.StorageID).
  157. WithField("ObjectID", entry.ObjectID).
  158. WithField("UserID", entry.UserID).
  159. Debugf("set storage object normal")
  160. }
  161. }
  162. if len(chkObjIDs) > 0 {
  163. execCtx.Executor.Post(NewCheckObject(chkObjIDs))
  164. }
  165. }
  166. func init() {
  167. RegisterMessageConvertor(func(msg scevt.AgentCheckStorage) Event { return NewAgentCheckStorage(msg.StorageID, msg.ObjectIDs) })
  168. }

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