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.

plan_builder.go 2.5 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package exec
  2. import (
  3. "context"
  4. "sync"
  5. "gitlink.org.cn/cloudream/common/pkgs/future"
  6. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  7. "gitlink.org.cn/cloudream/common/utils/lo2"
  8. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch"
  9. )
  10. type PlanBuilder struct {
  11. Vars []ioswitch.Var
  12. AgentPlans map[cdssdk.NodeID]*AgentPlanBuilder
  13. ExecutorPlan ExecutorPlanBuilder
  14. }
  15. func NewPlanBuilder() *PlanBuilder {
  16. bld := &PlanBuilder{
  17. AgentPlans: make(map[cdssdk.NodeID]*AgentPlanBuilder),
  18. ExecutorPlan: ExecutorPlanBuilder{
  19. StoreMap: &sync.Map{},
  20. },
  21. }
  22. return bld
  23. }
  24. func (b *PlanBuilder) AtExecutor() *ExecutorPlanBuilder {
  25. return &b.ExecutorPlan
  26. }
  27. func (b *PlanBuilder) AtAgent(node cdssdk.Node) *AgentPlanBuilder {
  28. agtPlan, ok := b.AgentPlans[node.NodeID]
  29. if !ok {
  30. agtPlan = &AgentPlanBuilder{
  31. Node: node,
  32. }
  33. b.AgentPlans[node.NodeID] = agtPlan
  34. }
  35. return agtPlan
  36. }
  37. func (b *PlanBuilder) NewStreamVar() *ioswitch.StreamVar {
  38. v := &ioswitch.StreamVar{
  39. ID: ioswitch.VarID(len(b.Vars)),
  40. }
  41. b.Vars = append(b.Vars, v)
  42. return v
  43. }
  44. func (b *PlanBuilder) NewIntVar() *ioswitch.IntVar {
  45. v := &ioswitch.IntVar{
  46. ID: ioswitch.VarID(len(b.Vars)),
  47. }
  48. b.Vars = append(b.Vars, v)
  49. return v
  50. }
  51. func (b *PlanBuilder) NewStringVar() *ioswitch.StringVar {
  52. v := &ioswitch.StringVar{
  53. ID: ioswitch.VarID(len(b.Vars)),
  54. }
  55. b.Vars = append(b.Vars, v)
  56. return v
  57. }
  58. func (b *PlanBuilder) NewSignalVar() *ioswitch.SignalVar {
  59. v := &ioswitch.SignalVar{
  60. ID: ioswitch.VarID(len(b.Vars)),
  61. }
  62. b.Vars = append(b.Vars, v)
  63. return v
  64. }
  65. func (b *PlanBuilder) Execute() *Executor {
  66. ctx, cancel := context.WithCancel(context.Background())
  67. planID := genRandomPlanID()
  68. execPlan := ioswitch.Plan{
  69. ID: planID,
  70. Ops: b.ExecutorPlan.Ops,
  71. }
  72. exec := Executor{
  73. planID: planID,
  74. planBlder: b,
  75. callback: future.NewSetVoid(),
  76. ctx: ctx,
  77. cancel: cancel,
  78. executorSw: ioswitch.NewSwitch(execPlan),
  79. }
  80. go exec.execute()
  81. return &exec
  82. }
  83. type AgentPlanBuilder struct {
  84. Node cdssdk.Node
  85. Ops []ioswitch.Op
  86. }
  87. func (b *AgentPlanBuilder) AddOp(op ioswitch.Op) {
  88. b.Ops = append(b.Ops, op)
  89. }
  90. func (b *AgentPlanBuilder) RemoveOp(op ioswitch.Op) {
  91. b.Ops = lo2.Remove(b.Ops, op)
  92. }
  93. type ExecutorPlanBuilder struct {
  94. Ops []ioswitch.Op
  95. StoreMap *sync.Map
  96. }
  97. func (b *ExecutorPlanBuilder) AddOp(op ioswitch.Op) {
  98. b.Ops = append(b.Ops, op)
  99. }
  100. func (b *ExecutorPlanBuilder) RemoveOp(op ioswitch.Op) {
  101. b.Ops = lo2.Remove(b.Ops, op)
  102. }

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