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.

ec_object_iterator.go 6.4 kB

2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package iterator
  2. import (
  3. "fmt"
  4. "io"
  5. "math/rand"
  6. "os"
  7. "github.com/samber/lo"
  8. "gitlink.org.cn/cloudream/common/pkgs/logger"
  9. stgsdk "gitlink.org.cn/cloudream/common/sdks/storage"
  10. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  11. stgmodels "gitlink.org.cn/cloudream/storage/common/models"
  12. "gitlink.org.cn/cloudream/storage/common/pkgs/db/model"
  13. "gitlink.org.cn/cloudream/storage/common/pkgs/ec"
  14. coormq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/coordinator"
  15. )
  16. type ECObjectIterator struct {
  17. OnClosing func()
  18. objects []model.Object
  19. objectECData []stgmodels.ObjectECData
  20. currentIndex int
  21. inited bool
  22. ecInfo stgsdk.ECRedundancyInfo
  23. ec model.Ec
  24. downloadCtx *DownloadContext
  25. cliLocation model.Location
  26. }
  27. func NewECObjectIterator(objects []model.Object, objectECData []stgmodels.ObjectECData, ecInfo stgsdk.ECRedundancyInfo, ec model.Ec, downloadCtx *DownloadContext) *ECObjectIterator {
  28. return &ECObjectIterator{
  29. objects: objects,
  30. objectECData: objectECData,
  31. ecInfo: ecInfo,
  32. ec: ec,
  33. downloadCtx: downloadCtx,
  34. }
  35. }
  36. func (i *ECObjectIterator) MoveNext() (*IterDownloadingObject, error) {
  37. // TODO 加锁
  38. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  39. if err != nil {
  40. return nil, fmt.Errorf("new coordinator client: %w", err)
  41. }
  42. defer coorCli.Close()
  43. if !i.inited {
  44. i.inited = true
  45. findCliLocResp, err := coorCli.FindClientLocation(coormq.NewFindClientLocation(stgglb.Local.ExternalIP))
  46. if err != nil {
  47. return nil, fmt.Errorf("finding client location: %w", err)
  48. }
  49. i.cliLocation = findCliLocResp.Location
  50. }
  51. if i.currentIndex >= len(i.objects) {
  52. return nil, ErrNoMoreItem
  53. }
  54. item, err := i.doMove(coorCli)
  55. i.currentIndex++
  56. return item, err
  57. }
  58. func (iter *ECObjectIterator) doMove(coorCli *coormq.PoolClient) (*IterDownloadingObject, error) {
  59. obj := iter.objects[iter.currentIndex]
  60. ecData := iter.objectECData[iter.currentIndex]
  61. blocks := ecData.Blocks
  62. ec := iter.ec
  63. ecK := ec.EcK
  64. ecN := ec.EcN
  65. //采取直接读,优先选内网节点
  66. hashs := make([]string, ecK)
  67. nds := make([]DownloadNodeInfo, ecK)
  68. for i := 0; i < ecK; i++ {
  69. hashs[i] = blocks[i].FileHash
  70. getNodesResp, err := coorCli.GetNodes(coormq.NewGetNodes(blocks[i].NodeIDs))
  71. if err != nil {
  72. return nil, fmt.Errorf("getting nodes: %w", err)
  73. }
  74. downloadNodes := lo.Map(getNodesResp.Nodes, func(node model.Node, index int) DownloadNodeInfo {
  75. return DownloadNodeInfo{
  76. Node: node,
  77. IsSameLocation: node.LocationID == iter.cliLocation.LocationID,
  78. }
  79. })
  80. nds[i] = iter.chooseDownloadNode(downloadNodes)
  81. }
  82. //nodeIDs, nodeIPs直接按照第1~ecK个排列
  83. nodeIDs := make([]int64, ecK)
  84. nodeIPs := make([]string, ecK)
  85. for i := 0; i < ecK; i++ {
  86. nodeIDs[i] = nds[i].Node.NodeID
  87. nodeIPs[i] = nds[i].Node.ExternalIP
  88. if nds[i].IsSameLocation {
  89. nodeIPs[i] = nds[i].Node.LocalIP
  90. logger.Infof("client and node %d are at the same location, use local ip", nds[i].Node.NodeID)
  91. }
  92. }
  93. fileSize := obj.Size
  94. blockIDs := make([]int, ecK)
  95. for i := 0; i < ecK; i++ {
  96. blockIDs[i] = i
  97. }
  98. reader, err := iter.downloadEcObject(fileSize, ecK, ecN, blockIDs, nodeIDs, nodeIPs, hashs)
  99. if err != nil {
  100. return nil, fmt.Errorf("ec read failed, err: %w", err)
  101. }
  102. return &IterDownloadingObject{
  103. File: reader,
  104. }, nil
  105. }
  106. func (i *ECObjectIterator) Close() {
  107. if i.OnClosing != nil {
  108. i.OnClosing()
  109. }
  110. }
  111. // chooseDownloadNode 选择一个下载节点
  112. // 1. 从与当前客户端相同地域的节点中随机选一个
  113. // 2. 没有用的话从所有节点中随机选一个
  114. func (i *ECObjectIterator) chooseDownloadNode(entries []DownloadNodeInfo) DownloadNodeInfo {
  115. sameLocationEntries := lo.Filter(entries, func(e DownloadNodeInfo, i int) bool { return e.IsSameLocation })
  116. if len(sameLocationEntries) > 0 {
  117. return sameLocationEntries[rand.Intn(len(sameLocationEntries))]
  118. }
  119. return entries[rand.Intn(len(entries))]
  120. }
  121. func (iter *ECObjectIterator) downloadEcObject(fileSize int64, ecK int, ecN int, blockIDs []int, nodeIDs []int64, nodeIPs []string, hashs []string) (io.ReadCloser, error) {
  122. // TODO zkx 先试用同步方式实现逻辑,做好错误处理。同时也方便下面直接使用uploadToNode和uploadToLocalIPFS来优化代码结构
  123. //wg := sync.WaitGroup{}
  124. numPacket := (fileSize + int64(ecK)*iter.ecInfo.PacketSize - 1) / (int64(ecK) * iter.ecInfo.PacketSize)
  125. getBufs := make([]chan []byte, ecN)
  126. decodeBufs := make([]chan []byte, ecK)
  127. for i := 0; i < ecN; i++ {
  128. getBufs[i] = make(chan []byte)
  129. }
  130. for i := 0; i < ecK; i++ {
  131. decodeBufs[i] = make(chan []byte)
  132. }
  133. for idx := 0; idx < len(blockIDs); idx++ {
  134. i := idx
  135. go func() {
  136. // TODO 处理错误
  137. file, _ := downloadFile(iter.downloadCtx, nodeIDs[i], nodeIPs[i], hashs[i])
  138. for p := int64(0); p < numPacket; p++ {
  139. buf := make([]byte, iter.ecInfo.PacketSize)
  140. // TODO 处理错误
  141. io.ReadFull(file, buf)
  142. getBufs[blockIDs[i]] <- buf
  143. }
  144. }()
  145. }
  146. print(numPacket)
  147. go decode(getBufs[:], decodeBufs[:], blockIDs, ecK, numPacket)
  148. r, w := io.Pipe()
  149. //persist函数,将解码得到的文件写入pipe
  150. go func() {
  151. for i := 0; int64(i) < numPacket; i++ {
  152. for j := 0; j < len(decodeBufs); j++ {
  153. tmp := <-decodeBufs[j]
  154. _, err := w.Write(tmp)
  155. if err != nil {
  156. fmt.Errorf("persist file falied, err:%w", err)
  157. }
  158. }
  159. }
  160. w.Close()
  161. }()
  162. return r, nil
  163. }
  164. func decode(inBufs []chan []byte, outBufs []chan []byte, blockSeq []int, ecK int, numPacket int64) {
  165. fmt.Println("decode ")
  166. var tmpIn [][]byte
  167. var zeroPkt []byte
  168. tmpIn = make([][]byte, len(inBufs))
  169. hasBlock := map[int]bool{}
  170. for j := 0; j < len(blockSeq); j++ {
  171. hasBlock[blockSeq[j]] = true
  172. }
  173. needRepair := false //检测是否传入了所有数据块
  174. for j := 0; j < len(outBufs); j++ {
  175. if blockSeq[j] != j {
  176. needRepair = true
  177. }
  178. }
  179. enc := ec.NewRsEnc(ecK, len(inBufs))
  180. for i := 0; int64(i) < numPacket; i++ {
  181. print("!!!!!")
  182. for j := 0; j < len(inBufs); j++ {
  183. if hasBlock[j] {
  184. tmpIn[j] = <-inBufs[j]
  185. } else {
  186. tmpIn[j] = zeroPkt
  187. }
  188. }
  189. if needRepair {
  190. err := enc.Repair(tmpIn)
  191. if err != nil {
  192. fmt.Fprintf(os.Stderr, "Decode Repair Error: %s", err.Error())
  193. }
  194. }
  195. for j := 0; j < len(outBufs); j++ {
  196. outBufs[j] <- tmpIn[j]
  197. }
  198. }
  199. for i := 0; i < len(outBufs); i++ {
  200. close(outBufs[i])
  201. }
  202. }

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