function.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. return &PortableFunc{
  56. symbolName: reg.Name,
  57. reg: reg,
  58. dataCh: dataCh,
  59. }, nil
  60. }
  61. func (f *PortableFunc) Validate(args []interface{}) error {
  62. // TODO function arg encoding
  63. jsonArg, err := encode("Validate", args)
  64. if err != nil {
  65. return err
  66. }
  67. res, err := f.dataCh.Req(jsonArg)
  68. if err != nil {
  69. return err
  70. }
  71. fr := &FuncReply{}
  72. err = json.Unmarshal(res, fr)
  73. if err != nil {
  74. return err
  75. }
  76. if fr.State {
  77. return nil
  78. } else {
  79. return fmt.Errorf("validate return state is false, got %+v", fr)
  80. }
  81. }
  82. func (f *PortableFunc) Exec(args []interface{}, ctx api.FunctionContext) (interface{}, bool) {
  83. ctx.GetLogger().Debugf("running portable func with args %+v", args)
  84. ctxRaw, err := encodeCtx(ctx)
  85. if err != nil {
  86. return err, false
  87. }
  88. jsonArg, err := encode("Exec", append(args, ctxRaw))
  89. if err != nil {
  90. return err, false
  91. }
  92. res, err := f.dataCh.Req(jsonArg)
  93. if err != nil {
  94. return err, false
  95. }
  96. fr := &FuncReply{}
  97. err = json.Unmarshal(res, fr)
  98. if err != nil { // retry if receive handshake after restart function process
  99. ctx.GetLogger().Warnf("Failed to unmarshal function result %s", string(res))
  100. res, err = f.dataCh.Req(jsonArg)
  101. if err != nil {
  102. return err, false
  103. }
  104. err = json.Unmarshal(res, fr)
  105. if err != nil {
  106. return err, false
  107. }
  108. }
  109. if !fr.State {
  110. if fr.Result != nil {
  111. return fmt.Errorf("%s", fr.Result), false
  112. } else {
  113. return nil, false
  114. }
  115. }
  116. return fr.Result, fr.State
  117. }
  118. func (f *PortableFunc) IsAggregate() bool {
  119. if f.isAgg > 0 {
  120. return f.isAgg > 1
  121. }
  122. jsonArg, err := encode("IsAggregate", nil)
  123. if err != nil {
  124. conf.Log.Error(err)
  125. return false
  126. }
  127. res, err := f.dataCh.Req(jsonArg)
  128. if err != nil {
  129. conf.Log.Error(err)
  130. return false
  131. }
  132. fr := &FuncReply{}
  133. err = json.Unmarshal(res, fr)
  134. if err != nil {
  135. conf.Log.Error(err)
  136. return false
  137. }
  138. if fr.State {
  139. r, ok := fr.Result.(bool)
  140. if !ok {
  141. conf.Log.Errorf("IsAggregate result is not bool, got %s", string(res))
  142. return false
  143. } else {
  144. if r {
  145. f.isAgg = 2
  146. } else {
  147. f.isAgg = 1
  148. }
  149. return r
  150. }
  151. } else {
  152. conf.Log.Errorf("IsAggregate return state is false, got %+v", fr)
  153. return false
  154. }
  155. }
  156. func (f *PortableFunc) Close() error {
  157. return f.dataCh.Close()
  158. // Symbol must be closed by instance manager
  159. // ins.StopSymbol(ctx, c)
  160. }
  161. func encode(funcName string, arg interface{}) ([]byte, error) {
  162. c := FuncData{
  163. Func: funcName,
  164. Arg: arg,
  165. }
  166. return json.Marshal(c)
  167. }
  168. func encodeCtx(ctx api.FunctionContext) (string, error) {
  169. m := FuncMeta{
  170. Meta: Meta{
  171. RuleId: ctx.GetRuleId(),
  172. OpId: ctx.GetOpId(),
  173. InstanceId: ctx.GetInstanceId(),
  174. },
  175. FuncId: ctx.GetFuncId(),
  176. }
  177. bs, err := json.Marshal(m)
  178. return string(bs), err
  179. }