plugin_ins_manager.go 5.4 KB

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