function.go 4.0 KB

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