node.go 5.4 KB

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