node.go 4.8 KB

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