plugin.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.PluginLifetime
  13. lastScheduledAt 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.PluginLifetime) error {
  21. identity, err := lifetime.Identity()
  22. if err != nil {
  23. return err
  24. }
  25. if c.showLog {
  26. log.Info("registering plugin %s", identity.String())
  27. }
  28. if c.plugins.Exists(identity.String()) {
  29. return errors.New("plugin has been registered")
  30. }
  31. l := &pluginLifeTime{
  32. lifetime: lifetime,
  33. }
  34. lifetime.OnStop(func() {
  35. c.pluginLock.Lock()
  36. c.plugins.Delete(identity.String())
  37. // remove plugin state
  38. c.doPluginStateUpdate(l)
  39. c.pluginLock.Unlock()
  40. })
  41. c.pluginLock.Lock()
  42. if !lifetime.Stopped() {
  43. c.plugins.Store(identity.String(), l)
  44. // do plugin state update immediately
  45. err = c.doPluginStateUpdate(l)
  46. if err != nil {
  47. c.pluginLock.Unlock()
  48. return err
  49. }
  50. }
  51. c.pluginLock.Unlock()
  52. if c.showLog {
  53. log.Info("start to schedule plugin %s", identity)
  54. }
  55. return nil
  56. }
  57. const (
  58. PLUGIN_STATE_MAP_KEY = "plugin_state"
  59. )
  60. func (c *Cluster) getPluginStateKey(nodeId string, plugin_id string) string {
  61. return nodeId + ":" + plugin_id
  62. }
  63. func (c *Cluster) getScanPluginsByNodeKey(nodeId string) string {
  64. return nodeId + ":*"
  65. }
  66. func (c *Cluster) getScanPluginsByIdKey(plugin_id string) string {
  67. return "*:" + plugin_id
  68. }
  69. // SchedulePlugin schedules a plugin to the cluster
  70. // it will walk through the plugin state map and update all the states
  71. // as for the plugin has exited, normally, it will be removed automatically
  72. // but once a plugin is not removed, it will be gc by the master node
  73. func (c *Cluster) schedulePlugins() error {
  74. if c.showLog {
  75. log.Info("scheduling %d plugins", c.plugins.Len())
  76. }
  77. c.notifyPluginSchedule()
  78. defer c.notifyPluginScheduleCompleted()
  79. c.plugins.Range(func(key string, value *pluginLifeTime) bool {
  80. if time.Since(value.lastScheduledAt) < c.pluginSchedulerInterval {
  81. return true
  82. }
  83. if c.showLog {
  84. log.Info("scheduling plugin %s", key)
  85. }
  86. // do plugin state update
  87. err := c.doPluginStateUpdate(value)
  88. if err != nil {
  89. log.Error("failed to update plugin state: %s", err.Error())
  90. }
  91. if c.showLog {
  92. log.Info("scheduled plugin %s", key)
  93. }
  94. return true
  95. })
  96. if c.showLog {
  97. log.Info("scheduled %d plugins", c.plugins.Len())
  98. }
  99. return nil
  100. }
  101. // doPluginUpdate updates the plugin state and schedule the plugin
  102. func (c *Cluster) doPluginStateUpdate(lifetime *pluginLifeTime) error {
  103. state := lifetime.lifetime.RuntimeState()
  104. identity, err := lifetime.lifetime.Identity()
  105. if err != nil {
  106. return err
  107. }
  108. if c.showLog {
  109. log.Info("updating plugin state %s", identity.String())
  110. }
  111. hashedIdentity := plugin_entities.HashedIdentity(identity.String())
  112. scheduleState := &pluginState{
  113. Identity: identity.String(),
  114. PluginRuntimeState: state,
  115. }
  116. stateKey := c.getPluginStateKey(c.id, hashedIdentity)
  117. // check if the plugin has been removed
  118. if !c.plugins.Exists(identity.String()) {
  119. if c.showLog {
  120. log.Info("removing plugin state %s due no longer exists", identity.String())
  121. }
  122. // remove state
  123. err = c.removePluginState(c.id, hashedIdentity)
  124. if err != nil {
  125. return err
  126. }
  127. } else {
  128. if c.showLog {
  129. log.Info("updating plugin state %s", identity.String())
  130. }
  131. // update plugin state
  132. scheduleState.ScheduledAt = &[]time.Time{time.Now()}[0]
  133. err = cache.SetMapOneField(PLUGIN_STATE_MAP_KEY, stateKey, scheduleState)
  134. if err != nil {
  135. return err
  136. }
  137. lifetime.lifetime.UpdateScheduledAt(*scheduleState.ScheduledAt)
  138. if c.showLog {
  139. log.Info("updated plugin state %s", identity.String())
  140. }
  141. }
  142. lifetime.lastScheduledAt = time.Now()
  143. return nil
  144. }
  145. func (c *Cluster) removePluginState(nodeId string, hashed_identity string) error {
  146. if c.showLog {
  147. log.Info("removing plugin state %s", hashed_identity)
  148. }
  149. err := cache.DelMapField(PLUGIN_STATE_MAP_KEY, c.getPluginStateKey(nodeId, hashed_identity))
  150. if err != nil {
  151. return err
  152. }
  153. if c.showLog {
  154. log.Info("plugin %s has been removed from node %s", hashed_identity, c.id)
  155. }
  156. return nil
  157. }
  158. // forceGCNodePlugins will force garbage collect all the plugins on the node
  159. func (c *Cluster) forceGCNodePlugins(nodeId string) error {
  160. return cache.ScanMapAsync(
  161. PLUGIN_STATE_MAP_KEY,
  162. c.getScanPluginsByNodeKey(nodeId),
  163. func(m map[string]pluginState) error {
  164. for _, plugin_state := range m {
  165. if err := c.forceGCNodePlugin(nodeId, plugin_state.Identity); err != nil {
  166. return err
  167. }
  168. }
  169. return nil
  170. },
  171. )
  172. }
  173. // forceGCNodePlugin will force garbage collect the plugin on the node
  174. func (c *Cluster) forceGCNodePlugin(nodeId string, plugin_id string) error {
  175. if nodeId == c.id {
  176. c.pluginLock.Lock()
  177. c.plugins.Delete(plugin_id)
  178. c.pluginLock.Unlock()
  179. }
  180. if err := c.removePluginState(nodeId, plugin_entities.HashedIdentity(plugin_id)); err != nil {
  181. return err
  182. }
  183. return nil
  184. }
  185. // forceGCPluginByNodePluginJoin will force garbage collect the plugin by node_plugin_join
  186. func (c *Cluster) forceGCPluginByNodePluginJoin(node_plugin_join string) error {
  187. return cache.DelMapField(PLUGIN_STATE_MAP_KEY, node_plugin_join)
  188. }
  189. func (c *Cluster) isPluginActive(state *pluginState) bool {
  190. if state == nil {
  191. return false
  192. }
  193. if state.ScheduledAt == nil {
  194. return false
  195. }
  196. if time.Since(*state.ScheduledAt) > c.pluginDeactivatedTimeout {
  197. return false
  198. }
  199. return true
  200. }
  201. func (c *Cluster) splitNodePluginJoin(node_plugin_join string) (nodeId string, plugin_hashed_id string, err error) {
  202. split := strings.Split(node_plugin_join, ":")
  203. if len(split) != 2 {
  204. return "", "", errors.New("invalid node_plugin_join")
  205. }
  206. return split[0], split[1], nil
  207. }
  208. // autoGCPlugins will automatically garbage collect the plugins that are no longer active
  209. func (c *Cluster) autoGCPlugins() error {
  210. // skip if already in auto gc
  211. if atomic.LoadInt32(&c.isInAutoGcPlugins) == 1 {
  212. return nil
  213. }
  214. defer atomic.StoreInt32(&c.isInAutoGcPlugins, 0)
  215. return cache.ScanMapAsync(
  216. PLUGIN_STATE_MAP_KEY,
  217. "*",
  218. func(m map[string]pluginState) error {
  219. for node_plugin_join, plugin_state := range m {
  220. if !c.isPluginActive(&plugin_state) {
  221. nodeId, _, err := c.splitNodePluginJoin(node_plugin_join)
  222. if err != nil {
  223. return err
  224. }
  225. // force gc the plugin
  226. if err := c.forceGCNodePlugin(nodeId, plugin_state.Identity); err != nil {
  227. return err
  228. }
  229. // one more time to force gc the plugin, there is a possibility
  230. // that the hash value of plugin's identity is not the same as the node_plugin_join
  231. // so we need to force gc the plugin by node_plugin_join again
  232. if err := c.forceGCPluginByNodePluginJoin(node_plugin_join); err != nil {
  233. return err
  234. }
  235. }
  236. }
  237. return nil
  238. },
  239. )
  240. }
  241. func (c *Cluster) IsPluginOnCurrentNode(identity plugin_entities.PluginUniqueIdentifier) (bool, error) {
  242. _, ok := c.plugins.Load(identity.String())
  243. if !ok {
  244. _, err := c.manager.Get(identity)
  245. if err != nil {
  246. return false, err
  247. } else {
  248. return true, nil
  249. }
  250. }
  251. return ok, nil
  252. }