|
| 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.action.search.SearchTask; |
| 12 | +import org.opensearch.common.settings.ClusterSettings; |
| 13 | +import org.opensearch.common.settings.Settings; |
| 14 | +import org.opensearch.common.util.concurrent.ThreadContext; |
| 15 | +import org.opensearch.core.tasks.TaskId; |
| 16 | +import org.opensearch.tasks.TaskResourceTrackingService; |
| 17 | +import org.opensearch.test.OpenSearchTestCase; |
| 18 | +import org.opensearch.threadpool.TestThreadPool; |
| 19 | +import org.opensearch.threadpool.ThreadPool; |
| 20 | +import org.opensearch.wlm.QueryGroupLevelResourceUsageView; |
| 21 | +import org.opensearch.wlm.QueryGroupTask; |
| 22 | + |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +public class QueryGroupTaskResourceTrackingTests extends OpenSearchTestCase { |
| 27 | + ThreadPool threadPool; |
| 28 | + QueryGroupResourceUsageTrackerService queryGroupResourceUsageTrackerService; |
| 29 | + TaskResourceTrackingService taskResourceTrackingService; |
| 30 | + |
| 31 | + @Override |
| 32 | + public void setUp() throws Exception { |
| 33 | + super.setUp(); |
| 34 | + threadPool = new TestThreadPool("workload-management-tracking-thread-pool"); |
| 35 | + taskResourceTrackingService = new TaskResourceTrackingService( |
| 36 | + Settings.EMPTY, |
| 37 | + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), |
| 38 | + threadPool |
| 39 | + ); |
| 40 | + queryGroupResourceUsageTrackerService = new QueryGroupResourceUsageTrackerService(taskResourceTrackingService); |
| 41 | + } |
| 42 | + |
| 43 | + public void tearDown() throws Exception { |
| 44 | + super.tearDown(); |
| 45 | + threadPool.shutdownNow(); |
| 46 | + } |
| 47 | + |
| 48 | + public void testValidQueryGroupTasksCase() { |
| 49 | + taskResourceTrackingService.setTaskResourceTrackingEnabled(true); |
| 50 | + QueryGroupTask task = new SearchTask(1, "test", "test", () -> "Test", TaskId.EMPTY_TASK_ID, new HashMap<>()); |
| 51 | + taskResourceTrackingService.startTracking(task); |
| 52 | + |
| 53 | + // since the query group id is not set we should not track this task |
| 54 | + Map<String, QueryGroupLevelResourceUsageView> resourceUsageViewMap = queryGroupResourceUsageTrackerService |
| 55 | + .constructQueryGroupLevelUsageViews(); |
| 56 | + assertTrue(resourceUsageViewMap.isEmpty()); |
| 57 | + |
| 58 | + // Now since this task has a valid queryGroupId header it should be tracked |
| 59 | + try (ThreadContext.StoredContext context = threadPool.getThreadContext().stashContext()) { |
| 60 | + threadPool.getThreadContext().putHeader(QueryGroupTask.QUERY_GROUP_ID_HEADER, "testHeader"); |
| 61 | + task.setQueryGroupId(threadPool.getThreadContext()); |
| 62 | + resourceUsageViewMap = queryGroupResourceUsageTrackerService.constructQueryGroupLevelUsageViews(); |
| 63 | + assertFalse(resourceUsageViewMap.isEmpty()); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments