operations.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. case <-ctx.Done():
  77. logger.Infof("unary operator %s cancelling....", o.name)
  78. o.mutex.Lock()
  79. cancel()
  80. o.cancelled = true
  81. o.mutex.Unlock()
  82. }
  83. return
  84. }
  85. o.mutex.Lock()
  86. o.statManagers = append(o.statManagers, stats)
  87. o.mutex.Unlock()
  88. fv, afv := xsql.NewFunctionValuersForOp(exeCtx)
  89. for {
  90. select {
  91. // process incoming item
  92. case item := <-o.input:
  93. stats.IncTotalRecordsIn()
  94. stats.ProcessTimeStart()
  95. result := o.op.Apply(exeCtx, item, fv, afv)
  96. switch val := result.(type) {
  97. case nil:
  98. continue
  99. case error:
  100. logger.Errorf("Operation %s error: %s", ctx.GetOpId(), val)
  101. o.Broadcast(val)
  102. stats.IncTotalExceptions()
  103. continue
  104. default:
  105. stats.ProcessTimeEnd()
  106. o.Broadcast(val)
  107. stats.IncTotalRecordsOut()
  108. stats.SetBufferLength(int64(len(o.input)))
  109. }
  110. // is cancelling
  111. case <-ctx.Done():
  112. logger.Infof("unary operator %s instance %d cancelling....", o.name, ctx.GetInstanceId())
  113. o.mutex.Lock()
  114. cancel()
  115. o.cancelled = true
  116. o.mutex.Unlock()
  117. return
  118. }
  119. }
  120. }