operations.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package nodes
  2. import (
  3. "fmt"
  4. "github.com/emqx/kuiper/xsql"
  5. "github.com/emqx/kuiper/xstream/api"
  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{}, fv *xsql.FunctionValuer, afv *xsql.AggregateFunctionValuer) 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. *defaultSinkNode
  20. op UnOperation
  21. mutex sync.RWMutex
  22. cancelled bool
  23. }
  24. // NewUnary creates *UnaryOperator value
  25. func New(name string, bufferLength int) *UnaryOperator {
  26. return &UnaryOperator{
  27. defaultSinkNode: &defaultSinkNode{
  28. input: make(chan interface{}, bufferLength),
  29. defaultNode: &defaultNode{
  30. name: name,
  31. outputs: make(map[string]chan<- interface{}),
  32. concurrency: 1,
  33. },
  34. },
  35. }
  36. }
  37. // SetOperation sets the executor operation
  38. func (o *UnaryOperator) SetOperation(op UnOperation) {
  39. o.op = op
  40. }
  41. // Exec is the entry point for the executor
  42. func (o *UnaryOperator) Exec(ctx api.StreamContext, errCh chan<- error) {
  43. o.ctx = ctx
  44. log := ctx.GetLogger()
  45. log.Debugf("Unary operator %s is started", o.name)
  46. if len(o.outputs) <= 0 {
  47. go func() { errCh <- fmt.Errorf("no output channel found") }()
  48. return
  49. }
  50. // validate p
  51. if o.concurrency < 1 {
  52. o.concurrency = 1
  53. }
  54. //reset status
  55. o.statManagers = nil
  56. for i := 0; i < o.concurrency; i++ { // workers
  57. instance := i
  58. go o.doOp(ctx.WithInstance(instance), errCh)
  59. }
  60. }
  61. func (o *UnaryOperator) doOp(ctx api.StreamContext, errCh chan<- error) {
  62. logger := ctx.GetLogger()
  63. if o.op == nil {
  64. logger.Infoln("Unary operator missing operation")
  65. return
  66. }
  67. exeCtx, cancel := ctx.WithCancel()
  68. defer func() {
  69. logger.Infof("unary operator %s instance %d done, cancelling future items", o.name, ctx.GetInstanceId())
  70. cancel()
  71. }()
  72. stats, err := NewStatManager("op", ctx)
  73. if err != nil {
  74. select {
  75. case errCh <- err:
  76. logger.Errorf("unary operator %s error %s", o.name, err)
  77. case <-ctx.Done():
  78. logger.Infof("unary operator %s cancelling....", o.name)
  79. o.mutex.Lock()
  80. cancel()
  81. o.cancelled = true
  82. o.mutex.Unlock()
  83. }
  84. return
  85. }
  86. o.mutex.Lock()
  87. o.statManagers = append(o.statManagers, stats)
  88. o.mutex.Unlock()
  89. fv, afv := xsql.NewFunctionValuersForOp(exeCtx)
  90. for {
  91. select {
  92. // process incoming item
  93. case item := <-o.input:
  94. processed := false
  95. if item, processed = o.preprocess(item); processed {
  96. break
  97. }
  98. stats.IncTotalRecordsIn()
  99. stats.ProcessTimeStart()
  100. result := o.op.Apply(exeCtx, item, fv, afv)
  101. switch val := result.(type) {
  102. case nil:
  103. continue
  104. case error:
  105. logger.Errorf("Operation %s error: %s", ctx.GetOpId(), val)
  106. o.Broadcast(val)
  107. stats.IncTotalExceptions()
  108. continue
  109. default:
  110. stats.ProcessTimeEnd()
  111. o.Broadcast(val)
  112. stats.IncTotalRecordsOut()
  113. stats.SetBufferLength(int64(len(o.input)))
  114. }
  115. // is cancelling
  116. case <-ctx.Done():
  117. logger.Infof("unary operator %s instance %d cancelling....", o.name, ctx.GetInstanceId())
  118. o.mutex.Lock()
  119. cancel()
  120. o.cancelled = true
  121. o.mutex.Unlock()
  122. return
  123. }
  124. }
  125. }