operations.go 3.8 KB

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