function.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 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/internal/conf"
  19. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. )
  22. // PortableFunc each function symbol only has a singleton
  23. // Each singleton are long running go routine.
  24. // TODO think about ending a portable func when needed.
  25. type PortableFunc struct {
  26. symbolName string
  27. reg *PluginMeta
  28. dataCh DataReqChannel
  29. isAgg int // 0 - not calculate yet, 1 - no, 2 - yes
  30. }
  31. func NewPortableFunc(symbolName string, reg *PluginMeta) (*PortableFunc, error) {
  32. // Setup channel and route the data
  33. conf.Log.Infof("Start running portable function meta %+v", reg)
  34. pm := GetPluginInsManager()
  35. ins, err := pm.getOrStartProcess(reg, PortbleConf, false)
  36. if err != nil {
  37. return nil, err
  38. }
  39. conf.Log.Infof("Plugin started successfully")
  40. // Create function channel
  41. dataCh, err := CreateFunctionChannel(symbolName)
  42. if err != nil {
  43. return nil, err
  44. }
  45. // Start symbol
  46. c := &Control{
  47. SymbolName: symbolName,
  48. PluginType: TYPE_FUNC,
  49. }
  50. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, conf.Log)
  51. err = ins.StartSymbol(ctx, c)
  52. if err != nil {
  53. return nil, err
  54. }
  55. err = dataCh.Handshake()
  56. if err != nil {
  57. return nil, fmt.Errorf("function %s handshake error: %v", reg.Name, err)
  58. }
  59. return &PortableFunc{
  60. symbolName: reg.Name,
  61. reg: reg,
  62. dataCh: dataCh,
  63. }, nil
  64. }
  65. func (f *PortableFunc) Validate(args []interface{}) error {
  66. // TODO function arg encoding
  67. jsonArg, err := encode("Validate", args)
  68. if err != nil {
  69. return err
  70. }
  71. res, err := f.dataCh.Req(jsonArg)
  72. if err != nil {
  73. return err
  74. }
  75. fr := &FuncReply{}
  76. err = json.Unmarshal(res, fr)
  77. if err != nil {
  78. return err
  79. }
  80. if fr.State {
  81. return nil
  82. } else {
  83. return fmt.Errorf("validate return state is false, got %+v", fr)
  84. }
  85. }
  86. func (f *PortableFunc) Exec(args []interface{}, ctx api.FunctionContext) (interface{}, bool) {
  87. ctx.GetLogger().Debugf("running portable func with args %+v", args)
  88. ctxRaw, err := encodeCtx(ctx)
  89. if err != nil {
  90. return err, false
  91. }
  92. jsonArg, err := encode("Exec", append(args, ctxRaw))
  93. if err != nil {
  94. return err, false
  95. }
  96. res, err := f.dataCh.Req(jsonArg)
  97. if err != nil {
  98. return err, false
  99. }
  100. fr := &FuncReply{}
  101. err = json.Unmarshal(res, fr)
  102. if err != nil {
  103. return err, false
  104. }
  105. if !fr.State {
  106. if fr.Result != nil {
  107. return fmt.Errorf("%s", fr.Result), false
  108. } else {
  109. return nil, false
  110. }
  111. }
  112. return fr.Result, fr.State
  113. }
  114. func (f *PortableFunc) IsAggregate() bool {
  115. if f.isAgg > 0 {
  116. return f.isAgg > 1
  117. }
  118. jsonArg, err := encode("IsAggregate", nil)
  119. if err != nil {
  120. conf.Log.Error(err)
  121. return false
  122. }
  123. res, err := f.dataCh.Req(jsonArg)
  124. if err != nil {
  125. conf.Log.Error(err)
  126. return false
  127. }
  128. fr := &FuncReply{}
  129. err = json.Unmarshal(res, fr)
  130. if err != nil {
  131. conf.Log.Error(err)
  132. return false
  133. }
  134. if fr.State {
  135. r, ok := fr.Result.(bool)
  136. if !ok {
  137. conf.Log.Errorf("IsAggregate result is not bool, got %s", string(res))
  138. return false
  139. } else {
  140. if r {
  141. f.isAgg = 2
  142. } else {
  143. f.isAgg = 1
  144. }
  145. return r
  146. }
  147. } else {
  148. conf.Log.Errorf("IsAggregate return state is false, got %+v", fr)
  149. return false
  150. }
  151. }
  152. func (f *PortableFunc) Close() error {
  153. return f.dataCh.Close()
  154. // Symbol must be closed by instance manager
  155. // ins.StopSymbol(ctx, c)
  156. }
  157. func encode(funcName string, arg interface{}) ([]byte, error) {
  158. c := FuncData{
  159. Func: funcName,
  160. Arg: arg,
  161. }
  162. return json.Marshal(c)
  163. }
  164. func encodeCtx(ctx api.FunctionContext) (string, error) {
  165. m := FuncMeta{
  166. Meta: Meta{
  167. RuleId: ctx.GetRuleId(),
  168. OpId: ctx.GetOpId(),
  169. InstanceId: ctx.GetInstanceId(),
  170. },
  171. FuncId: ctx.GetFuncId(),
  172. }
  173. bs, err := json.Marshal(m)
  174. return string(bs), err
  175. }