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.

command_service.go 7.2 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strconv"
  7. "sync"
  8. "gitlink.org.cn/cloudream/agent/config"
  9. "gitlink.org.cn/cloudream/ec"
  10. "gitlink.org.cn/cloudream/utils"
  11. racli "gitlink.org.cn/cloudream/rabbitmq/client"
  12. ramsg "gitlink.org.cn/cloudream/rabbitmq/message"
  13. "gitlink.org.cn/cloudream/utils/consts/errorcode"
  14. )
  15. type CommandService struct {
  16. }
  17. func (service *CommandService) RepMove(msg *ramsg.RepMoveCommand) ramsg.AgentMoveResp {
  18. /*fmt.Println("RepMove")
  19. fmt.Println(command.Hashs)
  20. hashs := command.Hashs
  21. //执行调度操作
  22. ipfsDir := "assets"
  23. goalDir := "assets2"
  24. goalName := command.BucketName + ":" + command.ObjectName + ":" + strconv.Itoa(command.UserId)
  25. //目标文件
  26. fDir, err := os.Executable()
  27. if err != nil {
  28. panic(err)
  29. }
  30. fURL := filepath.Join(filepath.Dir(fDir), goalDir)
  31. _, err = os.Stat(fURL)
  32. if os.IsNotExist(err) {
  33. os.MkdirAll(fURL, os.ModePerm)
  34. }
  35. fURL = filepath.Join(fURL, goalName)
  36. outFile, err := os.Create(fURL)
  37. fmt.Println(fURL)
  38. //源文件
  39. fURL = filepath.Join(filepath.Dir(fDir), ipfsDir)
  40. fURL = filepath.Join(fURL, hashs[0])
  41. inFile, _ := os.Open(fURL)
  42. fmt.Println(fURL)
  43. fileInfo, _ := inFile.Stat()
  44. fileSizeInBytes := fileInfo.Size()
  45. numWholePacket := fileSizeInBytes / config.Cfg().GRCPPacketSize
  46. lastPacketInBytes := fileSizeInBytes % config.Cfg().GRCPPacketSize
  47. fmt.Println(fileSizeInBytes)
  48. fmt.Println(numWholePacket)
  49. fmt.Println(lastPacketInBytes)
  50. for i := 0; int64(i) < numWholePacket; i++ {
  51. buf := make([]byte, config.Cfg().GRCPPacketSize)
  52. inFile.Read(buf)
  53. outFile.Write(buf)
  54. }
  55. if lastPacketInBytes > 0 {
  56. buf := make([]byte, lastPacketInBytes)
  57. inFile.Read(buf)
  58. outFile.Write(buf)
  59. }
  60. inFile.Close()
  61. outFile.Close()
  62. //返回消息
  63. res := rabbitmq.AgentMoveRes{
  64. MoveCode: 0,
  65. }
  66. c, _ := json.Marshal(res)
  67. rabbitSend(c, command.UserId)
  68. //向coor报告临时缓存hash
  69. command1 := rabbitmq.TempCacheReport{
  70. Ip: LocalIp,
  71. Hashs: hashs,
  72. }
  73. c, _ = json.Marshal(command1)
  74. b := append([]byte("06"), c...)
  75. fmt.Println(b)
  76. rabbit := rabbitmq.NewRabbitMQSimple("coorQueue")
  77. rabbit.PublishSimple(b)
  78. rabbit.Destroy()*/
  79. fmt.Println("RepMove")
  80. fmt.Println(msg.Hashs)
  81. hashs := msg.Hashs
  82. fileSizeInBytes := msg.FileSizeInBytes
  83. //执行调度操作
  84. goalDir := "assets2"
  85. goalName := msg.BucketName + ":" + msg.ObjectName + ":" + strconv.Itoa(msg.UserID)
  86. //目标文件
  87. fDir, err := os.Executable()
  88. if err != nil {
  89. panic(err)
  90. }
  91. fURL := filepath.Join(filepath.Dir(fDir), goalDir)
  92. _, err = os.Stat(fURL)
  93. if os.IsNotExist(err) {
  94. os.MkdirAll(fURL, os.ModePerm)
  95. }
  96. fURL = filepath.Join(fURL, goalName)
  97. outFile, err := os.Create(fURL)
  98. fmt.Println(fURL)
  99. //源文件
  100. data := CatIPFS(hashs[0])
  101. numWholePacket := fileSizeInBytes / int64(config.Cfg().GRCPPacketSize)
  102. lastPacketInBytes := fileSizeInBytes % int64(config.Cfg().GRCPPacketSize)
  103. fmt.Println(fileSizeInBytes)
  104. fmt.Println(numWholePacket)
  105. fmt.Println(lastPacketInBytes)
  106. for i := 0; int64(i) < numWholePacket; i++ {
  107. buf := []byte(data[i*config.Cfg().GRCPPacketSize : i*config.Cfg().GRCPPacketSize+config.Cfg().GRCPPacketSize])
  108. outFile.Write(buf)
  109. }
  110. if lastPacketInBytes > 0 {
  111. buf := []byte(data[numWholePacket*int64(config.Cfg().GRCPPacketSize) : numWholePacket*int64(config.Cfg().GRCPPacketSize)+lastPacketInBytes])
  112. outFile.Write(buf)
  113. }
  114. outFile.Close()
  115. //向coor报告临时缓存hash
  116. coorClient, err := racli.NewCoordinatorClient()
  117. if err != nil {
  118. // TODO 日志
  119. return ramsg.NewAgentMoveRespFailed(errorcode.OPERATION_FAILED, fmt.Sprintf("create coordinator client failed"))
  120. }
  121. defer coorClient.Close()
  122. coorClient.TempCacheReport(config.Cfg().LocalIP, hashs)
  123. return ramsg.NewAgentMoveRespOK()
  124. }
  125. func (service *CommandService) ECMove(msg *ramsg.ECMoveCommand) ramsg.AgentMoveResp {
  126. wg := sync.WaitGroup{}
  127. fmt.Println("EcMove")
  128. fmt.Println(msg.Hashs)
  129. hashs := msg.Hashs
  130. fileSizeInBytes := msg.FileSizeInBytes
  131. blockIds := msg.IDs
  132. ecName := msg.ECName
  133. goalName := msg.BucketName + ":" + msg.ObjectName + ":" + strconv.Itoa(msg.UserID)
  134. ecPolicies := *utils.GetEcPolicy()
  135. ecPolicy := ecPolicies[ecName]
  136. ecK := ecPolicy.GetK()
  137. ecN := ecPolicy.GetN()
  138. numPacket := (fileSizeInBytes + int64(ecK)*int64(config.Cfg().GRCPPacketSize) - 1) / (int64(ecK) * int64(config.Cfg().GRCPPacketSize))
  139. getBufs := make([]chan []byte, ecN)
  140. decodeBufs := make([]chan []byte, ecK)
  141. for i := 0; i < ecN; i++ {
  142. getBufs[i] = make(chan []byte)
  143. }
  144. for i := 0; i < ecK; i++ {
  145. decodeBufs[i] = make(chan []byte)
  146. }
  147. wg.Add(1)
  148. //执行调度操作
  149. for i := 0; i < len(blockIds); i++ {
  150. go get(hashs[i], getBufs[blockIds[i]], numPacket)
  151. }
  152. go decode(getBufs[:], decodeBufs[:], blockIds, ecK, numPacket)
  153. go persist(decodeBufs[:], numPacket, goalName, &wg)
  154. wg.Wait()
  155. //向coor报告临时缓存hash
  156. coorClient, err := racli.NewCoordinatorClient()
  157. if err != nil {
  158. // TODO 日志
  159. return ramsg.NewAgentMoveRespFailed(errorcode.OPERATION_FAILED, fmt.Sprintf("create coordinator client failed"))
  160. }
  161. defer coorClient.Close()
  162. coorClient.TempCacheReport(config.Cfg().LocalIP, hashs)
  163. return ramsg.NewAgentMoveRespOK()
  164. }
  165. func decode(inBufs []chan []byte, outBufs []chan []byte, blockSeq []int, ecK int, numPacket int64) {
  166. fmt.Println("decode ")
  167. var tmpIn [][]byte
  168. var zeroPkt []byte
  169. tmpIn = make([][]byte, len(inBufs))
  170. hasBlock := map[int]bool{}
  171. for j := 0; j < len(blockSeq); j++ {
  172. hasBlock[blockSeq[j]] = true
  173. }
  174. needRepair := false //检测是否传入了所有数据块
  175. for j := 0; j < len(outBufs); j++ {
  176. if blockSeq[j] != j {
  177. needRepair = true
  178. }
  179. }
  180. enc := ec.NewRsEnc(ecK, len(inBufs))
  181. for i := 0; int64(i) < numPacket; i++ {
  182. for j := 0; j < len(inBufs); j++ { //3
  183. if hasBlock[j] {
  184. tmpIn[j] = <-inBufs[j]
  185. } else {
  186. tmpIn[j] = zeroPkt
  187. }
  188. }
  189. fmt.Printf("%v", tmpIn)
  190. if needRepair {
  191. err := enc.Repair(tmpIn)
  192. print("&&&&&")
  193. if err != nil {
  194. fmt.Fprintf(os.Stderr, "Decode Repair Error: %s", err.Error())
  195. }
  196. }
  197. //fmt.Printf("%v",tmpIn)
  198. for j := 0; j < len(outBufs); j++ { //1,2,3//示意,需要调用纠删码编解码引擎: tmp[k] = tmp[k]+(tmpIn[w][k]*coefs[w][j])
  199. outBufs[j] <- tmpIn[j]
  200. }
  201. }
  202. fmt.Println("decode over")
  203. for i := 0; i < len(outBufs); i++ {
  204. close(outBufs[i])
  205. }
  206. }
  207. func get(blockHash string, getBuf chan []byte, numPacket int64) {
  208. data := CatIPFS(blockHash)
  209. for i := 0; int64(i) < numPacket; i++ {
  210. buf := []byte(data[i*config.Cfg().GRCPPacketSize : i*config.Cfg().GRCPPacketSize+config.Cfg().GRCPPacketSize])
  211. getBuf <- buf
  212. }
  213. close(getBuf)
  214. }
  215. func persist(inBuf []chan []byte, numPacket int64, localFilePath string, wg *sync.WaitGroup) {
  216. //这里的localFilePath应该是要写入的filename
  217. fDir, err := os.Executable()
  218. if err != nil {
  219. panic(err)
  220. }
  221. fURL := filepath.Join(filepath.Dir(fDir), "assets3")
  222. _, err = os.Stat(fURL)
  223. if os.IsNotExist(err) {
  224. os.MkdirAll(fURL, os.ModePerm)
  225. }
  226. file, err := os.Create(filepath.Join(fURL, localFilePath))
  227. if err != nil {
  228. return
  229. }
  230. for i := 0; int64(i) < numPacket; i++ {
  231. for j := 0; j < len(inBuf); j++ {
  232. tmp := <-inBuf[j]
  233. fmt.Println(tmp)
  234. file.Write(tmp)
  235. }
  236. }
  237. file.Close()
  238. wg.Done()
  239. }

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