diff --git a/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java b/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java index e7391644..78ab22e4 100644 --- a/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java +++ b/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java @@ -81,7 +81,7 @@ public Collection createComponents( client, metricsRegistry ); - return List.of(queryInsightsService, new QueryInsightsListener(clusterService, queryInsightsService)); + return List.of(queryInsightsService, new QueryInsightsListener(clusterService, queryInsightsService, false)); } @Override diff --git a/src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactory.java b/src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactory.java index 0e68994b..64697dd2 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactory.java +++ b/src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactory.java @@ -118,6 +118,7 @@ public QueryInsightsExporter updateExporter(QueryInsightsExporter exporter, Stri * Close an exporter * * @param exporter the exporter to close + * @throws IOException exception */ public void closeExporter(QueryInsightsExporter exporter) throws IOException { if (exporter != null) { diff --git a/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java b/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java index 43a24e2b..00bf997d 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java +++ b/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java @@ -8,6 +8,7 @@ package org.opensearch.plugin.insights.core.listener; +import static org.opensearch.plugin.insights.settings.QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING; import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.getTopNEnabledSetting; import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.getTopNSizeSetting; import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.getTopNWindowSizeSetting; @@ -56,10 +57,27 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener */ @Inject public QueryInsightsListener(final ClusterService clusterService, final QueryInsightsService queryInsightsService) { + this(clusterService, queryInsightsService, false); + } + + /** + * Constructor for QueryInsightsListener + * + * @param clusterService The Node's cluster service. + * @param queryInsightsService The topQueriesByLatencyService associated with this listener + * @param initiallyEnabled Is the listener initially enabled/disabled + */ + public QueryInsightsListener( + final ClusterService clusterService, + final QueryInsightsService queryInsightsService, + boolean initiallyEnabled + ) { + super(initiallyEnabled); this.clusterService = clusterService; this.queryInsightsService = queryInsightsService; - // Setting endpoints set up for top n queries, including enabling top n queries, window size and top n size - // Expected metricTypes are Latency, CPU and Memory. + + // Setting endpoints set up for top n queries, including enabling top n queries, window size, and top n size + // Expected metricTypes are Latency, CPU, and Memory. for (MetricType type : MetricType.allMetricTypes()) { clusterService.getClusterSettings() .addSettingsUpdateConsumer(getTopNEnabledSetting(type), v -> this.setEnableTopQueries(type, v)); @@ -82,10 +100,14 @@ public QueryInsightsListener(final ClusterService clusterService, final QueryIns this.queryInsightsService.validateWindowSize(type, clusterService.getClusterSettings().get(getTopNWindowSizeSetting(type))); this.queryInsightsService.setWindowSize(type, clusterService.getClusterSettings().get(getTopNWindowSizeSetting(type))); } + + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(SEARCH_QUERY_METRICS_ENABLED_SETTING, v -> setSearchQueryMetricsEnabled(v)); + setSearchQueryMetricsEnabled(clusterService.getClusterSettings().get(SEARCH_QUERY_METRICS_ENABLED_SETTING)); } /** - * Enable or disable top queries insights collection for {@link MetricType} + * Enable or disable top queries insights collection for {@link MetricType}. * This function will enable or disable the corresponding listeners * and query insights services. * @@ -93,20 +115,33 @@ public QueryInsightsListener(final ClusterService clusterService, final QueryIns * @param isCurrentMetricEnabled boolean */ public void setEnableTopQueries(final MetricType metricType, final boolean isCurrentMetricEnabled) { - boolean isTopNFeaturePreviouslyDisabled = !queryInsightsService.isTopNFeatureEnabled(); this.queryInsightsService.enableCollection(metricType, isCurrentMetricEnabled); - boolean isTopNFeatureCurrentlyDisabled = !queryInsightsService.isTopNFeatureEnabled(); + updateQueryInsightsState(); + } - if (isTopNFeatureCurrentlyDisabled) { - super.setEnabled(false); - if (!isTopNFeaturePreviouslyDisabled) { - queryInsightsService.checkAndStopQueryInsights(); - } - } else { + /** + * Set search query metrics enabled to enable collection of search query categorization metrics. + * @param searchQueryMetricsEnabled boolean flag + */ + public void setSearchQueryMetricsEnabled(boolean searchQueryMetricsEnabled) { + this.queryInsightsService.enableSearchQueryMetricsFeature(searchQueryMetricsEnabled); + updateQueryInsightsState(); + } + + /** + * Update the query insights service state based on the enabled features. + * If any feature is enabled, it starts the service. If no features are enabled, it stops the service. + */ + private void updateQueryInsightsState() { + boolean anyFeatureEnabled = queryInsightsService.isAnyFeatureEnabled(); + + if (anyFeatureEnabled && !super.isEnabled()) { super.setEnabled(true); - if (isTopNFeaturePreviouslyDisabled) { - queryInsightsService.checkAndRestartQueryInsights(); - } + queryInsightsService.stop(); // Ensures a clean restart + queryInsightsService.start(); + } else if (!anyFeatureEnabled && super.isEnabled()) { + super.setEnabled(false); + queryInsightsService.stop(); } } diff --git a/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java b/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java index 6c4e67ff..75dea9e2 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java +++ b/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java @@ -8,7 +8,6 @@ package org.opensearch.plugin.insights.core.service; -import static org.opensearch.plugin.insights.settings.QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING; import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.getExporterSettings; import java.io.IOException; @@ -111,15 +110,15 @@ public QueryInsightsService( ); } - this.searchQueryMetricsEnabled = clusterSettings.get(SEARCH_QUERY_METRICS_ENABLED_SETTING); this.searchQueryCategorizer = SearchQueryCategorizer.getInstance(metricsRegistry); - clusterSettings.addSettingsUpdateConsumer(SEARCH_QUERY_METRICS_ENABLED_SETTING, this::setSearchQueryMetricsEnabled); + this.enableSearchQueryMetricsFeature(false); } /** * Ingest the query data into in-memory stores * * @param record the record to ingest + * @return SearchQueryRecord */ public boolean addRecord(final SearchQueryRecord record) { boolean shouldAdd = searchQueryMetricsEnabled; @@ -228,22 +227,11 @@ public boolean isSearchQueryMetricsFeatureEnabled() { } /** - * Stops query insights service if no features enabled + * Enable/Disable search query metrics feature. + * @param enable enable/disable search query metrics feature */ - public void checkAndStopQueryInsights() { - if (!isAnyFeatureEnabled()) { - this.stop(); - } - } - - /** - * Restarts query insights service if any feature enabled - */ - public void checkAndRestartQueryInsights() { - if (isAnyFeatureEnabled()) { - this.stop(); - this.start(); - } + public void enableSearchQueryMetricsFeature(boolean enable) { + searchQueryMetricsEnabled = enable; } /** @@ -306,24 +294,6 @@ public void setExporter(final MetricType type, final Settings settings) { } } - /** - * Set search query metrics enabled to enable collection of search query categorization metrics - * @param searchQueryMetricsEnabled boolean flag - */ - public void setSearchQueryMetricsEnabled(boolean searchQueryMetricsEnabled) { - boolean oldSearchQueryMetricsEnabled = isSearchQueryMetricsFeatureEnabled(); - this.searchQueryMetricsEnabled = searchQueryMetricsEnabled; - if (searchQueryMetricsEnabled) { - if (!oldSearchQueryMetricsEnabled) { - checkAndRestartQueryInsights(); - } - } else { - if (oldSearchQueryMetricsEnabled) { - checkAndStopQueryInsights(); - } - } - } - /** * Get search query categorizer object * @return SearchQueryCategorizer object diff --git a/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java b/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java index 03b34898..b79e4e73 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java +++ b/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java @@ -363,6 +363,7 @@ public List getTopQueriesCurrentSnapshot() { /** * Close the top n queries service + * @throws IOException exception */ public void close() throws IOException { queryInsightsExporterFactory.closeExporter(this.exporter); diff --git a/src/main/java/org/opensearch/plugin/insights/rules/action/top_queries/TopQueriesRequest.java b/src/main/java/org/opensearch/plugin/insights/rules/action/top_queries/TopQueriesRequest.java index bd6303f3..a18e7f21 100644 --- a/src/main/java/org/opensearch/plugin/insights/rules/action/top_queries/TopQueriesRequest.java +++ b/src/main/java/org/opensearch/plugin/insights/rules/action/top_queries/TopQueriesRequest.java @@ -46,6 +46,7 @@ public TopQueriesRequest(final MetricType metricType, final String... nodesIds) /** * Get the type of requested metrics + * @return MetricType for current top query service */ public MetricType getMetricType() { return metricType; diff --git a/src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java b/src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java index aa7204a8..c8b14fd5 100644 --- a/src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java +++ b/src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java @@ -85,6 +85,7 @@ static void writeTo(final StreamOutput out, final Attribute attribute) throws IO * * @param out the StreamOutput to write * @param attributeValue the Attribute value to write + * @throws IOException exception */ @SuppressWarnings("unchecked") public static void writeValueTo(StreamOutput out, Object attributeValue) throws IOException { diff --git a/src/test/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListenerTests.java b/src/test/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListenerTests.java index 9bef5409..14c6ae78 100644 --- a/src/test/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListenerTests.java +++ b/src/test/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListenerTests.java @@ -10,12 +10,13 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -201,61 +202,147 @@ public void testConcurrentOnRequestEnd() throws InterruptedException { verify(queryInsightsService, times(numRequests)).addRecord(any()); } - public void testTopNFeatureEnabledDisabled() { - // Test case 1: Only latency enabled initially, disable latency and verify expected behavior - QueryInsightsService queryInsightsService1 = mock(QueryInsightsService.class); - QueryInsightsListener queryInsightsListener1 = new QueryInsightsListener(clusterService, queryInsightsService1); - - when(queryInsightsService1.isCollectionEnabled(MetricType.LATENCY)).thenReturn(true); - when(queryInsightsService1.isCollectionEnabled(MetricType.CPU)).thenReturn(false); - when(queryInsightsService1.isCollectionEnabled(MetricType.MEMORY)).thenReturn(false); - when(queryInsightsService1.isTopNFeatureEnabled()).thenReturn(true).thenReturn(false); - - queryInsightsListener1.setEnableTopQueries(MetricType.LATENCY, false); - assertFalse(queryInsightsListener1.isEnabled()); - verify(queryInsightsService1).checkAndStopQueryInsights(); - verify(queryInsightsService1, never()).checkAndRestartQueryInsights(); - - // Test case 2: All disabled initially, enable latency and verify expected behavior - QueryInsightsService queryInsightsService2 = mock(QueryInsightsService.class); - QueryInsightsListener queryInsightsListener2 = new QueryInsightsListener(clusterService, queryInsightsService2); - - when(queryInsightsService2.isCollectionEnabled(MetricType.LATENCY)).thenReturn(false); - when(queryInsightsService2.isCollectionEnabled(MetricType.CPU)).thenReturn(false); - when(queryInsightsService2.isCollectionEnabled(MetricType.MEMORY)).thenReturn(false); - when(queryInsightsService2.isTopNFeatureEnabled()).thenReturn(false).thenReturn(true); - - queryInsightsListener2.setEnableTopQueries(MetricType.LATENCY, true); - assertTrue(queryInsightsListener2.isEnabled()); - verify(queryInsightsService2).checkAndRestartQueryInsights(); - verify(queryInsightsService2, never()).checkAndStopQueryInsights(); - - // Test case 3: latency and CPU enabled initially, disable latency and verify expected behavior - QueryInsightsService queryInsightsService3 = mock(QueryInsightsService.class); - QueryInsightsListener queryInsightsListener3 = new QueryInsightsListener(clusterService, queryInsightsService3); - - when(queryInsightsService3.isCollectionEnabled(MetricType.LATENCY)).thenReturn(true); - when(queryInsightsService3.isCollectionEnabled(MetricType.CPU)).thenReturn(true); - when(queryInsightsService3.isCollectionEnabled(MetricType.MEMORY)).thenReturn(false); - when(queryInsightsService3.isTopNFeatureEnabled()).thenReturn(true).thenReturn(true); - - queryInsightsListener3.setEnableTopQueries(MetricType.LATENCY, false); - assertTrue(queryInsightsListener3.isEnabled()); - verify(queryInsightsService3, never()).checkAndStopQueryInsights(); - verify(queryInsightsService3, never()).checkAndRestartQueryInsights(); - - // Test case 4: Only CPU enabled initially, enable latency and verify expected behavior - QueryInsightsService queryInsightsService4 = mock(QueryInsightsService.class); - QueryInsightsListener queryInsightsListener4 = new QueryInsightsListener(clusterService, queryInsightsService4); - - when(queryInsightsService4.isCollectionEnabled(MetricType.LATENCY)).thenReturn(false); - when(queryInsightsService4.isCollectionEnabled(MetricType.CPU)).thenReturn(true); - when(queryInsightsService4.isCollectionEnabled(MetricType.MEMORY)).thenReturn(false); - when(queryInsightsService4.isTopNFeatureEnabled()).thenReturn(true).thenReturn(true); - - queryInsightsListener4.setEnableTopQueries(MetricType.LATENCY, true); - assertTrue(queryInsightsListener4.isEnabled()); - verify(queryInsightsService4, never()).checkAndRestartQueryInsights(); - verify(queryInsightsService4, never()).checkAndStopQueryInsights(); + public void testAllFeatureEnabledDisabled() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { + QueryInsightsListener queryInsightsListener; + + // Test case 1 + queryInsightsListener = testFeatureEnableDisableSetup( + Arrays.asList(true, false), // Latency enabled, then disabled + Arrays.asList(false), // CPU disabled + Arrays.asList(false), // Memory disabled + Arrays.asList(false) // Metrics disabled + ); + queryInsightsListener.setEnableTopQueries(MetricType.LATENCY, false); + assertFalse(queryInsightsListener.isEnabled()); // All metrics are disabled, so QI should be disabled + + // Test case 2 + queryInsightsListener = testFeatureEnableDisableSetup( + Arrays.asList(false, true), // Latency disabled, then enabled + Arrays.asList(false), // CPU disabled + Arrays.asList(false), // Memory disabled + Arrays.asList(false) // Metrics disabled + ); + queryInsightsListener.setEnableTopQueries(MetricType.LATENCY, true); + assertTrue(queryInsightsListener.isEnabled()); // Latency is enabled, so QI should be enabled + + // Test case 3 + queryInsightsListener = testFeatureEnableDisableSetup( + Arrays.asList(true, false), // Latency enabled, then disabled + Arrays.asList(true), // CPU enabled + Arrays.asList(false), // Memory disabled + Arrays.asList(false) // Metrics disabled + ); + queryInsightsListener.setEnableTopQueries(MetricType.LATENCY, false); + assertTrue(queryInsightsListener.isEnabled()); // CPU is still enabled, so QI should still be enabled + + // Test case 4 + queryInsightsListener = testFeatureEnableDisableSetup( + Arrays.asList(false, true), // Latency disabled, then enabled + Arrays.asList(true), // CPU enabled + Arrays.asList(false), // Memory disabled + Arrays.asList(false) // Metrics disabled + ); + queryInsightsListener.setEnableTopQueries(MetricType.LATENCY, true); + assertTrue(queryInsightsListener.isEnabled()); // Latency and CPU is enabled, so QI should be enabled + + // Test case 5 + queryInsightsListener = testFeatureEnableDisableSetup( + Arrays.asList(true, false), // Latency enabled, then disabled + Arrays.asList(true, false), // CPU enabled, then disabled + Arrays.asList(false), // Memory disabled + Arrays.asList(true) // Metrics enabled + ); + queryInsightsListener.setEnableTopQueries(MetricType.LATENCY, false); + queryInsightsListener.setEnableTopQueries(MetricType.CPU, false); + assertTrue(queryInsightsListener.isEnabled()); // Metrics is still enabled, so QI should still be enabled + + // Test case 6 + queryInsightsListener = testFeatureEnableDisableSetup( + Arrays.asList(false), // Latency disabled + Arrays.asList(false), // CPU disabled + Arrays.asList(true, false), // Memory enabled, then disabled + Arrays.asList(true) // Metrics enabled + ); + queryInsightsListener.setEnableTopQueries(MetricType.MEMORY, false); + assertTrue(queryInsightsListener.isEnabled()); // Metrics is still enabled, so QI should still be enabled + + // Test case 7 + queryInsightsListener = testFeatureEnableDisableSetup( + Arrays.asList(true, false), // Latency enabled, then disabled + Arrays.asList(true, false), // CPU enabled, then disabled + Arrays.asList(true, false), // Memory enabled, then disabled + Arrays.asList(true) // Metrics enabled + ); + queryInsightsListener.setEnableTopQueries(MetricType.LATENCY, false); + queryInsightsListener.setEnableTopQueries(MetricType.CPU, false); + queryInsightsListener.setEnableTopQueries(MetricType.MEMORY, false); + assertTrue(queryInsightsListener.isEnabled()); // Metrics is still enabled, so QI should still be enabled + + // Test case 8 + queryInsightsListener = testFeatureEnableDisableSetup( + Arrays.asList(true), // Latency enabled + Arrays.asList(false), // CPU disabled + Arrays.asList(false), // Memory disabled + Arrays.asList(true) // Metrics enabled then disabled + ); + queryInsightsListener.setSearchQueryMetricsEnabled(false); + assertTrue(queryInsightsListener.isEnabled()); // Latency is still enabled, so QI should be enabled + + // Test case 9 + queryInsightsListener = testFeatureEnableDisableSetup( + Arrays.asList(false), // Latency disabled + Arrays.asList(false), // CPU disabled + Arrays.asList(false), // Memory disabled + Arrays.asList(true, false) // Metrics enabled then disabled + ); + queryInsightsListener.setSearchQueryMetricsEnabled(false); + assertFalse(queryInsightsListener.isEnabled()); // All disabled, so QI should be disabled + } + + private QueryInsightsListener testFeatureEnableDisableSetup( + List latencyValues, + List cpuValues, + List memoryValues, + List metricsEnabledValues + ) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + + // Determine initial state: If any of the first values in the lists is true, enable it + boolean shouldEnable = latencyValues.get(0) || cpuValues.get(0) || memoryValues.get(0) || metricsEnabledValues.get(0); + + QueryInsightsService queryInsightsService = mock(QueryInsightsService.class); + QueryInsightsListener queryInsightsListener = new QueryInsightsListener(clusterService, queryInsightsService, shouldEnable); + + // Configure the mock to return multiple values in sequence for the various metrics + when(queryInsightsService.isCollectionEnabled(MetricType.LATENCY)).thenReturn( + latencyValues.get(0), + latencyValues.subList(1, latencyValues.size()).toArray(new Boolean[0]) + ); + + when(queryInsightsService.isCollectionEnabled(MetricType.CPU)).thenReturn( + cpuValues.get(0), + cpuValues.subList(1, cpuValues.size()).toArray(new Boolean[0]) + ); + + when(queryInsightsService.isCollectionEnabled(MetricType.MEMORY)).thenReturn( + memoryValues.get(0), + memoryValues.subList(1, memoryValues.size()).toArray(new Boolean[0]) + ); + + when(queryInsightsService.isSearchQueryMetricsFeatureEnabled()).thenReturn( + metricsEnabledValues.get(0), + metricsEnabledValues.subList(1, metricsEnabledValues.size()).toArray(new Boolean[0]) + ); + + when(queryInsightsService.isTopNFeatureEnabled()).thenCallRealMethod(); // Call real method if needed + + // Logic for isAnyFeatureEnabled() based on the last values in the lists + boolean isAnyFeatureEnabled = latencyValues.get(latencyValues.size() - 1) + || cpuValues.get(cpuValues.size() - 1) + || memoryValues.get(memoryValues.size() - 1) + || metricsEnabledValues.get(metricsEnabledValues.size() - 1); + + when(queryInsightsService.isAnyFeatureEnabled()).thenReturn(isAnyFeatureEnabled); + + return queryInsightsListener; } } diff --git a/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java b/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java index 4fc18778..56f3203d 100644 --- a/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java +++ b/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java @@ -10,7 +10,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; import org.junit.Before; import org.opensearch.client.Client; @@ -74,38 +73,18 @@ public void testSearchQueryMetricsEnabled() { assertNotNull(queryInsightsService.getSearchQueryCategorizer()); // Enable search query metrics - queryInsightsService.setSearchQueryMetricsEnabled(true); + queryInsightsService.enableSearchQueryMetricsFeature(true); // Assert that searchQueryMetricsEnabled is true and searchQueryCategorizer is initialized assertTrue(queryInsightsService.isSearchQueryMetricsFeatureEnabled()); assertNotNull(queryInsightsService.getSearchQueryCategorizer()); // Disable search query metrics - queryInsightsService.setSearchQueryMetricsEnabled(false); + queryInsightsService.enableSearchQueryMetricsFeature(false); // Assert that searchQueryMetricsEnabled is false and searchQueryCategorizer is not null assertFalse(queryInsightsService.isSearchQueryMetricsFeatureEnabled()); assertNotNull(queryInsightsService.getSearchQueryCategorizer()); } - - public void testFeaturesEnableDisable() { - // Test case 1: All metric type collection disabled and search query metrics disabled, enable search query metrics - queryInsightsServiceSpy.enableCollection(MetricType.LATENCY, false); - queryInsightsServiceSpy.enableCollection(MetricType.CPU, false); - queryInsightsServiceSpy.enableCollection(MetricType.MEMORY, false); - queryInsightsServiceSpy.setSearchQueryMetricsEnabled(false); - - queryInsightsServiceSpy.setSearchQueryMetricsEnabled(true); - verify(queryInsightsServiceSpy).checkAndRestartQueryInsights(); - - // Test case 2: All metric type collection disabled and search query metrics enabled, disable search query metrics - queryInsightsServiceSpy.enableCollection(MetricType.LATENCY, false); - queryInsightsServiceSpy.enableCollection(MetricType.CPU, false); - queryInsightsServiceSpy.enableCollection(MetricType.MEMORY, false); - queryInsightsServiceSpy.setSearchQueryMetricsEnabled(true); - - queryInsightsServiceSpy.setSearchQueryMetricsEnabled(false); - verify(queryInsightsServiceSpy).checkAndStopQueryInsights(); - } }