@@ -175,7 +175,6 @@ func NewDynamicController(
175
175
// pass version and pod id from env
176
176
}
177
177
178
- dc .ensureWeightedQueue (defaultQueueWeight )
179
178
dc .setGVRWeight (schema.GroupVersionResource {}, defaultQueueWeight )
180
179
181
180
return dc
@@ -198,6 +197,8 @@ func (dc *DynamicController) getGVRWeight(gvr schema.GroupVersionResource) int {
198
197
// setGVRWeight sets the weight for a given GroupVersionResource (GVR) and updates the
199
198
// corresponding weighted queue.
200
199
func (dc * DynamicController ) setGVRWeight (gvr schema.GroupVersionResource , weight int ) {
200
+ dc .ensureWeightedQueue (weight )
201
+
201
202
dc .mu .Lock ()
202
203
defer dc .mu .Unlock ()
203
204
@@ -215,6 +216,11 @@ func (dc *DynamicController) deleteGVRWeight(gvr schema.GroupVersionResource) {
215
216
weight := dc .gvrWeights [gvr ]
216
217
wq := dc .weightedQueues [weight ]
217
218
219
+ if weight == defaultQueueWeight {
220
+ dc .log .Error (nil , "cannot delete default queue" , "weight" , weight )
221
+ return
222
+ }
223
+
218
224
delete (dc .gvrWeights , gvr )
219
225
delete (wq .gvrSet , gvr )
220
226
if len (wq .gvrSet ) < 1 {
@@ -295,9 +301,9 @@ func (dc *DynamicController) WaitForInformersSync(stopCh <-chan struct{}) bool {
295
301
// shutdownQueues shuts down all the weighted queues managed by the DynamicController.
296
302
func (dc * DynamicController ) shutdownQueues () {
297
303
dc .log .Info ("Shutting down dynamic controller queues" )
298
- for key := range dc .weightedQueues {
304
+ for key , wq := range dc .weightedQueues {
299
305
dc .log .Info ("Shutting down weighted queue" , "key" , key )
300
- dc . weightedQueues [ key ] .queue .ShutDown ()
306
+ wq .queue .ShutDown ()
301
307
}
302
308
}
303
309
@@ -335,13 +341,18 @@ func (dc *DynamicController) worker(ctx context.Context) {
335
341
// A queue with a weight of 200 will be selected twice as often assuming an even
336
342
// number of events are distributed between the queues
337
343
func (dc * DynamicController ) selectQueueByWeight () * WeightedQueue {
344
+ startTime := time .Now ()
345
+
338
346
var (
339
347
totalWeight int = 0
340
348
maxWeight int = 0
341
349
selectedQueue * WeightedQueue
342
350
activeQueues = make ([]* WeightedQueue , 0 , len (dc .weightedQueues ))
343
351
)
344
352
353
+ dc .mu .RLock ()
354
+ defer dc .mu .RUnlock ()
355
+
345
356
for _ , wq := range dc .weightedQueues {
346
357
if wq .queue .Len () > 0 {
347
358
totalWeight += wq .weight
@@ -365,6 +376,9 @@ func (dc *DynamicController) selectQueueByWeight() *WeightedQueue {
365
376
}
366
377
}
367
378
379
+ duration := time .Since (startTime )
380
+ dc .log .V (1 ).Info ("Time to select queue" , "duration" , duration .String (), "weight" , selectedQueue .weight )
381
+
368
382
return selectedQueue
369
383
}
370
384
@@ -374,7 +388,7 @@ func (dc *DynamicController) processNextWorkItem(ctx context.Context) bool {
374
388
375
389
obj , shutdown := weightedQueue .queue .Get ()
376
390
if shutdown {
377
- return false
391
+ return true
378
392
}
379
393
defer weightedQueue .queue .Done (obj )
380
394
@@ -570,9 +584,6 @@ func (dc *DynamicController) StartServingGVK(ctx context.Context, gvr schema.Gro
570
584
dc .log .V (1 ).Info ("Registering new GVK" , "gvr" , gvr )
571
585
572
586
// Set the weight for the GVR and ensure the queue exists for that weight class
573
- if ok := dc .ensureWeightedQueue (queueWeight ); ! ok {
574
- return fmt .Errorf ("failed to create or get weighted queue with weight: %d" , queueWeight )
575
- }
576
587
dc .setGVRWeight (gvr , queueWeight )
577
588
578
589
_ , exists := dc .informers .Load (gvr )
0 commit comments