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

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. clitypes "gitlink.org.cn/cloudream/jcs-pub/client/types"
  9. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ec"
  10. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ec/lrc"
  11. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/dag"
  12. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/exec"
  13. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/utils"
  14. )
  15. func init() {
  16. exec.UseOp[*GalMultiply]()
  17. }
  18. type GalMultiply struct {
  19. Coef [][]byte `json:"coef"`
  20. Inputs []exec.VarID `json:"inputs"`
  21. Outputs []exec.VarID `json:"outputs"`
  22. ChunkSize int `json:"chunkSize"`
  23. }
  24. func (o *GalMultiply) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  25. inputs, err := exec.BindArray[*exec.StreamValue](e, ctx.Context, o.Inputs)
  26. if err != nil {
  27. return err
  28. }
  29. defer func() {
  30. for _, s := range inputs {
  31. s.Stream.Close()
  32. }
  33. }()
  34. outputWrs := make([]*io.PipeWriter, len(o.Outputs))
  35. outputVars := make([]*exec.StreamValue, len(o.Outputs))
  36. for i := range o.Outputs {
  37. rd, wr := io.Pipe()
  38. outputVars[i] = &exec.StreamValue{Stream: rd}
  39. outputWrs[i] = wr
  40. }
  41. fut := future.NewSetVoid()
  42. go func() {
  43. mul := ec.GaloisMultiplier().BuildGalois()
  44. inputChunks := make([][]byte, len(o.Inputs))
  45. for i := range o.Inputs {
  46. inputChunks[i] = make([]byte, o.ChunkSize)
  47. }
  48. outputChunks := make([][]byte, len(o.Outputs))
  49. for i := range o.Outputs {
  50. outputChunks[i] = make([]byte, o.ChunkSize)
  51. }
  52. for {
  53. err := sync2.ParallelDo(inputs, func(s *exec.StreamValue, i int) error {
  54. _, err := io.ReadFull(s.Stream, inputChunks[i])
  55. return err
  56. })
  57. if err == io.EOF {
  58. fut.SetVoid()
  59. return
  60. }
  61. if err != nil {
  62. fut.SetError(err)
  63. return
  64. }
  65. err = mul.Multiply(o.Coef, inputChunks, outputChunks)
  66. if err != nil {
  67. fut.SetError(err)
  68. return
  69. }
  70. for i := range o.Outputs {
  71. err := io2.WriteAll(outputWrs[i], outputChunks[i])
  72. if err != nil {
  73. fut.SetError(err)
  74. return
  75. }
  76. }
  77. }
  78. }()
  79. exec.PutArray(e, o.Outputs, outputVars)
  80. err = fut.Wait(ctx.Context)
  81. if err != nil {
  82. for _, wr := range outputWrs {
  83. wr.CloseWithError(err)
  84. }
  85. return err
  86. }
  87. for _, wr := range outputWrs {
  88. wr.Close()
  89. }
  90. return nil
  91. }
  92. func (o *GalMultiply) String() string {
  93. return fmt.Sprintf(
  94. "ECMultiply(coef=%v) (%v) -> (%v)",
  95. o.Coef,
  96. utils.FormatVarIDs(o.Inputs),
  97. utils.FormatVarIDs(o.Outputs),
  98. )
  99. }
  100. type LRCConstructAnyNode struct {
  101. dag.NodeBase
  102. LRC clitypes.LRCRedundancy
  103. InputIndexes []int
  104. OutputIndexes []int
  105. }
  106. func (b *GraphNodeBuilder) NewLRCConstructAny(lrc clitypes.LRCRedundancy) *LRCConstructAnyNode {
  107. node := &LRCConstructAnyNode{
  108. LRC: lrc,
  109. }
  110. b.AddNode(node)
  111. return node
  112. }
  113. func (t *LRCConstructAnyNode) AddInput(str *dag.StreamVar, dataIndex int) {
  114. t.InputIndexes = append(t.InputIndexes, dataIndex)
  115. idx := t.InputStreams().EnlargeOne()
  116. str.To(t, idx)
  117. }
  118. func (t *LRCConstructAnyNode) RemoveAllInputs() {
  119. t.InputStreams().ClearAllInput(t)
  120. t.InputStreams().Slots.Resize(0)
  121. t.InputIndexes = nil
  122. }
  123. func (t *LRCConstructAnyNode) NewOutput(dataIndex int) *dag.StreamVar {
  124. t.OutputIndexes = append(t.OutputIndexes, dataIndex)
  125. return t.OutputStreams().AppendNew(t).Var()
  126. }
  127. func (t *LRCConstructAnyNode) GenerateOp() (exec.Op, error) {
  128. l, err := lrc.New(t.LRC.N, t.LRC.K, t.LRC.Groups)
  129. if err != nil {
  130. return nil, err
  131. }
  132. coef, err := l.GenerateMatrix(t.InputIndexes, t.OutputIndexes)
  133. if err != nil {
  134. return nil, err
  135. }
  136. return &GalMultiply{
  137. Coef: coef,
  138. Inputs: t.InputStreams().GetVarIDs(),
  139. Outputs: t.OutputStreams().GetVarIDs(),
  140. ChunkSize: t.LRC.ChunkSize,
  141. }, nil
  142. }
  143. // func (t *LRCConstructAnyType) String() string {
  144. // return fmt.Sprintf("LRCAny[]%v%v", formatStreamIO(node), formatValueIO(node))
  145. // }
  146. type LRCConstructGroupNode struct {
  147. dag.NodeBase
  148. LRC clitypes.LRCRedundancy
  149. TargetBlockIndex int
  150. }
  151. func (b *GraphNodeBuilder) NewLRCConstructGroup(lrc clitypes.LRCRedundancy) *LRCConstructGroupNode {
  152. node := &LRCConstructGroupNode{
  153. LRC: lrc,
  154. }
  155. b.AddNode(node)
  156. node.OutputStreams().Init(node, 1)
  157. return node
  158. }
  159. func (t *LRCConstructGroupNode) SetupForTarget(blockIdx int, inputs []*dag.StreamVar) *dag.StreamVar {
  160. t.TargetBlockIndex = blockIdx
  161. t.InputStreams().Init(len(inputs))
  162. for i := 0; i < len(inputs); i++ {
  163. inputs[i].To(t, i)
  164. }
  165. return t.OutputStreams().Get(0)
  166. }
  167. func (t *LRCConstructGroupNode) GenerateOp() (exec.Op, error) {
  168. l, err := lrc.New(t.LRC.N, t.LRC.K, t.LRC.Groups)
  169. if err != nil {
  170. return nil, err
  171. }
  172. coef, err := l.GenerateGroupMatrix(t.TargetBlockIndex)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return &GalMultiply{
  177. Coef: coef,
  178. Inputs: t.InputStreams().GetVarIDs(),
  179. Outputs: t.OutputStreams().GetVarIDs(),
  180. ChunkSize: t.LRC.ChunkSize,
  181. }, nil
  182. }
  183. // func (t *LRCConstructGroupType) String() string {
  184. // return fmt.Sprintf("LRCGroup[]%v%v", formatStreamIO(node), formatValueIO(node))
  185. // }

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