|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.wlm.tracker; |
| 10 | + |
| 11 | +import org.opensearch.search.ResourceType; |
| 12 | +import org.opensearch.tasks.Task; |
| 13 | +import org.opensearch.tasks.TaskResourceTrackingService; |
| 14 | +import org.opensearch.wlm.QueryGroupLevelResourceUsageView; |
| 15 | +import org.opensearch.wlm.QueryGroupTask; |
| 16 | + |
| 17 | +import java.util.EnumMap; |
| 18 | +import java.util.EnumSet; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +/** |
| 25 | + * This class tracks resource usage per QueryGroup |
| 26 | + */ |
| 27 | +public class QueryGroupResourceUsageTrackerService { |
| 28 | + |
| 29 | + public static final EnumSet<ResourceType> TRACKED_RESOURCES = EnumSet.allOf(ResourceType.class); |
| 30 | + private final TaskResourceTrackingService taskResourceTrackingService; |
| 31 | + |
| 32 | + /** |
| 33 | + * QueryGroupResourceTrackerService constructor |
| 34 | + * |
| 35 | + * @param taskResourceTrackingService Service that helps track resource usage of tasks running on a node. |
| 36 | + */ |
| 37 | + public QueryGroupResourceUsageTrackerService(TaskResourceTrackingService taskResourceTrackingService) { |
| 38 | + this.taskResourceTrackingService = taskResourceTrackingService; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructs a map of QueryGroupLevelResourceUsageView instances for each QueryGroup. |
| 43 | + * |
| 44 | + * @return Map of QueryGroup views |
| 45 | + */ |
| 46 | + public Map<String, QueryGroupLevelResourceUsageView> constructQueryGroupLevelUsageViews() { |
| 47 | + final Map<String, List<Task>> tasksByQueryGroup = getTasksGroupedByQueryGroup(); |
| 48 | + final Map<String, QueryGroupLevelResourceUsageView> queryGroupViews = new HashMap<>(); |
| 49 | + |
| 50 | + // Iterate over each QueryGroup entry |
| 51 | + for (Map.Entry<String, List<Task>> queryGroupEntry : tasksByQueryGroup.entrySet()) { |
| 52 | + // Compute the QueryGroup usage |
| 53 | + final EnumMap<ResourceType, Long> queryGroupUsage = new EnumMap<>(ResourceType.class); |
| 54 | + for (ResourceType resourceType : TRACKED_RESOURCES) { |
| 55 | + long queryGroupResourceUsage = 0; |
| 56 | + for (Task task : queryGroupEntry.getValue()) { |
| 57 | + queryGroupResourceUsage += resourceType.getResourceUsage(task); |
| 58 | + } |
| 59 | + queryGroupUsage.put(resourceType, queryGroupResourceUsage); |
| 60 | + } |
| 61 | + |
| 62 | + // Add to the QueryGroup View |
| 63 | + queryGroupViews.put( |
| 64 | + queryGroupEntry.getKey(), |
| 65 | + new QueryGroupLevelResourceUsageView(queryGroupUsage, queryGroupEntry.getValue()) |
| 66 | + ); |
| 67 | + } |
| 68 | + return queryGroupViews; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Groups tasks by their associated QueryGroup. |
| 73 | + * |
| 74 | + * @return Map of tasks grouped by QueryGroup |
| 75 | + */ |
| 76 | + private Map<String, List<Task>> getTasksGroupedByQueryGroup() { |
| 77 | + return taskResourceTrackingService.getResourceAwareTasks() |
| 78 | + .values() |
| 79 | + .stream() |
| 80 | + .filter(QueryGroupTask.class::isInstance) |
| 81 | + .map(QueryGroupTask.class::cast) |
| 82 | + .collect(Collectors.groupingBy(QueryGroupTask::getQueryGroupId, Collectors.mapping(task -> (Task) task, Collectors.toList()))); |
| 83 | + } |
| 84 | +} |
0 commit comments