source.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 runtime
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/errorx"
  20. "github.com/lf-edge/ekuiper/pkg/infra"
  21. "go.nanomsg.org/mangos/v3"
  22. )
  23. // Error handling: wrap all error in a function to handle
  24. type PortableSource struct {
  25. symbolName string
  26. reg *PluginMeta
  27. clean func() error
  28. topic string
  29. props map[string]interface{}
  30. }
  31. func NewPortableSource(symbolName string, reg *PluginMeta) *PortableSource {
  32. return &PortableSource{
  33. symbolName: symbolName,
  34. reg: reg,
  35. }
  36. }
  37. func (ps *PortableSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  38. ctx.GetLogger().Infof("Start running portable source %s with datasource %s and conf %+v", ps.symbolName, ps.topic, ps.props)
  39. pm := GetPluginInsManager()
  40. ins, err := pm.getOrStartProcess(ps.reg, PortbleConf, false)
  41. if err != nil {
  42. infra.DrainError(ctx, err, errCh)
  43. return
  44. }
  45. ctx.GetLogger().Infof("Plugin started successfully")
  46. // wait for plugin data
  47. dataCh, err := CreateSourceChannel(ctx)
  48. if err != nil {
  49. infra.DrainError(ctx, err, errCh)
  50. return
  51. }
  52. // Control: send message to plugin to ask starting symbol
  53. c := &Control{
  54. Meta: Meta{
  55. RuleId: ctx.GetRuleId(),
  56. OpId: ctx.GetOpId(),
  57. InstanceId: ctx.GetInstanceId(),
  58. },
  59. SymbolName: ps.symbolName,
  60. PluginType: TYPE_SOURCE,
  61. DataSource: ps.topic,
  62. Config: ps.props,
  63. }
  64. err = ins.StartSymbol(ctx, c)
  65. if err != nil {
  66. ctx.GetLogger().Error(err)
  67. infra.DrainError(ctx, err, errCh)
  68. _ = dataCh.Close()
  69. return
  70. }
  71. ps.clean = func() error {
  72. ctx.GetLogger().Info("clean up source")
  73. err1 := dataCh.Close()
  74. err2 := ins.StopSymbol(ctx, c)
  75. e := make(errorx.MultiError)
  76. if err1 != nil {
  77. e["dataCh"] = err1
  78. }
  79. if err != nil {
  80. e["symbol"] = err2
  81. }
  82. return e.GetError()
  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{}
  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. }