switch_node.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/ast"
  21. "github.com/lf-edge/ekuiper/pkg/infra"
  22. )
  23. type SwitchConfig struct {
  24. Cases []ast.Expr
  25. StopAtFirstMatch bool
  26. }
  27. type SwitchNode struct {
  28. *defaultSinkNode
  29. conf *SwitchConfig
  30. statManager metric.StatManager
  31. outputNodes []defaultNode
  32. }
  33. // GetEmitter returns the nth emitter of the node. SwtichNode is the only node that has multiple emitters
  34. // In planner graph, fromNodes is a multi-dim array, switch node is the only node that could be in the second dim
  35. // The dim is the index
  36. func (n *SwitchNode) GetEmitter(outputIndex int) api.Emitter {
  37. return &n.outputNodes[outputIndex]
  38. }
  39. // AddOutput SwitchNode overrides the defaultSinkNode's AddOutput to add output to the outputNodes
  40. // SwitchNode itself has multiple outlets defined by the outputNodes.
  41. // This default function will add the output to the first outlet
  42. func (n *SwitchNode) AddOutput(output chan<- interface{}, name string) error {
  43. if len(n.outputNodes) == 0 { // should never happen
  44. return fmt.Errorf("no output node is available")
  45. }
  46. return n.outputNodes[0].AddOutput(output, name)
  47. }
  48. func NewSwitchNode(name string, conf *SwitchConfig, options *api.RuleOption) (*SwitchNode, error) {
  49. sn := &SwitchNode{
  50. conf: conf,
  51. }
  52. sn.defaultSinkNode = &defaultSinkNode{
  53. input: make(chan interface{}, options.BufferLength),
  54. defaultNode: &defaultNode{
  55. outputs: nil,
  56. name: name,
  57. sendError: options.SendError,
  58. },
  59. }
  60. outputs := make([]defaultNode, len(conf.Cases))
  61. for i := range conf.Cases {
  62. outputs[i] = defaultNode{
  63. outputs: make(map[string]chan<- interface{}),
  64. name: name + fmt.Sprintf("_%d", i),
  65. sendError: options.SendError,
  66. }
  67. }
  68. sn.outputNodes = outputs
  69. return sn, nil
  70. }
  71. func (n *SwitchNode) Exec(ctx api.StreamContext, errCh chan<- error) {
  72. ctx.GetLogger().Infof("SwitchNode %s is started", n.name)
  73. stats, err := metric.NewStatManager(ctx, "op")
  74. if err != nil {
  75. infra.DrainError(ctx, fmt.Errorf("cannot create state for switch node %s", n.name), errCh)
  76. return
  77. }
  78. n.statManager = stats
  79. n.ctx = ctx
  80. for i := range n.outputNodes {
  81. n.outputNodes[i].ctx = ctx
  82. }
  83. fv, afv := xsql.NewFunctionValuersForOp(ctx)
  84. go func() {
  85. err := infra.SafeRun(func() error {
  86. for {
  87. ctx.GetLogger().Debugf("Switch node %s is looping", n.name)
  88. select {
  89. // process incoming item from both streams(transformed) and tables
  90. case item, opened := <-n.input:
  91. processed := false
  92. if item, processed = n.preprocess(item); processed {
  93. break
  94. }
  95. n.statManager.IncTotalRecordsIn()
  96. n.statManager.ProcessTimeStart()
  97. if !opened {
  98. n.statManager.IncTotalExceptions("input channel closed")
  99. break
  100. }
  101. var ve *xsql.ValuerEval
  102. switch d := item.(type) {
  103. case error:
  104. _ = n.Broadcast(d)
  105. n.statManager.IncTotalExceptions(d.Error())
  106. case *xsql.WatermarkTuple:
  107. _ = n.Broadcast(d)
  108. case xsql.TupleRow:
  109. ctx.GetLogger().Debugf("SwitchNode receive tuple input %s", d)
  110. ve = &xsql.ValuerEval{Valuer: xsql.MultiValuer(d, fv)}
  111. case xsql.SingleCollection:
  112. ctx.GetLogger().Debugf("SwitchNode receive window input %s", d)
  113. afv.SetData(d)
  114. ve = &xsql.ValuerEval{Valuer: xsql.MultiAggregateValuer(d, fv, d, fv, afv, &xsql.WildcardValuer{Data: d})}
  115. default:
  116. e := fmt.Errorf("run switch node error: invalid input type but got %[1]T(%[1]v)", d)
  117. _ = n.Broadcast(e)
  118. n.statManager.IncTotalExceptions(e.Error())
  119. break
  120. }
  121. caseLoop:
  122. for i, c := range n.conf.Cases {
  123. result := ve.Eval(c)
  124. switch r := result.(type) {
  125. case error:
  126. ctx.GetLogger().Errorf("run switch node %s, case %s error: %s", n.name, c, r)
  127. n.statManager.IncTotalExceptions(r.Error())
  128. case bool:
  129. if r {
  130. _ = n.outputNodes[i].Broadcast(item)
  131. if n.conf.StopAtFirstMatch {
  132. break caseLoop
  133. }
  134. }
  135. case nil: // nil is false
  136. break
  137. default:
  138. m := fmt.Sprintf("run switch node %s, case %s error: invalid condition that returns non-bool value %[1]T(%[1]v)", n.name, c, r)
  139. ctx.GetLogger().Errorf(m)
  140. n.statManager.IncTotalExceptions(m)
  141. }
  142. }
  143. n.statManager.ProcessTimeEnd()
  144. n.statManager.IncTotalRecordsOut()
  145. n.statManager.SetBufferLength(int64(len(n.input)))
  146. case <-ctx.Done():
  147. ctx.GetLogger().Infoln("Cancelling switch node....")
  148. return nil
  149. }
  150. }
  151. })
  152. if err != nil {
  153. infra.DrainError(ctx, err, errCh)
  154. }
  155. }()
  156. }
  157. func (n *SwitchNode) GetMetrics() [][]interface{} {
  158. if n.statManager != nil {
  159. return [][]interface{}{
  160. n.statManager.GetMetrics(),
  161. }
  162. } else {
  163. return nil
  164. }
  165. }