mirror of
https://github.com/langgenius/dify-plugin-daemon.git
synced 2026-07-22 17:56:00 -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>
299 lines
6.9 KiB
Go
299 lines
6.9 KiB
Go
package cluster
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/langgenius/dify-plugin-daemon/internal/types/app"
|
|
"github.com/langgenius/dify-plugin-daemon/pkg/utils/mapping"
|
|
)
|
|
|
|
type Cluster struct {
|
|
// id is the unique id of the cluster
|
|
id string
|
|
|
|
// i_am_master is the flag to indicate whether the current node is the master node
|
|
iAmMaster bool
|
|
|
|
// main http port of the current node
|
|
port uint16
|
|
|
|
// plugins stores all the plugin life time of the current node
|
|
plugins mapping.Map[string, *pluginLifeTime]
|
|
pluginLock sync.RWMutex
|
|
|
|
// nodes stores all the nodes of the cluster
|
|
nodes mapping.Map[string, node]
|
|
|
|
// signals for waiting for the cluster to stop
|
|
stopChan chan bool
|
|
stopped int32
|
|
|
|
isInAutoGcNodes int32
|
|
isInAutoGcPlugins int32
|
|
|
|
// channels to notify cluster event
|
|
notifyBecomeMasterChan chan bool
|
|
notifyMasterGcChan chan bool
|
|
notifyMasterGcCompletedChan chan bool
|
|
notifyVotingChan chan bool
|
|
notifyVotingCompletedChan chan bool
|
|
notifyPluginScheduleChan chan bool
|
|
notifyPluginScheduleCompletedChan chan bool
|
|
notifyNodeUpdateChan chan bool
|
|
notifyNodeUpdateCompletedChan chan bool
|
|
notifyClusterStoppedChan chan bool
|
|
|
|
showLog bool
|
|
|
|
masterGcInterval time.Duration
|
|
masterLockingInterval time.Duration
|
|
masterLockExpiredTime time.Duration
|
|
nodeVoteInterval time.Duration
|
|
nodeDisconnectedTimeout time.Duration
|
|
updateNodeStatusInterval time.Duration
|
|
pluginSchedulerInterval time.Duration
|
|
pluginSchedulerTickerInterval time.Duration
|
|
pluginDeactivatedTimeout time.Duration
|
|
}
|
|
|
|
func NewCluster(config *app.Config) *Cluster {
|
|
return &Cluster{
|
|
id: uuid.New().String(),
|
|
port: uint16(config.ServerPort),
|
|
stopChan: make(chan bool),
|
|
showLog: config.DisplayClusterLog,
|
|
masterGcInterval: MASTER_GC_INTERVAL,
|
|
masterLockingInterval: MASTER_LOCKING_INTERVAL,
|
|
masterLockExpiredTime: MASTER_LOCK_EXPIRED_TIME,
|
|
nodeVoteInterval: NODE_VOTE_INTERVAL,
|
|
nodeDisconnectedTimeout: NODE_DISCONNECTED_TIMEOUT,
|
|
updateNodeStatusInterval: UPDATE_NODE_STATUS_INTERVAL,
|
|
pluginSchedulerInterval: PLUGIN_SCHEDULER_INTERVAL,
|
|
pluginSchedulerTickerInterval: PLUGIN_SCHEDULER_TICKER_INTERVAL,
|
|
pluginDeactivatedTimeout: PLUGIN_DEACTIVATED_TIMEOUT,
|
|
|
|
notifyBecomeMasterChan: make(chan bool),
|
|
notifyMasterGcChan: make(chan bool),
|
|
notifyMasterGcCompletedChan: make(chan bool),
|
|
notifyVotingChan: make(chan bool),
|
|
notifyVotingCompletedChan: make(chan bool),
|
|
notifyPluginScheduleChan: make(chan bool),
|
|
notifyPluginScheduleCompletedChan: make(chan bool),
|
|
notifyNodeUpdateChan: make(chan bool),
|
|
notifyNodeUpdateCompletedChan: make(chan bool),
|
|
notifyClusterStoppedChan: make(chan bool),
|
|
}
|
|
}
|
|
|
|
func (c *Cluster) Launch() {
|
|
go c.clusterLifetime()
|
|
}
|
|
|
|
func (c *Cluster) Close() error {
|
|
if atomic.CompareAndSwapInt32(&c.stopped, 0, 1) {
|
|
close(c.stopChan)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Cluster) ID() string {
|
|
return c.id
|
|
}
|
|
|
|
// trigger for master event
|
|
func (c *Cluster) notifyBecomeMaster() {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return
|
|
}
|
|
|
|
select {
|
|
case c.notifyBecomeMasterChan <- true:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// receive the master event
|
|
func (c *Cluster) NotifyBecomeMaster() <-chan bool {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return nil
|
|
}
|
|
return c.notifyBecomeMasterChan
|
|
}
|
|
|
|
// trigger for master gc event
|
|
func (c *Cluster) notifyMasterGC() {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return
|
|
}
|
|
|
|
select {
|
|
case c.notifyMasterGcChan <- true:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// trigger for master gc completed event
|
|
func (c *Cluster) notifyMasterGCCompleted() {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return
|
|
}
|
|
|
|
select {
|
|
case c.notifyMasterGcCompletedChan <- true:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// trigger for voting event
|
|
func (c *Cluster) notifyVoting() {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return
|
|
}
|
|
|
|
select {
|
|
case c.notifyVotingChan <- true:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// trigger for voting completed event
|
|
func (c *Cluster) notifyVotingCompleted() {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return
|
|
}
|
|
|
|
select {
|
|
case c.notifyVotingCompletedChan <- true:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// trigger for plugin schedule event
|
|
func (c *Cluster) notifyPluginSchedule() {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return
|
|
}
|
|
|
|
select {
|
|
case c.notifyPluginScheduleChan <- true:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// trigger for plugin schedule completed event
|
|
func (c *Cluster) notifyPluginScheduleCompleted() {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return
|
|
}
|
|
|
|
select {
|
|
case c.notifyPluginScheduleCompletedChan <- true:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// trigger for node update event
|
|
func (c *Cluster) notifyNodeUpdate() {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return
|
|
}
|
|
|
|
select {
|
|
case c.notifyNodeUpdateChan <- true:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// trigger for node update completed event
|
|
func (c *Cluster) notifyNodeUpdateCompleted() {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return
|
|
}
|
|
|
|
select {
|
|
case c.notifyNodeUpdateCompletedChan <- true:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// trigger for cluster stopped event
|
|
func (c *Cluster) notifyClusterStopped() {
|
|
select {
|
|
case c.notifyClusterStoppedChan <- true:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// receive the master gc event
|
|
func (c *Cluster) NotifyMasterGC() <-chan bool {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return nil
|
|
}
|
|
return c.notifyMasterGcChan
|
|
}
|
|
|
|
// receive the master gc completed event
|
|
func (c *Cluster) NotifyMasterGCCompleted() <-chan bool {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return nil
|
|
}
|
|
return c.notifyMasterGcCompletedChan
|
|
}
|
|
|
|
// receive the voting event
|
|
func (c *Cluster) NotifyVoting() <-chan bool {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return nil
|
|
}
|
|
return c.notifyVotingChan
|
|
}
|
|
|
|
// receive the voting completed event
|
|
func (c *Cluster) NotifyVotingCompleted() <-chan bool {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return nil
|
|
}
|
|
return c.notifyVotingCompletedChan
|
|
}
|
|
|
|
// receive the plugin schedule event
|
|
func (c *Cluster) NotifyPluginSchedule() <-chan bool {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return nil
|
|
}
|
|
return c.notifyPluginScheduleChan
|
|
}
|
|
|
|
// receive the plugin schedule completed event
|
|
func (c *Cluster) NotifyPluginScheduleCompleted() <-chan bool {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return nil
|
|
}
|
|
return c.notifyPluginScheduleCompletedChan
|
|
}
|
|
|
|
// receive the node update event
|
|
func (c *Cluster) NotifyNodeUpdate() <-chan bool {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return nil
|
|
}
|
|
return c.notifyNodeUpdateChan
|
|
}
|
|
|
|
// receive the node update completed event
|
|
func (c *Cluster) NotifyNodeUpdateCompleted() <-chan bool {
|
|
if atomic.LoadInt32(&c.stopped) == 1 {
|
|
return nil
|
|
}
|
|
return c.notifyNodeUpdateCompletedChan
|
|
}
|
|
|
|
// receive the cluster stopped event
|
|
func (c *Cluster) NotifyClusterStopped() <-chan bool {
|
|
return c.notifyClusterStoppedChan
|
|
}
|