-
Notifications
You must be signed in to change notification settings - Fork 286
feat(plugin): Added plugin-ready check endpoints and optimized local plugin startup logic #600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NieRonghua
wants to merge
2
commits into
langgenius:main
Choose a base branch
from
NieRonghua:feat-readness-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| package controlpanel | ||
|
|
||
| import ( | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities" | ||
| ) | ||
|
|
||
| type LocalReadinessSnapshot struct { | ||
| // ⭐ 核心:readiness 只基于初始插件状态 | ||
| // Pod 一旦 ready,永远不会因为运行时新增插件而变为 not ready | ||
| Ready bool | ||
|
|
||
| // 初始插件状态(Pod启动时锁定,之后永不改变) | ||
| InitialPluginsReady bool | ||
| InitialExpected int | ||
| InitialRunning int | ||
| InitialMissing []string | ||
| InitialFailed []string | ||
|
|
||
| // 运行时新增插件状态(与readiness无关,仅供监控) | ||
| RuntimePluginsLoading int | ||
| RuntimeMissing []string | ||
|
|
||
| // 全量统计(包含初始+运行时) | ||
| Expected int | ||
| Running int | ||
| Missing []string | ||
| Failed []string | ||
| UpdatedAt time.Time | ||
| Platform string | ||
| Installed int | ||
| Ignored int | ||
| MaxRetries int32 | ||
| } | ||
|
|
||
| type initialPluginSet struct { | ||
| lock sync.RWMutex | ||
| ids map[string]bool // plugin id → true | ||
| ready bool // 是否已锁定 | ||
| } | ||
|
|
||
| var initialPlugins = &initialPluginSet{ | ||
| ids: make(map[string]bool), | ||
| } | ||
|
|
||
| func (c *ControlPanel) LocalReadiness() (LocalReadinessSnapshot, bool) { | ||
| ptr := c.localReadinessSnapshot.Load() | ||
| if ptr == nil { | ||
| return LocalReadinessSnapshot{}, false | ||
| } | ||
| return *ptr, true | ||
| } | ||
|
|
||
| func (c *ControlPanel) updateLocalReadinessSnapshot( | ||
| installed []plugin_entities.PluginUniqueIdentifier, | ||
| ) { | ||
| now := time.Now() | ||
|
|
||
| expected := make([]plugin_entities.PluginUniqueIdentifier, 0, len(installed)) | ||
| ignored := 0 | ||
| for _, id := range installed { | ||
| if _, ok := c.localPluginWatchIgnoreList.Load(id); ok { | ||
| ignored++ | ||
| continue | ||
| } | ||
| expected = append(expected, id) | ||
| } | ||
|
|
||
| // 计算全量插件状态 | ||
| missing := make([]string, 0) | ||
| failed := make([]string, 0) | ||
| running := 0 | ||
| for _, id := range expected { | ||
| if c.localPluginRuntimes.Exists(id) { | ||
| running++ | ||
| continue | ||
| } | ||
|
|
||
| if retry, ok := c.localPluginFailsRecord.Load(id); ok && retry.RetryCount >= c.config.PluginLocalMaxRetryCount { | ||
| failed = append(failed, id.String()) | ||
| continue | ||
| } | ||
| missing = append(missing, id.String()) | ||
| } | ||
|
|
||
| // 计算初始插件的状态 | ||
| initialMissing := make([]string, 0) | ||
| initialFailed := make([]string, 0) | ||
| initialRunning := 0 | ||
| initialExpected := 0 | ||
|
|
||
| isInitialReady := c.isInitialPluginsReady(expected, &initialExpected, &initialRunning, &initialMissing, &initialFailed) | ||
|
|
||
| // 计算运行时新增插件 | ||
| runtimeMissing := make([]string, 0) | ||
| runtimeLoading := 0 | ||
|
|
||
| initialSet := c.getInitialPluginSet() | ||
| for _, id := range expected { | ||
| idStr := id.String() | ||
| if !initialSet[idStr] { | ||
| // 这是运行时新增的插件 | ||
| if !c.localPluginRuntimes.Exists(id) { | ||
| if retry, ok := c.localPluginFailsRecord.Load(id); !ok || retry.RetryCount < c.config.PluginLocalMaxRetryCount { | ||
| runtimeMissing = append(runtimeMissing, idStr) | ||
| runtimeLoading++ | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 🔑 关键:readiness ONLY depends on initial plugins | ||
| // Once ready, it will never become not ready due to runtime plugin additions | ||
| snapshot := &LocalReadinessSnapshot{ | ||
| Ready: isInitialReady, | ||
| InitialPluginsReady: isInitialReady, | ||
| InitialExpected: initialExpected, | ||
| InitialRunning: initialRunning, | ||
| InitialMissing: initialMissing, | ||
| InitialFailed: initialFailed, | ||
| RuntimePluginsLoading: runtimeLoading, | ||
| RuntimeMissing: runtimeMissing, | ||
| Expected: len(expected), | ||
| Installed: len(installed), | ||
| Ignored: ignored, | ||
| Running: running, | ||
| Missing: missing, | ||
| Failed: failed, | ||
| UpdatedAt: now, | ||
| Platform: string(c.config.Platform), | ||
| MaxRetries: c.config.PluginLocalMaxRetryCount, | ||
| } | ||
| c.localReadinessSnapshot.Store(snapshot) | ||
| } | ||
|
|
||
| // isInitialPluginsReady 检查初始插件是否全部启动完成 | ||
| func (c *ControlPanel) isInitialPluginsReady( | ||
| current []plugin_entities.PluginUniqueIdentifier, | ||
| initialExpected *int, | ||
| initialRunning *int, | ||
| initialMissing *[]string, | ||
| initialFailed *[]string, | ||
| ) bool { | ||
NieRonghua marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| initialSet := c.getInitialPluginSet() | ||
| if len(initialSet) == 0 && len(current) > 0 { | ||
| // 首次启动,锁定初始插件集合 | ||
| c.lockInitialPlugins(current) | ||
| initialSet = c.getInitialPluginSet() | ||
| } | ||
|
|
||
| missingList := make([]string, 0) | ||
| failedList := make([]string, 0) | ||
| running := 0 | ||
| expected := 0 | ||
|
|
||
| for _, id := range current { | ||
| idStr := id.String() | ||
| if !initialSet[idStr] { | ||
| continue | ||
| } | ||
|
|
||
| expected++ | ||
| if c.localPluginRuntimes.Exists(id) { | ||
| running++ | ||
| continue | ||
| } | ||
|
|
||
| if retry, ok := c.localPluginFailsRecord.Load(id); ok && retry.RetryCount >= c.config.PluginLocalMaxRetryCount { | ||
| failedList = append(failedList, idStr) | ||
| continue | ||
| } | ||
| missingList = append(missingList, idStr) | ||
| } | ||
|
|
||
| *initialExpected = expected | ||
| *initialRunning = running | ||
| *initialMissing = missingList | ||
| *initialFailed = failedList | ||
|
|
||
| return len(missingList) == 0 | ||
| } | ||
|
|
||
| // lockInitialPlugins 锁定初始插件集合(仅在首次调用时) | ||
| func (c *ControlPanel) lockInitialPlugins( | ||
| plugins []plugin_entities.PluginUniqueIdentifier, | ||
| ) { | ||
| initialPlugins.lock.Lock() | ||
| defer initialPlugins.lock.Unlock() | ||
|
|
||
| if initialPlugins.ready { | ||
| return | ||
| } | ||
|
|
||
| for _, id := range plugins { | ||
| initialPlugins.ids[id.String()] = true | ||
| } | ||
| initialPlugins.ready = true | ||
| } | ||
|
|
||
| // getInitialPluginSet 获取初始插件集合(只读) | ||
| func (c *ControlPanel) getInitialPluginSet() map[string]bool { | ||
| initialPlugins.lock.RLock() | ||
| defer initialPlugins.lock.RUnlock() | ||
|
|
||
| result := make(map[string]bool) | ||
| for k, v := range initialPlugins.ids { | ||
| result[k] = v | ||
| } | ||
| return result | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package plugin_manager | ||
|
|
||
| import ( | ||
| controlpanel "github.com/langgenius/dify-plugin-daemon/internal/core/control_panel" | ||
| "github.com/langgenius/dify-plugin-daemon/internal/types/app" | ||
| ) | ||
|
|
||
| type ReadinessReport struct { | ||
| Ready bool | ||
| Reason string | ||
| Plugins *controlpanel.LocalReadinessSnapshot | ||
| } | ||
|
|
||
| func (p *PluginManager) Readiness() ReadinessReport { | ||
| if p == nil || p.config == nil { | ||
| return ReadinessReport{Ready: false, Reason: "manager_not_initialized"} | ||
| } | ||
|
|
||
| if p.config.Platform != app.PLATFORM_LOCAL { | ||
| return ReadinessReport{Ready: true, Reason: "non_local_platform"} | ||
| } | ||
|
|
||
| snapshot, ok := p.controlPanel.LocalReadiness() | ||
| if !ok { | ||
| return ReadinessReport{Ready: false, Reason: "plugin_monitor_not_ready"} | ||
| } | ||
|
|
||
| if snapshot.Ready { | ||
| return ReadinessReport{Ready: true, Reason: "plugins_ready", Plugins: &snapshot} | ||
| } | ||
| if len(snapshot.Failed) > 0 { | ||
| return ReadinessReport{Ready: false, Reason: "plugins_failed", Plugins: &snapshot} | ||
| } | ||
| return ReadinessReport{Ready: false, Reason: "plugins_starting", Plugins: &snapshot} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package controllers | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager" | ||
| "github.com/langgenius/dify-plugin-daemon/internal/types/app" | ||
| ) | ||
|
|
||
| func ReadyCheck(appConfig *app.Config) gin.HandlerFunc { | ||
NieRonghua marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return func(c *gin.Context) { | ||
| _ = appConfig | ||
| report := plugin_manager.Manager().Readiness() | ||
| if report.Ready { | ||
| c.JSON(http.StatusOK, gin.H{ | ||
| "status": "ok", | ||
| "ready": true, | ||
| "reason": report.Reason, | ||
| "detail": report.Plugins, | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusServiceUnavailable, gin.H{ | ||
| "status": "unready", | ||
| "ready": false, | ||
| "reason": report.Reason, | ||
| "detail": report.Plugins, | ||
| }) | ||
NieRonghua marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.