node.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/binder/io"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/internal/topo/checkpoint"
  20. "github.com/lf-edge/ekuiper/internal/topo/context"
  21. "github.com/lf-edge/ekuiper/internal/topo/node/metric"
  22. "github.com/lf-edge/ekuiper/internal/xsql"
  23. "github.com/lf-edge/ekuiper/pkg/api"
  24. )
  25. type OperatorNode interface {
  26. api.Operator
  27. Broadcast(data interface{}) error
  28. GetStreamContext() api.StreamContext
  29. GetInputCount() int
  30. AddInputCount()
  31. SetQos(api.Qos)
  32. SetBarrierHandler(checkpoint.BarrierHandler)
  33. }
  34. type DataSourceNode interface {
  35. api.Emitter
  36. Open(ctx api.StreamContext, errCh chan<- error)
  37. GetName() string
  38. GetMetrics() [][]interface{}
  39. Broadcast(val interface{}) error
  40. GetStreamContext() api.StreamContext
  41. SetQos(api.Qos)
  42. }
  43. type defaultNode struct {
  44. name string
  45. outputs map[string]chan<- interface{}
  46. concurrency int
  47. sendError bool
  48. statManagers []metric.StatManager
  49. ctx api.StreamContext
  50. qos api.Qos
  51. }
  52. func (o *defaultNode) AddOutput(output chan<- interface{}, name string) error {
  53. if _, ok := o.outputs[name]; !ok {
  54. o.outputs[name] = output
  55. } else {
  56. return fmt.Errorf("fail to add output %s, node %s already has an output of the same name", name, o.name)
  57. }
  58. return nil
  59. }
  60. func (o *defaultNode) GetName() string {
  61. return o.name
  62. }
  63. // SetConcurrency sets the concurrency level for the operation
  64. func (o *defaultNode) SetConcurrency(concurr int) {
  65. o.concurrency = concurr
  66. if o.concurrency < 1 {
  67. o.concurrency = 1
  68. }
  69. }
  70. func (o *defaultNode) SetQos(qos api.Qos) {
  71. o.qos = qos
  72. }
  73. func (o *defaultNode) GetMetrics() (result [][]interface{}) {
  74. for _, stats := range o.statManagers {
  75. result = append(result, stats.GetMetrics())
  76. }
  77. return result
  78. }
  79. func (o *defaultNode) Broadcast(val interface{}) error {
  80. if _, ok := val.(error); ok && !o.sendError {
  81. return nil
  82. }
  83. if o.qos >= api.AtLeastOnce {
  84. boe := &checkpoint.BufferOrEvent{
  85. Data: val,
  86. Channel: o.name,
  87. }
  88. o.doBroadcast(boe)
  89. return nil
  90. }
  91. o.doBroadcast(val)
  92. return nil
  93. }
  94. func (o *defaultNode) doBroadcast(val interface{}) {
  95. for name, out := range o.outputs {
  96. select {
  97. case out <- val:
  98. // do nothing
  99. case <-o.ctx.Done():
  100. // rule stop so stop waiting
  101. default:
  102. o.statManagers[0].IncTotalExceptions(fmt.Sprintf("buffer full, drop message from to %s", name))
  103. o.ctx.GetLogger().Debugf("drop message from %s to %s", o.name, name)
  104. }
  105. switch vt := val.(type) {
  106. case xsql.Collection:
  107. val = vt.Clone()
  108. break
  109. case xsql.TupleRow:
  110. val = vt.Clone()
  111. }
  112. }
  113. }
  114. func (o *defaultNode) GetStreamContext() api.StreamContext {
  115. return o.ctx
  116. }
  117. type defaultSinkNode struct {
  118. *defaultNode
  119. input chan interface{}
  120. barrierHandler checkpoint.BarrierHandler
  121. inputCount int
  122. }
  123. func (o *defaultSinkNode) GetInput() (chan<- interface{}, string) {
  124. return o.input, o.name
  125. }
  126. func (o *defaultSinkNode) GetInputCount() int {
  127. return o.inputCount
  128. }
  129. func (o *defaultSinkNode) AddInputCount() {
  130. o.inputCount++
  131. }
  132. func (o *defaultSinkNode) SetBarrierHandler(bh checkpoint.BarrierHandler) {
  133. o.barrierHandler = bh
  134. }
  135. // return the data and if processed
  136. func (o *defaultSinkNode) preprocess(data interface{}) (interface{}, bool) {
  137. if o.qos >= api.AtLeastOnce {
  138. logger := o.ctx.GetLogger()
  139. logger.Debugf("%s preprocess receive data %+v", o.name, data)
  140. b, ok := data.(*checkpoint.BufferOrEvent)
  141. if ok {
  142. logger.Debugf("data is BufferOrEvent, start barrier handler")
  143. //if it is barrier return true and ignore the further processing
  144. //if it is blocked(align handler), return true and then write back to the channel later
  145. if o.barrierHandler.Process(b, o.ctx) {
  146. return nil, true
  147. } else {
  148. return b.Data, false
  149. }
  150. }
  151. }
  152. return data, false
  153. }
  154. func SinkOpen(sinkType string, config map[string]interface{}) error {
  155. sink, err := getSink(sinkType, config)
  156. if err != nil {
  157. return err
  158. }
  159. contextLogger := conf.Log.WithField("rule", "TestSinkOpen"+"_"+sinkType)
  160. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  161. defer func() {
  162. _ = sink.Close(ctx)
  163. }()
  164. return sink.Open(ctx)
  165. }
  166. func SourceOpen(sourceType string, config map[string]interface{}) error {
  167. dataSource := "/$$TEST_CONNECTION$$"
  168. if v, ok := config["DATASOURCE"]; ok {
  169. dataSource = v.(string)
  170. }
  171. ns, err := io.Source(sourceType)
  172. if err != nil {
  173. return err
  174. }
  175. err = ns.Configure(dataSource, config)
  176. if err != nil {
  177. return err
  178. }
  179. contextLogger := conf.Log.WithField("rule", "TestSourceOpen"+"_"+sourceType)
  180. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  181. defer ns.Close(ctx)
  182. return nil
  183. }