node.go 5.4 KB

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