source_node.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2021 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. "github.com/lf-edge/ekuiper/internal/conf"
  17. "github.com/lf-edge/ekuiper/internal/xsql"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/ast"
  20. "github.com/lf-edge/ekuiper/pkg/cast"
  21. "sync"
  22. )
  23. type SourceNode struct {
  24. *defaultNode
  25. streamType ast.StreamType
  26. sourceType string
  27. options *ast.Options
  28. bufferLength int
  29. props map[string]interface{}
  30. mutex sync.RWMutex
  31. sources []api.Source
  32. preprocessOp UnOperation
  33. }
  34. func NewSourceNode(name string, st ast.StreamType, op UnOperation, options *ast.Options, sendError bool) *SourceNode {
  35. t := options.TYPE
  36. if t == "" {
  37. if st == ast.TypeStream {
  38. t = "mqtt"
  39. } else if st == ast.TypeTable {
  40. t = "file"
  41. }
  42. }
  43. return &SourceNode{
  44. streamType: st,
  45. sourceType: t,
  46. defaultNode: &defaultNode{
  47. name: name,
  48. outputs: make(map[string]chan<- interface{}),
  49. concurrency: 1,
  50. sendError: sendError,
  51. },
  52. preprocessOp: op,
  53. options: options,
  54. }
  55. }
  56. const OffsetKey = "$$offset"
  57. func (m *SourceNode) Open(ctx api.StreamContext, errCh chan<- error) {
  58. m.ctx = ctx
  59. logger := ctx.GetLogger()
  60. logger.Infof("open source node %s with option %v", m.name, m.options)
  61. go func() {
  62. props := getSourceConf(ctx, m.sourceType, m.options)
  63. m.props = props
  64. if c, ok := props["concurrency"]; ok {
  65. if t, err := cast.ToInt(c, cast.STRICT); err != nil || t <= 0 {
  66. logger.Warnf("invalid type for concurrency property, should be positive integer but found %t", c)
  67. } else {
  68. m.concurrency = t
  69. }
  70. }
  71. bl := 102400
  72. if c, ok := props["bufferLength"]; ok {
  73. if t, err := cast.ToInt(c, cast.STRICT); err != nil || t <= 0 {
  74. logger.Warnf("invalid type for bufferLength property, should be positive integer but found %t", c)
  75. } else {
  76. bl = t
  77. }
  78. }
  79. m.bufferLength = bl
  80. // Set retain size for table type
  81. if m.options.RETAIN_SIZE > 0 && m.streamType == ast.TypeTable {
  82. props["$retainSize"] = m.options.RETAIN_SIZE
  83. }
  84. m.reset()
  85. logger.Infof("open source node %d instances", m.concurrency)
  86. for i := 0; i < m.concurrency; i++ { // workers
  87. go func(instance int) {
  88. //Do open source instances
  89. var (
  90. si *sourceInstance
  91. buffer *DynamicChannelBuffer
  92. err error
  93. )
  94. si, err = getSourceInstance(m, instance)
  95. if err != nil {
  96. m.drainError(errCh, err, ctx, logger)
  97. return
  98. }
  99. m.mutex.Lock()
  100. m.sources = append(m.sources, si.source)
  101. m.mutex.Unlock()
  102. buffer = si.dataCh
  103. defer func() {
  104. logger.Infof("source %s done", m.name)
  105. m.close(ctx, logger)
  106. buffer.Close()
  107. }()
  108. stats, err := NewStatManager("source", ctx)
  109. if err != nil {
  110. m.drainError(errCh, err, ctx, logger)
  111. return
  112. }
  113. m.mutex.Lock()
  114. m.statManagers = append(m.statManagers, stats)
  115. m.mutex.Unlock()
  116. logger.Infof("Start source %s instance %d successfully", m.name, instance)
  117. for {
  118. select {
  119. case <-ctx.Done():
  120. return
  121. case err := <-si.errorCh:
  122. m.drainError(errCh, err, ctx, logger)
  123. return
  124. case data := <-buffer.Out:
  125. stats.IncTotalRecordsIn()
  126. stats.ProcessTimeStart()
  127. tuple := &xsql.Tuple{Emitter: m.name, Message: data.Message(), Timestamp: conf.GetNowInMilli(), Metadata: data.Meta()}
  128. processedData := m.preprocessOp.Apply(ctx, tuple, nil, nil)
  129. stats.ProcessTimeEnd()
  130. logger.Debugf("source node %s is sending tuple %+v of timestamp %d", m.name, tuple, tuple.Timestamp)
  131. //blocking
  132. switch val := processedData.(type) {
  133. case nil:
  134. continue
  135. case error:
  136. logger.Errorf("Source %s preprocess error: %s", ctx.GetOpId(), val)
  137. m.Broadcast(val)
  138. stats.IncTotalExceptions()
  139. default:
  140. m.Broadcast(val)
  141. }
  142. stats.IncTotalRecordsOut()
  143. stats.SetBufferLength(int64(buffer.GetLength()))
  144. if rw, ok := si.source.(api.Rewindable); ok {
  145. if offset, err := rw.GetOffset(); err != nil {
  146. m.drainError(errCh, err, ctx, logger)
  147. } else {
  148. err = ctx.PutState(OffsetKey, offset)
  149. if err != nil {
  150. m.drainError(errCh, err, ctx, logger)
  151. }
  152. logger.Debugf("Source save offset %v", offset)
  153. }
  154. }
  155. logger.Debugf("source node %s has consumed tuple of timestamp %d", m.name, tuple.Timestamp)
  156. }
  157. }
  158. }(i)
  159. }
  160. }()
  161. }
  162. func (m *SourceNode) reset() {
  163. m.statManagers = nil
  164. }
  165. func (m *SourceNode) drainError(errCh chan<- error, err error, ctx api.StreamContext, logger api.Logger) {
  166. select {
  167. case errCh <- err:
  168. logger.Debugf("sent error: %v", err)
  169. case <-ctx.Done():
  170. }
  171. return
  172. }
  173. func (m *SourceNode) close(ctx api.StreamContext, logger api.Logger) {
  174. if !m.options.SHARED {
  175. for _, s := range m.sources {
  176. if err := s.Close(ctx); err != nil {
  177. logger.Warnf("close source fails: %v", err)
  178. }
  179. }
  180. } else {
  181. removeSourceInstance(m)
  182. }
  183. }