source_pool.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. "context"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/binder/io"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  21. "github.com/lf-edge/ekuiper/internal/topo/state"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "github.com/lf-edge/ekuiper/pkg/infra"
  24. "sync"
  25. )
  26. //// Package vars and funcs
  27. var (
  28. pool = &sourcePool{
  29. registry: make(map[string]*sourceSingleton),
  30. }
  31. )
  32. // node is readonly
  33. func getSourceInstance(node *SourceNode, index int) (*sourceInstance, error) {
  34. var si *sourceInstance
  35. if node.options.SHARED {
  36. rkey := fmt.Sprintf("%s.%s", node.sourceType, node.name)
  37. s, ok := pool.load(rkey)
  38. if !ok {
  39. ns, err := io.Source(node.sourceType)
  40. if ns != nil {
  41. s, err = pool.addInstance(rkey, node, ns)
  42. if err != nil {
  43. return nil, err
  44. }
  45. } else {
  46. if err != nil {
  47. return nil, err
  48. } else {
  49. return nil, fmt.Errorf("source %s not found", node.sourceType)
  50. }
  51. }
  52. }
  53. // attach
  54. instanceKey := fmt.Sprintf("%s.%s.%d", rkey, node.ctx.GetRuleId(), index)
  55. err := s.attach(instanceKey, node.bufferLength)
  56. if err != nil {
  57. return nil, err
  58. }
  59. si = &sourceInstance{
  60. source: s.source,
  61. ctx: s.ctx,
  62. sourceInstanceChannels: s.outputs[instanceKey],
  63. }
  64. } else {
  65. ns, err := io.Source(node.sourceType)
  66. if ns != nil {
  67. si, err = start(nil, node, ns)
  68. if err != nil {
  69. return nil, err
  70. }
  71. go func() {
  72. err := infra.SafeRun(func() error {
  73. nctx := node.ctx.WithInstance(index)
  74. defer si.source.Close(nctx)
  75. si.source.Open(nctx, si.dataCh.In, si.errorCh)
  76. return nil
  77. })
  78. if err != nil {
  79. infra.DrainError(node.ctx, err, si.errorCh)
  80. }
  81. }()
  82. } else {
  83. if err != nil {
  84. return nil, err
  85. } else {
  86. return nil, fmt.Errorf("source %s not found", node.sourceType)
  87. }
  88. }
  89. }
  90. return si, nil
  91. }
  92. // removeSourceInstance remove an attach from the sourceSingleton
  93. // If all attaches are removed, close the sourceSingleton and remove it from the pool registry
  94. // ONLY apply to shared instance
  95. func removeSourceInstance(node *SourceNode) {
  96. for i := 0; i < node.concurrency; i++ {
  97. rkey := fmt.Sprintf("%s.%s", node.sourceType, node.name)
  98. pool.deleteInstance(rkey, node, i)
  99. }
  100. }
  101. //// data types
  102. /*
  103. * Pool for all keyed source instance.
  104. * Create an instance, and start the source go routine when the keyed was hit the first time.
  105. * For later hit, create the new set of channels and attach to the instance
  106. * When hit a delete (when close a rule), remove the attached channels. If all channels removed, remove the instance from the pool
  107. * For performance reason, the pool only holds the shared instance. Rule specific instance are holden by rule source node itself
  108. */
  109. type sourcePool struct {
  110. registry map[string]*sourceSingleton
  111. sync.RWMutex
  112. }
  113. func (p *sourcePool) load(k string) (*sourceSingleton, bool) {
  114. p.RLock()
  115. defer p.RUnlock()
  116. s, ok := p.registry[k]
  117. return s, ok
  118. }
  119. func (p *sourcePool) addInstance(k string, node *SourceNode, source api.Source) (*sourceSingleton, error) {
  120. p.Lock()
  121. defer p.Unlock()
  122. s, ok := p.registry[k]
  123. if !ok {
  124. contextLogger := conf.Log.WithField("source_pool", k)
  125. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  126. ruleId := "$$source_pool_" + k
  127. opId := "source_pool_" + k
  128. store, err := state.CreateStore("source_pool_"+k, 0)
  129. if err != nil {
  130. ctx.GetLogger().Errorf("source pool %s create store error %v", k, err)
  131. return nil, err
  132. }
  133. sctx, cancel := ctx.WithMeta(ruleId, opId, store).WithCancel()
  134. sctx = kctx.WithValue(sctx.(*kctx.DefaultContext), kctx.DecodeKey, node.ctx.Value(kctx.DecodeKey))
  135. si, err := start(sctx, node, source)
  136. if err != nil {
  137. return nil, err
  138. }
  139. newS := &sourceSingleton{
  140. sourceInstance: si,
  141. outputs: make(map[string]*sourceInstanceChannels),
  142. cancel: cancel,
  143. }
  144. p.registry[k] = newS
  145. go func() {
  146. err := infra.SafeRun(func() error {
  147. defer si.source.Close(sctx)
  148. si.source.Open(sctx, si.dataCh.In, si.errorCh)
  149. return nil
  150. })
  151. if err != nil {
  152. newS.broadcastError(err)
  153. }
  154. }()
  155. go func() {
  156. err := infra.SafeRun(func() error {
  157. newS.run(node.sourceType, node.name)
  158. return nil
  159. })
  160. if err != nil {
  161. newS.broadcastError(err)
  162. }
  163. }()
  164. s = newS
  165. }
  166. return s, nil
  167. }
  168. func (p *sourcePool) deleteInstance(k string, node *SourceNode, index int) {
  169. p.Lock()
  170. defer p.Unlock()
  171. s, ok := p.registry[k]
  172. if ok {
  173. instanceKey := fmt.Sprintf("%s.%s.%d", k, node.ctx.GetRuleId(), index)
  174. end := s.detach(instanceKey)
  175. if end {
  176. s.cancel()
  177. s.dataCh.Close()
  178. delete(p.registry, k)
  179. }
  180. }
  181. }
  182. type sourceInstance struct {
  183. source api.Source
  184. ctx api.StreamContext
  185. *sourceInstanceChannels
  186. }
  187. // Hold the only instance for all shared source
  188. // And hold the reference to all shared source input channels. Must be sync when dealing with outputs
  189. type sourceSingleton struct {
  190. *sourceInstance // immutable
  191. cancel context.CancelFunc // immutable
  192. outputs map[string]*sourceInstanceChannels // read-write lock
  193. sync.RWMutex
  194. }
  195. type sourceInstanceChannels struct {
  196. dataCh *DynamicChannelBuffer
  197. errorCh chan error
  198. }
  199. func newSourceInstanceChannels(bl int) *sourceInstanceChannels {
  200. buffer := NewDynamicChannelBuffer()
  201. buffer.SetLimit(bl)
  202. errorOutput := make(chan error)
  203. return &sourceInstanceChannels{
  204. dataCh: buffer,
  205. errorCh: errorOutput,
  206. }
  207. }
  208. func (ss *sourceSingleton) run(name, key string) {
  209. logger := ss.ctx.GetLogger()
  210. logger.Infof("Start source %s shared instance %s successfully", name, key)
  211. for {
  212. select {
  213. case <-ss.ctx.Done():
  214. logger.Infof("source %s shared instance %s done", name, key)
  215. return
  216. case err := <-ss.errorCh:
  217. ss.broadcastError(err)
  218. return
  219. case data := <-ss.dataCh.Out:
  220. logger.Debugf("broadcast data %v from source pool %s:%s", data, name, key)
  221. ss.broadcast(data)
  222. }
  223. }
  224. }
  225. func (ss *sourceSingleton) broadcast(val api.SourceTuple) {
  226. ss.RLock()
  227. defer ss.RUnlock()
  228. for name, out := range ss.outputs {
  229. select {
  230. case out.dataCh.In <- val:
  231. case <-ss.ctx.Done():
  232. case <-out.dataCh.done:
  233. // detached
  234. default:
  235. ss.ctx.GetLogger().Errorf("share source drop message to %s", name)
  236. }
  237. }
  238. }
  239. func (ss *sourceSingleton) broadcastError(err error) {
  240. logger := ss.ctx.GetLogger()
  241. var wg sync.WaitGroup
  242. ss.RLock()
  243. wg.Add(len(ss.outputs))
  244. for n, out := range ss.outputs {
  245. go func(name string, output chan<- error) {
  246. infra.DrainError(ss.ctx, err, output)
  247. wg.Done()
  248. }(n, out.errorCh)
  249. }
  250. ss.RUnlock()
  251. logger.Debugf("broadcasting from source pool")
  252. wg.Wait()
  253. }
  254. func (ss *sourceSingleton) attach(instanceKey string, bl int) error {
  255. ss.Lock()
  256. defer ss.Unlock()
  257. if _, ok := ss.outputs[instanceKey]; !ok {
  258. ss.outputs[instanceKey] = newSourceInstanceChannels(bl)
  259. } else {
  260. // should not happen
  261. return fmt.Errorf("fail to attach source instance, already has an output of the same key %s", instanceKey)
  262. }
  263. return nil
  264. }
  265. // detach Detach an instance and return if the singleton is ended
  266. func (ss *sourceSingleton) detach(instanceKey string) bool {
  267. ss.Lock()
  268. defer ss.Unlock()
  269. if chs, ok := ss.outputs[instanceKey]; ok {
  270. chs.dataCh.Close()
  271. } else {
  272. // should not happen
  273. ss.ctx.GetLogger().Warnf("detach source instance %s, not found", instanceKey)
  274. return false
  275. }
  276. delete(ss.outputs, instanceKey)
  277. if len(ss.outputs) == 0 {
  278. ss.cancel()
  279. return true
  280. }
  281. return false
  282. }
  283. func start(poolCtx api.StreamContext, node *SourceNode, s api.Source) (*sourceInstance, error) {
  284. err := s.Configure(node.options.DATASOURCE, node.props)
  285. if err != nil {
  286. return nil, err
  287. }
  288. ctx := poolCtx
  289. if poolCtx == nil {
  290. ctx = node.ctx
  291. if rw, ok := s.(api.Rewindable); ok {
  292. if offset, err := ctx.GetState(OffsetKey); err != nil {
  293. return nil, err
  294. } else if offset != nil {
  295. ctx.GetLogger().Infof("Source rewind from %v", offset)
  296. err = rw.Rewind(offset)
  297. if err != nil {
  298. return nil, err
  299. }
  300. }
  301. }
  302. }
  303. chs := newSourceInstanceChannels(node.bufferLength)
  304. return &sourceInstance{
  305. source: s,
  306. sourceInstanceChannels: chs,
  307. ctx: ctx,
  308. }, nil
  309. }