join_align_node.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.TupleRow
  29. }
  30. const BatchKey = "$$batchInputs"
  31. func NewJoinAlignNode(name string, emitters []string, options *api.RuleOption) (*JoinAlignNode, error) {
  32. batch := make(map[string][]xsql.TupleRow, 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.TupleRow:
  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.TupleRow)
  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. temp := &xsql.WindowTuples{
  106. Content: make([]xsql.TupleRow, 0),
  107. }
  108. temp = temp.AddTuple(d)
  109. n.alignBatch(ctx, temp)
  110. case *xsql.WindowTuples:
  111. if d.WindowRange != nil { // real window
  112. log.Debugf("JoinAlignNode receive window input %s", d)
  113. n.alignBatch(ctx, d)
  114. } else { // table window
  115. log.Debugf("JoinAlignNode receive batch source %s", d)
  116. emitter := d.Content[0].GetEmitter()
  117. // Buffer and update batch inputs
  118. _, ok := n.batch[emitter]
  119. if !ok {
  120. e := fmt.Errorf("run JoinAlignNode error: receive batch input from unknown emitter %[1]T(%[1]v)", d)
  121. _ = n.Broadcast(e)
  122. n.statManager.IncTotalExceptions(e.Error())
  123. break
  124. }
  125. n.batch[emitter] = d.Content
  126. _ = ctx.PutState(BatchKey, n.batch)
  127. }
  128. default:
  129. e := fmt.Errorf("run JoinAlignNode error: invalid input type but got %[1]T(%[1]v)", d)
  130. _ = n.Broadcast(e)
  131. n.statManager.IncTotalExceptions(e.Error())
  132. }
  133. case <-ctx.Done():
  134. log.Infoln("Cancelling join align node....")
  135. return nil
  136. }
  137. }
  138. })
  139. if err != nil {
  140. infra.DrainError(ctx, err, errCh)
  141. }
  142. }()
  143. }
  144. func (n *JoinAlignNode) alignBatch(_ api.StreamContext, w *xsql.WindowTuples) {
  145. n.statManager.ProcessTimeStart()
  146. for _, v := range n.batch {
  147. if v != nil {
  148. w.Content = append(w.Content, v...)
  149. }
  150. }
  151. _ = n.Broadcast(w)
  152. n.statManager.ProcessTimeEnd()
  153. n.statManager.IncTotalRecordsOut()
  154. n.statManager.SetBufferLength(int64(len(n.input)))
  155. }
  156. func (n *JoinAlignNode) GetMetrics() [][]interface{} {
  157. if n.statManager != nil {
  158. return [][]interface{}{
  159. n.statManager.GetMetrics(),
  160. }
  161. } else {
  162. return nil
  163. }
  164. }