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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/ec/lrc"
  16. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitchlrc"
  17. )
  18. func init() {
  19. exec.UseOp[*GalMultiply]()
  20. }
  21. type GalMultiply struct {
  22. Coef [][]byte `json:"coef"`
  23. Inputs []*exec.StreamVar `json:"inputs"`
  24. Outputs []*exec.StreamVar `json:"outputs"`
  25. ChunkSize int `json:"chunkSize"`
  26. }
  27. func (o *GalMultiply) Execute(ctx context.Context, e *exec.Executor) error {
  28. err := exec.BindArrayVars(e, ctx, o.Inputs)
  29. if err != nil {
  30. return err
  31. }
  32. defer func() {
  33. for _, s := range o.Inputs {
  34. s.Stream.Close()
  35. }
  36. }()
  37. outputWrs := make([]*io.PipeWriter, len(o.Outputs))
  38. for i := range o.Outputs {
  39. rd, wr := io.Pipe()
  40. o.Outputs[i].Stream = rd
  41. outputWrs[i] = wr
  42. }
  43. fut := future.NewSetVoid()
  44. go func() {
  45. mul := ec.GaloisMultiplier().BuildGalois()
  46. inputChunks := make([][]byte, len(o.Inputs))
  47. for i := range o.Inputs {
  48. inputChunks[i] = make([]byte, o.ChunkSize)
  49. }
  50. outputChunks := make([][]byte, len(o.Outputs))
  51. for i := range o.Outputs {
  52. outputChunks[i] = make([]byte, o.ChunkSize)
  53. }
  54. for {
  55. err := sync2.ParallelDo(o.Inputs, func(s *exec.StreamVar, i int) error {
  56. _, err := io.ReadFull(s.Stream, inputChunks[i])
  57. return err
  58. })
  59. if err == io.EOF {
  60. fut.SetVoid()
  61. return
  62. }
  63. if err != nil {
  64. fut.SetError(err)
  65. return
  66. }
  67. err = mul.Multiply(o.Coef, inputChunks, outputChunks)
  68. if err != nil {
  69. fut.SetError(err)
  70. return
  71. }
  72. for i := range o.Outputs {
  73. err := io2.WriteAll(outputWrs[i], outputChunks[i])
  74. if err != nil {
  75. fut.SetError(err)
  76. return
  77. }
  78. }
  79. }
  80. }()
  81. exec.PutArrayVars(e, o.Outputs)
  82. err = fut.Wait(ctx)
  83. if err != nil {
  84. for _, wr := range outputWrs {
  85. wr.CloseWithError(err)
  86. }
  87. return err
  88. }
  89. for _, wr := range outputWrs {
  90. wr.Close()
  91. }
  92. return nil
  93. }
  94. func (o *GalMultiply) String() string {
  95. return fmt.Sprintf(
  96. "ECMultiply(coef=%v) (%v) -> (%v)",
  97. o.Coef,
  98. utils.FormatVarIDs(o.Inputs),
  99. utils.FormatVarIDs(o.Outputs),
  100. )
  101. }
  102. type LRCConstructAnyType struct {
  103. LRC cdssdk.LRCRedundancy
  104. InputIndexes []int
  105. OutputIndexes []int
  106. }
  107. func (t *LRCConstructAnyType) InitNode(node *dag.Node) {}
  108. func (t *LRCConstructAnyType) GenerateOp(op *dag.Node) (exec.Op, error) {
  109. l, err := lrc.New(t.LRC.N, t.LRC.K, t.LRC.Groups)
  110. if err != nil {
  111. return nil, err
  112. }
  113. coef, err := l.GenerateMatrix(t.InputIndexes, t.OutputIndexes)
  114. if err != nil {
  115. return nil, err
  116. }
  117. return &GalMultiply{
  118. Coef: coef,
  119. Inputs: lo.Map(op.InputStreams, func(v *dag.StreamVar, idx int) *exec.StreamVar { return v.Var }),
  120. Outputs: lo.Map(op.OutputStreams, func(v *dag.StreamVar, idx int) *exec.StreamVar { return v.Var }),
  121. ChunkSize: t.LRC.ChunkSize,
  122. }, nil
  123. }
  124. func (t *LRCConstructAnyType) AddInput(node *dag.Node, str *dag.StreamVar, dataIndex int) {
  125. t.InputIndexes = append(t.InputIndexes, dataIndex)
  126. node.InputStreams = append(node.InputStreams, str)
  127. str.To(node, len(node.InputStreams)-1)
  128. }
  129. func (t *LRCConstructAnyType) RemoveAllInputs(n *dag.Node) {
  130. for _, in := range n.InputStreams {
  131. in.From.Node.OutputStreams[in.From.SlotIndex].NotTo(n)
  132. }
  133. n.InputStreams = nil
  134. t.InputIndexes = nil
  135. }
  136. func (t *LRCConstructAnyType) NewOutput(node *dag.Node, dataIndex int) *dag.StreamVar {
  137. t.OutputIndexes = append(t.OutputIndexes, dataIndex)
  138. return dag.NodeNewOutputStream(node, &ioswitchlrc.VarProps{StreamIndex: dataIndex})
  139. }
  140. func (t *LRCConstructAnyType) String(node *dag.Node) string {
  141. return fmt.Sprintf("LRCAny[]%v%v", formatStreamIO(node), formatValueIO(node))
  142. }
  143. type LRCConstructGroupType struct {
  144. LRC cdssdk.LRCRedundancy
  145. TargetBlockIndex int
  146. }
  147. func (t *LRCConstructGroupType) InitNode(node *dag.Node) {
  148. dag.NodeNewOutputStream(node, &ioswitchlrc.VarProps{
  149. StreamIndex: t.TargetBlockIndex,
  150. })
  151. grpIdx := t.LRC.FindGroup(t.TargetBlockIndex)
  152. dag.NodeDeclareInputStream(node, t.LRC.Groups[grpIdx])
  153. }
  154. func (t *LRCConstructGroupType) GenerateOp(op *dag.Node) (exec.Op, error) {
  155. l, err := lrc.New(t.LRC.N, t.LRC.K, t.LRC.Groups)
  156. if err != nil {
  157. return nil, err
  158. }
  159. coef, err := l.GenerateGroupMatrix(t.TargetBlockIndex)
  160. if err != nil {
  161. return nil, err
  162. }
  163. return &GalMultiply{
  164. Coef: coef,
  165. Inputs: lo.Map(op.InputStreams, func(v *dag.StreamVar, idx int) *exec.StreamVar { return v.Var }),
  166. Outputs: lo.Map(op.OutputStreams, func(v *dag.StreamVar, idx int) *exec.StreamVar { return v.Var }),
  167. ChunkSize: t.LRC.ChunkSize,
  168. }, nil
  169. }
  170. func (t *LRCConstructGroupType) String(node *dag.Node) string {
  171. return fmt.Sprintf("LRCGroup[]%v%v", formatStreamIO(node), formatValueIO(node))
  172. }

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