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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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)
  17. s.swWorker.Add(sw)
  18. defer s.swWorker.Remove(sw)
  19. execCtx := exec.NewWithContext(ctx)
  20. exec.SetValueByType(execCtx, s.stgPool)
  21. _, err := sw.Run(execCtx)
  22. if err != nil {
  23. log.Warnf("running io plan: %v", err)
  24. return nil, rpc.Failed(errorcode.OperationFailed, "%v", err)
  25. }
  26. log.Infof("plan finished")
  27. return &hubrpc.ExecuteIOPlanResp{}, nil
  28. }
  29. func (s *Service) SendIOStream(ctx context.Context, req *hubrpc.SendIOStream) (*hubrpc.SendIOStreamResp, *rpc.CodeError) {
  30. logger.
  31. WithField("PlanID", req.PlanID).
  32. WithField("VarID", req.VarID).
  33. Debugf("stream input")
  34. // 同一批Plan中每个节点的Plan的启动时间有先后,但最多不应该超过30秒
  35. ctx2, cancel := context.WithTimeout(ctx, time.Second*30)
  36. defer cancel()
  37. sw := s.swWorker.FindByIDContexted(ctx2, exec.PlanID(req.PlanID))
  38. if sw == nil {
  39. return nil, rpc.Failed(errorcode.DataNotFound, "plan not found")
  40. }
  41. fut := future.NewSetVoid()
  42. varID := exec.VarID(req.VarID)
  43. sw.PutVar(varID, &exec.StreamValue{Stream: io2.DelegateReadCloser(req.Stream, func() error {
  44. fut.SetVoid()
  45. return nil
  46. })})
  47. err := fut.Wait(ctx)
  48. if err != nil {
  49. return nil, rpc.Failed(errorcode.OperationFailed, "%v", err)
  50. }
  51. return &hubrpc.SendIOStreamResp{}, nil
  52. }
  53. func (s *Service) GetIOStream(ctx context.Context, req *hubrpc.GetIOStream) (*hubrpc.GetIOStreamResp, *rpc.CodeError) {
  54. logger.
  55. WithField("PlanID", req.PlanID).
  56. WithField("VarID", req.VarID).
  57. Debugf("stream output")
  58. // 同上
  59. ctx2, cancel := context.WithTimeout(ctx, time.Second*30)
  60. defer cancel()
  61. sw := s.swWorker.FindByIDContexted(ctx2, exec.PlanID(req.PlanID))
  62. if sw == nil {
  63. return nil, rpc.Failed(errorcode.DataNotFound, "plan not found")
  64. }
  65. sw.PutVar(req.SignalID, req.Signal)
  66. strVar, err := exec.BindVar[*exec.StreamValue](sw, ctx, exec.VarID(req.VarID))
  67. if err != nil {
  68. return nil, rpc.Failed(errorcode.OperationFailed, "bind var: %v", err)
  69. }
  70. return &hubrpc.GetIOStreamResp{
  71. Stream: strVar.Stream,
  72. }, nil
  73. }
  74. func (s *Service) SendIOVar(ctx context.Context, req *hubrpc.SendIOVar) (*hubrpc.SendIOVarResp, *rpc.CodeError) {
  75. ctx2, cancel := context.WithTimeout(ctx, time.Second*30)
  76. defer cancel()
  77. sw := s.swWorker.FindByIDContexted(ctx2, exec.PlanID(req.PlanID))
  78. if sw == nil {
  79. return nil, rpc.Failed(errorcode.DataNotFound, "plan not found")
  80. }
  81. sw.PutVar(req.VarID, req.Value)
  82. return &hubrpc.SendIOVarResp{}, nil
  83. }
  84. func (s *Service) GetIOVar(ctx context.Context, req *hubrpc.GetIOVar) (*hubrpc.GetIOVarResp, *rpc.CodeError) {
  85. ctx2, cancel := context.WithTimeout(ctx, time.Second*30)
  86. defer cancel()
  87. sw := s.swWorker.FindByIDContexted(ctx2, exec.PlanID(req.PlanID))
  88. if sw == nil {
  89. return nil, rpc.Failed(errorcode.DataNotFound, "plan not found")
  90. }
  91. sw.PutVar(req.SignalID, req.Signal)
  92. v, err := sw.BindVar(ctx, exec.VarID(req.VarID))
  93. if err != nil {
  94. return nil, rpc.Failed(errorcode.OperationFailed, "bind var: %v", err)
  95. }
  96. return &hubrpc.GetIOVarResp{
  97. Value: v,
  98. }, nil
  99. }

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