source_node.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 with props %v, concurrency: %d, bufferLength: %d", conf.Printable(m.props), m.concurrency, m.bufferLength)
  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()
  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. //blocking
  131. switch val := processedData.(type) {
  132. case nil:
  133. continue
  134. case error:
  135. logger.Errorf("Source %s preprocess error: %s", ctx.GetOpId(), val)
  136. m.Broadcast(val)
  137. stats.IncTotalExceptions()
  138. default:
  139. m.Broadcast(val)
  140. }
  141. stats.IncTotalRecordsOut()
  142. stats.SetBufferLength(int64(buffer.GetLength()))
  143. if rw, ok := si.source.(api.Rewindable); ok {
  144. if offset, err := rw.GetOffset(); err != nil {
  145. m.drainError(errCh, err, ctx, logger)
  146. } else {
  147. err = ctx.PutState(OffsetKey, offset)
  148. if err != nil {
  149. m.drainError(errCh, err, ctx, logger)
  150. }
  151. logger.Debugf("Source save offset %v", offset)
  152. }
  153. }
  154. }
  155. }
  156. }(i)
  157. }
  158. }()
  159. }
  160. func (m *SourceNode) reset() {
  161. m.statManagers = nil
  162. }
  163. func (m *SourceNode) drainError(errCh chan<- error, err error, ctx api.StreamContext, logger api.Logger) {
  164. select {
  165. case errCh <- err:
  166. logger.Debugf("sent error: %v", err)
  167. case <-ctx.Done():
  168. }
  169. return
  170. }
  171. func (m *SourceNode) close() {
  172. if m.options.SHARED {
  173. removeSourceInstance(m)
  174. }
  175. }