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

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package ops2
  2. import (
  3. "fmt"
  4. "io"
  5. "gitlink.org.cn/cloudream/common/pkgs/future"
  6. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/dag"
  7. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  8. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/utils"
  9. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  10. "gitlink.org.cn/cloudream/common/utils/io2"
  11. "gitlink.org.cn/cloudream/common/utils/sync2"
  12. "gitlink.org.cn/cloudream/storage/common/pkgs/ec"
  13. )
  14. func init() {
  15. exec.UseOp[*ECMultiply]()
  16. }
  17. type ECMultiply struct {
  18. Coef [][]byte `json:"coef"`
  19. Inputs []exec.VarID `json:"inputs"`
  20. Outputs []exec.VarID `json:"outputs"`
  21. ChunkSize int `json:"chunkSize"`
  22. }
  23. func (o *ECMultiply) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  24. inputs, err := exec.BindArray[*exec.StreamValue](e, ctx.Context, o.Inputs)
  25. if err != nil {
  26. return err
  27. }
  28. defer func() {
  29. for _, s := range inputs {
  30. s.Stream.Close()
  31. }
  32. }()
  33. outputWrs := make([]*io.PipeWriter, len(o.Outputs))
  34. outputVars := make([]*exec.StreamValue, len(o.Outputs))
  35. for i := range o.Outputs {
  36. rd, wr := io.Pipe()
  37. outputVars[i] = &exec.StreamValue{Stream: rd}
  38. outputWrs[i] = wr
  39. }
  40. fut := future.NewSetVoid()
  41. go func() {
  42. mul := ec.GaloisMultiplier().BuildGalois()
  43. inputChunks := make([][]byte, len(o.Inputs))
  44. for i := range o.Inputs {
  45. inputChunks[i] = make([]byte, o.ChunkSize)
  46. }
  47. outputChunks := make([][]byte, len(o.Outputs))
  48. for i := range o.Outputs {
  49. outputChunks[i] = make([]byte, o.ChunkSize)
  50. }
  51. for {
  52. err := sync2.ParallelDo(inputs, func(s *exec.StreamValue, i int) error {
  53. _, err := io.ReadFull(s.Stream, inputChunks[i])
  54. return err
  55. })
  56. if err == io.EOF {
  57. fut.SetVoid()
  58. return
  59. }
  60. if err != nil {
  61. fut.SetError(err)
  62. return
  63. }
  64. err = mul.Multiply(o.Coef, inputChunks, outputChunks)
  65. if err != nil {
  66. fut.SetError(err)
  67. return
  68. }
  69. for i := range o.Outputs {
  70. err := io2.WriteAll(outputWrs[i], outputChunks[i])
  71. if err != nil {
  72. fut.SetError(err)
  73. return
  74. }
  75. }
  76. }
  77. }()
  78. exec.PutArray(e, o.Outputs, outputVars)
  79. err = fut.Wait(ctx.Context)
  80. if err != nil {
  81. for _, wr := range outputWrs {
  82. wr.CloseWithError(err)
  83. }
  84. return err
  85. }
  86. for _, wr := range outputWrs {
  87. wr.Close()
  88. }
  89. return nil
  90. }
  91. func (o *ECMultiply) String() string {
  92. return fmt.Sprintf(
  93. "ECMultiply(coef=%v) (%v) -> (%v)",
  94. o.Coef,
  95. utils.FormatVarIDs(o.Inputs),
  96. utils.FormatVarIDs(o.Outputs),
  97. )
  98. }
  99. type ECMultiplyNode struct {
  100. dag.NodeBase
  101. EC cdssdk.ECRedundancy
  102. InputIndexes []int
  103. OutputIndexes []int
  104. }
  105. func (b *GraphNodeBuilder) NewECMultiply(ec cdssdk.ECRedundancy) *ECMultiplyNode {
  106. node := &ECMultiplyNode{
  107. EC: ec,
  108. }
  109. b.AddNode(node)
  110. return node
  111. }
  112. func (t *ECMultiplyNode) AddInput(str *dag.StreamVar, dataIndex int) {
  113. t.InputIndexes = append(t.InputIndexes, dataIndex)
  114. idx := t.InputStreams().EnlargeOne()
  115. str.To(t, idx)
  116. }
  117. func (t *ECMultiplyNode) RemoveAllInputs() {
  118. t.InputStreams().ClearAllInput(t)
  119. t.InputStreams().Slots.Resize(0)
  120. t.InputIndexes = nil
  121. }
  122. func (t *ECMultiplyNode) NewOutput(dataIndex int) *dag.StreamVar {
  123. t.OutputIndexes = append(t.OutputIndexes, dataIndex)
  124. return t.OutputStreams().AppendNew(t).Var()
  125. }
  126. func (t *ECMultiplyNode) GenerateOp() (exec.Op, error) {
  127. rs, err := ec.NewRs(t.EC.K, t.EC.N)
  128. if err != nil {
  129. return nil, err
  130. }
  131. coef, err := rs.GenerateMatrix(t.InputIndexes, t.OutputIndexes)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &ECMultiply{
  136. Coef: coef,
  137. Inputs: t.InputStreams().GetVarIDs(),
  138. Outputs: t.OutputStreams().GetVarIDs(),
  139. ChunkSize: t.EC.ChunkSize,
  140. }, nil
  141. }
  142. // func (t *MultiplyType) String() string {
  143. // return fmt.Sprintf("Multiply[]%v%v", formatStreamIO(node), formatValueIO(node))
  144. // }

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