join_align_node.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package node
  2. import (
  3. "fmt"
  4. "github.com/lf-edge/ekuiper/internal/xsql"
  5. "github.com/lf-edge/ekuiper/pkg/api"
  6. )
  7. // JoinAlignNode will block the stream and buffer all the table tuples. Once buffered, it will combine the later input with the buffer
  8. // The input for batch table MUST be *WindowTuples
  9. type JoinAlignNode struct {
  10. *defaultSinkNode
  11. statManager StatManager
  12. emitters map[string]int
  13. // states
  14. batch *xsql.WindowTuplesSet
  15. }
  16. const BatchKey = "$$batchInputs"
  17. func NewJoinAlignNode(name string, emitters []string, options *api.RuleOption) (*JoinAlignNode, error) {
  18. emap := make(map[string]int, len(emitters))
  19. for i, e := range emitters {
  20. emap[e] = i
  21. }
  22. n := &JoinAlignNode{
  23. emitters: emap,
  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. go func() {
  50. // restore batch state
  51. if s, err := ctx.GetState(BatchKey); err == nil {
  52. switch st := s.(type) {
  53. case []xsql.WindowTuples:
  54. if len(st) == len(n.emitters) {
  55. n.batch = &xsql.WindowTuplesSet{Content: st}
  56. log.Infof("Restore batch state %+v", st)
  57. } else {
  58. log.Warnf("Restore batch state got different emitter length so discarded: %+v", st)
  59. }
  60. case nil:
  61. log.Debugf("Restore batch state, nothing")
  62. default:
  63. errCh <- fmt.Errorf("restore batch state %v error, invalid type", st)
  64. }
  65. } else {
  66. log.Warnf("Restore batch state fails: %s", err)
  67. }
  68. if n.batch == nil {
  69. n.batch = &xsql.WindowTuplesSet{
  70. Content: make([]xsql.WindowTuples, len(n.emitters)),
  71. }
  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. temp := xsql.WindowTuplesSet{
  95. Content: make([]xsql.WindowTuples, 0),
  96. }
  97. temp = temp.AddTuple(d)
  98. n.alignBatch(ctx, temp)
  99. case xsql.WindowTuplesSet:
  100. log.Debugf("JoinAlignNode receive window input %s", d)
  101. n.alignBatch(ctx, d)
  102. case xsql.WindowTuples: // batch input
  103. log.Debugf("JoinAlignNode receive batch source %s", d)
  104. // Buffer and update batch inputs
  105. index, ok := n.emitters[d.Emitter]
  106. if !ok {
  107. n.Broadcast(fmt.Errorf("run JoinAlignNode error: receive batch input from unknown emitter %[1]T(%[1]v)", d))
  108. n.statManager.IncTotalExceptions()
  109. }
  110. if n.batch != nil && len(n.batch.Content) > index {
  111. n.batch.Content[index] = d
  112. ctx.PutState(BatchKey, n.batch)
  113. } else {
  114. log.Errorf("Invalid index %d for batch %v", index, n.batch)
  115. }
  116. default:
  117. n.Broadcast(fmt.Errorf("run JoinAlignNode error: invalid input type but got %[1]T(%[1]v)", d))
  118. n.statManager.IncTotalExceptions()
  119. }
  120. case <-ctx.Done():
  121. log.Infoln("Cancelling join align node....")
  122. return
  123. }
  124. }
  125. }()
  126. }
  127. func (n *JoinAlignNode) alignBatch(_ api.StreamContext, w xsql.WindowTuplesSet) {
  128. n.statManager.ProcessTimeStart()
  129. w.Content = append(w.Content, n.batch.Content...)
  130. n.Broadcast(w)
  131. n.statManager.ProcessTimeEnd()
  132. n.statManager.IncTotalRecordsOut()
  133. n.statManager.SetBufferLength(int64(len(n.input)))
  134. }
  135. func (n *JoinAlignNode) GetMetrics() [][]interface{} {
  136. if n.statManager != nil {
  137. return [][]interface{}{
  138. n.statManager.GetMetrics(),
  139. }
  140. } else {
  141. return nil
  142. }
  143. }