From cba70484cdda639e13004155673216b8996fc88f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 23 Feb 2023 18:31:54 +0000 Subject: [PATCH] Adding unit test for test isHeapUsageDominatedBySearch (#6463) Signed-off-by: PritLadani (cherry picked from commit 55cc1096518ea7839ed9bbbc9fa616b54b40b8ac) Signed-off-by: github-actions[bot] --- .../trackers/HeapUsageTrackerTests.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/server/src/test/java/org/opensearch/search/backpressure/trackers/HeapUsageTrackerTests.java b/server/src/test/java/org/opensearch/search/backpressure/trackers/HeapUsageTrackerTests.java index 2acb23641667a..b2ef3215f869f 100644 --- a/server/src/test/java/org/opensearch/search/backpressure/trackers/HeapUsageTrackerTests.java +++ b/server/src/test/java/org/opensearch/search/backpressure/trackers/HeapUsageTrackerTests.java @@ -12,13 +12,16 @@ import org.opensearch.action.search.SearchTask; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; +import org.opensearch.monitor.jvm.JvmStats; import org.opensearch.search.backpressure.settings.SearchBackpressureSettings; import org.opensearch.search.backpressure.settings.SearchShardTaskSettings; import org.opensearch.search.backpressure.settings.SearchTaskSettings; +import org.opensearch.tasks.CancellableTask; import org.opensearch.tasks.Task; import org.opensearch.tasks.TaskCancellation; import org.opensearch.test.OpenSearchTestCase; +import java.util.List; import java.util.Optional; import static org.mockito.Mockito.spy; @@ -140,4 +143,17 @@ public void testNotEligibleForCancellation() { reason = tracker.checkAndMaybeGetCancellationReason(task); assertFalse(reason.isPresent()); } + + public void testIsHeapUsageDominatedBySearch() { + assumeTrue("Skip the test if the hardware doesn't support heap usage tracking", HeapUsageTracker.isHeapTrackingSupported()); + + // task with 1 byte of heap usage so that it does not breach the threshold + CancellableTask task = createMockTaskWithResourceStats(SearchShardTask.class, 1, 1); + assertFalse(HeapUsageTracker.isHeapUsageDominatedBySearch(List.of(task), 0.5)); + + long totalHeap = JvmStats.jvmStats().getMem().getHeapMax().getBytes(); + // task with heap usage of [totalHeap - 1] so that it breaches the threshold + task = createMockTaskWithResourceStats(SearchShardTask.class, 1, totalHeap - 1); + assertTrue(HeapUsageTracker.isHeapUsageDominatedBySearch(List.of(task), 0.5)); + } }