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.

ioswitch.go 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package rpc
  2. import (
  3. "context"
  4. "time"
  5. "gitlink.org.cn/cloudream/common/consts/errorcode"
  6. "gitlink.org.cn/cloudream/common/pkgs/future"
  7. "gitlink.org.cn/cloudream/common/pkgs/logger"
  8. "gitlink.org.cn/cloudream/common/utils/io2"
  9. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/exec"
  10. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc"
  11. hubrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/hub"
  12. )
  13. func (s *Service) ExecuteIOPlan(ctx context.Context, req *hubrpc.ExecuteIOPlan) (*hubrpc.ExecuteIOPlanResp, *rpc.CodeError) {
  14. log := logger.WithField("PlanID", req.Plan.ID)
  15. log.Infof("begin execute io plan")
  16. sw := exec.NewExecutor(req.Plan, exec.Location{
  17. IsDriver: false,
  18. WorkerName: req.WorkerName,
  19. })
  20. s.swWorker.Add(sw)
  21. defer s.swWorker.Remove(sw)
  22. execCtx := exec.NewWithContext(ctx)
  23. exec.SetValueByType(execCtx, s.stgPool)
  24. ret, err := sw.Run(execCtx)
  25. if err != nil {
  26. log.Warnf("running io plan: %v", err)
  27. return nil, rpc.Failed(errorcode.OperationFailed, "%v", err)
  28. }
  29. log.Infof("plan finished")
  30. return &hubrpc.ExecuteIOPlanResp{
  31. Result: ret,
  32. }, nil
  33. }
  34. func (s *Service) SendIOStream(ctx context.Context, req *hubrpc.SendIOStream) (*hubrpc.SendIOStreamResp, *rpc.CodeError) {
  35. logger.
  36. WithField("PlanID", req.PlanID).
  37. WithField("VarID", req.VarID).
  38. Debugf("stream input")
  39. // 同一批Plan中每个节点的Plan的启动时间有先后,但最多不应该超过30秒
  40. ctx2, cancel := context.WithTimeout(ctx, time.Second*30)
  41. defer cancel()
  42. sw := s.swWorker.FindByIDContexted(ctx2, exec.PlanID(req.PlanID))
  43. if sw == nil {
  44. return nil, rpc.Failed(errorcode.DataNotFound, "plan not found")
  45. }
  46. fut := future.NewSetVoid()
  47. varID := exec.VarID(req.VarID)
  48. sw.PutVar(varID, &exec.StreamValue{Stream: io2.DelegateReadCloser(req.Stream, func() error {
  49. fut.SetVoid()
  50. return nil
  51. })})
  52. err := fut.Wait(ctx)
  53. if err != nil {
  54. return nil, rpc.Failed(errorcode.OperationFailed, "%v", err)
  55. }
  56. return &hubrpc.SendIOStreamResp{}, nil
  57. }
  58. func (s *Service) GetIOStream(ctx context.Context, req *hubrpc.GetIOStream) (*hubrpc.GetIOStreamResp, *rpc.CodeError) {
  59. logger.
  60. WithField("PlanID", req.PlanID).
  61. WithField("VarID", req.VarID).
  62. Debugf("stream output")
  63. // 同上
  64. ctx2, cancel := context.WithTimeout(ctx, time.Second*30)
  65. defer cancel()
  66. sw := s.swWorker.FindByIDContexted(ctx2, exec.PlanID(req.PlanID))
  67. if sw == nil {
  68. return nil, rpc.Failed(errorcode.DataNotFound, "plan not found")
  69. }
  70. sw.PutVar(req.SignalID, req.Signal)
  71. strVar, err := exec.BindVar[*exec.StreamValue](sw, ctx, exec.VarID(req.VarID))
  72. if err != nil {
  73. return nil, rpc.Failed(errorcode.OperationFailed, "bind var: %v", err)
  74. }
  75. return &hubrpc.GetIOStreamResp{
  76. Stream: strVar.Stream,
  77. }, nil
  78. }
  79. func (s *Service) SendIOVar(ctx context.Context, req *hubrpc.SendIOVar) (*hubrpc.SendIOVarResp, *rpc.CodeError) {
  80. ctx2, cancel := context.WithTimeout(ctx, time.Second*30)
  81. defer cancel()
  82. sw := s.swWorker.FindByIDContexted(ctx2, exec.PlanID(req.PlanID))
  83. if sw == nil {
  84. return nil, rpc.Failed(errorcode.DataNotFound, "plan not found")
  85. }
  86. sw.PutVar(req.VarID, req.Value)
  87. return &hubrpc.SendIOVarResp{}, nil
  88. }
  89. func (s *Service) GetIOVar(ctx context.Context, req *hubrpc.GetIOVar) (*hubrpc.GetIOVarResp, *rpc.CodeError) {
  90. ctx2, cancel := context.WithTimeout(ctx, time.Second*30)
  91. defer cancel()
  92. sw := s.swWorker.FindByIDContexted(ctx2, exec.PlanID(req.PlanID))
  93. if sw == nil {
  94. return nil, rpc.Failed(errorcode.DataNotFound, "plan not found")
  95. }
  96. sw.PutVar(req.SignalID, req.Signal)
  97. v, err := sw.BindVar(ctx, exec.VarID(req.VarID))
  98. if err != nil {
  99. return nil, rpc.Failed(errorcode.OperationFailed, "bind var: %v", err)
  100. }
  101. return &hubrpc.GetIOVarResp{
  102. Value: v,
  103. }, nil
  104. }

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