node.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. "strings"
  22. "sync"
  23. )
  24. type OperatorNode interface {
  25. api.Operator
  26. Broadcast(data interface{}) error
  27. GetStreamContext() api.StreamContext
  28. GetInputCount() int
  29. AddInputCount()
  30. SetQos(api.Qos)
  31. SetBarrierHandler(checkpoint.BarrierHandler)
  32. }
  33. type DataSourceNode interface {
  34. api.Emitter
  35. Open(ctx api.StreamContext, errCh chan<- error)
  36. GetName() string
  37. GetMetrics() [][]interface{}
  38. Broadcast(val interface{}) error
  39. GetStreamContext() api.StreamContext
  40. SetQos(api.Qos)
  41. }
  42. type defaultNode struct {
  43. name string
  44. outputs map[string]chan<- interface{}
  45. concurrency int
  46. sendError bool
  47. statManagers []StatManager
  48. ctx api.StreamContext
  49. qos api.Qos
  50. }
  51. func (o *defaultNode) AddOutput(output chan<- interface{}, name string) error {
  52. if _, ok := o.outputs[name]; !ok {
  53. o.outputs[name] = output
  54. } else {
  55. return fmt.Errorf("fail to add output %s, node %s already has an output of the same name", name, o.name)
  56. }
  57. return nil
  58. }
  59. func (o *defaultNode) GetName() string {
  60. return o.name
  61. }
  62. // SetConcurrency sets the concurrency level for the operation
  63. func (o *defaultNode) SetConcurrency(concurr int) {
  64. o.concurrency = concurr
  65. if o.concurrency < 1 {
  66. o.concurrency = 1
  67. }
  68. }
  69. func (o *defaultNode) SetQos(qos api.Qos) {
  70. o.qos = qos
  71. }
  72. func (o *defaultNode) GetMetrics() (result [][]interface{}) {
  73. for _, stats := range o.statManagers {
  74. result = append(result, stats.GetMetrics())
  75. }
  76. return result
  77. }
  78. func (o *defaultNode) Broadcast(val interface{}) error {
  79. if !o.sendError {
  80. if _, ok := val.(error); ok {
  81. return nil
  82. }
  83. }
  84. if o.qos >= api.AtLeastOnce {
  85. boe := &checkpoint.BufferOrEvent{
  86. Data: val,
  87. Channel: o.name,
  88. }
  89. return o.doBroadcast(boe)
  90. }
  91. return o.doBroadcast(val)
  92. }
  93. func (o *defaultNode) doBroadcast(val interface{}) error {
  94. logger := o.ctx.GetLogger()
  95. var wg sync.WaitGroup
  96. wg.Add(len(o.outputs))
  97. for n, out := range o.outputs {
  98. go func(name string, output chan<- interface{}) {
  99. select {
  100. case output <- val:
  101. logger.Debugf("broadcast from %s to %s done", o.ctx.GetOpId(), name)
  102. case <-o.ctx.Done():
  103. // rule stop so stop waiting
  104. }
  105. wg.Done()
  106. }(n, out)
  107. }
  108. logger.Debugf("broadcasting from %s", o.ctx.GetOpId())
  109. wg.Wait()
  110. return nil
  111. }
  112. func (o *defaultNode) GetStreamContext() api.StreamContext {
  113. return o.ctx
  114. }
  115. type defaultSinkNode struct {
  116. *defaultNode
  117. input chan interface{}
  118. barrierHandler checkpoint.BarrierHandler
  119. inputCount int
  120. }
  121. func (o *defaultSinkNode) GetInput() (chan<- interface{}, string) {
  122. return o.input, o.name
  123. }
  124. func (o *defaultSinkNode) GetInputCount() int {
  125. return o.inputCount
  126. }
  127. func (o *defaultSinkNode) AddInputCount() {
  128. o.inputCount++
  129. }
  130. func (o *defaultSinkNode) SetBarrierHandler(bh checkpoint.BarrierHandler) {
  131. o.barrierHandler = bh
  132. }
  133. // return the data and if processed
  134. func (o *defaultSinkNode) preprocess(data interface{}) (interface{}, bool) {
  135. if o.qos >= api.AtLeastOnce {
  136. logger := o.ctx.GetLogger()
  137. logger.Debugf("%s preprocess receive data %+v", o.name, data)
  138. b, ok := data.(*checkpoint.BufferOrEvent)
  139. if ok {
  140. logger.Debugf("data is BufferOrEvent, start barrier handler")
  141. //if it is barrier return true and ignore the further processing
  142. //if it is blocked(align handler), return true and then write back to the channel later
  143. if o.barrierHandler.Process(b, o.ctx) {
  144. return nil, true
  145. } else {
  146. return b.Data, false
  147. }
  148. }
  149. }
  150. return data, false
  151. }
  152. func getSourceConf(ctx api.StreamContext, sourceType string, options *ast.Options) map[string]interface{} {
  153. confkey := options.CONF_KEY
  154. logger := ctx.GetLogger()
  155. confPath := "sources/" + sourceType + ".yaml"
  156. if sourceType == "mqtt" {
  157. confPath = "mqtt_source.yaml"
  158. }
  159. props := make(map[string]interface{})
  160. cfg := make(map[string]interface{})
  161. err := conf.LoadConfigByName(confPath, &cfg)
  162. if err != nil {
  163. logger.Warnf("fail to parse yaml for source %s. Return an empty configuration", sourceType)
  164. } else {
  165. def, ok := cfg["default"]
  166. if !ok {
  167. logger.Warnf("default conf is not found", confkey)
  168. } else {
  169. if def1, ok1 := def.(map[string]interface{}); ok1 {
  170. props = def1
  171. }
  172. if c, ok := cfg[confkey]; ok {
  173. if c1, ok := c.(map[string]interface{}); ok {
  174. c2 := c1
  175. for k, v := range c2 {
  176. props[k] = v
  177. }
  178. }
  179. }
  180. }
  181. }
  182. f := options.FORMAT
  183. if f == "" {
  184. f = "json"
  185. }
  186. props["format"] = strings.ToLower(f)
  187. logger.Debugf("get conf for %s with conf key %s: %v", sourceType, confkey, props)
  188. return props
  189. }