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.

storage.go 6.1 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package cmd
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "sync"
  8. "gitlink.org.cn/cloudream/agent/internal/config"
  9. "gitlink.org.cn/cloudream/common/consts"
  10. log "gitlink.org.cn/cloudream/common/pkg/logger"
  11. "gitlink.org.cn/cloudream/common/utils"
  12. "gitlink.org.cn/cloudream/common/utils/serder"
  13. "gitlink.org.cn/cloudream/ec"
  14. "gitlink.org.cn/cloudream/common/consts/errorcode"
  15. ramsg "gitlink.org.cn/cloudream/rabbitmq/message"
  16. agtmsg "gitlink.org.cn/cloudream/rabbitmq/message/agent"
  17. )
  18. func (service *Service) MoveObjectToStorage(msg *agtmsg.MoveObjectToStorage) *agtmsg.MoveObjectToStorageResp {
  19. outFileName := utils.MakeMoveOperationFileName(msg.Body.ObjectID, msg.Body.UserID)
  20. outFileDir := filepath.Join(config.Cfg().StorageBaseDir, msg.Body.Directory)
  21. outFilePath := filepath.Join(outFileDir, outFileName)
  22. err := os.MkdirAll(outFileDir, 0644)
  23. if err != nil {
  24. log.Warnf("create file directory %s failed, err: %s", outFileDir, err.Error())
  25. return ramsg.ReplyFailed[agtmsg.MoveObjectToStorageResp](errorcode.OPERATION_FAILED, "create local file directory failed")
  26. }
  27. outFile, err := os.Create(outFilePath)
  28. if err != nil {
  29. log.Warnf("create file %s failed, err: %s", outFilePath, err.Error())
  30. return ramsg.ReplyFailed[agtmsg.MoveObjectToStorageResp](errorcode.OPERATION_FAILED, "create local file failed")
  31. }
  32. defer outFile.Close()
  33. if msg.Body.Redundancy == consts.REDUNDANCY_REP {
  34. err = service.moveRepObject(msg, outFile)
  35. if err != nil {
  36. log.Warnf("move rep object failed, err: %s", outFilePath, err.Error())
  37. return ramsg.ReplyFailed[agtmsg.MoveObjectToStorageResp](errorcode.OPERATION_FAILED, "move rep object failed")
  38. }
  39. } else {
  40. return ramsg.ReplyFailed[agtmsg.MoveObjectToStorageResp](errorcode.OPERATION_FAILED, "not implement yet!")
  41. }
  42. return ramsg.ReplyOK(agtmsg.NewMoveObjectToStorageRespBody())
  43. }
  44. func (svc *Service) moveRepObject(msg *agtmsg.MoveObjectToStorage, outFile io.WriteCloser) error {
  45. var repInfo ramsg.ObjectRepInfo
  46. err := serder.MapToObject(msg.Body.RedundancyData.(map[string]any), &repInfo)
  47. if err != nil {
  48. return fmt.Errorf("redundancy data to rep info failed, err: %w", err)
  49. }
  50. ipfsRd, err := svc.ipfs.OpenRead(repInfo.FileHash)
  51. if err != nil {
  52. return fmt.Errorf("read ipfs file failed, err: %w", err)
  53. }
  54. defer ipfsRd.Close()
  55. _, err = io.Copy(outFile, ipfsRd)
  56. if err != nil {
  57. return fmt.Errorf("copy ipfs file data to local file failed, err: %s", err)
  58. }
  59. return nil
  60. }
  61. /*
  62. func (service *Service) ECMove(msg *agtmsg.ECMoveCommand) *agtmsg.MoveObjectToStorageResp {
  63. panic("not implement yet!")
  64. wg := sync.WaitGroup{}
  65. fmt.Println("EcMove")
  66. fmt.Println(msg.Hashs)
  67. hashs := msg.Hashs
  68. fileSize := msg.FileSize
  69. blockIds := msg.IDs
  70. ecName := msg.ECName
  71. goalName := msg.BucketName + ":" + msg.ObjectName + ":" + strconv.Itoa(msg.UserID)
  72. ecPolicies := *utils.GetEcPolicy()
  73. ecPolicy := ecPolicies[ecName]
  74. ecK := ecPolicy.GetK()
  75. ecN := ecPolicy.GetN()
  76. numPacket := (fileSize + int64(ecK)*int64(config.Cfg().GRCPPacketSize) - 1) / (int64(ecK) * int64(config.Cfg().GRCPPacketSize))
  77. getBufs := make([]chan []byte, ecN)
  78. decodeBufs := make([]chan []byte, ecK)
  79. for i := 0; i < ecN; i++ {
  80. getBufs[i] = make(chan []byte)
  81. }
  82. for i := 0; i < ecK; i++ {
  83. decodeBufs[i] = make(chan []byte)
  84. }
  85. wg.Add(1)
  86. //执行调度操作
  87. // TODO 这一块需要改写以适配IPFS流式读取
  88. for i := 0; i < len(blockIds); i++ {
  89. go service.get(hashs[i], getBufs[blockIds[i]], numPacket)
  90. }
  91. go decode(getBufs[:], decodeBufs[:], blockIds, ecK, numPacket)
  92. // TODO 写入的文件路径需要带上msg中的Directory字段,参考RepMove
  93. go persist(decodeBufs[:], numPacket, goalName, &wg)
  94. wg.Wait()
  95. //向coor报告临时缓存hash
  96. coorClient, err := racli.NewCoordinatorClient()
  97. if err != nil {
  98. // TODO 日志
  99. return ramsg.NewAgentMoveRespFailed(errorcode.OPERATION_FAILED, fmt.Sprintf("create coordinator client failed"))
  100. }
  101. defer coorClient.Close()
  102. coorClient.TempCacheReport(NodeID, hashs)
  103. return ramsg.NewAgentMoveRespOK()
  104. }
  105. */
  106. func decode(inBufs []chan []byte, outBufs []chan []byte, blockSeq []int, ecK int, numPacket int64) {
  107. fmt.Println("decode ")
  108. var tmpIn [][]byte
  109. var zeroPkt []byte
  110. tmpIn = make([][]byte, len(inBufs))
  111. hasBlock := map[int]bool{}
  112. for j := 0; j < len(blockSeq); j++ {
  113. hasBlock[blockSeq[j]] = true
  114. }
  115. needRepair := false //检测是否传入了所有数据块
  116. for j := 0; j < len(outBufs); j++ {
  117. if blockSeq[j] != j {
  118. needRepair = true
  119. }
  120. }
  121. enc := ec.NewRsEnc(ecK, len(inBufs))
  122. for i := 0; int64(i) < numPacket; i++ {
  123. for j := 0; j < len(inBufs); j++ { //3
  124. if hasBlock[j] {
  125. tmpIn[j] = <-inBufs[j]
  126. } else {
  127. tmpIn[j] = zeroPkt
  128. }
  129. }
  130. fmt.Printf("%v", tmpIn)
  131. if needRepair {
  132. err := enc.Repair(tmpIn)
  133. print("&&&&&")
  134. if err != nil {
  135. fmt.Fprintf(os.Stderr, "Decode Repair Error: %s", err.Error())
  136. }
  137. }
  138. //fmt.Printf("%v",tmpIn)
  139. for j := 0; j < len(outBufs); j++ { //1,2,3//示意,需要调用纠删码编解码引擎: tmp[k] = tmp[k]+(tmpIn[w][k]*coefs[w][j])
  140. outBufs[j] <- tmpIn[j]
  141. }
  142. }
  143. fmt.Println("decode over")
  144. for i := 0; i < len(outBufs); i++ {
  145. close(outBufs[i])
  146. }
  147. }
  148. func (service *Service) get(blockHash string, getBuf chan []byte, numPacket int64) {
  149. /*
  150. data := CatIPFS(blockHash)
  151. for i := 0; int64(i) < numPacket; i++ {
  152. buf := []byte(data[i*config.Cfg().GRCPPacketSize : i*config.Cfg().GRCPPacketSize+config.Cfg().GRCPPacketSize])
  153. getBuf <- buf
  154. }
  155. close(getBuf)
  156. */
  157. }
  158. func persist(inBuf []chan []byte, numPacket int64, localFilePath string, wg *sync.WaitGroup) {
  159. //这里的localFilePath应该是要写入的filename
  160. fDir, err := os.Executable()
  161. if err != nil {
  162. panic(err)
  163. }
  164. fURL := filepath.Join(filepath.Dir(fDir), "assets3")
  165. _, err = os.Stat(fURL)
  166. if os.IsNotExist(err) {
  167. os.MkdirAll(fURL, os.ModePerm)
  168. }
  169. file, err := os.Create(filepath.Join(fURL, localFilePath))
  170. if err != nil {
  171. return
  172. }
  173. for i := 0; int64(i) < numPacket; i++ {
  174. for j := 0; j < len(inBuf); j++ {
  175. tmp := <-inBuf[j]
  176. fmt.Println(tmp)
  177. file.Write(tmp)
  178. }
  179. }
  180. file.Close()
  181. wg.Done()
  182. }

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