plugin_ins_manager.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. "github.com/lf-edge/ekuiper/pkg/api"
  20. "os"
  21. "os/exec"
  22. "sync"
  23. )
  24. var (
  25. once sync.Once
  26. pm *pluginInsManager
  27. )
  28. // TODO setting configuration
  29. var PortbleConf = &PortableConfig{
  30. SendTimeout: 1000,
  31. }
  32. type PluginIns struct {
  33. process *os.Process
  34. ctrlChan ControlChannel
  35. runningCount int
  36. name string
  37. }
  38. func NewPluginIns(name string, ctrlChan ControlChannel, process *os.Process) *PluginIns {
  39. // if process is not passed, it is run in simulator mode. Then do not count running.
  40. // so that it won't be automatically close.
  41. rc := 0
  42. if process == nil {
  43. rc = 1
  44. }
  45. return &PluginIns{
  46. process: process,
  47. ctrlChan: ctrlChan,
  48. runningCount: rc,
  49. name: name,
  50. }
  51. }
  52. func (i *PluginIns) StartSymbol(ctx api.StreamContext, ctrl *Control) error {
  53. arg, err := json.Marshal(ctrl)
  54. if err != nil {
  55. return err
  56. }
  57. c := Command{
  58. Cmd: CMD_START,
  59. Arg: string(arg),
  60. }
  61. jsonArg, err := json.Marshal(c)
  62. if err != nil {
  63. return err
  64. }
  65. err = i.ctrlChan.SendCmd(jsonArg)
  66. if err == nil {
  67. i.runningCount++
  68. ctx.GetLogger().Infof("started symbol %s", ctrl.SymbolName)
  69. }
  70. return err
  71. }
  72. func (i *PluginIns) StopSymbol(ctx api.StreamContext, ctrl *Control) error {
  73. arg, err := json.Marshal(ctrl)
  74. if err != nil {
  75. return err
  76. }
  77. c := Command{
  78. Cmd: CMD_STOP,
  79. Arg: string(arg),
  80. }
  81. jsonArg, err := json.Marshal(c)
  82. if err != nil {
  83. return err
  84. }
  85. err = i.ctrlChan.SendCmd(jsonArg)
  86. i.runningCount--
  87. ctx.GetLogger().Infof("stopped symbol %s", ctrl.SymbolName)
  88. if i.runningCount == 0 {
  89. err := GetPluginInsManager().Kill(i.name)
  90. if err != nil {
  91. ctx.GetLogger().Infof("fail to stop plugin %s: %v", i.name, err)
  92. return err
  93. }
  94. ctx.GetLogger().Infof("stop plugin %s", i.name)
  95. }
  96. return err
  97. }
  98. func (i *PluginIns) Stop() error {
  99. var err error
  100. if i.ctrlChan != nil {
  101. err = i.ctrlChan.Close()
  102. }
  103. if i.process != nil {
  104. err = i.process.Kill()
  105. }
  106. return err
  107. }
  108. // Manager plugin process and control socket
  109. type pluginInsManager struct {
  110. instances map[string]*PluginIns
  111. sync.RWMutex
  112. }
  113. func GetPluginInsManager() *pluginInsManager {
  114. once.Do(func() {
  115. pm = &pluginInsManager{
  116. instances: make(map[string]*PluginIns),
  117. }
  118. })
  119. return pm
  120. }
  121. func (p *pluginInsManager) getPluginIns(name string) (*PluginIns, bool) {
  122. p.RLock()
  123. defer p.RUnlock()
  124. ins, ok := p.instances[name]
  125. return ins, ok
  126. }
  127. func (p *pluginInsManager) deletePluginIns(name string) {
  128. p.Lock()
  129. defer p.Unlock()
  130. delete(p.instances, name)
  131. }
  132. // AddPluginIns For mock only
  133. func (p *pluginInsManager) AddPluginIns(name string, ins *PluginIns) {
  134. p.Lock()
  135. defer p.Unlock()
  136. p.instances[name] = ins
  137. }
  138. func (p *pluginInsManager) getOrStartProcess(pluginMeta *PluginMeta, pconf *PortableConfig) (*PluginIns, error) {
  139. p.Lock()
  140. defer p.Unlock()
  141. if ins, ok := p.instances[pluginMeta.Name]; ok {
  142. return ins, nil
  143. }
  144. conf.Log.Infof("create control channel")
  145. ctrlChan, err := CreateControlChannel(pluginMeta.Name)
  146. if err != nil {
  147. return nil, fmt.Errorf("can't create new control channel: %s", err.Error())
  148. }
  149. conf.Log.Infof("executing plugin")
  150. jsonArg, err := json.Marshal(pconf)
  151. if err != nil {
  152. return nil, fmt.Errorf("invalid conf: %v", pconf)
  153. }
  154. var cmd *exec.Cmd
  155. switch pluginMeta.Language {
  156. case "go":
  157. conf.Log.Printf("starting go plugin executable %s", pluginMeta.Executable)
  158. cmd = exec.Command(pluginMeta.Executable, string(jsonArg))
  159. case "python":
  160. conf.Log.Printf("starting python plugin executable %s with script %s\n", conf.Config.Portable.PythonBin, pluginMeta.Executable)
  161. cmd = exec.Command(conf.Config.Portable.PythonBin, pluginMeta.Executable, string(jsonArg))
  162. default:
  163. return nil, fmt.Errorf("unsupported language: %s", pluginMeta.Language)
  164. }
  165. cmd.Stdout = conf.Log.Out
  166. cmd.Stderr = conf.Log.Out
  167. conf.Log.Println("plugin starting")
  168. err = cmd.Start()
  169. if err != nil {
  170. return nil, fmt.Errorf("plugin executable %s stops with error %v", pluginMeta.Executable, err)
  171. }
  172. process := cmd.Process
  173. conf.Log.Printf("plugin started pid: %d\n", process.Pid)
  174. go func() {
  175. err = cmd.Wait()
  176. if err != nil {
  177. conf.Log.Printf("plugin executable %s stops with error %v", pluginMeta.Executable, err)
  178. }
  179. if ins, ok := p.getPluginIns(pluginMeta.Name); ok {
  180. _ = ins.ctrlChan.Close()
  181. p.deletePluginIns(pluginMeta.Name)
  182. }
  183. }()
  184. conf.Log.Println("waiting handshake")
  185. err = ctrlChan.Handshake()
  186. if err != nil {
  187. return nil, fmt.Errorf("plugin %s control handshake error: %v", pluginMeta.Executable, err)
  188. }
  189. ins := NewPluginIns(pluginMeta.Name, ctrlChan, process)
  190. p.instances[pluginMeta.Name] = ins
  191. conf.Log.Println("plugin start running")
  192. return ins, nil
  193. }
  194. func (p *pluginInsManager) Kill(name string) error {
  195. p.Lock()
  196. defer p.Unlock()
  197. var err error
  198. if ins, ok := p.instances[name]; ok {
  199. err = ins.Stop()
  200. delete(p.instances, name)
  201. } else {
  202. return fmt.Errorf("instance %s not found", name)
  203. }
  204. return err
  205. }
  206. func (p *pluginInsManager) KillAll() error {
  207. p.Lock()
  208. defer p.Unlock()
  209. for _, ins := range p.instances {
  210. _ = ins.Stop()
  211. }
  212. p.instances = make(map[string]*PluginIns)
  213. return nil
  214. }
  215. type PluginMeta struct {
  216. Name string `json:"name"`
  217. Version string `json:"version"`
  218. Language string `json:"language"`
  219. Executable string `json:"executable"`
  220. }