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.go 7.2 kB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package ops2
  2. import (
  3. "fmt"
  4. "io"
  5. "gitlink.org.cn/cloudream/common/pkgs/future"
  6. "gitlink.org.cn/cloudream/common/utils/io2"
  7. "gitlink.org.cn/cloudream/common/utils/sync2"
  8. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ec"
  9. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/dag"
  10. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/exec"
  11. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/utils"
  12. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/pool"
  13. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/types"
  14. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  15. )
  16. func init() {
  17. exec.UseOp[*ECMultiply]()
  18. exec.UseOp[*CallECMultiplier]()
  19. }
  20. type ECMultiply struct {
  21. Coef [][]byte `json:"coef"`
  22. Inputs []exec.VarID `json:"inputs"`
  23. Outputs []exec.VarID `json:"outputs"`
  24. ChunkSize int `json:"chunkSize"`
  25. }
  26. func (o *ECMultiply) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  27. inputs, err := exec.BindArray[*exec.StreamValue](e, ctx.Context, o.Inputs)
  28. if err != nil {
  29. return err
  30. }
  31. defer func() {
  32. for _, s := range inputs {
  33. s.Stream.Close()
  34. }
  35. }()
  36. outputWrs := make([]*io.PipeWriter, len(o.Outputs))
  37. outputVars := make([]*exec.StreamValue, len(o.Outputs))
  38. for i := range o.Outputs {
  39. rd, wr := io.Pipe()
  40. outputVars[i] = &exec.StreamValue{Stream: rd}
  41. outputWrs[i] = wr
  42. }
  43. /// !!! 缓冲区大小必须是ChunkSize大小,因为Chunk数据很有可能来自于一个被Split的完整文件,此时必须保证按顺序读取每一个Chunk的数据 !!!
  44. inputChunks := make([][]byte, len(o.Inputs))
  45. for i := range o.Inputs {
  46. inputChunks[i] = make([]byte, o.ChunkSize)
  47. }
  48. // 输出用两个缓冲轮换
  49. outputBufPool := sync2.NewBucketPool[[][]byte]()
  50. for i := 0; i < 2; i++ {
  51. outputChunks := make([][]byte, len(o.Outputs))
  52. for i := range o.Outputs {
  53. outputChunks[i] = make([]byte, o.ChunkSize)
  54. }
  55. outputBufPool.PutEmpty(outputChunks)
  56. }
  57. fut := future.NewSetVoid()
  58. go func() {
  59. mul := ec.GaloisMultiplier().BuildGalois()
  60. defer outputBufPool.Close()
  61. for {
  62. err := sync2.ParallelDo(inputs, func(s *exec.StreamValue, i int) error {
  63. _, err := io.ReadFull(s.Stream, inputChunks[i])
  64. return err
  65. })
  66. if err == io.EOF {
  67. return
  68. }
  69. if err != nil {
  70. fut.SetError(err)
  71. return
  72. }
  73. outputBuf, ok := outputBufPool.GetEmpty()
  74. if !ok {
  75. return
  76. }
  77. err = mul.Multiply(o.Coef, inputChunks, outputBuf)
  78. if err != nil {
  79. fut.SetError(err)
  80. return
  81. }
  82. outputBufPool.PutFilled(outputBuf)
  83. }
  84. }()
  85. go func() {
  86. defer outputBufPool.Close()
  87. for {
  88. outputChunks, ok := outputBufPool.GetFilled()
  89. if !ok {
  90. fut.SetVoid()
  91. return
  92. }
  93. for i := range o.Outputs {
  94. err := io2.WriteAll(outputWrs[i], outputChunks[i])
  95. if err != nil {
  96. fut.SetError(err)
  97. return
  98. }
  99. }
  100. outputBufPool.PutEmpty(outputChunks)
  101. }
  102. }()
  103. exec.PutArray(e, o.Outputs, outputVars)
  104. err = fut.Wait(ctx.Context)
  105. if err != nil {
  106. for _, wr := range outputWrs {
  107. wr.CloseWithError(err)
  108. }
  109. return err
  110. }
  111. for _, wr := range outputWrs {
  112. wr.Close()
  113. }
  114. return nil
  115. }
  116. func (o *ECMultiply) String() string {
  117. return fmt.Sprintf(
  118. "ECMultiply(coef=%v) (%v) -> (%v)",
  119. o.Coef,
  120. utils.FormatVarIDs(o.Inputs),
  121. utils.FormatVarIDs(o.Outputs),
  122. )
  123. }
  124. type CallECMultiplier struct {
  125. UserSpace jcstypes.UserSpaceDetail
  126. Coef [][]byte
  127. Inputs []exec.VarID
  128. Outputs []exec.VarID
  129. ChunkSize int
  130. }
  131. func (o *CallECMultiplier) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  132. stgPool, err := exec.GetValueByType[*pool.Pool](ctx)
  133. if err != nil {
  134. return fmt.Errorf("getting storage pool: %w", err)
  135. }
  136. ecMul, err := stgPool.GetECMultiplier(&o.UserSpace)
  137. if err != nil {
  138. return err
  139. }
  140. defer ecMul.Close()
  141. inputs, err := exec.BindArray[*HTTPRequestValue](e, ctx.Context, o.Inputs)
  142. if err != nil {
  143. return err
  144. }
  145. reqs := make([]types.HTTPRequest, 0, len(inputs))
  146. for _, input := range inputs {
  147. reqs = append(reqs, input.HTTPRequest)
  148. }
  149. outputs, err := ecMul.Multiply(o.Coef, reqs, o.ChunkSize)
  150. if err != nil {
  151. return err
  152. }
  153. outputVals := make([]*FileInfoValue, 0, len(outputs))
  154. for _, output := range outputs {
  155. outputVals = append(outputVals, &FileInfoValue{
  156. FileInfo: output,
  157. })
  158. }
  159. exec.PutArray(e, o.Outputs, outputVals)
  160. return nil
  161. }
  162. func (o *CallECMultiplier) String() string {
  163. return fmt.Sprintf(
  164. "CallECMultiplier(userSpace=%v, coef=%v) (%v) -> (%v)",
  165. o.Coef,
  166. o.UserSpace,
  167. utils.FormatVarIDs(o.Inputs),
  168. utils.FormatVarIDs(o.Outputs),
  169. )
  170. }
  171. type ECMultiplyNode struct {
  172. dag.NodeBase
  173. EC jcstypes.ECRedundancy
  174. InputIndexes []int
  175. OutputIndexes []int
  176. }
  177. func (b *GraphNodeBuilder) NewECMultiply(ec jcstypes.ECRedundancy) *ECMultiplyNode {
  178. node := &ECMultiplyNode{
  179. EC: ec,
  180. }
  181. b.AddNode(node)
  182. return node
  183. }
  184. func (t *ECMultiplyNode) AddInput(str *dag.StreamVar, dataIndex int) {
  185. t.InputIndexes = append(t.InputIndexes, dataIndex)
  186. idx := t.InputStreams().EnlargeOne()
  187. str.To(t, idx)
  188. }
  189. func (t *ECMultiplyNode) RemoveAllInputs() {
  190. t.InputStreams().ClearAllInput(t)
  191. t.InputStreams().Slots.Resize(0)
  192. t.InputIndexes = nil
  193. }
  194. func (t *ECMultiplyNode) NewOutput(dataIndex int) *dag.StreamVar {
  195. t.OutputIndexes = append(t.OutputIndexes, dataIndex)
  196. return t.OutputStreams().AppendNew(t).Var()
  197. }
  198. func (t *ECMultiplyNode) GenerateOp() (exec.Op, error) {
  199. rs, err := ec.NewRs(t.EC.K, t.EC.N)
  200. if err != nil {
  201. return nil, err
  202. }
  203. coef, err := rs.GenerateMatrix(t.InputIndexes, t.OutputIndexes)
  204. if err != nil {
  205. return nil, err
  206. }
  207. return &ECMultiply{
  208. Coef: coef,
  209. Inputs: t.InputStreams().GetVarIDs(),
  210. Outputs: t.OutputStreams().GetVarIDs(),
  211. ChunkSize: t.EC.ChunkSize,
  212. }, nil
  213. }
  214. // func (t *MultiplyType) String() string {
  215. // return fmt.Sprintf("Multiply[]%v%v", formatStreamIO(node), formatValueIO(node))
  216. // }
  217. type CallECMultiplierNode struct {
  218. dag.NodeBase
  219. UserSpace jcstypes.UserSpaceDetail
  220. EC jcstypes.ECRedundancy
  221. InputIndexes []int
  222. OutputIndexes []int
  223. }
  224. func (b *GraphNodeBuilder) NewCallECMultiplier(userSpace jcstypes.UserSpaceDetail) *CallECMultiplierNode {
  225. node := &CallECMultiplierNode{
  226. UserSpace: userSpace,
  227. }
  228. b.AddNode(node)
  229. return node
  230. }
  231. func (t *CallECMultiplierNode) InitFrom(node *ECMultiplyNode) {
  232. t.EC = node.EC
  233. t.InputIndexes = node.InputIndexes
  234. t.OutputIndexes = node.OutputIndexes
  235. t.InputValues().Init(len(t.InputIndexes))
  236. t.OutputValues().Init(t, len(t.OutputIndexes))
  237. }
  238. func (t *CallECMultiplierNode) HTTPRequestSlot(idx int) dag.ValueInputSlot {
  239. return dag.ValueInputSlot{
  240. Node: t,
  241. Index: idx,
  242. }
  243. }
  244. func (t *CallECMultiplierNode) FileInfoVar(idx int) dag.ValueOutputSlot {
  245. return dag.ValueOutputSlot{
  246. Node: t,
  247. Index: idx,
  248. }
  249. }
  250. func (t *CallECMultiplierNode) GenerateOp() (exec.Op, error) {
  251. rs, err := ec.NewRs(t.EC.K, t.EC.N)
  252. if err != nil {
  253. return nil, err
  254. }
  255. coef, err := rs.GenerateMatrix(t.InputIndexes, t.OutputIndexes)
  256. if err != nil {
  257. return nil, err
  258. }
  259. return &CallECMultiplier{
  260. UserSpace: t.UserSpace,
  261. Coef: coef,
  262. Inputs: t.InputValues().GetVarIDs(),
  263. Outputs: t.OutputValues().GetVarIDs(),
  264. ChunkSize: t.EC.ChunkSize,
  265. }, nil
  266. }

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