node.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/internal/xsql"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. "github.com/lf-edge/ekuiper/pkg/ast"
  23. "strings"
  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 []metric.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 _, ok := val.(error); ok && !o.sendError {
  81. return nil
  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. switch vt := val.(type) {
  105. case xsql.Collection:
  106. val = vt.Clone()
  107. break
  108. case xsql.TupleRow:
  109. val = vt.Clone()
  110. }
  111. }
  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 printable(m map[string]interface{}) map[string]interface{} {
  154. printableMap := make(map[string]interface{})
  155. for k, v := range m {
  156. if strings.EqualFold(k, "password") {
  157. printableMap[k] = "*"
  158. } else {
  159. if vm, ok := v.(map[string]interface{}); ok {
  160. printableMap[k] = printable(vm)
  161. } else {
  162. printableMap[k] = v
  163. }
  164. }
  165. }
  166. return printableMap
  167. }
  168. func getSourceConf(ctx api.StreamContext, sourceType string, options *ast.Options) map[string]interface{} {
  169. confkey := options.CONF_KEY
  170. logger := ctx.GetLogger()
  171. confPath := "sources/" + sourceType + ".yaml"
  172. if sourceType == "mqtt" {
  173. confPath = "mqtt_source.yaml"
  174. }
  175. props := make(map[string]interface{})
  176. cfg := make(map[string]interface{})
  177. err := conf.LoadConfigByName(confPath, &cfg)
  178. if err != nil {
  179. logger.Warnf("fail to parse yaml for source %s. Return an empty configuration", sourceType)
  180. } else {
  181. def, ok := cfg["default"]
  182. if !ok {
  183. logger.Warnf("default conf is not found", confkey)
  184. } else {
  185. if def1, ok1 := def.(map[string]interface{}); ok1 {
  186. props = def1
  187. }
  188. if c, ok := cfg[strings.ToLower(confkey)]; ok {
  189. if c1, ok := c.(map[string]interface{}); ok {
  190. c2 := c1
  191. for k, v := range c2 {
  192. props[k] = v
  193. }
  194. }
  195. }
  196. }
  197. }
  198. f := options.FORMAT
  199. if f == "" {
  200. f = "json"
  201. }
  202. props["format"] = strings.ToLower(f)
  203. logger.Debugf("get conf for %s with conf key %s: %v", sourceType, confkey, printable(props))
  204. return props
  205. }