node.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2021 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/conf"
  18. "github.com/lf-edge/ekuiper/internal/topo/checkpoint"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "github.com/lf-edge/ekuiper/pkg/ast"
  21. "gopkg.in/yaml.v3"
  22. "strings"
  23. "sync"
  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 []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 !o.sendError {
  81. if _, ok := val.(error); ok {
  82. return nil
  83. }
  84. }
  85. if o.qos >= api.AtLeastOnce {
  86. boe := &checkpoint.BufferOrEvent{
  87. Data: val,
  88. Channel: o.name,
  89. }
  90. return o.doBroadcast(boe)
  91. }
  92. return o.doBroadcast(val)
  93. }
  94. func (o *defaultNode) doBroadcast(val interface{}) error {
  95. logger := o.ctx.GetLogger()
  96. var wg sync.WaitGroup
  97. wg.Add(len(o.outputs))
  98. for n, out := range o.outputs {
  99. go func(name string, output chan<- interface{}) {
  100. select {
  101. case output <- val:
  102. logger.Debugf("broadcast from %s to %s done", o.ctx.GetOpId(), name)
  103. case <-o.ctx.Done():
  104. // rule stop so stop waiting
  105. }
  106. wg.Done()
  107. }(n, out)
  108. }
  109. logger.Debugf("broadcasting from %s", o.ctx.GetOpId())
  110. wg.Wait()
  111. return nil
  112. }
  113. func (o *defaultNode) GetStreamContext() api.StreamContext {
  114. return o.ctx
  115. }
  116. type defaultSinkNode struct {
  117. *defaultNode
  118. input chan interface{}
  119. barrierHandler checkpoint.BarrierHandler
  120. inputCount int
  121. }
  122. func (o *defaultSinkNode) GetInput() (chan<- interface{}, string) {
  123. return o.input, o.name
  124. }
  125. func (o *defaultSinkNode) GetInputCount() int {
  126. return o.inputCount
  127. }
  128. func (o *defaultSinkNode) AddInputCount() {
  129. o.inputCount++
  130. }
  131. func (o *defaultSinkNode) SetBarrierHandler(bh checkpoint.BarrierHandler) {
  132. o.barrierHandler = bh
  133. }
  134. // return the data and if processed
  135. func (o *defaultSinkNode) preprocess(data interface{}) (interface{}, bool) {
  136. if o.qos >= api.AtLeastOnce {
  137. logger := o.ctx.GetLogger()
  138. logger.Debugf("%s preprocess receive data %+v", o.name, data)
  139. b, ok := data.(*checkpoint.BufferOrEvent)
  140. if ok {
  141. logger.Debugf("data is BufferOrEvent, start barrier handler")
  142. //if it is barrier return true and ignore the further processing
  143. //if it is blocked(align handler), return true and then write back to the channel later
  144. if o.barrierHandler.Process(b, o.ctx) {
  145. return nil, true
  146. } else {
  147. return b.Data, false
  148. }
  149. }
  150. }
  151. return data, false
  152. }
  153. func getSourceConf(ctx api.StreamContext, sourceType string, options *ast.Options) map[string]interface{} {
  154. confkey := options.CONF_KEY
  155. logger := ctx.GetLogger()
  156. confPath := "sources/" + sourceType + ".yaml"
  157. if sourceType == "mqtt" {
  158. confPath = "mqtt_source.yaml"
  159. }
  160. conf, err := conf.LoadConf(confPath)
  161. props := make(map[string]interface{})
  162. if err == nil {
  163. cfg := make(map[string]interface{})
  164. if err := yaml.Unmarshal(conf, &cfg); err != nil {
  165. logger.Warnf("fail to parse yaml for source %s. Return an empty configuration", sourceType)
  166. } else {
  167. def, ok := cfg["default"]
  168. if !ok {
  169. logger.Warnf("default conf is not found", confkey)
  170. } else {
  171. if def1, ok1 := def.(map[string]interface{}); ok1 {
  172. props = def1
  173. }
  174. if c, ok := cfg[confkey]; ok {
  175. if c1, ok := c.(map[string]interface{}); ok {
  176. c2 := c1
  177. for k, v := range c2 {
  178. props[k] = v
  179. }
  180. }
  181. }
  182. }
  183. }
  184. } else {
  185. logger.Warnf("config file %s.yaml is not loaded properly. Return an empty configuration", sourceType)
  186. }
  187. f := options.FORMAT
  188. if f == "" {
  189. f = "json"
  190. }
  191. props["format"] = strings.ToLower(f)
  192. logger.Debugf("get conf for %s with conf key %s: %v", sourceType, confkey, props)
  193. return props
  194. }