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.

check_package_redundancy.go 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. package event
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "github.com/samber/lo"
  7. "gitlink.org.cn/cloudream/common/pkgs/logger"
  8. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  9. "gitlink.org.cn/cloudream/common/utils/sort2"
  10. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  11. stgmod "gitlink.org.cn/cloudream/storage/common/models"
  12. "gitlink.org.cn/cloudream/storage/common/pkgs/distlock/reqbuilder"
  13. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch/plans"
  14. agtmq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/agent"
  15. coormq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/coordinator"
  16. scevt "gitlink.org.cn/cloudream/storage/common/pkgs/mq/scanner/event"
  17. "gitlink.org.cn/cloudream/storage/scanner/internal/config"
  18. )
  19. const (
  20. monthHours = 30 * 24
  21. yearHours = 365 * 24
  22. )
  23. type CheckPackageRedundancy struct {
  24. *scevt.CheckPackageRedundancy
  25. }
  26. func NewCheckPackageRedundancy(evt *scevt.CheckPackageRedundancy) *CheckPackageRedundancy {
  27. return &CheckPackageRedundancy{
  28. CheckPackageRedundancy: evt,
  29. }
  30. }
  31. type NodeLoadInfo struct {
  32. Node cdssdk.Node
  33. LoadsRecentMonth int
  34. LoadsRecentYear int
  35. }
  36. func (t *CheckPackageRedundancy) TryMerge(other Event) bool {
  37. event, ok := other.(*CheckPackageRedundancy)
  38. if !ok {
  39. return false
  40. }
  41. return event.PackageID == t.PackageID
  42. }
  43. func (t *CheckPackageRedundancy) Execute(execCtx ExecuteContext) {
  44. log := logger.WithType[CheckPackageRedundancy]("Event")
  45. startTime := time.Now()
  46. log.Debugf("begin with %v", logger.FormatStruct(t.CheckPackageRedundancy))
  47. defer func() {
  48. log.Debugf("end, time: %v", time.Since(startTime))
  49. }()
  50. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  51. if err != nil {
  52. log.Warnf("new coordinator client: %s", err.Error())
  53. return
  54. }
  55. defer stgglb.CoordinatorMQPool.Release(coorCli)
  56. getObjs, err := coorCli.GetPackageObjectDetails(coormq.ReqGetPackageObjectDetails(t.PackageID))
  57. if err != nil {
  58. log.Warnf("getting package objects: %s", err.Error())
  59. return
  60. }
  61. getLogs, err := coorCli.GetPackageLoadLogDetails(coormq.ReqGetPackageLoadLogDetails(t.PackageID))
  62. if err != nil {
  63. log.Warnf("getting package load log details: %s", err.Error())
  64. return
  65. }
  66. // TODO UserID
  67. getNodes, err := coorCli.GetUserNodes(coormq.NewGetUserNodes(1))
  68. if err != nil {
  69. log.Warnf("getting all nodes: %s", err.Error())
  70. return
  71. }
  72. if len(getNodes.Nodes) == 0 {
  73. log.Warnf("no available nodes")
  74. return
  75. }
  76. allNodes := make(map[cdssdk.NodeID]*NodeLoadInfo)
  77. for _, node := range getNodes.Nodes {
  78. allNodes[node.NodeID] = &NodeLoadInfo{
  79. Node: node,
  80. }
  81. }
  82. for _, log := range getLogs.Logs {
  83. info, ok := allNodes[log.Storage.NodeID]
  84. if !ok {
  85. continue
  86. }
  87. sinceNow := time.Since(log.CreateTime)
  88. if sinceNow.Hours() < monthHours {
  89. info.LoadsRecentMonth++
  90. } else if sinceNow.Hours() < yearHours {
  91. info.LoadsRecentYear++
  92. }
  93. }
  94. var changedObjects []coormq.UpdatingObjectRedundancy
  95. defRep := cdssdk.DefaultRepRedundancy
  96. defEC := cdssdk.DefaultECRedundancy
  97. // TODO 目前rep的备份数量固定为2,所以这里直接选出两个节点
  98. mostBlockNodeIDs := t.summaryRepObjectBlockNodes(getObjs.Objects, 2)
  99. newRepNodes := t.chooseNewNodesForRep(&defRep, allNodes)
  100. rechoosedRepNodes := t.rechooseNodesForRep(mostBlockNodeIDs, &defRep, allNodes)
  101. newECNodes := t.chooseNewNodesForEC(&defEC, allNodes)
  102. // 加锁
  103. builder := reqbuilder.NewBuilder()
  104. for _, node := range newRepNodes {
  105. builder.IPFS().Buzy(node.Node.NodeID)
  106. }
  107. for _, node := range newECNodes {
  108. builder.IPFS().Buzy(node.Node.NodeID)
  109. }
  110. mutex, err := builder.MutexLock(execCtx.Args.DistLock)
  111. if err != nil {
  112. log.Warnf("acquiring dist lock: %s", err.Error())
  113. return
  114. }
  115. defer mutex.Unlock()
  116. for _, obj := range getObjs.Objects {
  117. var updating *coormq.UpdatingObjectRedundancy
  118. var err error
  119. shouldUseEC := obj.Object.Size > config.Cfg().ECFileSizeThreshold
  120. switch red := obj.Object.Redundancy.(type) {
  121. case *cdssdk.NoneRedundancy:
  122. if shouldUseEC {
  123. log.WithField("ObjectID", obj.Object.ObjectID).Debugf("redundancy: none -> ec")
  124. updating, err = t.noneToEC(obj, &defEC, newECNodes)
  125. } else {
  126. log.WithField("ObjectID", obj.Object.ObjectID).Debugf("redundancy: none -> rep")
  127. updating, err = t.noneToRep(obj, &defRep, newRepNodes)
  128. }
  129. case *cdssdk.RepRedundancy:
  130. if shouldUseEC {
  131. log.WithField("ObjectID", obj.Object.ObjectID).Debugf("redundancy: rep -> ec")
  132. updating, err = t.repToEC(obj, &defEC, newECNodes)
  133. } else {
  134. updating, err = t.repToRep(obj, &defRep, rechoosedRepNodes)
  135. }
  136. case *cdssdk.ECRedundancy:
  137. if shouldUseEC {
  138. uploadNodes := t.rechooseNodesForEC(obj, red, allNodes)
  139. updating, err = t.ecToEC(obj, red, &defEC, uploadNodes)
  140. } else {
  141. log.WithField("ObjectID", obj.Object.ObjectID).Debugf("redundancy: ec -> rep")
  142. updating, err = t.ecToRep(obj, red, &defRep, newRepNodes)
  143. }
  144. }
  145. if updating != nil {
  146. changedObjects = append(changedObjects, *updating)
  147. }
  148. if err != nil {
  149. log.WithField("ObjectID", obj.Object.ObjectID).Warnf("%s, its redundancy wont be changed", err.Error())
  150. }
  151. }
  152. if len(changedObjects) == 0 {
  153. return
  154. }
  155. _, err = coorCli.UpdateObjectRedundancy(coormq.ReqUpdateObjectRedundancy(changedObjects))
  156. if err != nil {
  157. log.Warnf("requesting to change object redundancy: %s", err.Error())
  158. return
  159. }
  160. }
  161. // 统计每个对象块所在的节点,选出块最多的不超过nodeCnt个节点
  162. func (t *CheckPackageRedundancy) summaryRepObjectBlockNodes(objs []stgmod.ObjectDetail, nodeCnt int) []cdssdk.NodeID {
  163. type nodeBlocks struct {
  164. NodeID cdssdk.NodeID
  165. Count int
  166. }
  167. nodeBlocksMap := make(map[cdssdk.NodeID]*nodeBlocks)
  168. for _, obj := range objs {
  169. shouldUseEC := obj.Object.Size > config.Cfg().ECFileSizeThreshold
  170. if _, ok := obj.Object.Redundancy.(*cdssdk.RepRedundancy); ok && !shouldUseEC {
  171. for _, block := range obj.Blocks {
  172. if _, ok := nodeBlocksMap[block.NodeID]; !ok {
  173. nodeBlocksMap[block.NodeID] = &nodeBlocks{
  174. NodeID: block.NodeID,
  175. Count: 0,
  176. }
  177. }
  178. nodeBlocksMap[block.NodeID].Count++
  179. }
  180. }
  181. }
  182. nodes := lo.Values(nodeBlocksMap)
  183. sort2.Sort(nodes, func(left *nodeBlocks, right *nodeBlocks) int {
  184. return right.Count - left.Count
  185. })
  186. ids := lo.Map(nodes, func(item *nodeBlocks, idx int) cdssdk.NodeID { return item.NodeID })
  187. if len(ids) > nodeCnt {
  188. ids = ids[:nodeCnt]
  189. }
  190. return ids
  191. }
  192. func (t *CheckPackageRedundancy) chooseNewNodesForRep(red *cdssdk.RepRedundancy, allNodes map[cdssdk.NodeID]*NodeLoadInfo) []*NodeLoadInfo {
  193. sortedNodes := sort2.Sort(lo.Values(allNodes), func(left *NodeLoadInfo, right *NodeLoadInfo) int {
  194. dm := right.LoadsRecentMonth - left.LoadsRecentMonth
  195. if dm != 0 {
  196. return dm
  197. }
  198. return right.LoadsRecentYear - left.LoadsRecentYear
  199. })
  200. return t.chooseSoManyNodes(red.RepCount, sortedNodes)
  201. }
  202. func (t *CheckPackageRedundancy) chooseNewNodesForEC(red *cdssdk.ECRedundancy, allNodes map[cdssdk.NodeID]*NodeLoadInfo) []*NodeLoadInfo {
  203. sortedNodes := sort2.Sort(lo.Values(allNodes), func(left *NodeLoadInfo, right *NodeLoadInfo) int {
  204. dm := right.LoadsRecentMonth - left.LoadsRecentMonth
  205. if dm != 0 {
  206. return dm
  207. }
  208. return right.LoadsRecentYear - left.LoadsRecentYear
  209. })
  210. return t.chooseSoManyNodes(red.N, sortedNodes)
  211. }
  212. func (t *CheckPackageRedundancy) rechooseNodesForRep(mostBlockNodeIDs []cdssdk.NodeID, red *cdssdk.RepRedundancy, allNodes map[cdssdk.NodeID]*NodeLoadInfo) []*NodeLoadInfo {
  213. type rechooseNode struct {
  214. *NodeLoadInfo
  215. HasBlock bool
  216. }
  217. var rechooseNodes []*rechooseNode
  218. for _, node := range allNodes {
  219. hasBlock := false
  220. for _, id := range mostBlockNodeIDs {
  221. if id == node.Node.NodeID {
  222. hasBlock = true
  223. break
  224. }
  225. }
  226. rechooseNodes = append(rechooseNodes, &rechooseNode{
  227. NodeLoadInfo: node,
  228. HasBlock: hasBlock,
  229. })
  230. }
  231. sortedNodes := sort2.Sort(rechooseNodes, func(left *rechooseNode, right *rechooseNode) int {
  232. dm := right.LoadsRecentMonth - left.LoadsRecentMonth
  233. if dm != 0 {
  234. return dm
  235. }
  236. // 已经缓存了文件块的节点优先选择
  237. v := sort2.CmpBool(right.HasBlock, left.HasBlock)
  238. if v != 0 {
  239. return v
  240. }
  241. return right.LoadsRecentYear - left.LoadsRecentYear
  242. })
  243. return t.chooseSoManyNodes(red.RepCount, lo.Map(sortedNodes, func(node *rechooseNode, idx int) *NodeLoadInfo { return node.NodeLoadInfo }))
  244. }
  245. func (t *CheckPackageRedundancy) rechooseNodesForEC(obj stgmod.ObjectDetail, red *cdssdk.ECRedundancy, allNodes map[cdssdk.NodeID]*NodeLoadInfo) []*NodeLoadInfo {
  246. type rechooseNode struct {
  247. *NodeLoadInfo
  248. CachedBlockIndex int
  249. }
  250. var rechooseNodes []*rechooseNode
  251. for _, node := range allNodes {
  252. cachedBlockIndex := -1
  253. for _, block := range obj.Blocks {
  254. if block.NodeID == node.Node.NodeID {
  255. cachedBlockIndex = block.Index
  256. break
  257. }
  258. }
  259. rechooseNodes = append(rechooseNodes, &rechooseNode{
  260. NodeLoadInfo: node,
  261. CachedBlockIndex: cachedBlockIndex,
  262. })
  263. }
  264. sortedNodes := sort2.Sort(rechooseNodes, func(left *rechooseNode, right *rechooseNode) int {
  265. dm := right.LoadsRecentMonth - left.LoadsRecentMonth
  266. if dm != 0 {
  267. return dm
  268. }
  269. // 已经缓存了文件块的节点优先选择
  270. v := sort2.CmpBool(right.CachedBlockIndex > -1, left.CachedBlockIndex > -1)
  271. if v != 0 {
  272. return v
  273. }
  274. return right.LoadsRecentYear - left.LoadsRecentYear
  275. })
  276. // TODO 可以考虑选择已有块的节点时,能依然按照Index顺序选择
  277. return t.chooseSoManyNodes(red.N, lo.Map(sortedNodes, func(node *rechooseNode, idx int) *NodeLoadInfo { return node.NodeLoadInfo }))
  278. }
  279. func (t *CheckPackageRedundancy) chooseSoManyNodes(count int, nodes []*NodeLoadInfo) []*NodeLoadInfo {
  280. repeateCount := (count + len(nodes) - 1) / len(nodes)
  281. extedNodes := make([]*NodeLoadInfo, repeateCount*len(nodes))
  282. // 使用复制的方式将节点数扩充到要求的数量
  283. // 复制之后的结构:ABCD -> AAABBBCCCDDD
  284. for p := 0; p < repeateCount; p++ {
  285. for i, node := range nodes {
  286. putIdx := i*repeateCount + p
  287. extedNodes[putIdx] = node
  288. }
  289. }
  290. extedNodes = extedNodes[:count]
  291. var chosen []*NodeLoadInfo
  292. for len(chosen) < count {
  293. // 在每一轮内都选不同地区的节点,如果节点数不够,那么就再来一轮
  294. chosenLocations := make(map[cdssdk.LocationID]bool)
  295. for i, node := range extedNodes {
  296. if node == nil {
  297. continue
  298. }
  299. if chosenLocations[node.Node.LocationID] {
  300. continue
  301. }
  302. chosen = append(chosen, node)
  303. chosenLocations[node.Node.LocationID] = true
  304. extedNodes[i] = nil
  305. }
  306. }
  307. return chosen
  308. }
  309. func (t *CheckPackageRedundancy) noneToRep(obj stgmod.ObjectDetail, red *cdssdk.RepRedundancy, uploadNodes []*NodeLoadInfo) (*coormq.UpdatingObjectRedundancy, error) {
  310. if len(obj.Blocks) == 0 {
  311. return nil, fmt.Errorf("object is not cached on any nodes, cannot change its redundancy to rep")
  312. }
  313. // 如果选择的备份节点都是同一个,那么就只要上传一次
  314. uploadNodes = lo.UniqBy(uploadNodes, func(item *NodeLoadInfo) cdssdk.NodeID { return item.Node.NodeID })
  315. var blocks []stgmod.ObjectBlock
  316. for _, node := range uploadNodes {
  317. err := t.pinObject(node.Node.NodeID, obj.Object.FileHash)
  318. if err != nil {
  319. return nil, err
  320. }
  321. blocks = append(blocks, stgmod.ObjectBlock{
  322. ObjectID: obj.Object.ObjectID,
  323. Index: 0,
  324. NodeID: node.Node.NodeID,
  325. FileHash: obj.Object.FileHash,
  326. })
  327. }
  328. return &coormq.UpdatingObjectRedundancy{
  329. ObjectID: obj.Object.ObjectID,
  330. Redundancy: red,
  331. Blocks: blocks,
  332. }, nil
  333. }
  334. func (t *CheckPackageRedundancy) noneToEC(obj stgmod.ObjectDetail, red *cdssdk.ECRedundancy, uploadNodes []*NodeLoadInfo) (*coormq.UpdatingObjectRedundancy, error) {
  335. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  336. if err != nil {
  337. return nil, fmt.Errorf("new coordinator client: %w", err)
  338. }
  339. defer stgglb.CoordinatorMQPool.Release(coorCli)
  340. if len(obj.Blocks) == 0 {
  341. return nil, fmt.Errorf("object is not cached on any nodes, cannot change its redundancy to ec")
  342. }
  343. getNodes, err := coorCli.GetNodes(coormq.NewGetNodes([]cdssdk.NodeID{obj.Blocks[0].NodeID}))
  344. if err != nil {
  345. return nil, fmt.Errorf("requesting to get nodes: %w", err)
  346. }
  347. planBlder := plans.NewPlanBuilder()
  348. inputStrs := planBlder.AtAgent(getNodes.Nodes[0]).IPFSRead(obj.Object.FileHash).ChunkedSplit(red.ChunkSize, red.K, true)
  349. outputStrs := planBlder.AtAgent(getNodes.Nodes[0]).ECReconstructAny(*red, lo.Range(red.K), lo.Range(red.N), inputStrs.Streams...)
  350. for i := 0; i < red.N; i++ {
  351. outputStrs.Stream(i).GRPCSend(uploadNodes[i].Node).IPFSWrite(fmt.Sprintf("%d", i))
  352. }
  353. plan, err := planBlder.Build()
  354. if err != nil {
  355. return nil, fmt.Errorf("building io plan: %w", err)
  356. }
  357. exec, err := plans.Execute(*plan)
  358. if err != nil {
  359. return nil, fmt.Errorf("executing io plan: %w", err)
  360. }
  361. ioRet, err := exec.Wait()
  362. if err != nil {
  363. return nil, fmt.Errorf("executing io plan: %w", err)
  364. }
  365. var blocks []stgmod.ObjectBlock
  366. for i := 0; i < red.N; i++ {
  367. blocks = append(blocks, stgmod.ObjectBlock{
  368. ObjectID: obj.Object.ObjectID,
  369. Index: i,
  370. NodeID: uploadNodes[i].Node.NodeID,
  371. FileHash: ioRet.ResultValues[fmt.Sprintf("%d", i)].(string),
  372. })
  373. }
  374. return &coormq.UpdatingObjectRedundancy{
  375. ObjectID: obj.Object.ObjectID,
  376. Redundancy: red,
  377. Blocks: blocks,
  378. }, nil
  379. }
  380. func (t *CheckPackageRedundancy) repToRep(obj stgmod.ObjectDetail, red *cdssdk.RepRedundancy, uploadNodes []*NodeLoadInfo) (*coormq.UpdatingObjectRedundancy, error) {
  381. if len(obj.Blocks) == 0 {
  382. return nil, fmt.Errorf("object is not cached on any nodes, cannot change its redundancy to rep")
  383. }
  384. // 如果选择的备份节点都是同一个,那么就只要上传一次
  385. uploadNodes = lo.UniqBy(uploadNodes, func(item *NodeLoadInfo) cdssdk.NodeID { return item.Node.NodeID })
  386. for _, node := range uploadNodes {
  387. err := t.pinObject(node.Node.NodeID, obj.Object.FileHash)
  388. if err != nil {
  389. logger.WithField("ObjectID", obj.Object.ObjectID).
  390. Warn(err.Error())
  391. return nil, err
  392. }
  393. }
  394. var blocks []stgmod.ObjectBlock
  395. for _, node := range uploadNodes {
  396. // 由于更新冗余方式会删除所有Block记录然后重新填充,
  397. // 所以即使是节点跳过了上传,也需要为它添加一条Block记录
  398. blocks = append(blocks, stgmod.ObjectBlock{
  399. ObjectID: obj.Object.ObjectID,
  400. Index: 0,
  401. NodeID: node.Node.NodeID,
  402. FileHash: obj.Object.FileHash,
  403. })
  404. }
  405. return &coormq.UpdatingObjectRedundancy{
  406. ObjectID: obj.Object.ObjectID,
  407. Redundancy: red,
  408. Blocks: blocks,
  409. }, nil
  410. }
  411. func (t *CheckPackageRedundancy) repToEC(obj stgmod.ObjectDetail, red *cdssdk.ECRedundancy, uploadNodes []*NodeLoadInfo) (*coormq.UpdatingObjectRedundancy, error) {
  412. return t.noneToEC(obj, red, uploadNodes)
  413. }
  414. func (t *CheckPackageRedundancy) ecToRep(obj stgmod.ObjectDetail, srcRed *cdssdk.ECRedundancy, tarRed *cdssdk.RepRedundancy, uploadNodes []*NodeLoadInfo) (*coormq.UpdatingObjectRedundancy, error) {
  415. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  416. if err != nil {
  417. return nil, fmt.Errorf("new coordinator client: %w", err)
  418. }
  419. defer stgglb.CoordinatorMQPool.Release(coorCli)
  420. var chosenBlocks []stgmod.GrouppedObjectBlock
  421. var chosenBlockIndexes []int
  422. for _, block := range obj.GroupBlocks() {
  423. if len(block.NodeIDs) > 0 {
  424. chosenBlocks = append(chosenBlocks, block)
  425. chosenBlockIndexes = append(chosenBlockIndexes, block.Index)
  426. }
  427. if len(chosenBlocks) == srcRed.K {
  428. break
  429. }
  430. }
  431. if len(chosenBlocks) < srcRed.K {
  432. return nil, fmt.Errorf("no enough blocks to reconstruct the original file data")
  433. }
  434. // 如果选择的备份节点都是同一个,那么就只要上传一次
  435. uploadNodes = lo.UniqBy(uploadNodes, func(item *NodeLoadInfo) cdssdk.NodeID { return item.Node.NodeID })
  436. // 每个被选节点都在自己节点上重建原始数据
  437. planBlder := plans.NewPlanBuilder()
  438. for i := range uploadNodes {
  439. tarNode := planBlder.AtAgent(uploadNodes[i].Node)
  440. var inputs []*plans.AgentStream
  441. for _, block := range chosenBlocks {
  442. inputs = append(inputs, tarNode.IPFSRead(block.FileHash))
  443. }
  444. outputs := tarNode.ECReconstruct(*srcRed, chosenBlockIndexes, inputs...)
  445. tarNode.ChunkedJoin(srcRed.ChunkSize, outputs.Streams...).Length(obj.Object.Size).IPFSWrite(fmt.Sprintf("%d", i))
  446. }
  447. plan, err := planBlder.Build()
  448. if err != nil {
  449. return nil, fmt.Errorf("building io plan: %w", err)
  450. }
  451. exec, err := plans.Execute(*plan)
  452. if err != nil {
  453. return nil, fmt.Errorf("executing io plan: %w", err)
  454. }
  455. ioRet, err := exec.Wait()
  456. if err != nil {
  457. return nil, fmt.Errorf("executing io plan: %w", err)
  458. }
  459. var blocks []stgmod.ObjectBlock
  460. for i := range uploadNodes {
  461. blocks = append(blocks, stgmod.ObjectBlock{
  462. ObjectID: obj.Object.ObjectID,
  463. Index: 0,
  464. NodeID: uploadNodes[i].Node.NodeID,
  465. FileHash: ioRet.ResultValues[fmt.Sprintf("%d", i)].(string),
  466. })
  467. }
  468. return &coormq.UpdatingObjectRedundancy{
  469. ObjectID: obj.Object.ObjectID,
  470. Redundancy: tarRed,
  471. Blocks: blocks,
  472. }, nil
  473. }
  474. func (t *CheckPackageRedundancy) ecToEC(obj stgmod.ObjectDetail, srcRed *cdssdk.ECRedundancy, tarRed *cdssdk.ECRedundancy, uploadNodes []*NodeLoadInfo) (*coormq.UpdatingObjectRedundancy, error) {
  475. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  476. if err != nil {
  477. return nil, fmt.Errorf("new coordinator client: %w", err)
  478. }
  479. defer stgglb.CoordinatorMQPool.Release(coorCli)
  480. grpBlocks := obj.GroupBlocks()
  481. var chosenBlocks []stgmod.GrouppedObjectBlock
  482. var chosenBlockIndexes []int
  483. for _, block := range grpBlocks {
  484. if len(block.NodeIDs) > 0 {
  485. chosenBlocks = append(chosenBlocks, block)
  486. chosenBlockIndexes = append(chosenBlockIndexes, block.Index)
  487. }
  488. if len(chosenBlocks) == srcRed.K {
  489. break
  490. }
  491. }
  492. if len(chosenBlocks) < srcRed.K {
  493. return nil, fmt.Errorf("no enough blocks to reconstruct the original file data")
  494. }
  495. // 目前EC的参数都相同,所以可以不用重建出完整数据然后再分块,可以直接构建出目的节点需要的块
  496. planBlder := plans.NewPlanBuilder()
  497. var newBlocks []stgmod.ObjectBlock
  498. shouldUpdateBlocks := false
  499. for i, node := range uploadNodes {
  500. newBlock := stgmod.ObjectBlock{
  501. ObjectID: obj.Object.ObjectID,
  502. Index: i,
  503. NodeID: node.Node.NodeID,
  504. }
  505. grp, ok := lo.Find(grpBlocks, func(grp stgmod.GrouppedObjectBlock) bool { return grp.Index == i })
  506. // 如果新选中的节点已经记录在Block表中,那么就不需要任何变更
  507. if ok && lo.Contains(grp.NodeIDs, node.Node.NodeID) {
  508. newBlock.FileHash = grp.FileHash
  509. newBlocks = append(newBlocks, newBlock)
  510. continue
  511. }
  512. shouldUpdateBlocks = true
  513. // 否则就要重建出这个节点需要的块
  514. tarNode := planBlder.AtAgent(node.Node)
  515. var inputs []*plans.AgentStream
  516. for _, block := range chosenBlocks {
  517. inputs = append(inputs, tarNode.IPFSRead(block.FileHash))
  518. }
  519. // 输出只需要自己要保存的那一块
  520. tarNode.ECReconstructAny(*srcRed, chosenBlockIndexes, []int{i}, inputs...).Stream(0).IPFSWrite(fmt.Sprintf("%d", i))
  521. newBlocks = append(newBlocks, newBlock)
  522. }
  523. plan, err := planBlder.Build()
  524. if err != nil {
  525. return nil, fmt.Errorf("building io plan: %w", err)
  526. }
  527. exec, err := plans.Execute(*plan)
  528. if err != nil {
  529. return nil, fmt.Errorf("executing io plan: %w", err)
  530. }
  531. // 如果没有任何Plan,Wait会直接返回成功
  532. ret, err := exec.Wait()
  533. if err != nil {
  534. return nil, fmt.Errorf("executing io plan: %w", err)
  535. }
  536. if !shouldUpdateBlocks {
  537. return nil, nil
  538. }
  539. for k, v := range ret.ResultValues {
  540. idx, err := strconv.ParseInt(k, 10, 64)
  541. if err != nil {
  542. return nil, fmt.Errorf("parsing result key %s as index: %w", k, err)
  543. }
  544. newBlocks[idx].FileHash = v.(string)
  545. }
  546. return &coormq.UpdatingObjectRedundancy{
  547. ObjectID: obj.Object.ObjectID,
  548. Redundancy: tarRed,
  549. Blocks: newBlocks,
  550. }, nil
  551. }
  552. func (t *CheckPackageRedundancy) pinObject(nodeID cdssdk.NodeID, fileHash string) error {
  553. agtCli, err := stgglb.AgentMQPool.Acquire(nodeID)
  554. if err != nil {
  555. return fmt.Errorf("new agent client: %w", err)
  556. }
  557. defer stgglb.AgentMQPool.Release(agtCli)
  558. _, err = agtCli.PinObject(agtmq.ReqPinObject([]string{fileHash}, false))
  559. if err != nil {
  560. return fmt.Errorf("start pinning object: %w", err)
  561. }
  562. return nil
  563. }
  564. func init() {
  565. RegisterMessageConvertor(NewCheckPackageRedundancy)
  566. }

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