switch_node.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.statManagers = []metric.StatManager{stats}
  80. n.ctx = ctx
  81. for i := range n.outputNodes {
  82. n.outputNodes[i].ctx = ctx
  83. }
  84. fv, afv := xsql.NewFunctionValuersForOp(ctx)
  85. go func() {
  86. err := infra.SafeRun(func() error {
  87. for {
  88. ctx.GetLogger().Debugf("Switch node %s is looping", n.name)
  89. select {
  90. // process incoming item from both streams(transformed) and tables
  91. case item, opened := <-n.input:
  92. processed := false
  93. if item, processed = n.preprocess(item); processed {
  94. break
  95. }
  96. n.statManager.IncTotalRecordsIn()
  97. n.statManager.ProcessTimeStart()
  98. if !opened {
  99. n.statManager.IncTotalExceptions("input channel closed")
  100. break
  101. }
  102. var ve *xsql.ValuerEval
  103. switch d := item.(type) {
  104. case error:
  105. _ = n.Broadcast(d)
  106. n.statManager.IncTotalExceptions(d.Error())
  107. case *xsql.WatermarkTuple:
  108. _ = n.Broadcast(d)
  109. case xsql.TupleRow:
  110. ctx.GetLogger().Debugf("SwitchNode receive tuple input %s", d)
  111. ve = &xsql.ValuerEval{Valuer: xsql.MultiValuer(d, fv)}
  112. case xsql.SingleCollection:
  113. ctx.GetLogger().Debugf("SwitchNode receive window input %s", d)
  114. afv.SetData(d)
  115. ve = &xsql.ValuerEval{Valuer: xsql.MultiAggregateValuer(d, fv, d, fv, afv, &xsql.WildcardValuer{Data: d})}
  116. default:
  117. e := fmt.Errorf("run switch node error: invalid input type but got %[1]T(%[1]v)", d)
  118. _ = n.Broadcast(e)
  119. n.statManager.IncTotalExceptions(e.Error())
  120. break
  121. }
  122. caseLoop:
  123. for i, c := range n.conf.Cases {
  124. result := ve.Eval(c)
  125. switch r := result.(type) {
  126. case error:
  127. ctx.GetLogger().Errorf("run switch node %s, case %s error: %s", n.name, c, r)
  128. n.statManager.IncTotalExceptions(r.Error())
  129. case bool:
  130. if r {
  131. _ = n.outputNodes[i].Broadcast(item)
  132. if n.conf.StopAtFirstMatch {
  133. break caseLoop
  134. }
  135. }
  136. case nil: // nil is false
  137. break
  138. default:
  139. 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)
  140. ctx.GetLogger().Errorf(m)
  141. n.statManager.IncTotalExceptions(m)
  142. }
  143. }
  144. n.statManager.ProcessTimeEnd()
  145. n.statManager.IncTotalRecordsOut()
  146. n.statManager.SetBufferLength(int64(len(n.input)))
  147. case <-ctx.Done():
  148. ctx.GetLogger().Infoln("Cancelling switch node....")
  149. return nil
  150. }
  151. }
  152. })
  153. if err != nil {
  154. infra.DrainError(ctx, err, errCh)
  155. }
  156. }()
  157. }