source.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 runtime
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "go.nanomsg.org/mangos/v3"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. "github.com/lf-edge/ekuiper/pkg/infra"
  23. )
  24. // Error handling: wrap all error in a function to handle
  25. type PortableSource struct {
  26. symbolName string
  27. reg *PluginMeta
  28. clean func() error
  29. topic string
  30. props map[string]interface{}
  31. }
  32. func NewPortableSource(symbolName string, reg *PluginMeta) *PortableSource {
  33. return &PortableSource{
  34. symbolName: symbolName,
  35. reg: reg,
  36. }
  37. }
  38. func (ps *PortableSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  39. ctx.GetLogger().Infof("Start running portable source %s with datasource %s and conf %+v", ps.symbolName, ps.topic, ps.props)
  40. pm := GetPluginInsManager()
  41. ins, err := pm.getOrStartProcess(ps.reg, PortbleConf)
  42. if err != nil {
  43. infra.DrainError(ctx, err, errCh)
  44. return
  45. }
  46. ctx.GetLogger().Infof("Plugin started successfully")
  47. // wait for plugin data
  48. dataCh, err := CreateSourceChannel(ctx)
  49. if err != nil {
  50. infra.DrainError(ctx, err, errCh)
  51. return
  52. }
  53. // Control: send message to plugin to ask starting symbol
  54. c := &Control{
  55. Meta: Meta{
  56. RuleId: ctx.GetRuleId(),
  57. OpId: ctx.GetOpId(),
  58. InstanceId: ctx.GetInstanceId(),
  59. },
  60. SymbolName: ps.symbolName,
  61. PluginType: TYPE_SOURCE,
  62. DataSource: ps.topic,
  63. Config: ps.props,
  64. }
  65. err = ins.StartSymbol(ctx, c)
  66. if err != nil {
  67. ctx.GetLogger().Error(err)
  68. infra.DrainError(ctx, err, errCh)
  69. _ = dataCh.Close()
  70. return
  71. }
  72. ps.clean = func() error {
  73. ctx.GetLogger().Info("clean up source")
  74. err1 := dataCh.Close()
  75. err2 := ins.StopSymbol(ctx, c)
  76. if err1 != nil {
  77. err1 = fmt.Errorf("%s:%v", "dataCh", err1)
  78. }
  79. if err2 != nil {
  80. err2 = fmt.Errorf("%s:%v", "symbol", err2)
  81. }
  82. return errors.Join(err1, err2)
  83. }
  84. for {
  85. var msg []byte
  86. // make sure recv has timeout
  87. msg, err = dataCh.Recv()
  88. switch err {
  89. case mangos.ErrClosed:
  90. ctx.GetLogger().Info("stop source after close")
  91. return
  92. case mangos.ErrRecvTimeout:
  93. ctx.GetLogger().Debug("source receive timeout, retry")
  94. select {
  95. case <-ctx.Done():
  96. ctx.GetLogger().Info("stop source")
  97. return
  98. default:
  99. continue
  100. }
  101. case nil:
  102. // do nothing
  103. default:
  104. infra.DrainError(ctx, fmt.Errorf("cannot receive from mangos Socket: %s", err.Error()), errCh)
  105. return
  106. }
  107. result := &api.DefaultSourceTuple{Time: conf.GetNow()}
  108. e := json.Unmarshal(msg, result)
  109. if e != nil {
  110. ctx.GetLogger().Errorf("Invalid data format, cannot decode %s to json format with error %s", string(msg), e)
  111. continue
  112. }
  113. select {
  114. case consumer <- result:
  115. ctx.GetLogger().Debugf("send data to source node")
  116. case <-ctx.Done():
  117. ctx.GetLogger().Info("stop source")
  118. return
  119. }
  120. }
  121. }
  122. func (ps *PortableSource) Configure(topic string, props map[string]interface{}) error {
  123. ps.topic = topic
  124. ps.props = props
  125. return nil
  126. }
  127. func (ps *PortableSource) Close(ctx api.StreamContext) error {
  128. ctx.GetLogger().Infof("Closing source %s", ps.symbolName)
  129. if ps.clean != nil {
  130. return ps.clean()
  131. }
  132. return nil
  133. }