plugin_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package cluster
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/google/uuid"
  6. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/positive_manager"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/manifest_entities"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  10. )
  11. type fakePlugin struct {
  12. plugin_entities.PluginRuntime
  13. positive_manager.PositivePluginRuntime
  14. }
  15. func (r *fakePlugin) InitEnvironment() error {
  16. return nil
  17. }
  18. func (r *fakePlugin) Checksum() (string, error) {
  19. return "", nil
  20. }
  21. func (r *fakePlugin) Identity() (plugin_entities.PluginUniqueIdentifier, error) {
  22. return plugin_entities.PluginUniqueIdentifier(""), nil
  23. }
  24. func (r *fakePlugin) StartPlugin() error {
  25. return nil
  26. }
  27. func (r *fakePlugin) Type() plugin_entities.PluginRuntimeType {
  28. return plugin_entities.PLUGIN_RUNTIME_TYPE_LOCAL
  29. }
  30. func (r *fakePlugin) Wait() (<-chan bool, error) {
  31. return nil, nil
  32. }
  33. func (r *fakePlugin) Listen(string) *entities.Broadcast[plugin_entities.SessionMessage] {
  34. return nil
  35. }
  36. func (r *fakePlugin) Write(string, []byte) {
  37. }
  38. func getRandomPluginRuntime() fakePlugin {
  39. return fakePlugin{
  40. PluginRuntime: plugin_entities.PluginRuntime{
  41. Config: plugin_entities.PluginDeclaration{
  42. PluginDeclarationWithoutAdvancedFields: plugin_entities.PluginDeclarationWithoutAdvancedFields{
  43. Name: uuid.New().String(),
  44. Label: plugin_entities.I18nObject{
  45. EnUS: "label",
  46. },
  47. Version: "0.0.1",
  48. Type: manifest_entities.PluginType,
  49. Author: "Yeuoly",
  50. CreatedAt: time.Now(),
  51. Plugins: plugin_entities.PluginExtensions{
  52. Tools: []string{"test"},
  53. },
  54. },
  55. },
  56. },
  57. }
  58. }
  59. func TestPluginScheduleLifetime(t *testing.T) {
  60. plugin := getRandomPluginRuntime()
  61. cluster, err := createSimulationCluster(1)
  62. if err != nil {
  63. t.Errorf("create simulation cluster failed: %v", err)
  64. return
  65. }
  66. launchSimulationCluster(cluster)
  67. defer closeSimulationCluster(cluster, t)
  68. time.Sleep(time.Second * 1)
  69. // add plugin to the cluster
  70. err = cluster[0].RegisterPlugin(&plugin)
  71. if err != nil {
  72. t.Errorf("register plugin failed: %v", err)
  73. return
  74. }
  75. identity, err := plugin.Identity()
  76. if err != nil {
  77. t.Errorf("get plugin identity failed: %v", err)
  78. return
  79. }
  80. hashedIdentity := plugin_entities.HashedIdentity(identity.String())
  81. nodes, err := cluster[0].FetchPluginAvailableNodesByHashedId(hashedIdentity)
  82. if err != nil {
  83. t.Errorf("fetch plugin available nodes failed: %v", err)
  84. return
  85. }
  86. if len(nodes) != 1 {
  87. t.Errorf("plugin not scheduled")
  88. return
  89. }
  90. if nodes[0] != cluster[0].id {
  91. t.Errorf("plugin scheduled to wrong node")
  92. return
  93. }
  94. // trigger plugin stop
  95. plugin.TriggerStop()
  96. // wait for the plugin to stop
  97. time.Sleep(time.Second * 1)
  98. // check if the plugin is stopped
  99. nodes, err = cluster[0].FetchPluginAvailableNodesByHashedId(hashedIdentity)
  100. if err != nil {
  101. t.Errorf("fetch plugin available nodes failed: %v", err)
  102. return
  103. }
  104. if len(nodes) != 0 {
  105. t.Errorf("plugin not stopped")
  106. return
  107. }
  108. }
  109. func TestPluginScheduleWhenMasterClusterShutdown(t *testing.T) {
  110. plugins := []fakePlugin{
  111. getRandomPluginRuntime(),
  112. getRandomPluginRuntime(),
  113. }
  114. cluster, err := createSimulationCluster(2)
  115. if err != nil {
  116. t.Errorf("create simulation cluster failed: %v", err)
  117. return
  118. }
  119. // set master gc interval to 1 second
  120. for _, node := range cluster {
  121. node.masterGcInterval = time.Second * 1
  122. node.pluginSchedulerInterval = time.Second * 1
  123. node.pluginSchedulerTickerInterval = time.Second * 1
  124. node.updateNodeStatusInterval = time.Second * 1
  125. node.pluginDeactivatedTimeout = time.Second * 3
  126. }
  127. launchSimulationCluster(cluster)
  128. defer closeSimulationCluster(cluster, t)
  129. // add plugin to the cluster
  130. for i, plugin := range plugins {
  131. err = cluster[i].RegisterPlugin(&plugin)
  132. if err != nil {
  133. t.Errorf("register plugin failed: %v", err)
  134. return
  135. }
  136. }
  137. // wait for the plugin to be scheduled
  138. time.Sleep(time.Second * 1)
  139. // close master node and wait for new master to be elected
  140. masterIdx := -1
  141. for i, node := range cluster {
  142. if node.IsMaster() {
  143. masterIdx = i
  144. // close the master node
  145. node.Close()
  146. break
  147. }
  148. }
  149. if masterIdx == -1 {
  150. t.Errorf("master node not found")
  151. return
  152. }
  153. // wait for the new master to be elected
  154. i := 0
  155. for ; i < 10; i++ {
  156. time.Sleep(time.Second * 1)
  157. found := false
  158. for i, node := range cluster {
  159. if node.IsMaster() && i != masterIdx {
  160. found = true
  161. break
  162. }
  163. }
  164. if found {
  165. break
  166. }
  167. }
  168. if i == 10 {
  169. t.Errorf("master node is not elected")
  170. return
  171. }
  172. // check if plugins[master_idx] is removed
  173. identity, err := plugins[masterIdx].Identity()
  174. if err != nil {
  175. t.Errorf("get plugin identity failed: %v", err)
  176. return
  177. }
  178. hashedIdentity := plugin_entities.HashedIdentity(identity.String())
  179. ticker := time.NewTicker(time.Second)
  180. timeout := time.NewTimer(cluster[masterIdx].masterGcInterval * 10)
  181. done := false
  182. for !done {
  183. select {
  184. case <-ticker.C:
  185. nodes, err := cluster[masterIdx].FetchPluginAvailableNodesByHashedId(hashedIdentity)
  186. if err != nil {
  187. t.Errorf("fetch plugin available nodes failed: %v", err)
  188. return
  189. }
  190. if len(nodes) == 0 {
  191. done = true
  192. }
  193. case <-timeout.C:
  194. t.Errorf("plugin not removed")
  195. return
  196. }
  197. }
  198. // check if plugins[1-master_idx] is still scheduled
  199. identity, err = plugins[1-masterIdx].Identity()
  200. if err != nil {
  201. t.Errorf("get plugin identity failed: %v", err)
  202. return
  203. }
  204. hashedIdentity = plugin_entities.HashedIdentity(identity.String())
  205. nodes, err := cluster[1-masterIdx].FetchPluginAvailableNodesByHashedId(hashedIdentity)
  206. if err != nil {
  207. t.Errorf("fetch plugin available nodes failed: %v", err)
  208. return
  209. }
  210. if len(nodes) != 1 {
  211. t.Errorf("plugin not scheduled")
  212. return
  213. }
  214. }