join_align_node.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package node
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/topo/node/metric"
  18. "github.com/lf-edge/ekuiper/internal/xsql"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "github.com/lf-edge/ekuiper/pkg/infra"
  21. )
  22. // JoinAlignNode will block the stream and buffer all the table tuples. Once buffered, it will combine the later input with the buffer
  23. // The input for batch table MUST be *WindowTuples
  24. type JoinAlignNode struct {
  25. *defaultSinkNode
  26. statManager metric.StatManager
  27. // states
  28. batch map[string][]*xsql.Tuple
  29. }
  30. const BatchKey = "$$batchInputs"
  31. func NewJoinAlignNode(name string, emitters []string, options *api.RuleOption) (*JoinAlignNode, error) {
  32. batch := make(map[string][]*xsql.Tuple, len(emitters))
  33. for _, e := range emitters {
  34. batch[e] = nil
  35. }
  36. n := &JoinAlignNode{
  37. batch: batch,
  38. }
  39. n.defaultSinkNode = &defaultSinkNode{
  40. input: make(chan interface{}, options.BufferLength),
  41. defaultNode: &defaultNode{
  42. outputs: make(map[string]chan<- interface{}),
  43. name: name,
  44. sendError: options.SendError,
  45. },
  46. }
  47. return n, nil
  48. }
  49. func (n *JoinAlignNode) Exec(ctx api.StreamContext, errCh chan<- error) {
  50. n.ctx = ctx
  51. log := ctx.GetLogger()
  52. log.Debugf("JoinAlignNode %s is started", n.name)
  53. if len(n.outputs) <= 0 {
  54. infra.DrainError(ctx, fmt.Errorf("no output channel found"), errCh)
  55. return
  56. }
  57. stats, err := metric.NewStatManager(ctx, "op")
  58. if err != nil {
  59. infra.DrainError(ctx, fmt.Errorf("fail to create stat manager"), errCh)
  60. return
  61. }
  62. n.statManager = stats
  63. n.statManagers = []metric.StatManager{stats}
  64. go func() {
  65. err := infra.SafeRun(func() error {
  66. // restore batch state
  67. if s, err := ctx.GetState(BatchKey); err == nil {
  68. switch st := s.(type) {
  69. case map[string][]*xsql.Tuple:
  70. n.batch = st
  71. log.Infof("Restore batch state %+v", st)
  72. case nil:
  73. log.Debugf("Restore batch state, nothing")
  74. default:
  75. infra.DrainError(ctx, fmt.Errorf("restore batch state %v error, invalid type", st), errCh)
  76. }
  77. } else {
  78. log.Warnf("Restore batch state fails: %s", err)
  79. }
  80. if n.batch == nil {
  81. n.batch = make(map[string][]*xsql.Tuple)
  82. }
  83. for {
  84. log.Debugf("JoinAlignNode %s is looping", n.name)
  85. select {
  86. // process incoming item from both streams(transformed) and tables
  87. case item, opened := <-n.input:
  88. processed := false
  89. if item, processed = n.preprocess(item); processed {
  90. break
  91. }
  92. n.statManager.IncTotalRecordsIn()
  93. n.statManager.ProcessTimeStart()
  94. if !opened {
  95. n.statManager.IncTotalExceptions("input channel closed")
  96. break
  97. }
  98. switch d := item.(type) {
  99. case error:
  100. _ = n.Broadcast(d)
  101. n.statManager.IncTotalExceptions(d.Error())
  102. case *xsql.WatermarkTuple:
  103. _ = n.Broadcast(d)
  104. case *xsql.Tuple:
  105. log.Debugf("JoinAlignNode receive tuple input %s", d)
  106. n.alignBatch(ctx, d)
  107. case *xsql.WindowTuples:
  108. if d.WindowRange != nil { // real window
  109. log.Debugf("JoinAlignNode receive window input %s", d)
  110. n.alignBatch(ctx, d)
  111. } else { // table window
  112. log.Debugf("JoinAlignNode receive batch source %s", d)
  113. emitter := d.Content[0].GetEmitter()
  114. // Buffer and update batch inputs
  115. _, ok := n.batch[emitter]
  116. if !ok {
  117. e := fmt.Errorf("run JoinAlignNode error: receive batch input from unknown emitter %[1]T(%[1]v)", d)
  118. _ = n.Broadcast(e)
  119. n.statManager.IncTotalExceptions(e.Error())
  120. break
  121. }
  122. n.batch[emitter] = convertToTupleSlice(d.Content)
  123. _ = ctx.PutState(BatchKey, n.batch)
  124. }
  125. default:
  126. e := fmt.Errorf("run JoinAlignNode error: invalid input type but got %[1]T(%[1]v)", d)
  127. _ = n.Broadcast(e)
  128. n.statManager.IncTotalExceptions(e.Error())
  129. }
  130. case <-ctx.Done():
  131. log.Infoln("Cancelling join align node....")
  132. return nil
  133. }
  134. }
  135. })
  136. if err != nil {
  137. infra.DrainError(ctx, err, errCh)
  138. }
  139. }()
  140. }
  141. func convertToTupleSlice(content []xsql.TupleRow) []*xsql.Tuple {
  142. tuples := make([]*xsql.Tuple, len(content))
  143. for i, v := range content {
  144. tuples[i] = v.(*xsql.Tuple)
  145. }
  146. return tuples
  147. }
  148. func (n *JoinAlignNode) alignBatch(_ api.StreamContext, input any) {
  149. n.statManager.ProcessTimeStart()
  150. var w *xsql.WindowTuples
  151. switch t := input.(type) {
  152. case *xsql.Tuple:
  153. w = &xsql.WindowTuples{
  154. Content: make([]xsql.TupleRow, 0),
  155. }
  156. w.AddTuple(t)
  157. case *xsql.WindowTuples:
  158. w = t
  159. }
  160. for _, contents := range n.batch {
  161. if contents != nil {
  162. for _, v := range contents {
  163. w = w.AddTuple(v)
  164. }
  165. }
  166. }
  167. _ = n.Broadcast(w)
  168. n.statManager.ProcessTimeEnd()
  169. n.statManager.IncTotalRecordsOut()
  170. n.statManager.SetBufferLength(int64(len(n.input)))
  171. }