operations.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package operators
  2. import (
  3. "context"
  4. "engine/common"
  5. "engine/xstream/nodes"
  6. "fmt"
  7. "sync"
  8. )
  9. // UnOperation interface represents unary operations (i.e. Map, Filter, etc)
  10. type UnOperation interface {
  11. Apply(ctx context.Context, data interface{}) interface{}
  12. }
  13. // UnFunc implements UnOperation as type func (context.Context, interface{})
  14. type UnFunc func(context.Context, interface{}) interface{}
  15. // Apply implements UnOperation.Apply method
  16. func (f UnFunc) Apply(ctx context.Context, data interface{}) interface{} {
  17. return f(ctx, data)
  18. }
  19. type UnaryOperator struct {
  20. op UnOperation
  21. concurrency int
  22. input chan interface{}
  23. outputs map[string]chan<- interface{}
  24. mutex sync.RWMutex
  25. cancelled bool
  26. name string
  27. }
  28. // NewUnary creates *UnaryOperator value
  29. func New(name string) *UnaryOperator {
  30. // extract logger
  31. o := new(UnaryOperator)
  32. o.concurrency = 1
  33. o.input = make(chan interface{}, 1024)
  34. o.outputs = make(map[string]chan<- interface{})
  35. o.name = name
  36. return o
  37. }
  38. func (o *UnaryOperator) GetName() string {
  39. return o.name
  40. }
  41. // SetOperation sets the executor operation
  42. func (o *UnaryOperator) SetOperation(op UnOperation) {
  43. o.op = op
  44. }
  45. // SetConcurrency sets the concurrency level for the operation
  46. func (o *UnaryOperator) SetConcurrency(concurr int) {
  47. o.concurrency = concurr
  48. if o.concurrency < 1 {
  49. o.concurrency = 1
  50. }
  51. }
  52. func (o *UnaryOperator) AddOutput(output chan<- interface{}, name string) (err error){
  53. if _, ok := o.outputs[name]; !ok{
  54. o.outputs[name] = output
  55. }else{
  56. return fmt.Errorf("fail to add output %s, operator %s already has an output of the same name", name, o.name)
  57. }
  58. return nil
  59. }
  60. func (o *UnaryOperator) GetInput() (chan<- interface{}, string) {
  61. return o.input, o.name
  62. }
  63. // Exec is the entry point for the executor
  64. func (o *UnaryOperator) Exec(ctx context.Context) (err error) {
  65. log := common.GetLogger(ctx)
  66. log.Printf("Unary operator %s is started", o.name)
  67. if len(o.outputs) <= 0 {
  68. err = fmt.Errorf("no output channel found")
  69. return
  70. }
  71. // validate p
  72. if o.concurrency < 1 {
  73. o.concurrency = 1
  74. }
  75. go func() {
  76. var barrier sync.WaitGroup
  77. wgDelta := o.concurrency
  78. barrier.Add(wgDelta)
  79. for i := 0; i < o.concurrency; i++ { // workers
  80. go func(wg *sync.WaitGroup) {
  81. defer wg.Done()
  82. o.doOp(ctx)
  83. }(&barrier)
  84. }
  85. wait := make(chan struct{})
  86. go func() {
  87. defer close(wait)
  88. barrier.Wait()
  89. }()
  90. select {
  91. case <-wait:
  92. if o.cancelled {
  93. log.Printf("Component cancelling...")
  94. return
  95. }
  96. case <-ctx.Done():
  97. log.Printf("UnaryOp %s done.", o.name)
  98. return
  99. }
  100. }()
  101. return nil
  102. }
  103. func (o *UnaryOperator) doOp(ctx context.Context) {
  104. log := common.GetLogger(ctx)
  105. if o.op == nil {
  106. log.Println("Unary operator missing operation")
  107. return
  108. }
  109. exeCtx, cancel := context.WithCancel(ctx)
  110. defer func() {
  111. log.Infof("unary operator %s done, cancelling future items", o.name)
  112. cancel()
  113. }()
  114. for {
  115. select {
  116. // process incoming item
  117. case item := <-o.input:
  118. result := o.op.Apply(exeCtx, item)
  119. switch val := result.(type) {
  120. case nil:
  121. continue
  122. case error:
  123. log.Println(val)
  124. log.Println(val.Error())
  125. continue
  126. default:
  127. nodes.Broadcast(o.outputs, val)
  128. }
  129. // is cancelling
  130. case <-exeCtx.Done():
  131. log.Printf("unary operator %s cancelling....", o.name)
  132. o.mutex.Lock()
  133. cancel()
  134. o.cancelled = true
  135. o.mutex.Unlock()
  136. return
  137. }
  138. }
  139. }