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.

shardstore_gc.go 2.9 kB

6 months ago
6 months ago
6 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package ticktock
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "gitlink.org.cn/cloudream/common/pkgs/logger"
  7. "gitlink.org.cn/cloudream/common/utils/reflect2"
  8. "gitlink.org.cn/cloudream/jcs-pub/client/internal/db"
  9. "gitlink.org.cn/cloudream/jcs-pub/client/types"
  10. stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
  11. cortypes "gitlink.org.cn/cloudream/jcs-pub/coordinator/types"
  12. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/distlock/reqbuilder"
  13. hubrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/hub"
  14. )
  15. type ShardStoreGC struct {
  16. }
  17. func (j *ShardStoreGC) Name() string {
  18. return reflect2.TypeNameOf[ShardStoreGC]()
  19. }
  20. // Execute 执行垃圾回收操作。
  21. func (j *ShardStoreGC) Execute(t *TickTock) {
  22. log := logger.WithType[ShardStoreGC]("Event")
  23. startTime := time.Now()
  24. log.Debugf("job start")
  25. defer func() {
  26. log.Debugf("job end, time: %v", time.Since(startTime))
  27. }()
  28. spaceIDs, err := t.db.UserSpace().GetAllIDs(t.db.DefCtx())
  29. if err != nil {
  30. log.Warnf("getting user space ids: %v", err)
  31. return
  32. }
  33. for _, spaceID := range spaceIDs {
  34. detail := t.spaceMeta.Get(spaceID)
  35. if detail == nil {
  36. continue
  37. }
  38. err := j.gcOne(t, detail)
  39. if err != nil {
  40. log.Warnf("gc one user space: %v: %v", spaceID, err)
  41. continue
  42. }
  43. }
  44. }
  45. func (j *ShardStoreGC) gcOne(t *TickTock, space *types.UserSpaceDetail) error {
  46. mutex, err := reqbuilder.NewBuilder().Shard().GC(space.UserSpace.UserSpaceID).MutexLock(t.pubLock)
  47. if err != nil {
  48. return fmt.Errorf("acquire lock: %w", err)
  49. }
  50. defer mutex.Unlock()
  51. db2 := t.db
  52. // 收集需要进行垃圾回收的文件哈希值
  53. var allFileHashes []types.FileHash
  54. err = db2.DoTx(func(tx db.SQLContext) error {
  55. blocks, err := db2.ObjectBlock().GetByUserSpaceID(tx, space.UserSpace.UserSpaceID)
  56. if err != nil {
  57. return fmt.Errorf("getting object blocks by hub id: %w", err)
  58. }
  59. for _, c := range blocks {
  60. allFileHashes = append(allFileHashes, c.FileHash)
  61. }
  62. objs, err := db2.PinnedObject().GetObjectsByUserSpaceID(tx, space.UserSpace.UserSpaceID)
  63. if err != nil {
  64. return fmt.Errorf("getting pinned objects by hub id: %w", err)
  65. }
  66. for _, o := range objs {
  67. allFileHashes = append(allFileHashes, o.FileHash)
  68. }
  69. return nil
  70. })
  71. if err != nil {
  72. return err
  73. }
  74. // 获取与节点通信的代理客户端
  75. addr, ok := space.MasterHub.Address.(*cortypes.GRPCAddressInfo)
  76. if !ok {
  77. return fmt.Errorf("master of user space %v has no grpc address", space.UserSpace)
  78. }
  79. agtCli := stgglb.HubRPCPool.Get(stgglb.SelectGRPCAddress(*space.MasterHub, *addr))
  80. defer agtCli.Release()
  81. // 向代理发送垃圾回收请求
  82. ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Minute))
  83. defer cancel()
  84. _, cerr := agtCli.CacheGC(ctx, &hubrpc.CacheGC{
  85. UserSpace: *space,
  86. Availables: allFileHashes,
  87. })
  88. if cerr != nil {
  89. return fmt.Errorf("request to cache gc: %w", cerr.ToError())
  90. }
  91. return nil
  92. }

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