您最多选择25个标签 标签必须以中文、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package ops2
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/samber/lo"
  7. "gitlink.org.cn/cloudream/common/pkgs/future"
  8. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/dag"
  9. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  10. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  11. "gitlink.org.cn/cloudream/common/utils/io2"
  12. "gitlink.org.cn/cloudream/common/utils/sync2"
  13. "gitlink.org.cn/cloudream/storage/common/pkgs/ec"
  14. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch2"
  15. "golang.org/x/sync/semaphore"
  16. )
  17. func init() {
  18. exec.UseOp[*ECReconstructAny]()
  19. exec.UseOp[*ECReconstruct]()
  20. exec.UseOp[*ECMultiply]()
  21. }
  22. type ECReconstructAny struct {
  23. EC cdssdk.ECRedundancy `json:"ec"`
  24. Inputs []*exec.StreamVar `json:"inputs"`
  25. Outputs []*exec.StreamVar `json:"outputs"`
  26. InputBlockIndexes []int `json:"inputBlockIndexes"`
  27. OutputBlockIndexes []int `json:"outputBlockIndexes"`
  28. }
  29. func (o *ECReconstructAny) Execute(ctx context.Context, e *exec.Executor) error {
  30. rs, err := ec.NewStreamRs(o.EC.K, o.EC.N, o.EC.ChunkSize)
  31. if err != nil {
  32. return fmt.Errorf("new ec: %w", err)
  33. }
  34. err = exec.BindArrayVars(e, ctx, o.Inputs)
  35. if err != nil {
  36. return err
  37. }
  38. defer func() {
  39. for _, s := range o.Inputs {
  40. s.Stream.Close()
  41. }
  42. }()
  43. var inputs []io.Reader
  44. for _, s := range o.Inputs {
  45. inputs = append(inputs, s.Stream)
  46. }
  47. outputs := rs.ReconstructAny(inputs, o.InputBlockIndexes, o.OutputBlockIndexes)
  48. sem := semaphore.NewWeighted(int64(len(o.Outputs)))
  49. for i := range o.Outputs {
  50. sem.Acquire(ctx, 1)
  51. o.Outputs[i].Stream = io2.AfterReadClosedOnce(outputs[i], func(closer io.ReadCloser) {
  52. sem.Release(1)
  53. })
  54. }
  55. exec.PutArrayVars(e, o.Outputs)
  56. return sem.Acquire(ctx, int64(len(o.Outputs)))
  57. }
  58. type ECReconstruct struct {
  59. EC cdssdk.ECRedundancy `json:"ec"`
  60. Inputs []*exec.StreamVar `json:"inputs"`
  61. Outputs []*exec.StreamVar `json:"outputs"`
  62. InputBlockIndexes []int `json:"inputBlockIndexes"`
  63. }
  64. func (o *ECReconstruct) Execute(ctx context.Context, e *exec.Executor) error {
  65. rs, err := ec.NewStreamRs(o.EC.K, o.EC.N, o.EC.ChunkSize)
  66. if err != nil {
  67. return fmt.Errorf("new ec: %w", err)
  68. }
  69. err = exec.BindArrayVars(e, ctx, o.Inputs)
  70. if err != nil {
  71. return err
  72. }
  73. defer func() {
  74. for _, s := range o.Inputs {
  75. s.Stream.Close()
  76. }
  77. }()
  78. var inputs []io.Reader
  79. for _, s := range o.Inputs {
  80. inputs = append(inputs, s.Stream)
  81. }
  82. outputs := rs.ReconstructData(inputs, o.InputBlockIndexes)
  83. sem := semaphore.NewWeighted(int64(len(o.Outputs)))
  84. for i := range o.Outputs {
  85. sem.Acquire(ctx, 1)
  86. o.Outputs[i].Stream = io2.AfterReadClosedOnce(outputs[i], func(closer io.ReadCloser) {
  87. sem.Release(1)
  88. })
  89. }
  90. exec.PutArrayVars(e, o.Outputs)
  91. return sem.Acquire(ctx, int64(len(o.Outputs)))
  92. }
  93. type ECMultiply struct {
  94. Coef [][]byte `json:"coef"`
  95. Inputs []*exec.StreamVar `json:"inputs"`
  96. Outputs []*exec.StreamVar `json:"outputs"`
  97. ChunkSize int `json:"chunkSize"`
  98. }
  99. func (o *ECMultiply) Execute(ctx context.Context, e *exec.Executor) error {
  100. err := exec.BindArrayVars(e, ctx, o.Inputs)
  101. if err != nil {
  102. return err
  103. }
  104. defer func() {
  105. for _, s := range o.Inputs {
  106. s.Stream.Close()
  107. }
  108. }()
  109. outputWrs := make([]*io.PipeWriter, len(o.Outputs))
  110. for i := range o.Outputs {
  111. rd, wr := io.Pipe()
  112. o.Outputs[i].Stream = rd
  113. outputWrs[i] = wr
  114. }
  115. fut := future.NewSetVoid()
  116. go func() {
  117. mul := ec.GaloisMultiplier().BuildGalois()
  118. inputChunks := make([][]byte, len(o.Inputs))
  119. for i := range o.Inputs {
  120. inputChunks[i] = make([]byte, o.ChunkSize)
  121. }
  122. outputChunks := make([][]byte, len(o.Outputs))
  123. for i := range o.Outputs {
  124. outputChunks[i] = make([]byte, o.ChunkSize)
  125. }
  126. for {
  127. err := sync2.ParallelDo(o.Inputs, func(s *exec.StreamVar, i int) error {
  128. _, err := io.ReadFull(s.Stream, inputChunks[i])
  129. return err
  130. })
  131. if err == io.EOF {
  132. fut.SetVoid()
  133. return
  134. }
  135. if err != nil {
  136. fut.SetError(err)
  137. return
  138. }
  139. err = mul.Multiply(o.Coef, inputChunks, outputChunks)
  140. if err != nil {
  141. fut.SetError(err)
  142. return
  143. }
  144. for i := range o.Outputs {
  145. err := io2.WriteAll(outputWrs[i], outputChunks[i])
  146. if err != nil {
  147. fut.SetError(err)
  148. return
  149. }
  150. }
  151. }
  152. }()
  153. exec.PutArrayVars(e, o.Outputs)
  154. err = fut.Wait(ctx)
  155. if err != nil {
  156. for _, wr := range outputWrs {
  157. wr.CloseWithError(err)
  158. }
  159. return err
  160. }
  161. for _, wr := range outputWrs {
  162. wr.Close()
  163. }
  164. return nil
  165. }
  166. type MultiplyType struct {
  167. EC cdssdk.ECRedundancy
  168. InputIndexes []int
  169. OutputIndexes []int
  170. }
  171. func (t *MultiplyType) InitNode(node *dag.Node) {}
  172. func (t *MultiplyType) GenerateOp(op *dag.Node) (exec.Op, error) {
  173. rs, err := ec.NewRs(t.EC.K, t.EC.N)
  174. if err != nil {
  175. return nil, err
  176. }
  177. coef, err := rs.GenerateMatrix(t.InputIndexes, t.OutputIndexes)
  178. if err != nil {
  179. return nil, err
  180. }
  181. return &ECMultiply{
  182. Coef: coef,
  183. Inputs: lo.Map(op.InputStreams, func(v *dag.StreamVar, idx int) *exec.StreamVar { return v.Var }),
  184. Outputs: lo.Map(op.OutputStreams, func(v *dag.StreamVar, idx int) *exec.StreamVar { return v.Var }),
  185. ChunkSize: t.EC.ChunkSize,
  186. }, nil
  187. }
  188. func (t *MultiplyType) AddInput(node *dag.Node, str *dag.StreamVar, dataIndex int) {
  189. t.InputIndexes = append(t.InputIndexes, dataIndex)
  190. node.InputStreams = append(node.InputStreams, str)
  191. str.To(node, len(node.InputStreams)-1)
  192. }
  193. func (t *MultiplyType) NewOutput(node *dag.Node, dataIndex int) *dag.StreamVar {
  194. t.OutputIndexes = append(t.OutputIndexes, dataIndex)
  195. return dag.NodeNewOutputStream(node, &ioswitch2.VarProps{StreamIndex: dataIndex})
  196. }
  197. func (t *MultiplyType) String(node *dag.Node) string {
  198. return fmt.Sprintf("Multiply[]%v%v", formatStreamIO(node), formatValueIO(node))
  199. }

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