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 6.1 kB

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

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