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.

grpc.go 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package ops
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "gitlink.org.cn/cloudream/common/pkgs/future"
  7. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/dag"
  8. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  9. "gitlink.org.cn/cloudream/common/pkgs/logger"
  10. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  11. "gitlink.org.cn/cloudream/common/utils/io2"
  12. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  13. )
  14. func init() {
  15. OpUnion.AddT((*SendStream)(nil))
  16. OpUnion.AddT((*GetStream)(nil))
  17. OpUnion.AddT((*SendVar)(nil))
  18. OpUnion.AddT((*GetVar)(nil))
  19. }
  20. type SendStream struct {
  21. Input *exec.StreamVar `json:"input"`
  22. Send *exec.StreamVar `json:"send"`
  23. Node cdssdk.Node `json:"node"`
  24. }
  25. func (o *SendStream) Execute(ctx context.Context, e *exec.Executor) error {
  26. err := e.BindVars(ctx, o.Input)
  27. if err != nil {
  28. return err
  29. }
  30. defer o.Input.Stream.Close()
  31. agtCli, err := stgglb.AgentRPCPool.Acquire(stgglb.SelectGRPCAddress(&o.Node))
  32. if err != nil {
  33. return fmt.Errorf("new agent rpc client: %w", err)
  34. }
  35. defer stgglb.AgentRPCPool.Release(agtCli)
  36. logger.Debugf("sending stream %v as %v to node %v", o.Input.ID, o.Send.ID, o.Node)
  37. // 发送后流的ID不同
  38. err = agtCli.SendStream(ctx, e.Plan().ID, o.Send.ID, o.Input.Stream)
  39. if err != nil {
  40. return fmt.Errorf("sending stream: %w", err)
  41. }
  42. return nil
  43. }
  44. type GetStream struct {
  45. Signal *exec.SignalVar `json:"signal"`
  46. Target *exec.StreamVar `json:"target"`
  47. Output *exec.StreamVar `json:"output"`
  48. Node cdssdk.Node `json:"node"`
  49. }
  50. func (o *GetStream) Execute(ctx context.Context, e *exec.Executor) error {
  51. agtCli, err := stgglb.AgentRPCPool.Acquire(stgglb.SelectGRPCAddress(&o.Node))
  52. if err != nil {
  53. return fmt.Errorf("new agent rpc client: %w", err)
  54. }
  55. defer stgglb.AgentRPCPool.Release(agtCli)
  56. logger.Debugf("getting stream %v as %v from node %v", o.Target.ID, o.Output.ID, o.Node)
  57. str, err := agtCli.GetStream(e.Plan().ID, o.Target.ID, o.Signal)
  58. if err != nil {
  59. return fmt.Errorf("getting stream: %w", err)
  60. }
  61. fut := future.NewSetVoid()
  62. // 获取后送到本地的流ID是不同的
  63. o.Output.Stream = io2.AfterReadClosedOnce(str, func(closer io.ReadCloser) {
  64. fut.SetVoid()
  65. })
  66. e.PutVars(o.Output)
  67. return fut.Wait(ctx)
  68. }
  69. type SendVar struct {
  70. Input exec.Var `json:"input"`
  71. Send exec.Var `json:"send"`
  72. Node cdssdk.Node `json:"node"`
  73. }
  74. func (o *SendVar) Execute(ctx context.Context, e *exec.Executor) error {
  75. err := e.BindVars(ctx, o.Input)
  76. if err != nil {
  77. return err
  78. }
  79. agtCli, err := stgglb.AgentRPCPool.Acquire(stgglb.SelectGRPCAddress(&o.Node))
  80. if err != nil {
  81. return fmt.Errorf("new agent rpc client: %w", err)
  82. }
  83. defer stgglb.AgentRPCPool.Release(agtCli)
  84. logger.Debugf("sending var %v as %v to node %v", o.Input.GetID(), o.Send.GetID(), o.Node)
  85. exec.AssignVar(o.Input, o.Send)
  86. err = agtCli.SendVar(ctx, e.Plan().ID, o.Send)
  87. if err != nil {
  88. return fmt.Errorf("sending var: %w", err)
  89. }
  90. return nil
  91. }
  92. type GetVar struct {
  93. Signal *exec.SignalVar `json:"signal"`
  94. Target exec.Var `json:"target"`
  95. Output exec.Var `json:"output"`
  96. Node cdssdk.Node `json:"node"`
  97. }
  98. func (o *GetVar) Execute(ctx context.Context, e *exec.Executor) error {
  99. agtCli, err := stgglb.AgentRPCPool.Acquire(stgglb.SelectGRPCAddress(&o.Node))
  100. if err != nil {
  101. return fmt.Errorf("new agent rpc client: %w", err)
  102. }
  103. defer stgglb.AgentRPCPool.Release(agtCli)
  104. logger.Debugf("getting var %v as %v from node %v", o.Target.GetID(), o.Output.GetID(), o.Node)
  105. v2, err := agtCli.GetVar(ctx, e.Plan().ID, o.Target, o.Signal)
  106. if err != nil {
  107. return fmt.Errorf("getting var: %w", err)
  108. }
  109. exec.AssignVar(v2, o.Output)
  110. e.PutVars(o.Output)
  111. return nil
  112. }
  113. type SendStreamType struct {
  114. }
  115. func (t *SendStreamType) InitNode(node *Node) {
  116. dag.NodeDeclareInputStream(node, 1)
  117. dag.NodeNewOutputStream(node, VarProps{})
  118. }
  119. func (t *SendStreamType) GenerateOp(op *Node, blder *exec.PlanBuilder) error {
  120. toAgt := op.OutputStreams[0].Toes[0].Node.Env.Worker.(*AgentWorker)
  121. addOpByEnv(&SendStream{
  122. Input: op.InputStreams[0].Props.Var.(*exec.StreamVar),
  123. Send: op.OutputStreams[0].Props.Var.(*exec.StreamVar),
  124. Node: toAgt.Node,
  125. }, op.Env, blder)
  126. return nil
  127. }
  128. func (t *SendStreamType) String(node *Node) string {
  129. return fmt.Sprintf("SendStream[]%v%v", formatStreamIO(node), formatValueIO(node))
  130. }
  131. type SendVarType struct {
  132. }
  133. func (t *SendVarType) InitNode(node *Node) {
  134. dag.NodeDeclareInputValue(node, 1)
  135. dag.NodeNewOutputValue(node, VarProps{})
  136. }
  137. func (t *SendVarType) GenerateOp(op *Node, blder *exec.PlanBuilder) error {
  138. toAgt := op.OutputValues[0].Toes[0].Node.Env.Worker.(*AgentWorker)
  139. addOpByEnv(&SendVar{
  140. Input: op.InputValues[0].Props.Var,
  141. Send: op.OutputValues[0].Props.Var,
  142. Node: toAgt.Node,
  143. }, op.Env, blder)
  144. return nil
  145. }
  146. func (t *SendVarType) String(node *Node) string {
  147. return fmt.Sprintf("SendVar[]%v%v", formatStreamIO(node), formatValueIO(node))
  148. }
  149. type GetStreamType struct {
  150. }
  151. func (t *GetStreamType) InitNode(node *Node) {
  152. dag.NodeDeclareInputStream(node, 1)
  153. dag.NodeNewOutputValue(node, VarProps{})
  154. dag.NodeNewOutputStream(node, VarProps{})
  155. }
  156. func (t *GetStreamType) GenerateOp(op *Node, blder *exec.PlanBuilder) error {
  157. fromAgt := op.InputStreams[0].From.Node.Env.Worker.(*AgentWorker)
  158. addOpByEnv(&GetStream{
  159. Signal: op.OutputValues[0].Props.Var.(*exec.SignalVar),
  160. Output: op.OutputStreams[0].Props.Var.(*exec.StreamVar),
  161. Target: op.InputStreams[0].Props.Var.(*exec.StreamVar),
  162. Node: fromAgt.Node,
  163. }, op.Env, blder)
  164. return nil
  165. }
  166. func (t *GetStreamType) String(node *Node) string {
  167. return fmt.Sprintf("GetStream[]%v%v", formatStreamIO(node), formatValueIO(node))
  168. }
  169. type GetVaType struct {
  170. }
  171. func (t *GetVaType) InitNode(node *Node) {
  172. dag.NodeDeclareInputValue(node, 1)
  173. dag.NodeNewOutputValue(node, VarProps{})
  174. dag.NodeNewOutputValue(node, VarProps{})
  175. }
  176. func (t *GetVaType) GenerateOp(op *Node, blder *exec.PlanBuilder) error {
  177. fromAgt := op.InputValues[0].From.Node.Env.Worker.(*AgentWorker)
  178. addOpByEnv(&GetVar{
  179. Signal: op.OutputValues[0].Props.Var.(*exec.SignalVar),
  180. Output: op.OutputValues[1].Props.Var,
  181. Target: op.InputValues[0].Props.Var,
  182. Node: fromAgt.Node,
  183. }, op.Env, blder)
  184. return nil
  185. }
  186. func (t *GetVaType) String(node *Node) string {
  187. return fmt.Sprintf("GetVar[]%v%v", formatStreamIO(node), formatValueIO(node))
  188. }

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