source_node.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2021-2023 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. "sync"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/internal/converter"
  20. "github.com/lf-edge/ekuiper/internal/topo/context"
  21. nodeConf "github.com/lf-edge/ekuiper/internal/topo/node/conf"
  22. "github.com/lf-edge/ekuiper/internal/topo/node/metric"
  23. "github.com/lf-edge/ekuiper/internal/xsql"
  24. "github.com/lf-edge/ekuiper/pkg/api"
  25. "github.com/lf-edge/ekuiper/pkg/ast"
  26. "github.com/lf-edge/ekuiper/pkg/cast"
  27. "github.com/lf-edge/ekuiper/pkg/infra"
  28. )
  29. type SourceNode struct {
  30. *defaultNode
  31. streamType ast.StreamType
  32. sourceType string
  33. options *ast.Options
  34. bufferLength int
  35. props map[string]interface{}
  36. mutex sync.RWMutex
  37. sources []api.Source
  38. preprocessOp UnOperation
  39. schema map[string]*ast.JsonStreamField
  40. }
  41. func NewSourceNode(name string, st ast.StreamType, op UnOperation, options *ast.Options, sendError bool, schema map[string]*ast.JsonStreamField) *SourceNode {
  42. t := options.TYPE
  43. if t == "" {
  44. if st == ast.TypeStream {
  45. t = "mqtt"
  46. } else if st == ast.TypeTable {
  47. t = "file"
  48. }
  49. }
  50. return &SourceNode{
  51. streamType: st,
  52. sourceType: t,
  53. defaultNode: &defaultNode{
  54. name: name,
  55. outputs: make(map[string]chan<- interface{}),
  56. concurrency: 1,
  57. sendError: sendError,
  58. },
  59. preprocessOp: op,
  60. options: options,
  61. schema: schema,
  62. }
  63. }
  64. const OffsetKey = "$$offset"
  65. func (m *SourceNode) Open(ctx api.StreamContext, errCh chan<- error) {
  66. m.ctx = ctx
  67. logger := ctx.GetLogger()
  68. logger.Infof("open source node %s with option %v", m.name, m.options)
  69. go func() {
  70. panicOrError := infra.SafeRun(func() error {
  71. props := nodeConf.GetSourceConf(m.sourceType, m.options)
  72. m.props = props
  73. if c, ok := props["concurrency"]; ok {
  74. if t, err := cast.ToInt(c, cast.STRICT); err != nil || t <= 0 {
  75. logger.Warnf("invalid type for concurrency property, should be positive integer but found %t", c)
  76. } else {
  77. m.concurrency = t
  78. }
  79. }
  80. bl := 102400
  81. if c, ok := props["bufferLength"]; ok {
  82. if t, err := cast.ToInt(c, cast.STRICT); err != nil || t <= 0 {
  83. logger.Warnf("invalid type for bufferLength property, should be positive integer but found %t", c)
  84. } else {
  85. bl = t
  86. }
  87. }
  88. m.bufferLength = bl
  89. if m.streamType == ast.TypeTable {
  90. props["isTable"] = true
  91. }
  92. props["delimiter"] = m.options.DELIMITER
  93. m.options.Schema = nil
  94. if m.schema != nil {
  95. m.options.Schema = m.schema
  96. }
  97. converterTool, err := converter.GetOrCreateConverter(m.options)
  98. if err != nil {
  99. msg := fmt.Sprintf("cannot get converter from format %s, schemaId %s: %v", m.options.FORMAT, m.options.SCHEMAID, err)
  100. logger.Warnf(msg)
  101. return fmt.Errorf(msg)
  102. }
  103. ctx = context.WithValue(ctx.(*context.DefaultContext), context.DecodeKey, converterTool)
  104. m.reset()
  105. logger.Infof("open source node with props %v, concurrency: %d, bufferLength: %d", conf.Printable(m.props), m.concurrency, m.bufferLength)
  106. for i := 0; i < m.concurrency; i++ { // workers
  107. go func(instance int) {
  108. poe := infra.SafeRun(func() error {
  109. // Do open source instances
  110. var (
  111. si *sourceInstance
  112. buffer *DynamicChannelBuffer
  113. err error
  114. )
  115. stats, err := metric.NewStatManager(ctx, "source")
  116. if err != nil {
  117. return err
  118. }
  119. m.mutex.Lock()
  120. m.statManagers = append(m.statManagers, stats)
  121. m.mutex.Unlock()
  122. si, err = getSourceInstance(m, instance)
  123. if err != nil {
  124. return err
  125. }
  126. m.mutex.Lock()
  127. m.sources = append(m.sources, si.source)
  128. m.mutex.Unlock()
  129. buffer = si.dataCh
  130. defer func() {
  131. logger.Infof("source %s done", m.name)
  132. m.close()
  133. buffer.Close()
  134. }()
  135. logger.Infof("Start source %s instance %d successfully", m.name, instance)
  136. for {
  137. select {
  138. case <-ctx.Done():
  139. // We should clear the schema after we close the topo in order to avoid the following problem:
  140. // 1. stop the rule
  141. // 2. change the schema
  142. // 3. restart the rule
  143. // As the schema has changed, it will be error if we hold the old schema here
  144. // TODO: fetch the latest stream schema after we open the topo
  145. m.schema = nil
  146. return nil
  147. case err := <-si.errorCh:
  148. return err
  149. case data := <-buffer.Out:
  150. if t, ok := data.(*xsql.ErrorSourceTuple); ok {
  151. logger.Errorf("Source %s error: %v", ctx.GetOpId(), t.Error)
  152. stats.IncTotalExceptions(t.Error.Error())
  153. continue
  154. }
  155. stats.IncTotalRecordsIn()
  156. rcvTime := conf.GetNow()
  157. if !data.Timestamp().IsZero() {
  158. rcvTime = data.Timestamp()
  159. }
  160. stats.SetProcessTimeStart(rcvTime)
  161. tuple := &xsql.Tuple{Emitter: m.name, Message: data.Message(), Timestamp: rcvTime.UnixMilli(), Metadata: data.Meta()}
  162. var processedData interface{}
  163. if m.preprocessOp != nil {
  164. processedData = m.preprocessOp.Apply(ctx, tuple, nil, nil)
  165. } else {
  166. processedData = tuple
  167. }
  168. stats.ProcessTimeEnd()
  169. // blocking
  170. switch val := processedData.(type) {
  171. case nil:
  172. continue
  173. case error:
  174. logger.Errorf("Source %s preprocess error: %s", ctx.GetOpId(), val)
  175. _ = m.Broadcast(val)
  176. stats.IncTotalExceptions(val.Error())
  177. default:
  178. _ = m.Broadcast(val)
  179. stats.IncTotalRecordsOut()
  180. }
  181. stats.SetBufferLength(int64(buffer.GetLength()))
  182. if rw, ok := si.source.(api.Rewindable); ok {
  183. if offset, err := rw.GetOffset(); err != nil {
  184. infra.DrainError(ctx, err, errCh)
  185. } else {
  186. err = ctx.PutState(OffsetKey, offset)
  187. if err != nil {
  188. return err
  189. }
  190. logger.Debugf("Source save offset %v", offset)
  191. }
  192. }
  193. }
  194. }
  195. })
  196. if poe != nil {
  197. infra.DrainError(ctx, poe, errCh)
  198. }
  199. }(i)
  200. }
  201. return nil
  202. })
  203. if panicOrError != nil {
  204. infra.DrainError(ctx, panicOrError, errCh)
  205. }
  206. }()
  207. }
  208. func (m *SourceNode) reset() {
  209. m.statManagers = nil
  210. }
  211. func (m *SourceNode) close() {
  212. if m.options.SHARED {
  213. removeSourceInstance(m)
  214. }
  215. }