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.

clean_pinned.go 33 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. package event
  2. import (
  3. "context"
  4. "fmt"
  5. "math"
  6. "math/rand"
  7. "sync"
  8. "time"
  9. "github.com/samber/lo"
  10. "gitlink.org.cn/cloudream/common/pkgs/bitmap"
  11. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  12. "gitlink.org.cn/cloudream/common/pkgs/logger"
  13. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  14. "gitlink.org.cn/cloudream/common/utils/lo2"
  15. "gitlink.org.cn/cloudream/common/utils/math2"
  16. "gitlink.org.cn/cloudream/common/utils/sort2"
  17. "gitlink.org.cn/cloudream/storage/common/consts"
  18. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  19. stgmod "gitlink.org.cn/cloudream/storage/common/models"
  20. "gitlink.org.cn/cloudream/storage/common/pkgs/distlock/reqbuilder"
  21. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch2"
  22. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch2/ops2"
  23. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch2/parser"
  24. coormq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/coordinator"
  25. scevt "gitlink.org.cn/cloudream/storage/common/pkgs/mq/scanner/event"
  26. )
  27. type CleanPinned struct {
  28. *scevt.CleanPinned
  29. }
  30. func NewCleanPinned(evt *scevt.CleanPinned) *CleanPinned {
  31. return &CleanPinned{
  32. CleanPinned: evt,
  33. }
  34. }
  35. func (t *CleanPinned) TryMerge(other Event) bool {
  36. event, ok := other.(*CleanPinned)
  37. if !ok {
  38. return false
  39. }
  40. return t.PackageID == event.PackageID
  41. }
  42. func (t *CleanPinned) Execute(execCtx ExecuteContext) {
  43. log := logger.WithType[CleanPinned]("Event")
  44. startTime := time.Now()
  45. log.Debugf("begin with %v", logger.FormatStruct(t.CleanPinned))
  46. defer func() {
  47. log.Debugf("end, time: %v", time.Since(startTime))
  48. }()
  49. // TODO 应该与其他event一样,直接访问数据库
  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. stats, err := execCtx.Args.DB.PackageAccessStat().GetByPackageID(execCtx.Args.DB.DefCtx(), t.PackageID)
  62. if err != nil {
  63. log.Warnf("getting package access stat: %s", err.Error())
  64. return
  65. }
  66. var readerStgIDs []cdssdk.StorageID
  67. for _, item := range stats {
  68. // TODO 可以考虑做成配置
  69. if item.Amount >= float64(len(getObjs.Objects)/2) {
  70. readerStgIDs = append(readerStgIDs, item.StorageID)
  71. }
  72. }
  73. // 注意!需要保证allStgID包含所有之后可能用到的节点ID
  74. // TOOD 可以考虑设计Cache机制
  75. var allStgID []cdssdk.StorageID
  76. for _, obj := range getObjs.Objects {
  77. for _, block := range obj.Blocks {
  78. allStgID = append(allStgID, block.StorageID)
  79. }
  80. allStgID = append(allStgID, obj.PinnedAt...)
  81. }
  82. allStgID = append(allStgID, readerStgIDs...)
  83. getStgResp, err := coorCli.GetStorageDetails(coormq.ReqGetStorageDetails(lo.Union(allStgID)))
  84. if err != nil {
  85. log.Warnf("getting nodes: %s", err.Error())
  86. return
  87. }
  88. allStgInfos := make(map[cdssdk.StorageID]*stgmod.StorageDetail)
  89. for _, stg := range getStgResp.Storages {
  90. allStgInfos[stg.Storage.StorageID] = stg
  91. }
  92. // 只对ec和rep对象进行处理
  93. var ecObjects []stgmod.ObjectDetail
  94. var repObjects []stgmod.ObjectDetail
  95. for _, obj := range getObjs.Objects {
  96. if _, ok := obj.Object.Redundancy.(*cdssdk.ECRedundancy); ok {
  97. ecObjects = append(ecObjects, obj)
  98. } else if _, ok := obj.Object.Redundancy.(*cdssdk.RepRedundancy); ok {
  99. repObjects = append(repObjects, obj)
  100. }
  101. }
  102. planBld := exec.NewPlanBuilder()
  103. planningStgIDs := make(map[cdssdk.StorageID]bool)
  104. var sysEvents []stgmod.SysEventBody
  105. // 对于rep对象,统计出所有对象块分布最多的两个节点,用这两个节点代表所有rep对象块的分布,去进行退火算法
  106. var repObjectsUpdating []coormq.UpdatingObjectRedundancy
  107. repMostHubIDs := t.summaryRepObjectBlockNodes(repObjects)
  108. solu := t.startAnnealing(allStgInfos, readerStgIDs, annealingObject{
  109. totalBlockCount: 1,
  110. minBlockCnt: 1,
  111. pinnedAt: repMostHubIDs,
  112. blocks: nil,
  113. })
  114. for _, obj := range repObjects {
  115. repObjectsUpdating = append(repObjectsUpdating, t.makePlansForRepObject(allStgInfos, solu, obj, planBld, planningStgIDs))
  116. sysEvents = append(sysEvents, t.generateSysEventForRepObject(solu, obj)...)
  117. }
  118. // 对于ec对象,则每个对象单独进行退火算法
  119. var ecObjectsUpdating []coormq.UpdatingObjectRedundancy
  120. for _, obj := range ecObjects {
  121. ecRed := obj.Object.Redundancy.(*cdssdk.ECRedundancy)
  122. solu := t.startAnnealing(allStgInfos, readerStgIDs, annealingObject{
  123. totalBlockCount: ecRed.N,
  124. minBlockCnt: ecRed.K,
  125. pinnedAt: obj.PinnedAt,
  126. blocks: obj.Blocks,
  127. })
  128. ecObjectsUpdating = append(ecObjectsUpdating, t.makePlansForECObject(allStgInfos, solu, obj, planBld, planningStgIDs))
  129. sysEvents = append(sysEvents, t.generateSysEventForECObject(solu, obj)...)
  130. }
  131. ioSwRets, err := t.executePlans(execCtx, planBld, planningStgIDs)
  132. if err != nil {
  133. log.Warn(err.Error())
  134. return
  135. }
  136. // 根据按照方案进行调整的结果,填充更新元数据的命令
  137. for i := range ecObjectsUpdating {
  138. t.populateECObjectEntry(&ecObjectsUpdating[i], ecObjects[i], ioSwRets)
  139. }
  140. finalEntries := append(repObjectsUpdating, ecObjectsUpdating...)
  141. if len(finalEntries) > 0 {
  142. _, err = coorCli.UpdateObjectRedundancy(coormq.ReqUpdateObjectRedundancy(finalEntries))
  143. if err != nil {
  144. log.Warnf("changing object redundancy: %s", err.Error())
  145. return
  146. }
  147. for _, e := range sysEvents {
  148. execCtx.Args.EvtPub.Publish(e)
  149. }
  150. }
  151. }
  152. func (t *CleanPinned) summaryRepObjectBlockNodes(objs []stgmod.ObjectDetail) []cdssdk.StorageID {
  153. type stgBlocks struct {
  154. StorageID cdssdk.StorageID
  155. Count int
  156. }
  157. stgBlocksMap := make(map[cdssdk.StorageID]*stgBlocks)
  158. for _, obj := range objs {
  159. cacheBlockStgs := make(map[cdssdk.StorageID]bool)
  160. for _, block := range obj.Blocks {
  161. if _, ok := stgBlocksMap[block.StorageID]; !ok {
  162. stgBlocksMap[block.StorageID] = &stgBlocks{
  163. StorageID: block.StorageID,
  164. Count: 0,
  165. }
  166. }
  167. stgBlocksMap[block.StorageID].Count++
  168. cacheBlockStgs[block.StorageID] = true
  169. }
  170. for _, hubID := range obj.PinnedAt {
  171. if cacheBlockStgs[hubID] {
  172. continue
  173. }
  174. if _, ok := stgBlocksMap[hubID]; !ok {
  175. stgBlocksMap[hubID] = &stgBlocks{
  176. StorageID: hubID,
  177. Count: 0,
  178. }
  179. }
  180. stgBlocksMap[hubID].Count++
  181. }
  182. }
  183. stgs := lo.Values(stgBlocksMap)
  184. sort2.Sort(stgs, func(left *stgBlocks, right *stgBlocks) int {
  185. return right.Count - left.Count
  186. })
  187. // 只选出块数超过一半的节点,但要保证至少有两个节点
  188. for i := 2; i < len(stgs); i++ {
  189. if stgs[i].Count < len(objs)/2 {
  190. stgs = stgs[:i]
  191. break
  192. }
  193. }
  194. return lo.Map(stgs, func(item *stgBlocks, idx int) cdssdk.StorageID { return item.StorageID })
  195. }
  196. type annealingState struct {
  197. allStgInfos map[cdssdk.StorageID]*stgmod.StorageDetail // 所有节点的信息
  198. readerStgIDs []cdssdk.StorageID // 近期可能访问此对象的节点
  199. stgsSortedByReader map[cdssdk.StorageID][]stgDist // 拥有数据的节点到每个可能访问对象的节点按距离排序
  200. object annealingObject // 进行退火的对象
  201. blockList []objectBlock // 排序后的块分布情况
  202. stgBlockBitmaps map[cdssdk.StorageID]*bitmap.Bitmap64 // 用位图的形式表示每一个节点上有哪些块
  203. stgCombTree combinatorialTree // 节点组合树,用于加速计算容灾度
  204. maxScore float64 // 搜索过程中得到过的最大分数
  205. maxScoreRmBlocks []bool // 最大分数对应的删除方案
  206. rmBlocks []bool // 当前删除方案
  207. inversedIndex int // 当前删除方案是从上一次的方案改动哪个flag而来的
  208. lastDisasterTolerance float64 // 上一次方案的容灾度
  209. lastSpaceCost float64 // 上一次方案的冗余度
  210. lastMinAccessCost float64 // 上一次方案的最小访问费用
  211. lastScore float64 // 上一次方案的分数
  212. }
  213. type objectBlock struct {
  214. Index int
  215. StorageID cdssdk.StorageID
  216. HasEntity bool // 节点拥有实际的文件数据块
  217. HasShadow bool // 如果节点拥有完整文件数据,那么认为这个节点拥有所有块,这些块被称为影子块
  218. FileHash cdssdk.FileHash // 只有在拥有实际文件数据块时,这个字段才有值
  219. Size int64 // 块大小
  220. }
  221. type stgDist struct {
  222. StorageID cdssdk.StorageID
  223. Distance float64
  224. }
  225. type combinatorialTree struct {
  226. nodes []combinatorialTreeNode
  227. blocksMaps map[int]bitmap.Bitmap64
  228. stgIDToLocalStgID map[cdssdk.StorageID]int
  229. localStgIDToStgID []cdssdk.StorageID
  230. }
  231. type annealingObject struct {
  232. totalBlockCount int
  233. minBlockCnt int
  234. pinnedAt []cdssdk.StorageID
  235. blocks []stgmod.ObjectBlock
  236. }
  237. const (
  238. iterActionNone = 0
  239. iterActionSkip = 1
  240. iterActionBreak = 2
  241. )
  242. func newCombinatorialTree(stgBlocksMaps map[cdssdk.StorageID]*bitmap.Bitmap64) combinatorialTree {
  243. tree := combinatorialTree{
  244. blocksMaps: make(map[int]bitmap.Bitmap64),
  245. stgIDToLocalStgID: make(map[cdssdk.StorageID]int),
  246. }
  247. tree.nodes = make([]combinatorialTreeNode, (1 << len(stgBlocksMaps)))
  248. for id, mp := range stgBlocksMaps {
  249. tree.stgIDToLocalStgID[id] = len(tree.localStgIDToStgID)
  250. tree.blocksMaps[len(tree.localStgIDToStgID)] = *mp
  251. tree.localStgIDToStgID = append(tree.localStgIDToStgID, id)
  252. }
  253. tree.nodes[0].localHubID = -1
  254. index := 1
  255. tree.initNode(0, &tree.nodes[0], &index)
  256. return tree
  257. }
  258. func (t *combinatorialTree) initNode(minAvaiLocalHubID int, parent *combinatorialTreeNode, index *int) {
  259. for i := minAvaiLocalHubID; i < len(t.stgIDToLocalStgID); i++ {
  260. curIndex := *index
  261. *index++
  262. bitMp := t.blocksMaps[i]
  263. bitMp.Or(&parent.blocksBitmap)
  264. t.nodes[curIndex] = combinatorialTreeNode{
  265. localHubID: i,
  266. parent: parent,
  267. blocksBitmap: bitMp,
  268. }
  269. t.initNode(i+1, &t.nodes[curIndex], index)
  270. }
  271. }
  272. // 获得索引指定的节点所在的层
  273. func (t *combinatorialTree) GetDepth(index int) int {
  274. depth := 0
  275. // 反复判断节点在哪个子树。从左到右,子树节点的数量呈现8 4 2的变化,由此可以得到每个子树的索引值的范围
  276. subTreeCount := 1 << len(t.stgIDToLocalStgID)
  277. for index > 0 {
  278. if index < subTreeCount {
  279. // 定位到一个子树后,深度+1,然后进入这个子树,使用同样的方法再进行定位。
  280. // 进入子树后需要将索引值-1,因为要去掉子树的根节点
  281. index--
  282. depth++
  283. } else {
  284. // 如果索引值不在这个子树范围内,则将值减去子树的节点数量,
  285. // 这样每一次都可以视为使用同样的逻辑对不同大小的树进行判断。
  286. index -= subTreeCount
  287. }
  288. subTreeCount >>= 1
  289. }
  290. return depth
  291. }
  292. // 更新某一个算力中心节点的块分布位图,同时更新它对应组合树节点的所有子节点。
  293. // 如果更新到某个节点时,已有K个块,那么就不会再更新它的子节点
  294. func (t *combinatorialTree) UpdateBitmap(stgID cdssdk.StorageID, mp bitmap.Bitmap64, k int) {
  295. t.blocksMaps[t.stgIDToLocalStgID[stgID]] = mp
  296. // 首先定义两种遍历树节点时的移动方式:
  297. // 1. 竖直移动(深度增加):从一个节点移动到它最左边的子节点。每移动一步,index+1
  298. // 2. 水平移动:从一个节点移动到它右边的兄弟节点。每移动一步,根据它所在的深度,index+8,+4,+2
  299. // LocalID从0开始,将其+1后得到移动步数steps。
  300. // 将移动步数拆成多部分,分配到上述的两种移动方式上,并进行任意组合,且保证第一次为至少进行一次的竖直移动,移动之后的节点都会是同一个计算中心节点。
  301. steps := t.stgIDToLocalStgID[stgID] + 1
  302. for d := 1; d <= steps; d++ {
  303. t.iterCombBits(len(t.stgIDToLocalStgID)-1, steps-d, 0, func(i int) {
  304. index := d + i
  305. node := &t.nodes[index]
  306. newMp := t.blocksMaps[node.localHubID]
  307. newMp.Or(&node.parent.blocksBitmap)
  308. node.blocksBitmap = newMp
  309. if newMp.Weight() >= k {
  310. return
  311. }
  312. t.iterChildren(index, func(index, parentIndex, depth int) int {
  313. curNode := &t.nodes[index]
  314. parentNode := t.nodes[parentIndex]
  315. newMp := t.blocksMaps[curNode.localHubID]
  316. newMp.Or(&parentNode.blocksBitmap)
  317. curNode.blocksBitmap = newMp
  318. if newMp.Weight() >= k {
  319. return iterActionSkip
  320. }
  321. return iterActionNone
  322. })
  323. })
  324. }
  325. }
  326. // 遍历树,找到至少拥有K个块的树节点的最大深度
  327. func (t *combinatorialTree) FindKBlocksMaxDepth(k int) int {
  328. maxDepth := -1
  329. t.iterChildren(0, func(index, parentIndex, depth int) int {
  330. if t.nodes[index].blocksBitmap.Weight() >= k {
  331. if maxDepth < depth {
  332. maxDepth = depth
  333. }
  334. return iterActionSkip
  335. }
  336. // 如果到了叶子节点,还没有找到K个块,那就认为要满足K个块,至少需要再多一个节点,即深度+1。
  337. // 由于遍历时采用的是深度优先的算法,因此遍历到这个叶子节点时,叶子节点再加一个节点的组合已经在前面搜索过,
  338. // 所以用当前叶子节点深度+1来作为当前分支的结果就可以,即使当前情况下增加任意一个节点依然不够K块,
  339. // 可以使用同样的思路去递推到当前叶子节点增加两个块的情况。
  340. if t.nodes[index].localHubID == len(t.stgIDToLocalStgID)-1 {
  341. if maxDepth < depth+1 {
  342. maxDepth = depth + 1
  343. }
  344. }
  345. return iterActionNone
  346. })
  347. if maxDepth == -1 || maxDepth > len(t.stgIDToLocalStgID) {
  348. return len(t.stgIDToLocalStgID)
  349. }
  350. return maxDepth
  351. }
  352. func (t *combinatorialTree) iterCombBits(width int, count int, offset int, callback func(int)) {
  353. if count == 0 {
  354. callback(offset)
  355. return
  356. }
  357. for b := width; b >= count; b-- {
  358. t.iterCombBits(b-1, count-1, offset+(1<<b), callback)
  359. }
  360. }
  361. func (t *combinatorialTree) iterChildren(index int, do func(index int, parentIndex int, depth int) int) {
  362. curNode := &t.nodes[index]
  363. childIndex := index + 1
  364. curDepth := t.GetDepth(index)
  365. childCounts := len(t.stgIDToLocalStgID) - 1 - curNode.localHubID
  366. if childCounts == 0 {
  367. return
  368. }
  369. childTreeNodeCnt := 1 << (childCounts - 1)
  370. for c := 0; c < childCounts; c++ {
  371. act := t.itering(childIndex, index, curDepth+1, do)
  372. if act == iterActionBreak {
  373. return
  374. }
  375. childIndex += childTreeNodeCnt
  376. childTreeNodeCnt >>= 1
  377. }
  378. }
  379. func (t *combinatorialTree) itering(index int, parentIndex int, depth int, do func(index int, parentIndex int, depth int) int) int {
  380. act := do(index, parentIndex, depth)
  381. if act == iterActionBreak {
  382. return act
  383. }
  384. if act == iterActionSkip {
  385. return iterActionNone
  386. }
  387. curNode := &t.nodes[index]
  388. childIndex := index + 1
  389. childCounts := len(t.stgIDToLocalStgID) - 1 - curNode.localHubID
  390. if childCounts == 0 {
  391. return iterActionNone
  392. }
  393. childTreeNodeCnt := 1 << (childCounts - 1)
  394. for c := 0; c < childCounts; c++ {
  395. act = t.itering(childIndex, index, depth+1, do)
  396. if act == iterActionBreak {
  397. return act
  398. }
  399. childIndex += childTreeNodeCnt
  400. childTreeNodeCnt >>= 1
  401. }
  402. return iterActionNone
  403. }
  404. type combinatorialTreeNode struct {
  405. localHubID int
  406. parent *combinatorialTreeNode
  407. blocksBitmap bitmap.Bitmap64 // 选择了这个中心之后,所有中心一共包含多少种块
  408. }
  409. type annealingSolution struct {
  410. blockList []objectBlock // 所有节点的块分布情况
  411. rmBlocks []bool // 要删除哪些块
  412. disasterTolerance float64 // 本方案的容灾度
  413. spaceCost float64 // 本方案的冗余度
  414. minAccessCost float64 // 本方案的最小访问费用
  415. }
  416. func (t *CleanPinned) startAnnealing(allStgInfos map[cdssdk.StorageID]*stgmod.StorageDetail, readerStgIDs []cdssdk.StorageID, object annealingObject) annealingSolution {
  417. state := &annealingState{
  418. allStgInfos: allStgInfos,
  419. readerStgIDs: readerStgIDs,
  420. stgsSortedByReader: make(map[cdssdk.StorageID][]stgDist),
  421. object: object,
  422. stgBlockBitmaps: make(map[cdssdk.StorageID]*bitmap.Bitmap64),
  423. }
  424. t.initBlockList(state)
  425. if state.blockList == nil {
  426. return annealingSolution{}
  427. }
  428. t.initNodeBlockBitmap(state)
  429. t.sortNodeByReaderDistance(state)
  430. state.rmBlocks = make([]bool, len(state.blockList))
  431. state.inversedIndex = -1
  432. state.stgCombTree = newCombinatorialTree(state.stgBlockBitmaps)
  433. state.lastScore = t.calcScore(state)
  434. state.maxScore = state.lastScore
  435. state.maxScoreRmBlocks = lo2.ArrayClone(state.rmBlocks)
  436. // 模拟退火算法的温度
  437. curTemp := state.lastScore
  438. // 结束温度
  439. finalTemp := curTemp * 0.2
  440. // 冷却率
  441. coolingRate := 0.95
  442. for curTemp > finalTemp {
  443. state.inversedIndex = rand.Intn(len(state.rmBlocks))
  444. block := state.blockList[state.inversedIndex]
  445. state.rmBlocks[state.inversedIndex] = !state.rmBlocks[state.inversedIndex]
  446. state.stgBlockBitmaps[block.StorageID].Set(block.Index, !state.rmBlocks[state.inversedIndex])
  447. state.stgCombTree.UpdateBitmap(block.StorageID, *state.stgBlockBitmaps[block.StorageID], state.object.minBlockCnt)
  448. curScore := t.calcScore(state)
  449. dScore := curScore - state.lastScore
  450. // 如果新方案比旧方案得分低,且没有要求强制接受新方案,那么就将变化改回去
  451. if curScore == 0 || (dScore < 0 && !t.alwaysAccept(curTemp, dScore, coolingRate)) {
  452. state.rmBlocks[state.inversedIndex] = !state.rmBlocks[state.inversedIndex]
  453. state.stgBlockBitmaps[block.StorageID].Set(block.Index, !state.rmBlocks[state.inversedIndex])
  454. state.stgCombTree.UpdateBitmap(block.StorageID, *state.stgBlockBitmaps[block.StorageID], state.object.minBlockCnt)
  455. // fmt.Printf("\n")
  456. } else {
  457. // fmt.Printf(" accept!\n")
  458. state.lastScore = curScore
  459. if state.maxScore < curScore {
  460. state.maxScore = state.lastScore
  461. state.maxScoreRmBlocks = lo2.ArrayClone(state.rmBlocks)
  462. }
  463. }
  464. curTemp *= coolingRate
  465. }
  466. // fmt.Printf("final: %v\n", state.maxScoreRmBlocks)
  467. return annealingSolution{
  468. blockList: state.blockList,
  469. rmBlocks: state.maxScoreRmBlocks,
  470. disasterTolerance: state.lastDisasterTolerance,
  471. spaceCost: state.lastSpaceCost,
  472. minAccessCost: state.lastMinAccessCost,
  473. }
  474. }
  475. func (t *CleanPinned) initBlockList(ctx *annealingState) {
  476. blocksMap := make(map[cdssdk.StorageID][]objectBlock)
  477. // 先生成所有的影子块
  478. for _, pinned := range ctx.object.pinnedAt {
  479. blocks := make([]objectBlock, 0, ctx.object.totalBlockCount)
  480. for i := 0; i < ctx.object.totalBlockCount; i++ {
  481. blocks = append(blocks, objectBlock{
  482. Index: i,
  483. StorageID: pinned,
  484. HasShadow: true,
  485. })
  486. }
  487. blocksMap[pinned] = blocks
  488. }
  489. // 再填充实际块
  490. for _, b := range ctx.object.blocks {
  491. blocks := blocksMap[b.StorageID]
  492. has := false
  493. for i := range blocks {
  494. if blocks[i].Index == b.Index {
  495. blocks[i].HasEntity = true
  496. blocks[i].FileHash = b.FileHash
  497. has = true
  498. break
  499. }
  500. }
  501. if has {
  502. continue
  503. }
  504. blocks = append(blocks, objectBlock{
  505. Index: b.Index,
  506. StorageID: b.StorageID,
  507. HasEntity: true,
  508. FileHash: b.FileHash,
  509. Size: b.Size,
  510. })
  511. blocksMap[b.StorageID] = blocks
  512. }
  513. var sortedBlocks []objectBlock
  514. for _, bs := range blocksMap {
  515. sortedBlocks = append(sortedBlocks, bs...)
  516. }
  517. sortedBlocks = sort2.Sort(sortedBlocks, func(left objectBlock, right objectBlock) int {
  518. d := left.StorageID - right.StorageID
  519. if d != 0 {
  520. return int(d)
  521. }
  522. return left.Index - right.Index
  523. })
  524. ctx.blockList = sortedBlocks
  525. }
  526. func (t *CleanPinned) initNodeBlockBitmap(state *annealingState) {
  527. for _, b := range state.blockList {
  528. mp, ok := state.stgBlockBitmaps[b.StorageID]
  529. if !ok {
  530. nb := bitmap.Bitmap64(0)
  531. mp = &nb
  532. state.stgBlockBitmaps[b.StorageID] = mp
  533. }
  534. mp.Set(b.Index, true)
  535. }
  536. }
  537. func (t *CleanPinned) sortNodeByReaderDistance(state *annealingState) {
  538. for _, r := range state.readerStgIDs {
  539. var nodeDists []stgDist
  540. for n := range state.stgBlockBitmaps {
  541. if r == n {
  542. // 同节点时距离视为0.1
  543. nodeDists = append(nodeDists, stgDist{
  544. StorageID: n,
  545. Distance: consts.StorageDistanceSameStorage,
  546. })
  547. } else if state.allStgInfos[r].MasterHub.LocationID == state.allStgInfos[n].MasterHub.LocationID {
  548. // 同地区时距离视为1
  549. nodeDists = append(nodeDists, stgDist{
  550. StorageID: n,
  551. Distance: consts.StorageDistanceSameLocation,
  552. })
  553. } else {
  554. // 不同地区时距离视为5
  555. nodeDists = append(nodeDists, stgDist{
  556. StorageID: n,
  557. Distance: consts.StorageDistanceOther,
  558. })
  559. }
  560. }
  561. state.stgsSortedByReader[r] = sort2.Sort(nodeDists, func(left, right stgDist) int { return sort2.Cmp(left.Distance, right.Distance) })
  562. }
  563. }
  564. func (t *CleanPinned) calcScore(state *annealingState) float64 {
  565. dt := t.calcDisasterTolerance(state)
  566. ac := t.calcMinAccessCost(state)
  567. sc := t.calcSpaceCost(state)
  568. state.lastDisasterTolerance = dt
  569. state.lastMinAccessCost = ac
  570. state.lastSpaceCost = sc
  571. dtSc := 1.0
  572. if dt < 1 {
  573. dtSc = 0
  574. } else if dt >= 2 {
  575. dtSc = 1.5
  576. }
  577. newSc := 0.0
  578. if dt == 0 || ac == 0 {
  579. newSc = 0
  580. } else {
  581. newSc = dtSc / (sc * ac)
  582. }
  583. // fmt.Printf("solu: %v, cur: %v, dt: %v, ac: %v, sc: %v \n", state.rmBlocks, newSc, dt, ac, sc)
  584. return newSc
  585. }
  586. // 计算容灾度
  587. func (t *CleanPinned) calcDisasterTolerance(state *annealingState) float64 {
  588. if state.inversedIndex != -1 {
  589. node := state.blockList[state.inversedIndex]
  590. state.stgCombTree.UpdateBitmap(node.StorageID, *state.stgBlockBitmaps[node.StorageID], state.object.minBlockCnt)
  591. }
  592. return float64(len(state.stgBlockBitmaps) - state.stgCombTree.FindKBlocksMaxDepth(state.object.minBlockCnt))
  593. }
  594. // 计算最小访问数据的代价
  595. func (t *CleanPinned) calcMinAccessCost(state *annealingState) float64 {
  596. cost := math.MaxFloat64
  597. for _, reader := range state.readerStgIDs {
  598. tarNodes := state.stgsSortedByReader[reader]
  599. gotBlocks := bitmap.Bitmap64(0)
  600. thisCost := 0.0
  601. for _, tar := range tarNodes {
  602. tarNodeMp := state.stgBlockBitmaps[tar.StorageID]
  603. // 只需要从目的节点上获得缺少的块
  604. curWeigth := gotBlocks.Weight()
  605. // 下面的if会在拿到k个块之后跳出循环,所以or多了块也没关系
  606. gotBlocks.Or(tarNodeMp)
  607. // 但是算读取块的消耗时,不能多算,最多算读了k个块的消耗
  608. willGetBlocks := math2.Min(gotBlocks.Weight()-curWeigth, state.object.minBlockCnt-curWeigth)
  609. thisCost += float64(willGetBlocks) * float64(tar.Distance)
  610. if gotBlocks.Weight() >= state.object.minBlockCnt {
  611. break
  612. }
  613. }
  614. if gotBlocks.Weight() >= state.object.minBlockCnt {
  615. cost = math.Min(cost, thisCost)
  616. }
  617. }
  618. return cost
  619. }
  620. // 计算冗余度
  621. func (t *CleanPinned) calcSpaceCost(ctx *annealingState) float64 {
  622. blockCount := 0
  623. for i, b := range ctx.blockList {
  624. if ctx.rmBlocks[i] {
  625. continue
  626. }
  627. if b.HasEntity {
  628. blockCount++
  629. }
  630. if b.HasShadow {
  631. blockCount++
  632. }
  633. }
  634. // 所有算力中心上拥有的块的总数 / 一个对象被分成了几个块
  635. return float64(blockCount) / float64(ctx.object.minBlockCnt)
  636. }
  637. // 如果新方案得分比旧方案小,那么在一定概率内也接受新方案
  638. func (t *CleanPinned) alwaysAccept(curTemp float64, dScore float64, coolingRate float64) bool {
  639. v := math.Exp(dScore / curTemp / coolingRate)
  640. // fmt.Printf(" -- chance: %v, temp: %v", v, curTemp)
  641. return v > rand.Float64()
  642. }
  643. func (t *CleanPinned) makePlansForRepObject(allStgInfos map[cdssdk.StorageID]*stgmod.StorageDetail, solu annealingSolution, obj stgmod.ObjectDetail, planBld *exec.PlanBuilder, planningHubIDs map[cdssdk.StorageID]bool) coormq.UpdatingObjectRedundancy {
  644. entry := coormq.UpdatingObjectRedundancy{
  645. ObjectID: obj.Object.ObjectID,
  646. Redundancy: obj.Object.Redundancy,
  647. }
  648. ft := ioswitch2.NewFromTo()
  649. fromStg := allStgInfos[obj.Blocks[0].StorageID]
  650. ft.AddFrom(ioswitch2.NewFromShardstore(obj.Object.FileHash, *fromStg.MasterHub, *fromStg, ioswitch2.RawStream()))
  651. for i, f := range solu.rmBlocks {
  652. hasCache := lo.ContainsBy(obj.Blocks, func(b stgmod.ObjectBlock) bool { return b.StorageID == solu.blockList[i].StorageID }) ||
  653. lo.ContainsBy(obj.PinnedAt, func(n cdssdk.StorageID) bool { return n == solu.blockList[i].StorageID })
  654. willRm := f
  655. if !willRm {
  656. // 如果对象在退火后要保留副本的节点没有副本,则需要在这个节点创建副本
  657. if !hasCache {
  658. toStg := allStgInfos[solu.blockList[i].StorageID]
  659. ft.AddTo(ioswitch2.NewToShardStore(*toStg.MasterHub, *toStg, ioswitch2.RawStream(), fmt.Sprintf("%d.0", obj.Object.ObjectID)))
  660. planningHubIDs[solu.blockList[i].StorageID] = true
  661. }
  662. entry.Blocks = append(entry.Blocks, stgmod.ObjectBlock{
  663. ObjectID: obj.Object.ObjectID,
  664. Index: solu.blockList[i].Index,
  665. StorageID: solu.blockList[i].StorageID,
  666. FileHash: obj.Object.FileHash,
  667. Size: solu.blockList[i].Size,
  668. })
  669. }
  670. }
  671. err := parser.Parse(ft, planBld)
  672. if err != nil {
  673. // TODO 错误处理
  674. }
  675. return entry
  676. }
  677. func (t *CleanPinned) generateSysEventForRepObject(solu annealingSolution, obj stgmod.ObjectDetail) []stgmod.SysEventBody {
  678. var blockChgs []stgmod.BlockChange
  679. for i, f := range solu.rmBlocks {
  680. hasCache := lo.ContainsBy(obj.Blocks, func(b stgmod.ObjectBlock) bool { return b.StorageID == solu.blockList[i].StorageID }) ||
  681. lo.ContainsBy(obj.PinnedAt, func(n cdssdk.StorageID) bool { return n == solu.blockList[i].StorageID })
  682. willRm := f
  683. if !willRm {
  684. // 如果对象在退火后要保留副本的节点没有副本,则需要在这个节点创建副本
  685. if !hasCache {
  686. blockChgs = append(blockChgs, &stgmod.BlockChangeClone{
  687. BlockType: stgmod.BlockTypeRaw,
  688. SourceStorageID: obj.Blocks[0].StorageID,
  689. TargetStorageID: solu.blockList[i].StorageID,
  690. })
  691. }
  692. } else {
  693. blockChgs = append(blockChgs, &stgmod.BlockChangeDeleted{
  694. Index: 0,
  695. StorageID: solu.blockList[i].StorageID,
  696. })
  697. }
  698. }
  699. transEvt := &stgmod.BodyBlockTransfer{
  700. ObjectID: obj.Object.ObjectID,
  701. PackageID: obj.Object.PackageID,
  702. BlockChanges: blockChgs,
  703. }
  704. var blockDist []stgmod.BlockDistributionObjectInfo
  705. for i, f := range solu.rmBlocks {
  706. if !f {
  707. blockDist = append(blockDist, stgmod.BlockDistributionObjectInfo{
  708. BlockType: stgmod.BlockTypeRaw,
  709. Index: 0,
  710. StorageID: solu.blockList[i].StorageID,
  711. })
  712. }
  713. }
  714. distEvt := &stgmod.BodyBlockDistribution{
  715. ObjectID: obj.Object.ObjectID,
  716. PackageID: obj.Object.PackageID,
  717. Path: obj.Object.Path,
  718. Size: obj.Object.Size,
  719. FileHash: obj.Object.FileHash,
  720. FaultTolerance: solu.disasterTolerance,
  721. Redundancy: solu.spaceCost,
  722. AvgAccessCost: 0, // TODO 计算平均访问代价,从日常访问数据中统计
  723. BlockDistribution: blockDist,
  724. // TODO 不好计算传输量
  725. }
  726. return []stgmod.SysEventBody{transEvt, distEvt}
  727. }
  728. func (t *CleanPinned) makePlansForECObject(allStgInfos map[cdssdk.StorageID]*stgmod.StorageDetail, solu annealingSolution, obj stgmod.ObjectDetail, planBld *exec.PlanBuilder, planningHubIDs map[cdssdk.StorageID]bool) coormq.UpdatingObjectRedundancy {
  729. entry := coormq.UpdatingObjectRedundancy{
  730. ObjectID: obj.Object.ObjectID,
  731. Redundancy: obj.Object.Redundancy,
  732. }
  733. reconstrct := make(map[cdssdk.StorageID]*[]int)
  734. for i, f := range solu.rmBlocks {
  735. block := solu.blockList[i]
  736. if !f {
  737. entry.Blocks = append(entry.Blocks, stgmod.ObjectBlock{
  738. ObjectID: obj.Object.ObjectID,
  739. Index: block.Index,
  740. StorageID: block.StorageID,
  741. FileHash: block.FileHash,
  742. Size: block.Size,
  743. })
  744. // 如果这个块是影子块,那么就要从完整对象里重建这个块
  745. if !block.HasEntity {
  746. re, ok := reconstrct[block.StorageID]
  747. if !ok {
  748. re = &[]int{}
  749. reconstrct[block.StorageID] = re
  750. }
  751. *re = append(*re, block.Index)
  752. }
  753. }
  754. }
  755. ecRed := obj.Object.Redundancy.(*cdssdk.ECRedundancy)
  756. for id, idxs := range reconstrct {
  757. // 依次生成每个节点上的执行计划,因为如果放到一个计划里一起生成,不能保证每个节点上的块用的都是本节点上的副本
  758. ft := ioswitch2.NewFromTo()
  759. ft.ECParam = ecRed
  760. ft.AddFrom(ioswitch2.NewFromShardstore(obj.Object.FileHash, *allStgInfos[id].MasterHub, *allStgInfos[id], ioswitch2.RawStream()))
  761. for _, i := range *idxs {
  762. ft.AddTo(ioswitch2.NewToShardStore(*allStgInfos[id].MasterHub, *allStgInfos[id], ioswitch2.ECStream(i), fmt.Sprintf("%d.%d", obj.Object.ObjectID, i)))
  763. }
  764. err := parser.Parse(ft, planBld)
  765. if err != nil {
  766. // TODO 错误处理
  767. continue
  768. }
  769. planningHubIDs[id] = true
  770. }
  771. return entry
  772. }
  773. func (t *CleanPinned) generateSysEventForECObject(solu annealingSolution, obj stgmod.ObjectDetail) []stgmod.SysEventBody {
  774. var blockChgs []stgmod.BlockChange
  775. reconstrct := make(map[cdssdk.StorageID]*[]int)
  776. for i, f := range solu.rmBlocks {
  777. block := solu.blockList[i]
  778. if !f {
  779. // 如果这个块是影子块,那么就要从完整对象里重建这个块
  780. if !block.HasEntity {
  781. re, ok := reconstrct[block.StorageID]
  782. if !ok {
  783. re = &[]int{}
  784. reconstrct[block.StorageID] = re
  785. }
  786. *re = append(*re, block.Index)
  787. }
  788. } else {
  789. blockChgs = append(blockChgs, &stgmod.BlockChangeDeleted{
  790. Index: block.Index,
  791. StorageID: block.StorageID,
  792. })
  793. }
  794. }
  795. // 由于每一个需要被重建的块都是从同中心的副本里构建出来的,所以对于每一个中心都要产生一个BlockChangeEnDecode
  796. for id, idxs := range reconstrct {
  797. var tarBlocks []stgmod.Block
  798. for _, idx := range *idxs {
  799. tarBlocks = append(tarBlocks, stgmod.Block{
  800. BlockType: stgmod.BlockTypeEC,
  801. Index: idx,
  802. StorageID: id,
  803. })
  804. }
  805. blockChgs = append(blockChgs, &stgmod.BlockChangeEnDecode{
  806. SourceBlocks: []stgmod.Block{{
  807. BlockType: stgmod.BlockTypeRaw,
  808. Index: 0,
  809. StorageID: id, // 影子块的原始对象就在同一个节点上
  810. }},
  811. TargetBlocks: tarBlocks,
  812. // 传输量为0
  813. })
  814. }
  815. transEvt := &stgmod.BodyBlockTransfer{
  816. ObjectID: obj.Object.ObjectID,
  817. PackageID: obj.Object.PackageID,
  818. BlockChanges: blockChgs,
  819. }
  820. var blockDist []stgmod.BlockDistributionObjectInfo
  821. for i, f := range solu.rmBlocks {
  822. if !f {
  823. blockDist = append(blockDist, stgmod.BlockDistributionObjectInfo{
  824. BlockType: stgmod.BlockTypeEC,
  825. Index: solu.blockList[i].Index,
  826. StorageID: solu.blockList[i].StorageID,
  827. })
  828. }
  829. }
  830. distEvt := &stgmod.BodyBlockDistribution{
  831. ObjectID: obj.Object.ObjectID,
  832. PackageID: obj.Object.PackageID,
  833. Path: obj.Object.Path,
  834. Size: obj.Object.Size,
  835. FileHash: obj.Object.FileHash,
  836. FaultTolerance: solu.disasterTolerance,
  837. Redundancy: solu.spaceCost,
  838. AvgAccessCost: 0, // TODO 计算平均访问代价,从日常访问数据中统计
  839. BlockDistribution: blockDist,
  840. // TODO 不好计算传输量
  841. }
  842. return []stgmod.SysEventBody{transEvt, distEvt}
  843. }
  844. func (t *CleanPinned) executePlans(ctx ExecuteContext, planBld *exec.PlanBuilder, planningStgIDs map[cdssdk.StorageID]bool) (map[string]exec.VarValue, error) {
  845. // 统一加锁,有重复也没关系
  846. lockBld := reqbuilder.NewBuilder()
  847. for id := range planningStgIDs {
  848. lockBld.Shard().Buzy(id)
  849. }
  850. lock, err := lockBld.MutexLock(ctx.Args.DistLock)
  851. if err != nil {
  852. return nil, fmt.Errorf("acquiring distlock: %w", err)
  853. }
  854. defer lock.Unlock()
  855. wg := sync.WaitGroup{}
  856. // 执行IO计划
  857. var ioSwRets map[string]exec.VarValue
  858. var ioSwErr error
  859. wg.Add(1)
  860. go func() {
  861. defer wg.Done()
  862. execCtx := exec.NewExecContext()
  863. exec.SetValueByType(execCtx, ctx.Args.StgMgr)
  864. ret, err := planBld.Execute(execCtx).Wait(context.TODO())
  865. if err != nil {
  866. ioSwErr = fmt.Errorf("executing io switch plan: %w", err)
  867. return
  868. }
  869. ioSwRets = ret
  870. }()
  871. wg.Wait()
  872. if ioSwErr != nil {
  873. return nil, ioSwErr
  874. }
  875. return ioSwRets, nil
  876. }
  877. func (t *CleanPinned) populateECObjectEntry(entry *coormq.UpdatingObjectRedundancy, obj stgmod.ObjectDetail, ioRets map[string]exec.VarValue) {
  878. for i := range entry.Blocks {
  879. if entry.Blocks[i].FileHash != "" {
  880. continue
  881. }
  882. key := fmt.Sprintf("%d.%d", obj.Object.ObjectID, entry.Blocks[i].Index)
  883. // 不应该出现key不存在的情况
  884. r := ioRets[key].(*ops2.ShardInfoValue)
  885. entry.Blocks[i].FileHash = r.Hash
  886. entry.Blocks[i].Size = r.Size
  887. }
  888. }
  889. func init() {
  890. RegisterMessageConvertor(NewCleanPinned)
  891. }

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