source.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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)
  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. infra.DrainError(ctx, err, errCh)
  67. _ = dataCh.Close()
  68. return
  69. }
  70. ps.clean = func() error {
  71. ctx.GetLogger().Info("clean up source")
  72. err1 := dataCh.Close()
  73. err2 := ins.StopSymbol(ctx, c)
  74. e := make(errorx.MultiError)
  75. if err1 != nil {
  76. e["dataCh"] = err1
  77. }
  78. if err != nil {
  79. e["symbol"] = err2
  80. }
  81. return e.GetError()
  82. }
  83. for {
  84. var msg []byte
  85. // make sure recv has timeout
  86. msg, err = dataCh.Recv()
  87. switch err {
  88. case mangos.ErrClosed:
  89. ctx.GetLogger().Info("stop source after close")
  90. return
  91. case mangos.ErrRecvTimeout:
  92. ctx.GetLogger().Debug("source receive timeout, retry")
  93. select {
  94. case <-ctx.Done():
  95. ctx.GetLogger().Info("stop source")
  96. return
  97. }
  98. case nil:
  99. // do nothing
  100. default:
  101. infra.DrainError(ctx, fmt.Errorf("cannot receive from mangos Socket: %s", err.Error()), errCh)
  102. return
  103. }
  104. result := &api.DefaultSourceTuple{}
  105. e := json.Unmarshal(msg, result)
  106. if e != nil {
  107. ctx.GetLogger().Errorf("Invalid data format, cannot decode %s to json format with error %s", string(msg), e)
  108. continue
  109. }
  110. select {
  111. case consumer <- result:
  112. ctx.GetLogger().Debugf("send data to source node")
  113. case <-ctx.Done():
  114. ctx.GetLogger().Info("stop source")
  115. return
  116. }
  117. }
  118. }
  119. func (ps *PortableSource) Configure(topic string, props map[string]interface{}) error {
  120. ps.topic = topic
  121. ps.props = props
  122. return nil
  123. }
  124. func (ps *PortableSource) Close(ctx api.StreamContext) error {
  125. ctx.GetLogger().Infof("Closing source %s", ps.symbolName)
  126. if ps.clean != nil {
  127. return ps.clean()
  128. }
  129. return nil
  130. }