-
Notifications
You must be signed in to change notification settings - Fork 1
feat: added GetChangedStrategies API #10
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
|
|
||
| "github.com/Gthulhu/plugin/models" | ||
| reg "github.com/Gthulhu/plugin/plugin/internal/registry" | ||
| "github.com/Gthulhu/plugin/plugin/util" | ||
| ) | ||
|
|
||
| func init() { | ||
|
|
@@ -65,8 +66,11 @@ type GthulhuPlugin struct { | |
| minVruntime uint64 | ||
|
|
||
| // Strategy map for PID-based scheduling strategies | ||
| strategyMap map[int32]SchedulingStrategy | ||
| strategyMu sync.RWMutex | ||
| oldStrategyMap map[int32]util.SchedulingStrategy | ||
| strategyMap map[int32]util.SchedulingStrategy | ||
| newStrategy []util.SchedulingStrategy | ||
| removedStrategy []util.SchedulingStrategy | ||
| strategyMu sync.RWMutex | ||
|
|
||
| // JWT client for API authentication | ||
| jwtClient *JWTClient | ||
|
|
@@ -82,7 +86,7 @@ func NewGthulhuPlugin(sliceNsDefault, sliceNsMin uint64) *GthulhuPlugin { | |
| taskPool: make([]Task, taskPoolSize), | ||
| taskPoolCount: 0, | ||
| minVruntime: 0, | ||
| strategyMap: make(map[int32]SchedulingStrategy), | ||
| strategyMap: make(map[int32]util.SchedulingStrategy), | ||
| } | ||
|
|
||
| // Override defaults if provided | ||
|
|
@@ -348,24 +352,63 @@ func (g *GthulhuPlugin) GetSchedulerConfig() (uint64, uint64) { | |
| } | ||
|
|
||
| // FetchSchedulingStrategies fetches scheduling strategies from the API server | ||
| func (g *GthulhuPlugin) FetchSchedulingStrategies(apiUrl string) ([]SchedulingStrategy, error) { | ||
| func (g *GthulhuPlugin) FetchSchedulingStrategies(apiUrl string) ([]util.SchedulingStrategy, error) { | ||
| if g.jwtClient == nil { | ||
| return nil, nil // Silently skip if JWT client not initialized | ||
| } | ||
| return fetchSchedulingStrategies(g.jwtClient, apiUrl) | ||
| } | ||
|
|
||
| // UpdateStrategyMap updates the strategy map from a slice of strategies | ||
| func (g *GthulhuPlugin) UpdateStrategyMap(strategies []SchedulingStrategy) { | ||
| func (g *GthulhuPlugin) UpdateStrategyMap(strategies []util.SchedulingStrategy) { | ||
| // Create a new map to avoid concurrent access issues | ||
| newMap := make(map[int32]SchedulingStrategy) | ||
| newMap := make(map[int32]util.SchedulingStrategy) | ||
|
|
||
| for _, strategy := range strategies { | ||
| newMap[int32(strategy.PID)] = strategy | ||
| } | ||
|
|
||
| // Replace the old map with the new one | ||
| g.strategyMu.Lock() | ||
| g.oldStrategyMap = g.strategyMap | ||
| g.strategyMap = newMap | ||
| changed, removed := g.caculateChangedStrategies() | ||
| g.newStrategy = append(g.newStrategy, changed...) | ||
| g.removedStrategy = append(g.removedStrategy, removed...) | ||
| g.strategyMu.Unlock() | ||
| } | ||
|
|
||
| // Campare g.oldStrategyMap and g.strategyMap and return the list of SchedulingStrategy that have changed strategies | ||
| func (g *GthulhuPlugin) caculateChangedStrategies() ([]util.SchedulingStrategy, []util.SchedulingStrategy) { | ||
|
Comment on lines
+381
to
+382
|
||
| changed := []util.SchedulingStrategy{} | ||
| removed := []util.SchedulingStrategy{} | ||
|
|
||
| // Check for removed strategies | ||
| for pid, oldStrategy := range g.oldStrategyMap { | ||
| _, exists := g.strategyMap[pid] | ||
| if !exists { | ||
| removed = append(removed, oldStrategy) | ||
| } | ||
| } | ||
|
|
||
| // Check for changed or new strategies | ||
| for pid, newStrategy := range g.strategyMap { | ||
| oldStrategy, exists := g.oldStrategyMap[pid] | ||
| if !exists || oldStrategy != newStrategy { | ||
| changed = append(changed, newStrategy) | ||
| } | ||
| } | ||
| return changed, removed | ||
| } | ||
|
|
||
| // Campare g.oldStrategyMap and g.strategyMap and return the list of SchedulingStrategy that have changed strategies | ||
| func (g *GthulhuPlugin) GetChangedStrategies() ([]util.SchedulingStrategy, []util.SchedulingStrategy) { | ||
| changed := []util.SchedulingStrategy{} | ||
|
Comment on lines
+405
to
+406
|
||
| g.strategyMu.RLock() | ||
| defer g.strategyMu.RUnlock() | ||
|
|
||
| // copy g.newStrategy to changed and clear g.newStrategy | ||
| changed = append(changed, g.newStrategy...) | ||
| g.newStrategy = []util.SchedulingStrategy{} | ||
| return changed, nil | ||
|
Comment on lines
+410
to
+413
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ import ( | |||||||||
| "sync" | ||||||||||
|
|
||||||||||
| "github.com/Gthulhu/plugin/models" | ||||||||||
| "github.com/Gthulhu/plugin/plugin/util" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| type Sched interface { | ||||||||||
|
|
@@ -27,6 +28,8 @@ type CustomScheduler interface { | |||||||||
| GetPoolCount() uint64 | ||||||||||
| // SendMetrics sends custom metrics to the monitoring system | ||||||||||
| SendMetrics(interface{}) | ||||||||||
| // GetChangedStrategies returns the list of scheduling strategies that have changed since the last call | ||||||||||
|
||||||||||
| // GetChangedStrategies returns the list of scheduling strategies that have changed since the last call | |
| // GetChangedStrategies returns the scheduling strategies that have changed since the last call. | |
| // The first slice contains strategies that were added or updated. | |
| // The second slice contains strategies that were removed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UpdateStrategyMap accumulates both g.newStrategy and g.removedStrategy, but GetChangedStrategies currently returns only the changed slice and always returns nil for the removed slice, and never clears g.removedStrategy. This will both break the intended API and can cause unbounded memory growth. Return (changed, removed) and clear both buffers once they’ve been read.