join_align_node.go 4.0 KB

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