join_align_node.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package nodes
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/emqx/kuiper/xsql"
  6. "github.com/emqx/kuiper/xstream/api"
  7. )
  8. /*
  9. * This node will block the stream and buffer all the table tuples. Once buffered, it will combine the later input with the buffer
  10. * The input for batch table MUST be *WindowTuples
  11. */
  12. type JoinAlignNode struct {
  13. *defaultSinkNode
  14. statManager StatManager
  15. emitters []string
  16. // states
  17. batch xsql.WindowTuplesSet
  18. }
  19. const StreamInputsKey = "$$streamInputs"
  20. func NewJoinAlignNode(name string, emitters []string, options *api.RuleOption) (*JoinAlignNode, error) {
  21. n := &JoinAlignNode{
  22. emitters: emitters,
  23. batch: make([]xsql.WindowTuples, len(emitters)),
  24. }
  25. n.defaultSinkNode = &defaultSinkNode{
  26. input: make(chan interface{}, options.BufferLength),
  27. defaultNode: &defaultNode{
  28. outputs: make(map[string]chan<- interface{}),
  29. name: name,
  30. sendError: options.SendError,
  31. },
  32. }
  33. return n, nil
  34. }
  35. func (n *JoinAlignNode) Exec(ctx api.StreamContext, errCh chan<- error) {
  36. n.ctx = ctx
  37. log := ctx.GetLogger()
  38. log.Debugf("JoinAlignNode %s is started", n.name)
  39. if len(n.outputs) <= 0 {
  40. go func() { errCh <- fmt.Errorf("no output channel found") }()
  41. return
  42. }
  43. stats, err := NewStatManager("op", ctx)
  44. if err != nil {
  45. go func() { errCh <- err }()
  46. return
  47. }
  48. n.statManager = stats
  49. var inputs []xsql.WindowTuplesSet
  50. batchLen := len(n.emitters)
  51. go func() {
  52. for {
  53. log.Debugf("JoinAlignNode %s is looping", n.name)
  54. select {
  55. // process incoming item from both streams(transformed) and tables
  56. case item, opened := <-n.input:
  57. processed := false
  58. if item, processed = n.preprocess(item); processed {
  59. break
  60. }
  61. n.statManager.IncTotalRecordsIn()
  62. n.statManager.ProcessTimeStart()
  63. if !opened {
  64. n.statManager.IncTotalExceptions()
  65. break
  66. }
  67. switch d := item.(type) {
  68. case error:
  69. n.Broadcast(d)
  70. n.statManager.IncTotalExceptions()
  71. case *xsql.Tuple:
  72. log.Debugf("JoinAlignNode receive tuple input %s", d)
  73. var temp xsql.WindowTuplesSet = make([]xsql.WindowTuples, 0)
  74. temp = temp.AddTuple(d)
  75. if batchLen == 0 {
  76. n.alignBatch(ctx, temp)
  77. } else {
  78. log.Debugf("JoinAlignNode buffer input")
  79. inputs = append(inputs, temp)
  80. ctx.PutState(StreamInputsKey, inputs)
  81. n.statManager.SetBufferLength(int64(len(n.input)))
  82. }
  83. case xsql.WindowTuplesSet:
  84. log.Debugf("JoinAlignNode receive window input %s", d)
  85. if batchLen == 0 {
  86. n.alignBatch(ctx, d)
  87. } else {
  88. log.Debugf("JoinAlignNode buffer input")
  89. inputs = append(inputs, d)
  90. ctx.PutState(StreamInputsKey, inputs)
  91. n.statManager.SetBufferLength(int64(len(n.input)))
  92. }
  93. case xsql.WindowTuples: // batch input
  94. log.Debugf("JoinAlignNode receive batch source %s", d)
  95. if batchLen <= 0 {
  96. errCh <- errors.New("Join receives too many table content")
  97. }
  98. n.batch[len(n.emitters)-batchLen] = d
  99. batchLen -= 1
  100. if batchLen == 0 {
  101. for _, w := range inputs {
  102. n.alignBatch(ctx, w)
  103. }
  104. }
  105. default:
  106. n.Broadcast(fmt.Errorf("run JoinAlignNode error: invalid input type but got %[1]T(%[1]v)", d))
  107. n.statManager.IncTotalExceptions()
  108. }
  109. case <-ctx.Done():
  110. log.Infoln("Cancelling join align node....")
  111. return
  112. }
  113. }
  114. }()
  115. }
  116. func (n *JoinAlignNode) alignBatch(ctx api.StreamContext, w xsql.WindowTuplesSet) {
  117. n.statManager.ProcessTimeStart()
  118. w = append(w, n.batch...)
  119. n.Broadcast(w)
  120. n.statManager.ProcessTimeEnd()
  121. n.statManager.IncTotalRecordsOut()
  122. n.statManager.SetBufferLength(int64(len(n.input)))
  123. ctx.PutState(StreamInputsKey, nil)
  124. }
  125. func (n *JoinAlignNode) GetMetrics() [][]interface{} {
  126. if n.statManager != nil {
  127. return [][]interface{}{
  128. n.statManager.GetMetrics(),
  129. }
  130. } else {
  131. return nil
  132. }
  133. }