join_align_node.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2021-2022 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/xsql"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/infra"
  20. )
  21. // JoinAlignNode will block the stream and buffer all the table tuples. Once buffered, it will combine the later input with the buffer
  22. // The input for batch table MUST be *WindowTuples
  23. type JoinAlignNode struct {
  24. *defaultSinkNode
  25. statManager StatManager
  26. emitters map[string]int
  27. // states
  28. batch *xsql.WindowTuplesSet
  29. }
  30. const BatchKey = "$$batchInputs"
  31. func NewJoinAlignNode(name string, emitters []string, options *api.RuleOption) (*JoinAlignNode, error) {
  32. emap := make(map[string]int, len(emitters))
  33. for i, e := range emitters {
  34. emap[e] = i
  35. }
  36. n := &JoinAlignNode{
  37. emitters: emap,
  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 := NewStatManager(ctx, "op")
  58. if err != nil {
  59. infra.DrainError(ctx, fmt.Errorf("no output channel found"), 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 []xsql.WindowTuples:
  69. if len(st) == len(n.emitters) {
  70. n.batch = &xsql.WindowTuplesSet{Content: st}
  71. log.Infof("Restore batch state %+v", st)
  72. } else {
  73. log.Warnf("Restore batch state got different emitter length so discarded: %+v", st)
  74. }
  75. case nil:
  76. log.Debugf("Restore batch state, nothing")
  77. default:
  78. infra.DrainError(ctx, fmt.Errorf("restore batch state %v error, invalid type", st), errCh)
  79. }
  80. } else {
  81. log.Warnf("Restore batch state fails: %s", err)
  82. }
  83. if n.batch == nil {
  84. n.batch = &xsql.WindowTuplesSet{
  85. Content: make([]xsql.WindowTuples, len(n.emitters)),
  86. }
  87. }
  88. for {
  89. log.Debugf("JoinAlignNode %s is looping", n.name)
  90. select {
  91. // process incoming item from both streams(transformed) and tables
  92. case item, opened := <-n.input:
  93. processed := false
  94. if item, processed = n.preprocess(item); processed {
  95. break
  96. }
  97. n.statManager.IncTotalRecordsIn()
  98. n.statManager.ProcessTimeStart()
  99. if !opened {
  100. n.statManager.IncTotalExceptions()
  101. break
  102. }
  103. switch d := item.(type) {
  104. case error:
  105. n.Broadcast(d)
  106. n.statManager.IncTotalExceptions()
  107. case *xsql.Tuple:
  108. log.Debugf("JoinAlignNode receive tuple input %s", d)
  109. temp := xsql.WindowTuplesSet{
  110. Content: make([]xsql.WindowTuples, 0),
  111. }
  112. temp = temp.AddTuple(d)
  113. n.alignBatch(ctx, temp)
  114. case xsql.WindowTuplesSet:
  115. log.Debugf("JoinAlignNode receive window input %s", d)
  116. n.alignBatch(ctx, d)
  117. case xsql.WindowTuples: // batch input
  118. log.Debugf("JoinAlignNode receive batch source %s", d)
  119. // Buffer and update batch inputs
  120. index, ok := n.emitters[d.Emitter]
  121. if !ok {
  122. n.Broadcast(fmt.Errorf("run JoinAlignNode error: receive batch input from unknown emitter %[1]T(%[1]v)", d))
  123. n.statManager.IncTotalExceptions()
  124. }
  125. if n.batch != nil && len(n.batch.Content) > index {
  126. n.batch.Content[index] = d
  127. ctx.PutState(BatchKey, n.batch)
  128. } else {
  129. log.Errorf("Invalid index %d for batch %v", index, n.batch)
  130. }
  131. default:
  132. n.Broadcast(fmt.Errorf("run JoinAlignNode error: invalid input type but got %[1]T(%[1]v)", d))
  133. n.statManager.IncTotalExceptions()
  134. }
  135. case <-ctx.Done():
  136. log.Infoln("Cancelling join align node....")
  137. return nil
  138. }
  139. }
  140. })
  141. if err != nil {
  142. infra.DrainError(ctx, err, errCh)
  143. }
  144. }()
  145. }
  146. func (n *JoinAlignNode) alignBatch(_ api.StreamContext, w xsql.WindowTuplesSet) {
  147. n.statManager.ProcessTimeStart()
  148. w.Content = append(w.Content, n.batch.Content...)
  149. n.Broadcast(w)
  150. n.statManager.ProcessTimeEnd()
  151. n.statManager.IncTotalRecordsOut()
  152. n.statManager.SetBufferLength(int64(len(n.input)))
  153. }
  154. func (n *JoinAlignNode) GetMetrics() [][]interface{} {
  155. if n.statManager != nil {
  156. return [][]interface{}{
  157. n.statManager.GetMetrics(),
  158. }
  159. } else {
  160. return nil
  161. }
  162. }