compaction merges HookRun tasks for the same hook. It iterates through the list once, making it an O(N) operation. DEV WARNING! Do not use HookMetadataAccessor here. Use only *Accessor interfaces because this method is used from addon-operator. TODO: consider compaction only for tasks with one compa
(compactionIDs map[string]struct{})
| 397 | // DEV WARNING! Do not use HookMetadataAccessor here. Use only *Accessor interfaces because this method is used from addon-operator. |
| 398 | // TODO: consider compaction only for tasks with one compaction ID (to not affect good tasks) |
| 399 | func (q *TaskQueue) compaction(compactionIDs map[string]struct{}) { |
| 400 | if q.storage.Length() < 2 { |
| 401 | return |
| 402 | } |
| 403 | |
| 404 | // Get objects from pools |
| 405 | hookGroups := q.getHookGroupsMap() |
| 406 | defer func() { |
| 407 | // Return all compaction groups to pool |
| 408 | for _, group := range hookGroups { |
| 409 | q.putCompactionGroup(group) |
| 410 | } |
| 411 | q.putHookGroupsMap(hookGroups) |
| 412 | }() |
| 413 | |
| 414 | // First pass: identify groups and calculate sizes |
| 415 | q.storage.Iterate(func(e *list.Element[task.Task]) { |
| 416 | t := e.Value |
| 417 | taskType := t.GetType() |
| 418 | |
| 419 | if _, ok := q.compactableTypes[taskType]; !ok { |
| 420 | return |
| 421 | } |
| 422 | |
| 423 | if len(compactionIDs) > 0 { |
| 424 | if _, ok := compactionIDs[t.GetCompactionID()]; !ok { |
| 425 | return |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | metadata := t.GetMetadata() |
| 430 | if isNil(metadata) || t.IsProcessing() { |
| 431 | return |
| 432 | } |
| 433 | |
| 434 | hookNameAcessor, ok := metadata.(task_metadata.HookNameAccessor) |
| 435 | if !ok { |
| 436 | return |
| 437 | } |
| 438 | bindingContextAcessor, ok := metadata.(task_metadata.BindingContextAccessor) |
| 439 | if !ok { |
| 440 | return |
| 441 | } |
| 442 | monitorIDsAcessor, ok := metadata.(task_metadata.MonitorIDAccessor) |
| 443 | if !ok { |
| 444 | return |
| 445 | } |
| 446 | |
| 447 | hookName := hookNameAcessor.GetHookName() |
| 448 | bindingContext := bindingContextAcessor.GetBindingContext() |
| 449 | monitorIDs := monitorIDsAcessor.GetMonitorIDs() |
| 450 | |
| 451 | if group, exists := hookGroups[hookName]; exists { |
| 452 | // Add to existing group |
| 453 | group.elementsToMerge = append(group.elementsToMerge, e) |
| 454 | group.totalContexts += len(bindingContext) |
| 455 | group.totalMonitorIDs += len(monitorIDs) |
| 456 | } else { |