node.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/topo/checkpoint"
  18. "github.com/lf-edge/ekuiper/internal/topo/node/metric"
  19. "github.com/lf-edge/ekuiper/internal/xsql"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. )
  22. type OperatorNode interface {
  23. api.Operator
  24. Broadcast(data interface{}) error
  25. GetStreamContext() api.StreamContext
  26. GetInputCount() int
  27. AddInputCount()
  28. SetQos(api.Qos)
  29. SetBarrierHandler(checkpoint.BarrierHandler)
  30. }
  31. type DataSourceNode interface {
  32. api.Emitter
  33. Open(ctx api.StreamContext, errCh chan<- error)
  34. GetName() string
  35. GetMetrics() [][]interface{}
  36. Broadcast(val interface{}) error
  37. GetStreamContext() api.StreamContext
  38. SetQos(api.Qos)
  39. }
  40. type defaultNode struct {
  41. name string
  42. outputs map[string]chan<- interface{}
  43. concurrency int
  44. sendError bool
  45. statManagers []metric.StatManager
  46. ctx api.StreamContext
  47. qos api.Qos
  48. }
  49. func (o *defaultNode) AddOutput(output chan<- interface{}, name string) error {
  50. if _, ok := o.outputs[name]; !ok {
  51. o.outputs[name] = output
  52. } else {
  53. return fmt.Errorf("fail to add output %s, node %s already has an output of the same name", name, o.name)
  54. }
  55. return nil
  56. }
  57. func (o *defaultNode) GetName() string {
  58. return o.name
  59. }
  60. // SetConcurrency sets the concurrency level for the operation
  61. func (o *defaultNode) SetConcurrency(concurr int) {
  62. o.concurrency = concurr
  63. if o.concurrency < 1 {
  64. o.concurrency = 1
  65. }
  66. }
  67. func (o *defaultNode) SetQos(qos api.Qos) {
  68. o.qos = qos
  69. }
  70. func (o *defaultNode) GetMetrics() (result [][]interface{}) {
  71. for _, stats := range o.statManagers {
  72. result = append(result, stats.GetMetrics())
  73. }
  74. return result
  75. }
  76. func (o *defaultNode) Broadcast(val interface{}) error {
  77. if _, ok := val.(error); ok && !o.sendError {
  78. return nil
  79. }
  80. if o.qos >= api.AtLeastOnce {
  81. boe := &checkpoint.BufferOrEvent{
  82. Data: val,
  83. Channel: o.name,
  84. }
  85. o.doBroadcast(boe)
  86. return nil
  87. }
  88. o.doBroadcast(val)
  89. return nil
  90. }
  91. func (o *defaultNode) doBroadcast(val interface{}) {
  92. for name, out := range o.outputs {
  93. select {
  94. case out <- val:
  95. // do nothing
  96. case <-o.ctx.Done():
  97. // rule stop so stop waiting
  98. default:
  99. o.ctx.GetLogger().Errorf("drop message from %s to %s", o.name, name)
  100. }
  101. switch vt := val.(type) {
  102. case xsql.Collection:
  103. val = vt.Clone()
  104. break
  105. case xsql.TupleRow:
  106. val = vt.Clone()
  107. }
  108. }
  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. }