mirror of
https://github.com/langgenius/dify-plugin-daemon.git
synced 2026-07-21 17:25:23 -04:00
888ad788bc
* refactor: introduce local plugin control panel and cleanup environment setup process * fix: args * refactor: new local runtime * temp: stash work for refactor on RemotePluginServer * refactor: unify local runtime lifetime and sperate init environment process * chore: add missing files * stash * refactor: local plugin lifetime control * refactor: complete installation process of control panel * refactor: adapt service layer to new controlpanel * refactor: pluginManager.Install * fix: add routine wrap to InstallServerless, avoid blocking main thread * feat: reinstall serverless runtime * chore: add comments to Reinstall and update confusing naming * refactor: unify install plugin service * refactor: add labels to debugging runtime * refactor: add getters to plugin manager * refactor: split install service to decode/install_task/install service * ??? * refactor: adapt controllers * refactor: session write * refactor: session runtime * Refine install task orchestration (#501) * refactor: installing task * refactor cluster management, decouple lifetime management and cluster * fix cli test command * fix: cleanup TODO comments and implement GracefulStop for instance * feat: add logger to control panel * fix: multiple nil references * refactor: better lifetime control * refactor: better cycle interval * fix(LocalPluginRuntime): prevent returning err when it's not error * fix: avoid adding empty PipExtraArgs * fix: missing errors in Environment init * fix: add truncateMessage to avoid db explosion * cleanup: better lifecycle management * fix: init status at the beginning of installation * optimize: GracefulStop for pluginInstance * refactor: tests * refactor: centralize routine labels (#504) * cleanup: RoutineKey * fix: init routine pool * fix: correctly handle cluster register error * fix: memory leak * fix: add \n to instance write * fix(installer.go): set success to true after succeed for defer func * refactor * fix: missing cwd in testutils * fix: scaleup default runtime nums to 1 when testing * fix: localruntime appconfig in testing module * Update internal/core/local_runtime/load_balancing.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * fix: more efficiency implement in installer_local.go * fix: returns after failing in onDebuggingRuntimeDisconnected * fix: returns after failing in onDebuggingRuntimeDisconnected * fix: splits tests * refactor: naming * refactor: manifest.VersionX * fix: adapt SetDefault to tests * fix: enforce use constants in DBType * fix: generate * fix: linter * cleanup tests * refactor: change package to * cleanup: useless codes * Update internal/cluster/plugin.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * cleanup * refactor: decouple connection_key management from debugging_time * refactor: confused naming * feat: recycle resources to adapt to https://github.com/langgenius/dify-plugin-daemon/pull/500 * refactor: confusing redirecting * fix: support get serverless runtime * fix: race condition in Launching * fix: avoid ManifestValidate in first step of debugging handshake * fix: adding ReleaseAllLocks to finalizers * wtf: what a beautiful code * refactor: rename Stream.Async to Stream.Process * fix: kill process if daed instance was detected * fix: correctly handle failures * fix: consistence of difference interfaces * fix: add stacktrace to panic * fix: only trigger once event * fix: ensure plugin runtime was shutdown * feat: cleanup install tasks * fix: add scale logs --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
300 lines
7.5 KiB
Go
300 lines
7.5 KiB
Go
package cluster
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
|
|
"github.com/langgenius/dify-plugin-daemon/pkg/utils/cache"
|
|
"github.com/langgenius/dify-plugin-daemon/pkg/utils/log"
|
|
)
|
|
|
|
type pluginLifeTime struct {
|
|
lifetime plugin_entities.PluginLifetime
|
|
lastScheduledAt time.Time
|
|
}
|
|
|
|
type pluginState struct {
|
|
plugin_entities.PluginRuntimeState
|
|
Identity string `json:"identity"`
|
|
}
|
|
|
|
func (l *pluginLifeTime) UpdateScheduledAt(t time.Time) {
|
|
l.lastScheduledAt = t
|
|
}
|
|
|
|
// RegisterPlugin registers a plugin to the cluster, and start to be scheduled
|
|
func (c *Cluster) RegisterPlugin(lifetime plugin_entities.PluginLifetime) error {
|
|
identity, err := lifetime.Identity()
|
|
if err != nil {
|
|
return errors.Join(err, errors.New("failed to get plugin identity"))
|
|
}
|
|
|
|
if c.showLog {
|
|
log.Info("registering plugin %s", identity.String())
|
|
}
|
|
|
|
if c.plugins.Exists(identity.String()) {
|
|
return errors.New("plugin has been registered")
|
|
}
|
|
|
|
l := &pluginLifeTime{
|
|
lifetime: lifetime,
|
|
}
|
|
|
|
c.plugins.Store(identity.String(), l)
|
|
|
|
// do plugin state update immediately
|
|
err = c.doPluginStateUpdate(l)
|
|
if err != nil {
|
|
return errors.Join(err, errors.New("failed to update plugin state"))
|
|
}
|
|
|
|
if c.showLog {
|
|
log.Info("start to schedule plugin %s", identity)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Cluster) UnregisterPlugin(lifetime plugin_entities.PluginLifetime) error {
|
|
identity, err := lifetime.Identity()
|
|
if err != nil {
|
|
return errors.Join(err, errors.New("failed to get plugin identity"))
|
|
}
|
|
|
|
if c.showLog {
|
|
log.Info("unregistering plugin %s", identity.String())
|
|
}
|
|
|
|
// remove plugin from cluster
|
|
err = c.removePluginState(c.id, plugin_entities.HashedIdentity(identity.String()))
|
|
if err != nil {
|
|
return errors.Join(err, errors.New("failed to remove plugin state"))
|
|
}
|
|
|
|
c.plugins.Delete(identity.String())
|
|
|
|
return nil
|
|
}
|
|
|
|
const (
|
|
PLUGIN_STATE_MAP_KEY = "plugin_state"
|
|
)
|
|
|
|
func (c *Cluster) getPluginStateKey(nodeId string, plugin_id string) string {
|
|
return nodeId + ":" + plugin_id
|
|
}
|
|
|
|
func (c *Cluster) getScanPluginsByNodeKey(nodeId string) string {
|
|
return nodeId + ":*"
|
|
}
|
|
|
|
func (c *Cluster) getScanPluginsByIdKey(plugin_id string) string {
|
|
return "*:" + plugin_id
|
|
}
|
|
|
|
// SchedulePlugin schedules a plugin to the cluster
|
|
// it will walk through the plugin state map and update all the states
|
|
// as for the plugin has exited, normally, it will be removed automatically
|
|
// but once a plugin is not removed, it will be gc by the master node
|
|
func (c *Cluster) schedulePlugins() error {
|
|
if c.showLog {
|
|
log.Info("scheduling %d plugins", c.plugins.Len())
|
|
}
|
|
|
|
c.notifyPluginSchedule()
|
|
defer c.notifyPluginScheduleCompleted()
|
|
|
|
c.plugins.Range(func(key string, value *pluginLifeTime) bool {
|
|
if time.Since(value.lastScheduledAt) < c.pluginSchedulerInterval {
|
|
return true
|
|
}
|
|
if c.showLog {
|
|
log.Info("scheduling plugin %s", key)
|
|
}
|
|
// do plugin state update
|
|
err := c.doPluginStateUpdate(value)
|
|
if err != nil {
|
|
log.Error("failed to update plugin state: %s", err.Error())
|
|
}
|
|
|
|
if c.showLog {
|
|
log.Info("scheduled plugin %s", key)
|
|
}
|
|
|
|
return true
|
|
})
|
|
|
|
if c.showLog {
|
|
log.Info("scheduled %d plugins", c.plugins.Len())
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// doPluginUpdate updates the plugin state and schedule the plugin
|
|
func (c *Cluster) doPluginStateUpdate(lifetime *pluginLifeTime) error {
|
|
state := lifetime.lifetime.RuntimeState()
|
|
identity, err := lifetime.lifetime.Identity()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if c.showLog {
|
|
log.Info("updating plugin state %s", identity.String())
|
|
}
|
|
|
|
hashedIdentity := plugin_entities.HashedIdentity(identity.String())
|
|
|
|
scheduleState := &pluginState{
|
|
Identity: identity.String(),
|
|
PluginRuntimeState: state,
|
|
}
|
|
|
|
stateKey := c.getPluginStateKey(c.id, hashedIdentity)
|
|
|
|
// check if the plugin has been removed
|
|
if !c.plugins.Exists(identity.String()) {
|
|
if c.showLog {
|
|
log.Info("removing plugin state %s due no longer exists", identity.String())
|
|
}
|
|
// remove state
|
|
err = c.removePluginState(c.id, hashedIdentity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if c.showLog {
|
|
log.Info("updating plugin state %s", identity.String())
|
|
}
|
|
// update plugin state
|
|
scheduleState.ScheduledAt = &[]time.Time{time.Now()}[0]
|
|
err = cache.SetMapOneField(PLUGIN_STATE_MAP_KEY, stateKey, scheduleState)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
lifetime.UpdateScheduledAt(*scheduleState.ScheduledAt)
|
|
if c.showLog {
|
|
log.Info("updated plugin state %s", identity.String())
|
|
}
|
|
}
|
|
|
|
lifetime.lastScheduledAt = time.Now()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Cluster) removePluginState(nodeId string, hashed_identity string) error {
|
|
if c.showLog {
|
|
log.Info("removing plugin state %s", hashed_identity)
|
|
}
|
|
err := cache.DelMapField(PLUGIN_STATE_MAP_KEY, c.getPluginStateKey(nodeId, hashed_identity))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if c.showLog {
|
|
log.Info("plugin %s has been removed from node %s", hashed_identity, c.id)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// forceGCNodePlugins will force garbage collect all the plugins on the node
|
|
func (c *Cluster) forceGCNodePlugins(nodeId string) error {
|
|
return cache.ScanMapAsync(
|
|
PLUGIN_STATE_MAP_KEY,
|
|
c.getScanPluginsByNodeKey(nodeId),
|
|
func(m map[string]pluginState) error {
|
|
for _, plugin_state := range m {
|
|
if err := c.forceGCNodePlugin(nodeId, plugin_state.Identity); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
},
|
|
)
|
|
}
|
|
|
|
// forceGCNodePlugin will force garbage collect the plugin on the node
|
|
func (c *Cluster) forceGCNodePlugin(nodeId string, plugin_id string) error {
|
|
if nodeId == c.id {
|
|
c.pluginLock.Lock()
|
|
c.plugins.Delete(plugin_id)
|
|
c.pluginLock.Unlock()
|
|
}
|
|
|
|
if err := c.removePluginState(nodeId, plugin_entities.HashedIdentity(plugin_id)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// forceGCPluginByNodePluginJoin will force garbage collect the plugin by node_plugin_join
|
|
func (c *Cluster) forceGCPluginByNodePluginJoin(node_plugin_join string) error {
|
|
return cache.DelMapField(PLUGIN_STATE_MAP_KEY, node_plugin_join)
|
|
}
|
|
|
|
func (c *Cluster) isPluginActive(state *pluginState) bool {
|
|
if state == nil {
|
|
return false
|
|
}
|
|
if state.ScheduledAt == nil {
|
|
return false
|
|
}
|
|
if time.Since(*state.ScheduledAt) > c.pluginDeactivatedTimeout {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (c *Cluster) splitNodePluginJoin(node_plugin_join string) (nodeId string, plugin_hashed_id string, err error) {
|
|
split := strings.Split(node_plugin_join, ":")
|
|
if len(split) != 2 {
|
|
return "", "", errors.New("invalid node_plugin_join")
|
|
}
|
|
|
|
return split[0], split[1], nil
|
|
}
|
|
|
|
// autoGCPlugins will automatically garbage collect the plugins that are no longer active
|
|
func (c *Cluster) autoGCPlugins() error {
|
|
// skip if already in auto gc
|
|
if atomic.LoadInt32(&c.isInAutoGcPlugins) == 1 {
|
|
return nil
|
|
}
|
|
defer atomic.StoreInt32(&c.isInAutoGcPlugins, 0)
|
|
|
|
return cache.ScanMapAsync(
|
|
PLUGIN_STATE_MAP_KEY,
|
|
"*",
|
|
func(m map[string]pluginState) error {
|
|
for node_plugin_join, plugin_state := range m {
|
|
if !c.isPluginActive(&plugin_state) {
|
|
nodeId, _, err := c.splitNodePluginJoin(node_plugin_join)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// force gc the plugin
|
|
if err := c.forceGCNodePlugin(nodeId, plugin_state.Identity); err != nil {
|
|
return err
|
|
}
|
|
|
|
// one more time to force gc the plugin, there is a possibility
|
|
// that the hash value of plugin's identity is not the same as the node_plugin_join
|
|
// so we need to force gc the plugin by node_plugin_join again
|
|
if err := c.forceGCPluginByNodePluginJoin(node_plugin_join); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
},
|
|
)
|
|
}
|