function.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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)
  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. return fr.Result, fr.State
  106. }
  107. func (f *PortableFunc) IsAggregate() bool {
  108. if f.isAgg > 0 {
  109. return f.isAgg > 1
  110. }
  111. jsonArg, err := encode("IsAggregate", nil)
  112. if err != nil {
  113. conf.Log.Error(err)
  114. return false
  115. }
  116. res, err := f.dataCh.Req(jsonArg)
  117. if err != nil {
  118. conf.Log.Error(err)
  119. return false
  120. }
  121. fr := &FuncReply{}
  122. err = json.Unmarshal(res, fr)
  123. if err != nil {
  124. conf.Log.Error(err)
  125. return false
  126. }
  127. if fr.State {
  128. r, ok := fr.Result.(bool)
  129. if !ok {
  130. conf.Log.Errorf("IsAggregate result is not bool, got %s", string(res))
  131. return false
  132. } else {
  133. if r {
  134. f.isAgg = 2
  135. } else {
  136. f.isAgg = 1
  137. }
  138. return r
  139. }
  140. } else {
  141. conf.Log.Errorf("IsAggregate return state is false, got %+v", fr)
  142. return false
  143. }
  144. }
  145. func (f *PortableFunc) Close() error {
  146. return f.dataCh.Close()
  147. // Symbol must be closed by instance manager
  148. // ins.StopSymbol(ctx, c)
  149. }
  150. func encode(funcName string, arg interface{}) ([]byte, error) {
  151. c := FuncData{
  152. Func: funcName,
  153. Arg: arg,
  154. }
  155. return json.Marshal(c)
  156. }
  157. func encodeCtx(ctx api.FunctionContext) (string, error) {
  158. m := FuncMeta{
  159. Meta: Meta{
  160. RuleId: ctx.GetRuleId(),
  161. OpId: ctx.GetOpId(),
  162. InstanceId: ctx.GetInstanceId(),
  163. },
  164. FuncId: ctx.GetFuncId(),
  165. }
  166. bs, err := json.Marshal(m)
  167. return string(bs), err
  168. }