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.

create_ec_package.go 9.9 kB

2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package cmd
  2. import (
  3. "fmt"
  4. "io"
  5. "math/rand"
  6. "os"
  7. "path/filepath"
  8. "sync"
  9. "github.com/samber/lo"
  10. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  11. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  12. "gitlink.org.cn/cloudream/storage/common/pkgs/db/model"
  13. "gitlink.org.cn/cloudream/storage/common/pkgs/distlock/reqbuilder"
  14. "gitlink.org.cn/cloudream/storage/common/pkgs/ec"
  15. "gitlink.org.cn/cloudream/storage/common/pkgs/iterator"
  16. coormq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/coordinator"
  17. )
  18. type CreateECPackage struct {
  19. userID int64
  20. bucketID int64
  21. name string
  22. objectIter iterator.UploadingObjectIterator
  23. redundancy cdssdk.ECRedundancyInfo
  24. nodeAffinity *int64
  25. }
  26. type CreateECPackageResult struct {
  27. PackageID int64
  28. ObjectResults []ECObjectUploadResult
  29. }
  30. type ECObjectUploadResult struct {
  31. Info *iterator.IterUploadingObject
  32. Error error
  33. ObjectID int64
  34. }
  35. func NewCreateECPackage(userID int64, bucketID int64, name string, objIter iterator.UploadingObjectIterator, redundancy cdssdk.ECRedundancyInfo, nodeAffinity *int64) *CreateECPackage {
  36. return &CreateECPackage{
  37. userID: userID,
  38. bucketID: bucketID,
  39. name: name,
  40. objectIter: objIter,
  41. redundancy: redundancy,
  42. nodeAffinity: nodeAffinity,
  43. }
  44. }
  45. func (t *CreateECPackage) Execute(ctx *UpdatePackageContext) (*CreateECPackageResult, error) {
  46. defer t.objectIter.Close()
  47. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  48. if err != nil {
  49. return nil, fmt.Errorf("new coordinator client: %w", err)
  50. }
  51. mutex, err := reqbuilder.NewBuilder().
  52. Metadata().
  53. // 用于判断用户是否有桶的权限
  54. UserBucket().ReadOne(t.userID, t.bucketID).
  55. // 用于查询可用的上传节点
  56. Node().ReadAny().
  57. // 用于创建包信息
  58. Package().CreateOne(t.bucketID, t.name).
  59. // 用于创建包中的文件的信息
  60. Object().CreateAny().
  61. // 用于设置EC配置
  62. ObjectBlock().CreateAny().
  63. // 用于创建Cache记录
  64. Cache().CreateAny().
  65. MutexLock(ctx.Distlock)
  66. if err != nil {
  67. return nil, fmt.Errorf("acquire locks failed, err: %w", err)
  68. }
  69. defer mutex.Unlock()
  70. createPkgResp, err := coorCli.CreatePackage(coormq.NewCreatePackage(t.userID, t.bucketID, t.name,
  71. cdssdk.NewTypedRedundancyInfo(t.redundancy)))
  72. if err != nil {
  73. return nil, fmt.Errorf("creating package: %w", err)
  74. }
  75. getUserNodesResp, err := coorCli.GetUserNodes(coormq.NewGetUserNodes(t.userID))
  76. if err != nil {
  77. return nil, fmt.Errorf("getting user nodes: %w", err)
  78. }
  79. findCliLocResp, err := coorCli.FindClientLocation(coormq.NewFindClientLocation(stgglb.Local.ExternalIP))
  80. if err != nil {
  81. return nil, fmt.Errorf("finding client location: %w", err)
  82. }
  83. uploadNodeInfos := lo.Map(getUserNodesResp.Nodes, func(node model.Node, index int) UploadNodeInfo {
  84. return UploadNodeInfo{
  85. Node: node,
  86. IsSameLocation: node.LocationID == findCliLocResp.Location.LocationID,
  87. }
  88. })
  89. getECResp, err := coorCli.GetECConfig(coormq.NewGetECConfig(t.redundancy.ECName))
  90. if err != nil {
  91. return nil, fmt.Errorf("getting ec: %w", err)
  92. }
  93. // 给上传节点的IPFS加锁
  94. ipfsReqBlder := reqbuilder.NewBuilder()
  95. // 如果本地的IPFS也是存储系统的一个节点,那么从本地上传时,需要加锁
  96. if stgglb.Local.NodeID != nil {
  97. ipfsReqBlder.IPFS().CreateAnyRep(*stgglb.Local.NodeID)
  98. }
  99. for _, node := range uploadNodeInfos {
  100. if stgglb.Local.NodeID != nil && node.Node.NodeID == *stgglb.Local.NodeID {
  101. continue
  102. }
  103. ipfsReqBlder.IPFS().CreateAnyRep(node.Node.NodeID)
  104. }
  105. // 防止上传的副本被清除
  106. ipfsMutex, err := ipfsReqBlder.MutexLock(ctx.Distlock)
  107. if err != nil {
  108. return nil, fmt.Errorf("acquire locks failed, err: %w", err)
  109. }
  110. defer ipfsMutex.Unlock()
  111. // TODO 需要支持设置节点亲和性
  112. rets, err := uploadAndUpdateECPackage(createPkgResp.PackageID, t.objectIter, uploadNodeInfos, t.redundancy, getECResp.Config)
  113. if err != nil {
  114. return nil, err
  115. }
  116. return &CreateECPackageResult{
  117. PackageID: createPkgResp.PackageID,
  118. ObjectResults: rets,
  119. }, nil
  120. }
  121. func uploadAndUpdateECPackage(packageID int64, objectIter iterator.UploadingObjectIterator, uploadNodes []UploadNodeInfo, ecInfo cdssdk.ECRedundancyInfo, ec model.Ec) ([]ECObjectUploadResult, error) {
  122. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  123. if err != nil {
  124. return nil, fmt.Errorf("new coordinator client: %w", err)
  125. }
  126. var uploadRets []ECObjectUploadResult
  127. //上传文件夹
  128. var adds []coormq.AddECObjectInfo
  129. for {
  130. objInfo, err := objectIter.MoveNext()
  131. if err == iterator.ErrNoMoreItem {
  132. break
  133. }
  134. if err != nil {
  135. return nil, fmt.Errorf("reading object: %w", err)
  136. }
  137. err = func() error {
  138. defer objInfo.File.Close()
  139. fileHashes, uploadedNodeIDs, err := uploadECObject(objInfo, uploadNodes, ecInfo, ec)
  140. uploadRets = append(uploadRets, ECObjectUploadResult{
  141. Info: objInfo,
  142. Error: err,
  143. })
  144. if err != nil {
  145. return fmt.Errorf("uploading object: %w", err)
  146. }
  147. adds = append(adds, coormq.NewAddECObjectInfo(objInfo.Path, objInfo.Size, fileHashes, uploadedNodeIDs))
  148. return nil
  149. }()
  150. if err != nil {
  151. return nil, err
  152. }
  153. }
  154. _, err = coorCli.UpdateECPackage(coormq.NewUpdateECPackage(packageID, adds, nil))
  155. if err != nil {
  156. return nil, fmt.Errorf("updating package: %w", err)
  157. }
  158. return uploadRets, nil
  159. }
  160. // 上传文件
  161. func uploadECObject(obj *iterator.IterUploadingObject, uploadNodes []UploadNodeInfo, ecInfo cdssdk.ECRedundancyInfo, ec model.Ec) ([]string, []int64, error) {
  162. //生成纠删码的写入节点序列
  163. nodes := make([]UploadNodeInfo, ec.EcN)
  164. numNodes := len(uploadNodes)
  165. startWriteNodeID := rand.Intn(numNodes)
  166. for i := 0; i < ec.EcN; i++ {
  167. nodes[i] = uploadNodes[(startWriteNodeID+i)%numNodes]
  168. }
  169. hashs, err := ecWrite(obj.File, obj.Size, ecInfo.PacketSize, ec.EcK, ec.EcN, nodes)
  170. if err != nil {
  171. return nil, nil, fmt.Errorf("EcWrite failed, err: %w", err)
  172. }
  173. nodeIDs := make([]int64, len(nodes))
  174. for i := 0; i < len(nodes); i++ {
  175. nodeIDs[i] = nodes[i].Node.NodeID
  176. }
  177. return hashs, nodeIDs, nil
  178. }
  179. // chooseUploadNode 选择一个上传文件的节点
  180. // 1. 从与当前客户端相同地域的节点中随机选一个
  181. // 2. 没有用的话从所有节点中随机选一个
  182. func (t *CreateECPackage) chooseUploadNode(nodes []UploadNodeInfo) UploadNodeInfo {
  183. sameLocationNodes := lo.Filter(nodes, func(e UploadNodeInfo, i int) bool { return e.IsSameLocation })
  184. if len(sameLocationNodes) > 0 {
  185. return sameLocationNodes[rand.Intn(len(sameLocationNodes))]
  186. }
  187. return nodes[rand.Intn(len(nodes))]
  188. }
  189. func ecWrite(file io.ReadCloser, fileSize int64, packetSize int64, ecK int, ecN int, nodes []UploadNodeInfo) ([]string, error) {
  190. // TODO 需要参考RepWrite函数的代码逻辑,做好错误处理
  191. //获取文件大小
  192. var coefs = [][]int64{{1, 1, 1}, {1, 2, 3}} //2应替换为ecK,3应替换为ecN
  193. //计算每个块的packet数
  194. numPacket := (fileSize + int64(ecK)*packetSize - 1) / (int64(ecK) * packetSize)
  195. //fmt.Println(numPacket)
  196. //创建channel
  197. loadBufs := make([]chan []byte, ecN)
  198. encodeBufs := make([]chan []byte, ecN)
  199. for i := 0; i < ecN; i++ {
  200. loadBufs[i] = make(chan []byte)
  201. }
  202. for i := 0; i < ecN; i++ {
  203. encodeBufs[i] = make(chan []byte)
  204. }
  205. hashs := make([]string, ecN)
  206. //正式开始写入
  207. go load(file, loadBufs[:ecN], ecK, numPacket*int64(ecK), packetSize) //从本地文件系统加载数据
  208. go encode(loadBufs[:ecN], encodeBufs[:ecN], ecK, coefs, numPacket)
  209. var wg sync.WaitGroup
  210. wg.Add(ecN)
  211. for idx := 0; idx < ecN; idx++ {
  212. i := idx
  213. reader := channelBytesReader{
  214. channel: encodeBufs[idx],
  215. packetCount: numPacket,
  216. }
  217. go func() {
  218. // TODO 处理错误
  219. fileHash, _ := uploadFile(&reader, nodes[i])
  220. hashs[i] = fileHash
  221. wg.Done()
  222. }()
  223. }
  224. wg.Wait()
  225. return hashs, nil
  226. }
  227. func load(file io.ReadCloser, loadBufs []chan []byte, ecK int, totalNumPacket int64, ecPacketSize int64) error {
  228. for i := 0; int64(i) < totalNumPacket; i++ {
  229. buf := make([]byte, ecPacketSize)
  230. idx := i % ecK
  231. _, err := file.Read(buf)
  232. if err != nil {
  233. return fmt.Errorf("read file falied, err:%w", err)
  234. }
  235. loadBufs[idx] <- buf
  236. if idx == ecK-1 {
  237. for j := ecK; j < len(loadBufs); j++ {
  238. zeroPkt := make([]byte, ecPacketSize)
  239. loadBufs[j] <- zeroPkt
  240. }
  241. }
  242. if err != nil && err != io.EOF {
  243. return fmt.Errorf("load file to buf failed, err:%w", err)
  244. }
  245. }
  246. for i := 0; i < len(loadBufs); i++ {
  247. close(loadBufs[i])
  248. }
  249. file.Close()
  250. return nil
  251. }
  252. func encode(inBufs []chan []byte, outBufs []chan []byte, ecK int, coefs [][]int64, numPacket int64) {
  253. var tmpIn [][]byte
  254. tmpIn = make([][]byte, len(outBufs))
  255. enc := ec.NewRsEnc(ecK, len(outBufs))
  256. for i := 0; int64(i) < numPacket; i++ {
  257. for j := 0; j < len(outBufs); j++ {
  258. tmpIn[j] = <-inBufs[j]
  259. }
  260. enc.Encode(tmpIn)
  261. for j := 0; j < len(outBufs); j++ {
  262. outBufs[j] <- tmpIn[j]
  263. }
  264. }
  265. for i := 0; i < len(outBufs); i++ {
  266. close(outBufs[i])
  267. }
  268. }
  269. type channelBytesReader struct {
  270. channel chan []byte
  271. packetCount int64
  272. readingData []byte
  273. }
  274. func (r *channelBytesReader) Read(buf []byte) (int, error) {
  275. if len(r.readingData) == 0 {
  276. if r.packetCount == 0 {
  277. return 0, io.EOF
  278. }
  279. r.readingData = <-r.channel
  280. r.packetCount--
  281. }
  282. len := copy(buf, r.readingData)
  283. r.readingData = r.readingData[:len]
  284. return len, nil
  285. }
  286. func persist(inBuf []chan []byte, numPacket int64, localFilePath string, wg *sync.WaitGroup) {
  287. fDir, err := os.Executable()
  288. if err != nil {
  289. panic(err)
  290. }
  291. fURL := filepath.Join(filepath.Dir(fDir), "assets")
  292. _, err = os.Stat(fURL)
  293. if os.IsNotExist(err) {
  294. os.MkdirAll(fURL, os.ModePerm)
  295. }
  296. file, err := os.Create(filepath.Join(fURL, localFilePath))
  297. if err != nil {
  298. return
  299. }
  300. for i := 0; int64(i) < numPacket; i++ {
  301. for j := 0; j < len(inBuf); j++ {
  302. tmp := <-inBuf[j]
  303. fmt.Println(tmp)
  304. file.Write(tmp)
  305. }
  306. }
  307. file.Close()
  308. wg.Done()
  309. }

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