join_align_node.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. go func() {
  64. err := infra.SafeRun(func() error {
  65. // restore batch state
  66. if s, err := ctx.GetState(BatchKey); err == nil {
  67. switch st := s.(type) {
  68. case map[string][]*xsql.Tuple:
  69. n.batch = st
  70. log.Infof("Restore batch state %+v", st)
  71. case nil:
  72. log.Debugf("Restore batch state, nothing")
  73. default:
  74. infra.DrainError(ctx, fmt.Errorf("restore batch state %v error, invalid type", st), errCh)
  75. }
  76. } else {
  77. log.Warnf("Restore batch state fails: %s", err)
  78. }
  79. if n.batch == nil {
  80. n.batch = make(map[string][]*xsql.Tuple)
  81. }
  82. for {
  83. log.Debugf("JoinAlignNode %s is looping", n.name)
  84. select {
  85. // process incoming item from both streams(transformed) and tables
  86. case item, opened := <-n.input:
  87. processed := false
  88. if item, processed = n.preprocess(item); processed {
  89. break
  90. }
  91. n.statManager.IncTotalRecordsIn()
  92. n.statManager.ProcessTimeStart()
  93. if !opened {
  94. n.statManager.IncTotalExceptions("input channel closed")
  95. break
  96. }
  97. switch d := item.(type) {
  98. case error:
  99. _ = n.Broadcast(d)
  100. n.statManager.IncTotalExceptions(d.Error())
  101. case *xsql.WatermarkTuple:
  102. _ = n.Broadcast(d)
  103. case *xsql.Tuple:
  104. log.Debugf("JoinAlignNode receive tuple input %s", d)
  105. n.alignBatch(ctx, d)
  106. case *xsql.WindowTuples:
  107. if d.WindowRange != nil { // real window
  108. log.Debugf("JoinAlignNode receive window input %s", d)
  109. n.alignBatch(ctx, d)
  110. } else { // table window
  111. log.Debugf("JoinAlignNode receive batch source %s", d)
  112. emitter := d.Content[0].GetEmitter()
  113. // Buffer and update batch inputs
  114. _, ok := n.batch[emitter]
  115. if !ok {
  116. e := fmt.Errorf("run JoinAlignNode error: receive batch input from unknown emitter %[1]T(%[1]v)", d)
  117. _ = n.Broadcast(e)
  118. n.statManager.IncTotalExceptions(e.Error())
  119. break
  120. }
  121. n.batch[emitter] = convertToTupleSlice(d.Content)
  122. _ = ctx.PutState(BatchKey, n.batch)
  123. }
  124. default:
  125. e := fmt.Errorf("run JoinAlignNode error: invalid input type but got %[1]T(%[1]v)", d)
  126. _ = n.Broadcast(e)
  127. n.statManager.IncTotalExceptions(e.Error())
  128. }
  129. case <-ctx.Done():
  130. log.Infoln("Cancelling join align node....")
  131. return nil
  132. }
  133. }
  134. })
  135. if err != nil {
  136. infra.DrainError(ctx, err, errCh)
  137. }
  138. }()
  139. }
  140. func convertToTupleSlice(content []xsql.TupleRow) []*xsql.Tuple {
  141. tuples := make([]*xsql.Tuple, len(content))
  142. for i, v := range content {
  143. tuples[i] = v.(*xsql.Tuple)
  144. }
  145. return tuples
  146. }
  147. func (n *JoinAlignNode) alignBatch(_ api.StreamContext, input any) {
  148. n.statManager.ProcessTimeStart()
  149. var w *xsql.WindowTuples
  150. switch t := input.(type) {
  151. case *xsql.Tuple:
  152. w = &xsql.WindowTuples{
  153. Content: make([]xsql.TupleRow, 0),
  154. }
  155. w.AddTuple(t)
  156. case *xsql.WindowTuples:
  157. w = t
  158. }
  159. for _, contents := range n.batch {
  160. if contents != nil {
  161. for _, v := range contents {
  162. w = w.AddTuple(v)
  163. }
  164. }
  165. }
  166. _ = n.Broadcast(w)
  167. n.statManager.ProcessTimeEnd()
  168. n.statManager.IncTotalRecordsOut()
  169. n.statManager.SetBufferLength(int64(len(n.input)))
  170. }
  171. func (n *JoinAlignNode) GetMetrics() [][]interface{} {
  172. if n.statManager != nil {
  173. return [][]interface{}{
  174. n.statManager.GetMetrics(),
  175. }
  176. } else {
  177. return nil
  178. }
  179. }