operations.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package operators
  2. import (
  3. "github.com/emqx/kuiper/xstream/api"
  4. "github.com/emqx/kuiper/xstream/nodes"
  5. "fmt"
  6. "sync"
  7. )
  8. // UnOperation interface represents unary operations (i.e. Map, Filter, etc)
  9. type UnOperation interface {
  10. Apply(ctx api.StreamContext, data interface{}) interface{}
  11. }
  12. // UnFunc implements UnOperation as type func (context.Context, interface{})
  13. type UnFunc func(api.StreamContext, interface{}) interface{}
  14. // Apply implements UnOperation.Apply method
  15. func (f UnFunc) Apply(ctx api.StreamContext, 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) error{
  52. if _, ok := o.outputs[name]; !ok{
  53. o.outputs[name] = output
  54. }else{
  55. return fmt.Errorf("fail to add output %s, operator %s already has an output of the same name", name, o.name)
  56. }
  57. return nil
  58. }
  59. func (o *UnaryOperator) GetInput() (chan<- interface{}, string) {
  60. return o.input, o.name
  61. }
  62. // Exec is the entry point for the executor
  63. func (o *UnaryOperator) Exec(ctx api.StreamContext, errCh chan<- error ) {
  64. log := ctx.GetLogger()
  65. log.Debugf("Unary operator %s is started", o.name)
  66. if len(o.outputs) <= 0 {
  67. go func(){errCh <- fmt.Errorf("no output channel found")}()
  68. return
  69. }
  70. // validate p
  71. if o.concurrency < 1 {
  72. o.concurrency = 1
  73. }
  74. go func() {
  75. var barrier sync.WaitGroup
  76. wgDelta := o.concurrency
  77. barrier.Add(wgDelta)
  78. for i := 0; i < o.concurrency; i++ { // workers
  79. go func(wg *sync.WaitGroup) {
  80. defer wg.Done()
  81. o.doOp(ctx, errCh)
  82. }(&barrier)
  83. }
  84. wait := make(chan struct{})
  85. go func() {
  86. defer close(wait)
  87. barrier.Wait()
  88. }()
  89. select {
  90. case <-wait:
  91. if o.cancelled {
  92. log.Infof("Component cancelling...")
  93. return
  94. }
  95. case <-ctx.Done():
  96. log.Infof("UnaryOp %s done.", o.name)
  97. return
  98. }
  99. }()
  100. }
  101. func (o *UnaryOperator) doOp(ctx api.StreamContext, errCh chan<- error) {
  102. log := ctx.GetLogger()
  103. if o.op == nil {
  104. log.Infoln("Unary operator missing operation")
  105. return
  106. }
  107. exeCtx, cancel := ctx.WithCancel()
  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 error: //TODO error handling
  121. log.Infoln(val)
  122. log.Infoln(val.Error())
  123. continue
  124. default:
  125. nodes.Broadcast(o.outputs, val, ctx)
  126. }
  127. // is cancelling
  128. case <-ctx.Done():
  129. log.Infof("unary operator %s cancelling....", o.name)
  130. o.mutex.Lock()
  131. cancel()
  132. o.cancelled = true
  133. o.mutex.Unlock()
  134. return
  135. }
  136. }
  137. }