source.go 3.4 KB

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