plugin.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package cluster
  2. import (
  3. "errors"
  4. "strings"
  5. "sync/atomic"
  6. "time"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  10. )
  11. type pluginLifeTime struct {
  12. lifetime plugin_entities.PluginRuntimeTimeLifeInterface
  13. last_scheduled_at time.Time
  14. }
  15. type pluginState struct {
  16. plugin_entities.PluginRuntimeState
  17. Identity string `json:"identity"`
  18. }
  19. // RegisterPlugin registers a plugin to the cluster, and start to be scheduled
  20. func (c *Cluster) RegisterPlugin(lifetime plugin_entities.PluginRuntimeTimeLifeInterface) error {
  21. identity, err := lifetime.Identity()
  22. if err != nil {
  23. return err
  24. }
  25. if c.plugins.Exits(identity.String()) {
  26. return errors.New("plugin has been registered")
  27. }
  28. l := &pluginLifeTime{
  29. lifetime: lifetime,
  30. }
  31. lifetime.OnStop(func() {
  32. c.plugin_lock.Lock()
  33. c.plugins.Delete(identity.String())
  34. // remove plugin state
  35. c.doPluginStateUpdate(l)
  36. c.plugin_lock.Unlock()
  37. })
  38. c.plugin_lock.Lock()
  39. if !lifetime.Stopped() {
  40. c.plugins.Store(identity.String(), l)
  41. // do plugin state update immediately
  42. err = c.doPluginStateUpdate(l)
  43. if err != nil {
  44. c.plugin_lock.Unlock()
  45. return err
  46. }
  47. }
  48. c.plugin_lock.Unlock()
  49. log.Info("start to schedule plugin %s", identity)
  50. return nil
  51. }
  52. const (
  53. PLUGIN_STATE_MAP_KEY = "plugin_state"
  54. )
  55. func (c *Cluster) getPluginStateKey(node_id string, plugin_id string) string {
  56. return node_id + ":" + plugin_id
  57. }
  58. func (c *Cluster) getScanPluginsByNodeKey(node_id string) string {
  59. return node_id + ":*"
  60. }
  61. func (c *Cluster) getScanPluginsByIdKey(plugin_id string) string {
  62. return "*:" + plugin_id
  63. }
  64. // SchedulePlugin schedules a plugin to the cluster
  65. // it will walk through the plugin state map and update all the states
  66. // as for the plugin has exited, normally, it will be removed automatically
  67. // but once a plugin is not removed, it will be gc by the master node
  68. func (c *Cluster) schedulePlugins() error {
  69. c.notifyPluginSchedule()
  70. defer c.notifyPluginScheduleCompleted()
  71. c.plugins.Range(func(key string, value *pluginLifeTime) bool {
  72. if time.Since(value.last_scheduled_at) < PLUGIN_SCHEDULER_INTERVAL {
  73. return true
  74. }
  75. // do plugin state update
  76. err := c.doPluginStateUpdate(value)
  77. if err != nil {
  78. log.Error("failed to update plugin state: %s", err.Error())
  79. }
  80. return true
  81. })
  82. return nil
  83. }
  84. // doPluginUpdate updates the plugin state and schedule the plugin
  85. func (c *Cluster) doPluginStateUpdate(lifetime *pluginLifeTime) error {
  86. state := lifetime.lifetime.RuntimeState()
  87. hash_identity, err := lifetime.lifetime.HashedIdentity()
  88. if err != nil {
  89. return err
  90. }
  91. identity, err := lifetime.lifetime.Identity()
  92. if err != nil {
  93. return err
  94. }
  95. schedule_state := &pluginState{
  96. Identity: identity.String(),
  97. PluginRuntimeState: state,
  98. }
  99. state_key := c.getPluginStateKey(c.id, hash_identity)
  100. // check if the plugin has been removed
  101. if !c.plugins.Exits(identity.String()) {
  102. // remove state
  103. err = c.removePluginState(c.id, hash_identity)
  104. if err != nil {
  105. return err
  106. }
  107. } else {
  108. // update plugin state
  109. schedule_state.ScheduledAt = &[]time.Time{time.Now()}[0]
  110. err = cache.SetMapOneField(PLUGIN_STATE_MAP_KEY, state_key, schedule_state)
  111. if err != nil {
  112. return err
  113. }
  114. lifetime.lifetime.UpdateScheduledAt(*schedule_state.ScheduledAt)
  115. }
  116. lifetime.last_scheduled_at = time.Now()
  117. return nil
  118. }
  119. func (c *Cluster) removePluginState(node_id string, hashed_identity string) error {
  120. err := cache.DelMapField(PLUGIN_STATE_MAP_KEY, c.getPluginStateKey(node_id, hashed_identity))
  121. if err != nil {
  122. return err
  123. }
  124. log.Info("plugin %s has been removed from node %s", hashed_identity, c.id)
  125. return nil
  126. }
  127. // forceGCNodePlugins will force garbage collect all the plugins on the node
  128. func (c *Cluster) forceGCNodePlugins(node_id string) error {
  129. return cache.ScanMapAsync[pluginState](
  130. PLUGIN_STATE_MAP_KEY,
  131. c.getScanPluginsByNodeKey(node_id),
  132. func(m map[string]pluginState) error {
  133. for _, plugin_state := range m {
  134. if err := c.forceGCNodePlugin(node_id, plugin_state.Identity); err != nil {
  135. return err
  136. }
  137. }
  138. return nil
  139. },
  140. )
  141. }
  142. // forceGCNodePlugin will force garbage collect the plugin on the node
  143. func (c *Cluster) forceGCNodePlugin(node_id string, plugin_id string) error {
  144. if node_id == c.id {
  145. c.plugin_lock.Lock()
  146. c.plugins.Delete(plugin_id)
  147. c.plugin_lock.Unlock()
  148. }
  149. if err := c.removePluginState(node_id, plugin_entities.HashedIdentity(plugin_id)); err != nil {
  150. return err
  151. }
  152. return nil
  153. }
  154. // forceGCPluginByNodePluginJoin will force garbage collect the plugin by node_plugin_join
  155. func (c *Cluster) forceGCPluginByNodePluginJoin(node_plugin_join string) error {
  156. return cache.DelMapField(PLUGIN_STATE_MAP_KEY, node_plugin_join)
  157. }
  158. func (c *Cluster) isPluginActive(state *pluginState) bool {
  159. return state != nil && state.ScheduledAt != nil && time.Since(*state.ScheduledAt) < 60*time.Second
  160. }
  161. func (c *Cluster) splitNodePluginJoin(node_plugin_join string) (node_id string, plugin_hashed_id string, err error) {
  162. split := strings.Split(node_plugin_join, ":")
  163. if len(split) != 2 {
  164. return "", "", errors.New("invalid node_plugin_join")
  165. }
  166. return split[0], split[1], nil
  167. }
  168. // autoGCPlugins will automatically garbage collect the plugins that are no longer active
  169. func (c *Cluster) autoGCPlugins() error {
  170. // skip if already in auto gc
  171. if atomic.LoadInt32(&c.is_in_auto_gc_plugins) == 1 {
  172. return nil
  173. }
  174. defer atomic.StoreInt32(&c.is_in_auto_gc_plugins, 0)
  175. return cache.ScanMapAsync[pluginState](
  176. PLUGIN_STATE_MAP_KEY,
  177. "*",
  178. func(m map[string]pluginState) error {
  179. for node_plugin_join, plugin_state := range m {
  180. if !c.isPluginActive(&plugin_state) {
  181. node_id, _, err := c.splitNodePluginJoin(node_plugin_join)
  182. if err != nil {
  183. return err
  184. }
  185. // force gc the plugin
  186. if err := c.forceGCNodePlugin(node_id, plugin_state.Identity); err != nil {
  187. return err
  188. }
  189. // one more time to force gc the plugin, there is a possibility
  190. // that the hash value of plugin's identity is not the same as the node_plugin_join
  191. // so we need to force gc the plugin by node_plugin_join again
  192. if err := c.forceGCPluginByNodePluginJoin(node_plugin_join); err != nil {
  193. return err
  194. }
  195. }
  196. }
  197. return nil
  198. },
  199. )
  200. }
  201. func (c *Cluster) IsPluginNoCurrentNode(identity plugin_entities.PluginUniqueIdentifier) bool {
  202. _, ok := c.plugins.Load(identity.String())
  203. return ok
  204. }