@@ -18,7 +18,7 @@ type WorkerController struct {
1818 quotaController framework.QuotaController
1919
2020 mu sync.RWMutex
21- workers map [string ]bool // worker UID -> exists
21+ workers map [string ]* api. WorkerInfo
2222 workerWatchStop chan struct {}
2323 workerWatchStopOnce sync.Once
2424}
@@ -31,7 +31,7 @@ func NewWorkerController(
3131 mode : mode ,
3232 backend : backend ,
3333 quotaController : quotaController ,
34- workers : make (map [string ]bool ),
34+ workers : make (map [string ]* api. WorkerInfo , 16 ),
3535 workerWatchStop : make (chan struct {}),
3636 }
3737}
@@ -63,9 +63,8 @@ func (w *WorkerController) Start() error {
6363 }
6464 // Update worker cache
6565 w .mu .Lock ()
66- w .workers = make (map [string ]bool )
67- for _ , workerUID := range workers {
68- w .workers [workerUID ] = true
66+ for _ , worker := range workers {
67+ w .workers [worker .WorkerUID ] = worker
6968 }
7069 w .mu .Unlock ()
7170 klog .V (4 ).Infof ("Updated worker list: %d workers" , len (workers ))
@@ -271,50 +270,15 @@ func (w *WorkerController) GetWorkerMetrics() (map[string]map[string]map[string]
271270 return result , nil
272271}
273272
274- func (w * WorkerController ) ListWorkers () ([]string , error ) {
275- // First check cache (updated by ListAndWatchWorkers)
273+ func (w * WorkerController ) ListWorkers () ([]* api.WorkerInfo , error ) {
276274 w .mu .RLock ()
277- cachedWorkers := make ([]string , 0 , len (w .workers ))
278- for workerUID := range w .workers {
279- cachedWorkers = append (cachedWorkers , workerUID )
280- }
281- w .mu .RUnlock ()
282-
283- // If cache has workers, return them
284- if len (cachedWorkers ) > 0 {
285- return cachedWorkers , nil
286- }
287-
288- // If cache is empty, directly query device allocations to get immediate results
289- // This ensures we hit the key logic path and return accurate results
290- allocations , err := w .deviceController .GetDeviceAllocations ("" )
291- if err != nil {
292- return cachedWorkers , err
293- }
294-
295- // Extract unique worker UIDs from allocations
296- workerSet := make (map [string ]bool )
297- for _ , allocation := range allocations {
298- workerUID := allocation .WorkerInfo .WorkerUID
299- if workerUID == "" {
300- workerUID = allocation .WorkerInfo .PodUID
301- }
302- if workerUID != "" {
303- workerSet [workerUID ] = true
275+ defer w .mu .RUnlock ()
276+ workerSnapshot := make ([]* api.WorkerInfo , 0 , len (w .workers ))
277+ for _ , worker := range w .workers {
278+ if worker .Deleted {
279+ continue
304280 }
281+ workerSnapshot = append (workerSnapshot , worker )
305282 }
306-
307- // Update cache with discovered workers
308- w .mu .Lock ()
309- for workerUID := range workerSet {
310- w .workers [workerUID ] = true
311- }
312- w .mu .Unlock ()
313-
314- // Return list of workers
315- workers := make ([]string , 0 , len (workerSet ))
316- for workerUID := range workerSet {
317- workers = append (workers , workerUID )
318- }
319- return workers , nil
283+ return workerSnapshot , nil
320284}
0 commit comments