source_node.go 5.9 KB

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