node.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. var wg sync.WaitGroup
  95. wg.Add(len(o.outputs))
  96. for n, out := range o.outputs {
  97. go func(name string, output chan<- interface{}) {
  98. select {
  99. case output <- val:
  100. // do nothing
  101. case <-o.ctx.Done():
  102. // rule stop so stop waiting
  103. }
  104. wg.Done()
  105. }(n, out)
  106. }
  107. wg.Wait()
  108. return nil
  109. }
  110. func (o *defaultNode) GetStreamContext() api.StreamContext {
  111. return o.ctx
  112. }
  113. type defaultSinkNode struct {
  114. *defaultNode
  115. input chan interface{}
  116. barrierHandler checkpoint.BarrierHandler
  117. inputCount int
  118. }
  119. func (o *defaultSinkNode) GetInput() (chan<- interface{}, string) {
  120. return o.input, o.name
  121. }
  122. func (o *defaultSinkNode) GetInputCount() int {
  123. return o.inputCount
  124. }
  125. func (o *defaultSinkNode) AddInputCount() {
  126. o.inputCount++
  127. }
  128. func (o *defaultSinkNode) SetBarrierHandler(bh checkpoint.BarrierHandler) {
  129. o.barrierHandler = bh
  130. }
  131. // return the data and if processed
  132. func (o *defaultSinkNode) preprocess(data interface{}) (interface{}, bool) {
  133. if o.qos >= api.AtLeastOnce {
  134. logger := o.ctx.GetLogger()
  135. logger.Debugf("%s preprocess receive data %+v", o.name, data)
  136. b, ok := data.(*checkpoint.BufferOrEvent)
  137. if ok {
  138. logger.Debugf("data is BufferOrEvent, start barrier handler")
  139. //if it is barrier return true and ignore the further processing
  140. //if it is blocked(align handler), return true and then write back to the channel later
  141. if o.barrierHandler.Process(b, o.ctx) {
  142. return nil, true
  143. } else {
  144. return b.Data, false
  145. }
  146. }
  147. }
  148. return data, false
  149. }
  150. func printable(m map[string]interface{}) map[string]interface{} {
  151. printableMap := make(map[string]interface{})
  152. for k, v := range m {
  153. if strings.EqualFold(k, "password") {
  154. printableMap[k] = "*"
  155. } else {
  156. if vm, ok := v.(map[string]interface{}); ok {
  157. printableMap[k] = printable(vm)
  158. } else {
  159. printableMap[k] = v
  160. }
  161. }
  162. }
  163. return printableMap
  164. }
  165. func getSourceConf(ctx api.StreamContext, sourceType string, options *ast.Options) map[string]interface{} {
  166. confkey := options.CONF_KEY
  167. logger := ctx.GetLogger()
  168. confPath := "sources/" + sourceType + ".yaml"
  169. if sourceType == "mqtt" {
  170. confPath = "mqtt_source.yaml"
  171. }
  172. props := make(map[string]interface{})
  173. cfg := make(map[string]interface{})
  174. err := conf.LoadConfigByName(confPath, &cfg)
  175. if err != nil {
  176. logger.Warnf("fail to parse yaml for source %s. Return an empty configuration", sourceType)
  177. } else {
  178. def, ok := cfg["default"]
  179. if !ok {
  180. logger.Warnf("default conf is not found", confkey)
  181. } else {
  182. if def1, ok1 := def.(map[string]interface{}); ok1 {
  183. props = def1
  184. }
  185. if c, ok := cfg[strings.ToLower(confkey)]; ok {
  186. if c1, ok := c.(map[string]interface{}); ok {
  187. c2 := c1
  188. for k, v := range c2 {
  189. props[k] = v
  190. }
  191. }
  192. }
  193. }
  194. }
  195. f := options.FORMAT
  196. if f == "" {
  197. f = "json"
  198. }
  199. props["format"] = strings.ToLower(f)
  200. logger.Debugf("get conf for %s with conf key %s: %v", sourceType, confkey, printable(props))
  201. return props
  202. }