diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java index e5790d4b827bb..0acd7e2dcac0d 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java @@ -53,25 +53,25 @@ public QueryInsightsPlugin() {} @Override public Collection createComponents( - Client client, - ClusterService clusterService, - ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, - ScriptService scriptService, - NamedXContentRegistry xContentRegistry, - Environment environment, - NodeEnvironment nodeEnvironment, - NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier repositoriesServiceSupplier + final Client client, + final ClusterService clusterService, + final ThreadPool threadPool, + final ResourceWatcherService resourceWatcherService, + final ScriptService scriptService, + final NamedXContentRegistry xContentRegistry, + final Environment environment, + final NodeEnvironment nodeEnvironment, + final NamedWriteableRegistry namedWriteableRegistry, + final IndexNameExpressionResolver indexNameExpressionResolver, + final Supplier repositoriesServiceSupplier ) { // create top n queries service - QueryInsightsService queryInsightsService = new QueryInsightsService(threadPool); + final QueryInsightsService queryInsightsService = new QueryInsightsService(threadPool); return List.of(queryInsightsService); } @Override - public List> getExecutorBuilders(Settings settings) { + public List> getExecutorBuilders(final Settings settings) { return List.of( new ScalingExecutorBuilder( QueryInsightsSettings.QUERY_INSIGHTS_EXECUTOR, @@ -84,13 +84,13 @@ public List> getExecutorBuilders(Settings settings) { @Override public List getRestHandlers( - Settings settings, - RestController restController, - ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, - SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster + final Settings settings, + final RestController restController, + final ClusterSettings clusterSettings, + final IndexScopedSettings indexScopedSettings, + final SettingsFilter settingsFilter, + final IndexNameExpressionResolver indexNameExpressionResolver, + final Supplier nodesInCluster ) { return List.of(); } diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java index c78f446fbaf18..525ca0d4a3d33 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java @@ -8,81 +8,67 @@ package org.opensearch.plugin.insights.core.service; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import org.opensearch.common.inject.Inject; -import org.opensearch.common.unit.TimeValue; +import org.opensearch.common.lifecycle.AbstractLifecycleComponent; import org.opensearch.plugin.insights.rules.model.MetricType; import org.opensearch.plugin.insights.rules.model.SearchQueryRecord; import org.opensearch.plugin.insights.settings.QueryInsightsSettings; +import org.opensearch.threadpool.Scheduler; import org.opensearch.threadpool.ThreadPool; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.time.ZoneOffset; -import java.time.temporal.ChronoUnit; import java.util.ArrayList; -import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.List; -import java.util.Locale; import java.util.Map; -import java.util.concurrent.PriorityBlockingQueue; -import java.util.stream.Collectors; -import java.util.stream.Stream; +import java.util.concurrent.LinkedBlockingQueue; /** * Service responsible for gathering, analyzing, storing and exporting - * top N queries with high latency data for search queries + * information related to search queries * * @opensearch.internal */ -public class QueryInsightsService { - private static final Logger log = LogManager.getLogger(QueryInsightsService.class); - - private static final TimeValue delay = TimeValue.ZERO; +public class QueryInsightsService extends AbstractLifecycleComponent { /** * The internal OpenSearch thread pool that execute async processing and exporting tasks */ private final ThreadPool threadPool; /** - * enable insight data collection + * Services to capture top n queries for different metric types */ - private final Map enableCollect = new HashMap<>(); - - private int topNSize = QueryInsightsSettings.DEFAULT_TOP_N_SIZE; + private final Map topQueriesServices; - private TimeValue windowSize = TimeValue.timeValueSeconds(QueryInsightsSettings.DEFAULT_WINDOW_SIZE); /** - * The internal thread-safe store that holds the top n queries insight data, by different MetricType + * Flags for enabling insight data collection for different metric types */ - private final Map> topQueriesStores; + private final Map enableCollect; /** - * The internal store that holds historical top n queries insight data by different MetricType in the last window + * The internal thread-safe queue to ingest the search query data and subsequently forward to processors */ - private final Map> topQueriesHistoryStores; + private final LinkedBlockingQueue queryRecordsQueue; + /** - * window start timestamp for each top queries collector + * Holds a reference to delayed operation {@link Scheduler.Cancellable} so it can be cancelled when + * the service closed concurrently. */ - private final Map topQueriesWindowStart; + protected volatile Scheduler.Cancellable scheduledFuture; /** - * Create the TopQueriesByLatencyService Object + * Constructor of the QueryInsightsService * * @param threadPool The OpenSearch thread pool to run async tasks */ @Inject - public QueryInsightsService(ThreadPool threadPool) { - topQueriesStores = new HashMap<>(); - topQueriesHistoryStores = new HashMap<>(); - topQueriesWindowStart = new HashMap<>(); + public QueryInsightsService(final ThreadPool threadPool) { + enableCollect = new HashMap<>(); + queryRecordsQueue = new LinkedBlockingQueue<>(QueryInsightsSettings.QUERY_RECORD_QUEUE_CAPACITY); + topQueriesServices = new HashMap<>(); for (MetricType metricType : MetricType.allMetricTypes()) { - topQueriesStores.put(metricType, new PriorityBlockingQueue<>(topNSize, (a, b) -> SearchQueryRecord.compare(a, b, metricType))); - topQueriesHistoryStores.put(metricType, new ArrayList<>()); - topQueriesWindowStart.put(metricType, -1L); + enableCollect.put(metricType, false); + topQueriesServices.put(metricType, new TopQueriesService(metricType)); } this.threadPool = threadPool; } @@ -92,75 +78,48 @@ public QueryInsightsService(ThreadPool threadPool) { * * @param record the record to ingest */ - public void addRecord(SearchQueryRecord record) { - for (MetricType metricType : record.getMeasurements().keySet()) { - this.threadPool.schedule(() -> { - if (!topQueriesStores.containsKey(metricType)) { - return; - } - // add the record to corresponding priority queues to calculate top n queries insights - PriorityBlockingQueue store = topQueriesStores.get(metricType); - checkAndResetWindow(metricType, record.getTimestamp()); - if (record.getTimestamp() > topQueriesWindowStart.get(metricType)) { - store.add(record); - // remove top elements for fix sizing priority queue - if (store.size() > this.getTopNSize()) { - store.poll(); - } - } - }, delay, QueryInsightsSettings.QUERY_INSIGHTS_EXECUTOR); + public boolean addRecord(final SearchQueryRecord record) { + boolean shouldAdd = false; + for (Map.Entry entry : topQueriesServices.entrySet()) { + if (!enableCollect.get(entry.getKey())) { + continue; + } + List currentSnapshot = entry.getValue().getTopQueriesCurrentSnapshot(); + // skip add to top N queries store if the incoming record is smaller than the Nth record + if (currentSnapshot.size() < entry.getValue().getTopNSize() + || SearchQueryRecord.compare(record, currentSnapshot.get(0), entry.getKey()) > 0) { + shouldAdd = true; + break; + } } - } - - private void checkAndResetWindow(MetricType metricType, Long timestamp) { - Long windowStart = calculateWindowStart(timestamp); - // reset window if the current window is outdated - if (topQueriesWindowStart.get(metricType) < windowStart) { - resetWindow(metricType, windowStart); + if (shouldAdd) { + return queryRecordsQueue.offer(record); } + return false; } - private synchronized void resetWindow(MetricType metricType, Long newWindowStart) { - // rotate the current window to history store only if the data belongs to the last window - if (topQueriesWindowStart.get(metricType) == newWindowStart - windowSize.getMillis()) { - topQueriesHistoryStores.put(metricType, new ArrayList<>(topQueriesStores.get(metricType))); - } else { - topQueriesHistoryStores.get(metricType).clear(); + /** + * Drain the queryRecordsQueue into internal stores and services + */ + public void drainRecords() { + final List records = new ArrayList<>(); + queryRecordsQueue.drainTo(records); + records.sort(Comparator.comparingLong(SearchQueryRecord::getTimestamp)); + for (MetricType metricType : MetricType.allMetricTypes()) { + if (enableCollect.get(metricType)) { + // ingest the records into topQueriesService + topQueriesServices.get(metricType).consumeRecords(records); + } } - topQueriesStores.get(metricType).clear(); - topQueriesWindowStart.put(metricType, newWindowStart); } /** - * Get all top queries records that are in the current query insight store, based on the input MetricType - * Optionally include top N records from the last window. - * - * By default, return the records in sorted order. - * + * Get the top queries service based on metricType * @param metricType {@link MetricType} - * @param includeLastWindow if the top N queries from the last window should be included - * @return List of the records that are in the query insight store - * @throws IllegalArgumentException if query insight is disabled in the cluster + * @return {@link TopQueriesService} */ - public List getTopNRecords(MetricType metricType, boolean includeLastWindow) throws IllegalArgumentException { - if (!enableCollect.containsKey(metricType) || !enableCollect.get(metricType)) { - throw new IllegalArgumentException( - String.format( - Locale.ROOT, - "Cannot get query data when query insight feature is not enabled for MetricType [%s].", - metricType - ) - ); - } - checkAndResetWindow(metricType, System.currentTimeMillis()); - List queries = new ArrayList<>(topQueriesStores.get(metricType)); - if (includeLastWindow) { - queries.addAll(topQueriesHistoryStores.get(metricType)); - } - return Stream.of(queries) - .flatMap(Collection::stream) - .sorted((a, b) -> SearchQueryRecord.compare(a, b, metricType) * -1) - .collect(Collectors.toList()); + public TopQueriesService getTopQueriesService(final MetricType metricType) { + return topQueriesServices.get(metricType); } /** @@ -169,12 +128,9 @@ public List getTopNRecords(MetricType metricType, boolean inc * @param metricType {@link MetricType} * @param enable Flag to enable or disable Query Insights data collection */ - public void enableCollection(MetricType metricType, boolean enable) { + public void enableCollection(final MetricType metricType, final boolean enable) { this.enableCollect.put(metricType, enable); - // set topQueriesWindowStart to enable top n queries collection - if (enable) { - topQueriesWindowStart.put(metricType, calculateWindowStart(System.currentTimeMillis())); - } + this.topQueriesServices.get(metricType).setEnabled(enable); } /** @@ -183,122 +139,42 @@ public void enableCollection(MetricType metricType, boolean enable) { * @param metricType {@link MetricType} * @return if the Query Insights data collection is enabled */ - public boolean isCollectionEnabled(MetricType metricType) { - return this.enableCollect.containsKey(metricType) && this.enableCollect.get(metricType); - } - - /** - * Set the top N size for TopQueriesByLatencyService service. - * - * @param size the top N size to set - */ - public void setTopNSize(int size) { - this.topNSize = size; - } - - /** - * Validate the top N size based on the internal constrains - * - * @param size the wanted top N size - */ - public void validateTopNSize(int size) { - if (size > QueryInsightsSettings.MAX_N_SIZE) { - throw new IllegalArgumentException( - "Top N size setting [" - + QueryInsightsSettings.TOP_N_LATENCY_QUERIES_SIZE.getKey() - + "]" - + " should be smaller than max top N size [" - + QueryInsightsSettings.MAX_N_SIZE - + "was (" - + size - + " > " - + QueryInsightsSettings.MAX_N_SIZE - + ")" - ); - } - } - - /** - * Get the top N size set for TopQueriesByLatencyService - * - * @return the top N size - */ - public int getTopNSize() { - return this.topNSize; + public boolean isCollectionEnabled(final MetricType metricType) { + return this.enableCollect.get(metricType); } /** - * Set the window size for TopQueriesByLatencyService + * Check if query insights service is enabled * - * @param windowSize window size to set + * @return if query insights service is enabled */ - public void setWindowSize(TimeValue windowSize) { - this.windowSize = windowSize; - for (MetricType metricType : MetricType.allMetricTypes()) { - topQueriesWindowStart.put(metricType, -1L); + public boolean isEnabled() { + for (MetricType t : MetricType.allMetricTypes()) { + if (isCollectionEnabled(t)) { + return true; + } } + return false; } - /** - * Validate if the window size is valid, based on internal constrains. - * - * @param windowSize the window size to validate - */ - public void validateWindowSize(TimeValue windowSize) { - if (windowSize.compareTo(QueryInsightsSettings.MAX_WINDOW_SIZE) > 0 - || windowSize.compareTo(QueryInsightsSettings.MIN_WINDOW_SIZE) < 0) { - throw new IllegalArgumentException( - "Window size setting [" - + QueryInsightsSettings.TOP_N_LATENCY_QUERIES_WINDOW_SIZE.getKey() - + "]" - + " should be between [" - + QueryInsightsSettings.MAX_WINDOW_SIZE - + "," - + QueryInsightsSettings.MAX_WINDOW_SIZE - + "]" - + "was (" - + windowSize - + ")" - ); - } - if (!(QueryInsightsSettings.VALID_WINDOW_SIZES_IN_MINUTES.contains(windowSize) || windowSize.getMinutes() % 60 == 0)) { - throw new IllegalArgumentException( - "Window size setting [" - + QueryInsightsSettings.TOP_N_LATENCY_QUERIES_WINDOW_SIZE.getKey() - + "]" - + " should be multiple of 1 hour, or one of " - + QueryInsightsSettings.VALID_WINDOW_SIZES_IN_MINUTES - + ", was (" - + windowSize - + ")" + @Override + protected void doStart() { + if (isEnabled()) { + scheduledFuture = threadPool.scheduleWithFixedDelay( + this::drainRecords, + QueryInsightsSettings.QUERY_RECORD_QUEUE_DRAIN_INTERVAL, + QueryInsightsSettings.QUERY_INSIGHTS_EXECUTOR ); } } - /** - * Get the size of top N queries store - * @param metricType {@link MetricType} - * @return top N queries store size - */ - public int getTopNStoreSize(MetricType metricType) { - return topQueriesStores.get(metricType).size(); - } - - /** - * Get the size of top N queries history store - * @param metricType {@link MetricType} - * @return top N queries history store size - */ - public int getTopNHistoryStoreSize(MetricType metricType) { - return topQueriesHistoryStores.get(metricType).size(); - } - - private Long calculateWindowStart(Long timestamp) { - LocalDateTime currentTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of("UTC")); - LocalDateTime windowStartTime = currentTime.truncatedTo(ChronoUnit.HOURS); - while (!windowStartTime.plusMinutes(windowSize.getMinutes()).isAfter(currentTime)) { - windowStartTime = windowStartTime.plusMinutes(windowSize.getMinutes()); + @Override + protected void doStop() { + if (scheduledFuture != null) { + scheduledFuture.cancel(); } - return windowStartTime.toInstant(ZoneOffset.UTC).getEpochSecond() * 1000; } + + @Override + protected void doClose() {} } diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java new file mode 100644 index 0000000000000..d2c30cbdf98e7 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java @@ -0,0 +1,282 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.plugin.insights.core.service; + +import org.opensearch.common.unit.TimeValue; +import org.opensearch.plugin.insights.rules.model.MetricType; +import org.opensearch.plugin.insights.rules.model.SearchQueryRecord; +import org.opensearch.plugin.insights.settings.QueryInsightsSettings; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Locale; +import java.util.PriorityQueue; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Service responsible for gathering and storing top N queries + * with high latency or resource usage + * + * @opensearch.internal + */ +public class TopQueriesService { + private boolean enabled; + /** + * The metric type to measure top n queries + */ + private final MetricType metricType; + private int topNSize; + /** + * The window size to keep the top n queries + */ + private TimeValue windowSize; + /** + * The current window start timestamp + */ + private long windowStart; + /** + * The internal thread-safe store that holds the top n queries insight data + */ + private final PriorityQueue topQueriesStore; + + /** + * The AtomicReference of a snapshot of the current window top queries for getters to consume + */ + private final AtomicReference> topQueriesCurrentSnapshot; + + /** + * The AtomicReference of a snapshot of the last window top queries for getters to consume + */ + private final AtomicReference> topQueriesHistorySnapshot; + + TopQueriesService(final MetricType metricType) { + this.enabled = false; + this.metricType = metricType; + this.topNSize = QueryInsightsSettings.DEFAULT_TOP_N_SIZE; + this.windowSize = QueryInsightsSettings.DEFAULT_WINDOW_SIZE; + this.windowStart = -1L; + topQueriesStore = new PriorityQueue<>(topNSize, (a, b) -> SearchQueryRecord.compare(a, b, metricType)); + topQueriesCurrentSnapshot = new AtomicReference<>(new ArrayList<>()); + topQueriesHistorySnapshot = new AtomicReference<>(new ArrayList<>()); + } + + /** + * Set the top N size for TopQueriesService service. + * + * @param topNSize the top N size to set + */ + public void setTopNSize(final int topNSize) { + this.topNSize = topNSize; + } + + /** + * Get the current configured top n size + * + * @return top n size + */ + public int getTopNSize() { + return topNSize; + } + + /** + * Validate the top N size based on the internal constrains + * + * @param size the wanted top N size + */ + public void validateTopNSize(final int size) { + if (size > QueryInsightsSettings.MAX_N_SIZE) { + throw new IllegalArgumentException( + "Top N size setting for [" + + metricType + + "]" + + " should be smaller than max top N size [" + + QueryInsightsSettings.MAX_N_SIZE + + "was (" + + size + + " > " + + QueryInsightsSettings.MAX_N_SIZE + + ")" + ); + } + } + + /** + * Set enable flag for the service + * @param enabled boolean + */ + public void setEnabled(final boolean enabled) { + this.enabled = enabled; + } + + /** + * Set the window size for top N queries service + * + * @param windowSize window size to set + */ + public void setWindowSize(final TimeValue windowSize) { + this.windowSize = windowSize; + // reset the window start time since the window size has changed + this.windowStart = -1L; + } + + /** + * Validate if the window size is valid, based on internal constrains. + * + * @param windowSize the window size to validate + */ + public void validateWindowSize(final TimeValue windowSize) { + if (windowSize.compareTo(QueryInsightsSettings.MAX_WINDOW_SIZE) > 0 + || windowSize.compareTo(QueryInsightsSettings.MIN_WINDOW_SIZE) < 0) { + throw new IllegalArgumentException( + "Window size setting for [" + + metricType + + "]" + + " should be between [" + + QueryInsightsSettings.MIN_WINDOW_SIZE + + "," + + QueryInsightsSettings.MAX_WINDOW_SIZE + + "]" + + "was (" + + windowSize + + ")" + ); + } + if (!(QueryInsightsSettings.VALID_WINDOW_SIZES_IN_MINUTES.contains(windowSize) || windowSize.getMinutes() % 60 == 0)) { + throw new IllegalArgumentException( + "Window size setting for [" + + metricType + + "]" + + " should be multiple of 1 hour, or one of " + + QueryInsightsSettings.VALID_WINDOW_SIZES_IN_MINUTES + + ", was (" + + windowSize + + ")" + ); + } + } + + /** + * Get all top queries records that are in the current top n queries store + * Optionally include top N records from the last window. + * + * By default, return the records in sorted order. + * + * @param includeLastWindow if the top N queries from the last window should be included + * @return List of the records that are in the query insight store + * @throws IllegalArgumentException if query insight is disabled in the cluster + */ + public List getTopQueriesRecords(final boolean includeLastWindow) throws IllegalArgumentException { + if (!enabled) { + throw new IllegalArgumentException( + String.format(Locale.ROOT, "Cannot get top n queries for [%s] when it is not enabled.", metricType.toString()) + ); + } + // read from window snapshots + final List queries = new ArrayList<>(topQueriesCurrentSnapshot.get()); + if (includeLastWindow) { + queries.addAll(topQueriesHistorySnapshot.get()); + } + return Stream.of(queries) + .flatMap(Collection::stream) + .sorted((a, b) -> SearchQueryRecord.compare(a, b, metricType) * -1) + .collect(Collectors.toList()); + } + + /** + * Consume records to top queries stores + * + * @param records a list of {@link SearchQueryRecord} + */ + void consumeRecords(final List records) { + final long currentWindowStart = calculateWindowStart(System.currentTimeMillis()); + List recordsInLastWindow = new ArrayList<>(); + List recordsInThisWindow = new ArrayList<>(); + for (SearchQueryRecord record : records) { + // skip the records that does not have the corresponding measurement + if (!record.getMeasurements().containsKey(metricType)) { + continue; + } + if (record.getTimestamp() < currentWindowStart) { + recordsInLastWindow.add(record); + } else { + recordsInThisWindow.add(record); + } + } + // add records in last window, if there are any, to the top n store + addToTopNStore(recordsInLastWindow); + // rotate window and reset window start if necessary + rotateWindowIfNecessary(currentWindowStart); + // add records in current window, if there are any, to the top n store + addToTopNStore(recordsInThisWindow); + // update the current window snapshot for getters to consume + final List newSnapShot = new ArrayList<>(topQueriesStore); + newSnapShot.sort((a, b) -> SearchQueryRecord.compare(a, b, metricType)); + topQueriesCurrentSnapshot.set(newSnapShot); + } + + private void addToTopNStore(final List records) { + topQueriesStore.addAll(records); + // remove top elements for fix sizing priority queue + while (topQueriesStore.size() > topNSize) { + topQueriesStore.poll(); + } + } + + /** + * Reset the current window and rotate the data to history snapshot for top n queries, + * This function would be invoked zero time or only once in each consumeRecords call + * + * @param newWindowStart the new windowStart to set to + */ + private void rotateWindowIfNecessary(final long newWindowStart) { + // reset window if the current window is outdated + if (windowStart < newWindowStart) { + final List history = new ArrayList<>(); + // rotate the current window to history store only if the data belongs to the last window + if (windowStart == newWindowStart - windowSize.getMillis()) { + history.addAll(topQueriesStore); + } + topQueriesHistorySnapshot.set(history); + topQueriesStore.clear(); + topQueriesCurrentSnapshot.set(new ArrayList<>()); + windowStart = newWindowStart; + } + } + + /** + * Calculate the window start for the given timestamp + * + * @param timestamp the given timestamp to calculate window start + */ + private long calculateWindowStart(final long timestamp) { + final LocalDateTime currentTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of("UTC")); + LocalDateTime windowStartTime = currentTime.truncatedTo(ChronoUnit.HOURS); + while (!windowStartTime.plusMinutes(windowSize.getMinutes()).isAfter(currentTime)) { + windowStartTime = windowStartTime.plusMinutes(windowSize.getMinutes()); + } + return windowStartTime.toInstant(ZoneOffset.UTC).getEpochSecond() * 1000; + } + + /** + * Get the current top queries snapshot from the AtomicReference. + * + * @return a list of {@link SearchQueryRecord} + */ + public List getTopQueriesCurrentSnapshot() { + return topQueriesCurrentSnapshot.get(); + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java index cb798dd6ed1f4..c1d17edf9ff14 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java @@ -16,6 +16,8 @@ /** * Valid attributes for a search query record + * + * @opensearch.internal */ public enum Attribute { /** @@ -45,21 +47,23 @@ public enum Attribute { /** * Read an Attribute from a StreamInput + * * @param in the StreamInput to read from * @return Attribute * @throws IOException IOException */ - public static Attribute readFromStream(StreamInput in) throws IOException { + static Attribute readFromStream(final StreamInput in) throws IOException { return Attribute.valueOf(in.readString().toUpperCase(Locale.ROOT)); } /** * Write Attribute to a StreamOutput + * * @param out the StreamOutput to write * @param attribute the Attribute to write * @throws IOException IOException */ - public static void writeTo(StreamOutput out, Attribute attribute) throws IOException { + static void writeTo(final StreamOutput out, final Attribute attribute) throws IOException { out.writeString(attribute.toString()); } diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/Measurement.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/Measurement.java deleted file mode 100644 index 5fc27ac745146..0000000000000 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/Measurement.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -package org.opensearch.plugin.insights.rules.model; - -import org.opensearch.core.common.io.stream.StreamInput; -import org.opensearch.core.common.io.stream.StreamOutput; - -import java.io.IOException; -import java.util.Locale; -import java.util.Objects; - -/** - * Represents a measurement with a name and a corresponding value. - * - * @param the type of the value - */ -public class Measurement> { - String name; - T value; - - /** - * Create a Measurement from a StreamInput - * @param in the StreamInput to read from - * @throws IOException IOException - */ - @SuppressWarnings("unchecked") - public Measurement(StreamInput in) throws IOException { - this.name = in.readString(); - this.value = (T) in.readGenericValue(); - } - - /** - * Write Measurement to a StreamOutput - * @param out the StreamOutput to write - * @param measurement the Measurement to write - * @throws IOException IOException - */ - public static void writeTo(StreamOutput out, Measurement measurement) throws IOException { - out.writeString(measurement.getName()); - out.writeGenericValue(measurement.getValue()); - } - - /** - * Constructs a new Measurement with input name and value. - * - * @param name the name of the measurement - * @param value the value of the measurement - */ - public Measurement(String name, T value) { - this.name = name; - this.value = value; - } - - /** - * Returns the name of the measurement. - * - * @return the name of the measurement - */ - public String getName() { - return name; - } - - /** - * Returns the value of the measurement. - * - * @return the value of the measurement - */ - public T getValue() { - return value; - } - - /** - * Compare two measurements on the value - * @param other the other measure to compare - * @return 0 if the value that the two measurements holds are the same - * -1 if the value of the measurement is smaller than the other one - * 1 if the value of the measurement is greater than the other one - */ - @SuppressWarnings("unchecked") - public int compareTo(Measurement other) { - Number otherValue = other.getValue(); - if (value.getClass().equals(otherValue.getClass())) { - return value.compareTo((T) otherValue); - } else { - throw new UnsupportedOperationException( - String.format( - Locale.ROOT, - "comparison between different types are not supported : %s, %s", - value.getClass(), - otherValue.getClass() - ) - ); - } - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Measurement other = (Measurement) o; - return Objects.equals(name, other.name) && Objects.equals(value, other.value); - } - - @Override - public int hashCode() { - return Objects.hash(name, value); - } -} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/MetricType.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/MetricType.java index 2b03bec7a722f..cdd090fbf4804 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/MetricType.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/MetricType.java @@ -13,14 +13,17 @@ import java.io.IOException; import java.util.Arrays; +import java.util.Comparator; import java.util.Locale; import java.util.Set; import java.util.stream.Collectors; /** * Valid metric types for a search query record + * + * @opensearch.internal */ -public enum MetricType { +public enum MetricType implements Comparator { /** * Latency metric type */ @@ -36,30 +39,33 @@ public enum MetricType { /** * Read a MetricType from a StreamInput + * * @param in the StreamInput to read from * @return MetricType * @throws IOException IOException */ - public static MetricType readFromStream(StreamInput in) throws IOException { + public static MetricType readFromStream(final StreamInput in) throws IOException { return fromString(in.readString()); } /** * Create MetricType from String + * * @param metricType the String representation of MetricType * @return MetricType */ - public static MetricType fromString(String metricType) { + public static MetricType fromString(final String metricType) { return MetricType.valueOf(metricType.toUpperCase(Locale.ROOT)); } /** * Write MetricType to a StreamOutput + * * @param out the StreamOutput to write * @param metricType the MetricType to write * @throws IOException IOException */ - public static void writeTo(StreamOutput out, MetricType metricType) throws IOException { + static void writeTo(final StreamOutput out, final MetricType metricType) throws IOException { out.writeString(metricType.toString()); } @@ -70,9 +76,46 @@ public String toString() { /** * Get all valid metrics + * * @return A set of String that contains all valid metrics */ public static Set allMetricTypes() { return Arrays.stream(values()).collect(Collectors.toSet()); } + + /** + * Compare two numbers based on the metric type + * + * @param a the first Number to be compared. + * @param b the second Number to be compared. + * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second + */ + public int compare(final Number a, final Number b) { + switch (this) { + case LATENCY: + return Long.compare(a.longValue(), b.longValue()); + case JVM: + case CPU: + return Double.compare(a.doubleValue(), b.doubleValue()); + } + return -1; + } + + /** + * Parse a value with the correct type based on MetricType + * + * @param o the generic object to parse + * @return {@link Number} + */ + Number parseValue(final Object o) { + switch (this) { + case LATENCY: + return (Long) o; + case JVM: + case CPU: + return (Double) o; + default: + return (Number) o; + } + } } diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/SearchQueryRecord.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/SearchQueryRecord.java index 04392d54af42f..060711edb5580 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/SearchQueryRecord.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/SearchQueryRecord.java @@ -16,28 +16,33 @@ import org.opensearch.core.xcontent.XContentBuilder; import java.io.IOException; +import java.util.HashMap; import java.util.Map; import java.util.Objects; /** - * Simple abstract class that represent record stored in the Query Insight Framework + * SearchQueryRecord represents a minimal atomic record stored in the Query Insight Framework, + * which contains extensive information related to a search query. * * @opensearch.internal */ public class SearchQueryRecord implements ToXContentObject, Writeable { - private final Long timestamp; - private final Map> measurements; + private final long timestamp; + private final Map measurements; private final Map attributes; /** * Constructor of SearchQueryRecord + * * @param in the StreamInput to read the SearchQueryRecord from * @throws IOException IOException * @throws ClassCastException ClassCastException */ public SearchQueryRecord(final StreamInput in) throws IOException, ClassCastException { this.timestamp = in.readLong(); - this.measurements = in.readMap(MetricType::readFromStream, Measurement::new); + measurements = new HashMap<>(); + in.readMap(MetricType::readFromStream, StreamInput::readGenericValue) + .forEach(((metricType, o) -> measurements.put(metricType, metricType.parseValue(o)))); this.attributes = in.readMap(Attribute::readFromStream, StreamInput::readGenericValue); } @@ -48,15 +53,10 @@ public SearchQueryRecord(final StreamInput in) throws IOException, ClassCastExce * @param measurements A list of Measurement associated with this query * @param attributes A list of Attributes associated with this query */ - public SearchQueryRecord( - final Long timestamp, - Map> measurements, - Map attributes - ) { + public SearchQueryRecord(final long timestamp, Map measurements, final Map attributes) { if (measurements == null) { throw new IllegalArgumentException("Measurements cannot be null"); } - this.measurements = measurements; this.attributes = attributes; this.timestamp = timestamp; @@ -77,23 +77,23 @@ public long getTimestamp() { * @param name the name of the measurement * @return the measurement object, or null if not found */ - public Measurement getMeasurement(MetricType name) { + public Number getMeasurement(final MetricType name) { return measurements.get(name); } /** - * Returns an unmodifiable map of all the measurements associated with the metric. + * Returns a map of all the measurements associated with the metric. * - * @return an unmodifiable map of measurement names to measurement objects + * @return a map of measurement names to measurement objects */ - public Map> getMeasurements() { + public Map getMeasurements() { return measurements; } /** - * Returns an unmodifiable map of the attributes associated with the metric. + * Returns a map of the attributes associated with the metric. * - * @return an unmodifiable map of attribute keys to attribute values + * @return a map of attribute keys to attribute values */ public Map getAttributes() { return attributes; @@ -101,39 +101,37 @@ public Map getAttributes() { /** * Add an attribute to this record + * * @param attribute attribute to add * @param value the value associated with the attribute */ - public void addAttribute(Attribute attribute, Object value) { + public void addAttribute(final Attribute attribute, final Object value) { attributes.put(attribute, value); } @Override - public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { + public XContentBuilder toXContent(final XContentBuilder builder, final ToXContent.Params params) throws IOException { builder.startObject(); builder.field("timestamp", timestamp); for (Map.Entry entry : attributes.entrySet()) { builder.field(entry.getKey().toString(), entry.getValue()); } - for (Map.Entry> entry : measurements.entrySet()) { - builder.field(entry.getKey().toString(), entry.getValue().getValue()); + for (Map.Entry entry : measurements.entrySet()) { + builder.field(entry.getKey().toString(), entry.getValue()); } return builder.endObject(); } /** * Write a SearchQueryRecord to a StreamOutput + * * @param out the StreamOutput to write * @throws IOException IOException */ @Override - public void writeTo(StreamOutput out) throws IOException { + public void writeTo(final StreamOutput out) throws IOException { out.writeLong(timestamp); - out.writeMap( - measurements, - (stream, metricType) -> MetricType.writeTo(out, metricType), - (stream, measurement) -> Measurement.writeTo(out, measurement) - ); + out.writeMap(measurements, (stream, metricType) -> MetricType.writeTo(out, metricType), StreamOutput::writeGenericValue); out.writeMap(attributes, (stream, attribute) -> Attribute.writeTo(out, attribute), StreamOutput::writeGenericValue); } @@ -147,24 +145,25 @@ public void writeTo(StreamOutput out) throws IOException { * -1 if the first SearchQueryRecord is numerically less than the second SearchQueryRecord; * 1 if the first SearchQueryRecord is numerically greater than the second SearchQueryRecord. */ - public static int compare(SearchQueryRecord a, SearchQueryRecord b, MetricType metricType) { - return a.getMeasurement(metricType).compareTo(b.getMeasurement(metricType)); + public static int compare(final SearchQueryRecord a, final SearchQueryRecord b, final MetricType metricType) { + return metricType.compare(a.getMeasurement(metricType), b.getMeasurement(metricType)); } /** * Check if a SearchQueryRecord is deep equal to another record + * * @param o the other SearchQueryRecord record * @return true if two records are deep equal, false otherwise. */ @Override - public boolean equals(Object o) { + public boolean equals(final Object o) { if (this == o) { return true; } if (!(o instanceof SearchQueryRecord)) { return false; } - SearchQueryRecord other = (SearchQueryRecord) o; + final SearchQueryRecord other = (SearchQueryRecord) o; return timestamp == other.getTimestamp() && measurements.equals(other.getMeasurements()) && attributes.size() == other.getAttributes().size(); diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/settings/QueryInsightsSettings.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/settings/QueryInsightsSettings.java index 978185cda1268..52cc1fbde790f 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/settings/QueryInsightsSettings.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/settings/QueryInsightsSettings.java @@ -31,6 +31,14 @@ public class QueryInsightsSettings { * Max number of thread */ public static final int MAX_THREAD_COUNT = 5; + /** + * Max number of requests for the consumer to collect at one time + */ + public static final int QUERY_RECORD_QUEUE_CAPACITY = 1000; + /** + * Time interval for record queue consumer to run + */ + public static final TimeValue QUERY_RECORD_QUEUE_DRAIN_INTERVAL = new TimeValue(5, TimeUnit.SECONDS); /** * Default Values and Settings */ @@ -54,7 +62,7 @@ public class QueryInsightsSettings { /** Default N size for top N queries */ public static final int MAX_N_SIZE = 100; /** Default window size in seconds to keep the top N queries with latency data in query insight store */ - public static final int DEFAULT_WINDOW_SIZE = 60; + public static final TimeValue DEFAULT_WINDOW_SIZE = new TimeValue(60, TimeUnit.SECONDS); /** Default top N size to keep the data in query insight store */ public static final int DEFAULT_TOP_N_SIZE = 3; /** @@ -68,7 +76,7 @@ public class QueryInsightsSettings { */ public static final String TOP_QUERIES_BASE_URI = PLUGINS_BASE_URI + "/top_queries"; /** Default prefix for top N queries feature */ - public static final String TOP_N_QUERIES_SETTING_PREFIX = "search.top_n_queries"; + public static final String TOP_N_QUERIES_SETTING_PREFIX = "search.insights.top_queries"; /** Default prefix for top N queries by latency feature */ public static final String TOP_N_LATENCY_QUERIES_PREFIX = TOP_N_QUERIES_SETTING_PREFIX + ".latency"; /** @@ -96,7 +104,7 @@ public class QueryInsightsSettings { */ public static final Setting TOP_N_LATENCY_QUERIES_WINDOW_SIZE = Setting.positiveTimeSetting( TOP_N_LATENCY_QUERIES_PREFIX + ".window_size", - new TimeValue(DEFAULT_WINDOW_SIZE, TimeUnit.SECONDS), + DEFAULT_WINDOW_SIZE, Setting.Property.NodeScope, Setting.Property.Dynamic ); diff --git a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java index 3d99183efab17..04f49d5fbc7dd 100644 --- a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java +++ b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java @@ -13,7 +13,6 @@ import org.opensearch.core.xcontent.ToXContent; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.plugin.insights.rules.model.Attribute; -import org.opensearch.plugin.insights.rules.model.Measurement; import org.opensearch.plugin.insights.rules.model.MetricType; import org.opensearch.plugin.insights.rules.model.SearchQueryRecord; @@ -53,13 +52,13 @@ public static List generateQueryInsightRecords(int lower, int int countOfRecords = randomIntBetween(lower, upper); long timestamp = startTimeStamp; for (int i = 0; i < countOfRecords; ++i) { - Map> measurements = Map.of( + Map measurements = Map.of( MetricType.LATENCY, - new Measurement<>(MetricType.LATENCY.name(), randomLongBetween(1000, 10000)), + randomLongBetween(1000, 10000), MetricType.CPU, - new Measurement<>(MetricType.CPU.name(), randomDouble()), + randomDouble(), MetricType.JVM, - new Measurement<>(MetricType.JVM.name(), randomDouble()) + randomDouble() ); Map phaseLatencyMap = new HashMap<>(); @@ -82,10 +81,7 @@ public static List generateQueryInsightRecords(int lower, int public static SearchQueryRecord createFixedSearchQueryRecord() { long timestamp = 1706574180000L; - Map> measurements = Map.of( - MetricType.LATENCY, - new Measurement<>(MetricType.LATENCY.name(), 1L) - ); + Map measurements = Map.of(MetricType.LATENCY, 1L); Map phaseLatencyMap = new HashMap<>(); Map attributes = new HashMap<>(); diff --git a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java index 2d875375eddc1..c29b48b9690d1 100644 --- a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java +++ b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java @@ -8,10 +8,6 @@ package org.opensearch.plugin.insights.core.service; -import org.opensearch.cluster.coordination.DeterministicTaskQueue; -import org.opensearch.common.settings.Settings; -import org.opensearch.common.unit.TimeValue; -import org.opensearch.node.Node; import org.opensearch.plugin.insights.QueryInsightsTestUtils; import org.opensearch.plugin.insights.rules.model.MetricType; import org.opensearch.plugin.insights.rules.model.SearchQueryRecord; @@ -20,185 +16,34 @@ import org.opensearch.threadpool.ThreadPool; import org.junit.Before; -import java.util.List; -import java.util.concurrent.TimeUnit; +import static org.mockito.Mockito.mock; /** * Unit Tests for {@link QueryInsightsService}. */ public class QueryInsightsServiceTests extends OpenSearchTestCase { - - private DeterministicTaskQueue deterministicTaskQueue; - private ThreadPool threadPool; + private final ThreadPool threadPool = mock(ThreadPool.class); private QueryInsightsService queryInsightsService; @Before public void setup() { - final Settings settings = Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "top n queries tests").build(); - deterministicTaskQueue = new DeterministicTaskQueue(settings, random()); - threadPool = deterministicTaskQueue.getThreadPool(); queryInsightsService = new QueryInsightsService(threadPool); queryInsightsService.enableCollection(MetricType.LATENCY, true); queryInsightsService.enableCollection(MetricType.CPU, true); queryInsightsService.enableCollection(MetricType.JVM, true); - queryInsightsService.setTopNSize(Integer.MAX_VALUE); - queryInsightsService.setWindowSize(new TimeValue(Long.MAX_VALUE)); - } - - public void testIngestQueryDataWithLargeWindow() { - final List records = QueryInsightsTestUtils.generateQueryInsightRecords(10); - for (SearchQueryRecord record : records) { - queryInsightsService.addRecord(record); - } - runUntilTimeoutOrFinish(deterministicTaskQueue, 5000); - assertTrue( - QueryInsightsTestUtils.checkRecordsEqualsWithoutOrder( - queryInsightsService.getTopNRecords(MetricType.LATENCY, false), - records, - MetricType.LATENCY - ) - ); - } - - public void testConcurrentIngestQueryDataWithLargeWindow() { - final List records = QueryInsightsTestUtils.generateQueryInsightRecords(10); - - int numRequests = records.size(); - for (int i = 0; i < numRequests; i++) { - int finalI = i; - threadPool.schedule(() -> { queryInsightsService.addRecord(records.get(finalI)); }, TimeValue.ZERO, ThreadPool.Names.GENERIC); - } - runUntilTimeoutOrFinish(deterministicTaskQueue, 5000); - assertTrue( - QueryInsightsTestUtils.checkRecordsEqualsWithoutOrder( - queryInsightsService.getTopNRecords(MetricType.LATENCY, false), - records, - MetricType.LATENCY - ) - ); } - public void testRollingWindow() { - // Create records with starting timestamp Monday, January 1, 2024 8:13:23 PM, with interval 10 minutes - final List records = QueryInsightsTestUtils.generateQueryInsightRecords(10, 10, 1704140003000L, 1000 * 60 * 10); - queryInsightsService.setWindowSize(TimeValue.timeValueMinutes(10)); - queryInsightsService.addRecord(records.get(0)); - runUntilTimeoutOrFinish(deterministicTaskQueue, 5000); - assertEquals(1, queryInsightsService.getTopNStoreSize(MetricType.LATENCY)); - assertEquals(0, queryInsightsService.getTopNHistoryStoreSize(MetricType.LATENCY)); - for (SearchQueryRecord record : records.subList(1, records.size())) { - queryInsightsService.addRecord(record); - runUntilTimeoutOrFinish(deterministicTaskQueue, 5000); - assertEquals(1, queryInsightsService.getTopNStoreSize(MetricType.LATENCY)); - assertEquals(1, queryInsightsService.getTopNHistoryStoreSize(MetricType.LATENCY)); + public void testAddRecordToLimitAndDrain() { + SearchQueryRecord record = QueryInsightsTestUtils.generateQueryInsightRecords(1, 1, System.currentTimeMillis(), 0).get(0); + for (int i = 0; i < QueryInsightsSettings.QUERY_RECORD_QUEUE_CAPACITY; i++) { + assertTrue(queryInsightsService.addRecord(record)); } - assertEquals(0, queryInsightsService.getTopNRecords(MetricType.LATENCY, false).size()); - } - - public void testRollingWindowWithHistory() { - // Create 2 records with starting Now and last 10 minutes - final List records = QueryInsightsTestUtils.generateQueryInsightRecords( - 2, - 2, - System.currentTimeMillis() - 1000 * 60 * 10, - 1000 * 60 * 10 - ); - queryInsightsService.setWindowSize(TimeValue.timeValueMinutes(3)); - queryInsightsService.addRecord(records.get(0)); - runUntilTimeoutOrFinish(deterministicTaskQueue, 5000); - assertEquals(1, queryInsightsService.getTopNStoreSize(MetricType.LATENCY)); - assertEquals(0, queryInsightsService.getTopNHistoryStoreSize(MetricType.LATENCY)); - queryInsightsService.addRecord(records.get(1)); - runUntilTimeoutOrFinish(deterministicTaskQueue, 5000); - assertEquals(1, queryInsightsService.getTopNStoreSize(MetricType.LATENCY)); - assertEquals(0, queryInsightsService.getTopNHistoryStoreSize(MetricType.LATENCY)); - assertEquals(1, queryInsightsService.getTopNRecords(MetricType.LATENCY, true).size()); - } - - public void testSmallWindowClearOutdatedData() { - final List records = QueryInsightsTestUtils.generateQueryInsightRecords( - 2, - 2, - System.currentTimeMillis(), - 1000 * 60 * 20 + // exceed capacity + assertFalse(queryInsightsService.addRecord(record)); + queryInsightsService.drainRecords(); + assertEquals( + QueryInsightsSettings.DEFAULT_TOP_N_SIZE, + queryInsightsService.getTopQueriesService(MetricType.LATENCY).getTopQueriesRecords(false).size() ); - queryInsightsService.setWindowSize(TimeValue.timeValueMinutes(10)); - - for (SearchQueryRecord record : records) { - queryInsightsService.addRecord(record); - } - runUntilTimeoutOrFinish(deterministicTaskQueue, 5000); - assertTrue(queryInsightsService.getTopNRecords(MetricType.LATENCY, false).size() <= 1); - } - - public void testSmallNSize() { - final List records = QueryInsightsTestUtils.generateQueryInsightRecords(10); - queryInsightsService.setTopNSize(1); - - for (SearchQueryRecord record : records) { - queryInsightsService.addRecord(record); - } - runUntilTimeoutOrFinish(deterministicTaskQueue, 5000); - assertEquals(1, queryInsightsService.getTopNRecords(MetricType.LATENCY, false).size()); - } - - public void testSmallNSizeWithCPU() { - final List records = QueryInsightsTestUtils.generateQueryInsightRecords(10); - queryInsightsService.setTopNSize(1); - - for (SearchQueryRecord record : records) { - queryInsightsService.addRecord(record); - } - runUntilTimeoutOrFinish(deterministicTaskQueue, 5000); - assertEquals(1, queryInsightsService.getTopNRecords(MetricType.CPU, false).size()); - } - - public void testSmallNSizeWithJVM() { - final List records = QueryInsightsTestUtils.generateQueryInsightRecords(10); - queryInsightsService.setTopNSize(1); - - for (SearchQueryRecord record : records) { - queryInsightsService.addRecord(record); - } - runUntilTimeoutOrFinish(deterministicTaskQueue, 5000); - assertEquals(1, queryInsightsService.getTopNRecords(MetricType.JVM, false).size()); - } - - public void testValidateTopNSize() { - assertThrows( - IllegalArgumentException.class, - () -> { queryInsightsService.validateTopNSize(QueryInsightsSettings.MAX_N_SIZE + 1); } - ); - } - - public void testGetTopQueriesWhenNotEnabled() { - queryInsightsService.enableCollection(MetricType.LATENCY, false); - assertThrows(IllegalArgumentException.class, () -> { queryInsightsService.getTopNRecords(MetricType.LATENCY, false); }); - } - - public void testValidateWindowSize() { - assertThrows(IllegalArgumentException.class, () -> { - queryInsightsService.validateWindowSize( - new TimeValue(QueryInsightsSettings.MAX_WINDOW_SIZE.getSeconds() + 1, TimeUnit.SECONDS) - ); - }); - assertThrows(IllegalArgumentException.class, () -> { - queryInsightsService.validateWindowSize( - new TimeValue(QueryInsightsSettings.MIN_WINDOW_SIZE.getSeconds() - 1, TimeUnit.SECONDS) - ); - }); - assertThrows(IllegalArgumentException.class, () -> { queryInsightsService.validateWindowSize(new TimeValue(2, TimeUnit.DAYS)); }); - } - - private static void runUntilTimeoutOrFinish(DeterministicTaskQueue deterministicTaskQueue, long duration) { - final long endTime = deterministicTaskQueue.getCurrentTimeMillis() + duration; - while (deterministicTaskQueue.getCurrentTimeMillis() < endTime - && (deterministicTaskQueue.hasRunnableTasks() || deterministicTaskQueue.hasDeferredTasks())) { - if (deterministicTaskQueue.hasDeferredTasks() && randomBoolean()) { - deterministicTaskQueue.advanceTime(); - } else if (deterministicTaskQueue.hasRunnableTasks()) { - deterministicTaskQueue.runRandomTask(); - } - } } } diff --git a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/TopQueriesServiceTests.java b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/TopQueriesServiceTests.java new file mode 100644 index 0000000000000..060df84a89485 --- /dev/null +++ b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/TopQueriesServiceTests.java @@ -0,0 +1,102 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.plugin.insights.core.service; + +import org.opensearch.cluster.coordination.DeterministicTaskQueue; +import org.opensearch.common.unit.TimeValue; +import org.opensearch.plugin.insights.QueryInsightsTestUtils; +import org.opensearch.plugin.insights.rules.model.MetricType; +import org.opensearch.plugin.insights.rules.model.SearchQueryRecord; +import org.opensearch.plugin.insights.settings.QueryInsightsSettings; +import org.opensearch.test.OpenSearchTestCase; +import org.junit.Before; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Unit Tests for {@link QueryInsightsService}. + */ +public class TopQueriesServiceTests extends OpenSearchTestCase { + private TopQueriesService topQueriesService; + + @Before + public void setup() { + topQueriesService = new TopQueriesService(MetricType.LATENCY); + topQueriesService.setTopNSize(Integer.MAX_VALUE); + topQueriesService.setWindowSize(new TimeValue(Long.MAX_VALUE)); + topQueriesService.setEnabled(true); + } + + public void testIngestQueryDataWithLargeWindow() { + final List records = QueryInsightsTestUtils.generateQueryInsightRecords(10); + topQueriesService.consumeRecords(records); + assertTrue( + QueryInsightsTestUtils.checkRecordsEqualsWithoutOrder( + topQueriesService.getTopQueriesRecords(false), + records, + MetricType.LATENCY + ) + ); + } + + public void testRollingWindows() { + List records; + // Create 5 records at Now - 10 minutes to make sure they belong to the last window + records = QueryInsightsTestUtils.generateQueryInsightRecords(5, 5, System.currentTimeMillis() - 1000 * 60 * 10, 0); + topQueriesService.setWindowSize(TimeValue.timeValueMinutes(10)); + topQueriesService.consumeRecords(records); + assertEquals(0, topQueriesService.getTopQueriesRecords(true).size()); + + // Create 10 records at now + 1 minute, to make sure they belong to the current window + records = QueryInsightsTestUtils.generateQueryInsightRecords(10, 10, System.currentTimeMillis() + 1000 * 60, 0); + topQueriesService.setWindowSize(TimeValue.timeValueMinutes(10)); + topQueriesService.consumeRecords(records); + assertEquals(10, topQueriesService.getTopQueriesRecords(true).size()); + } + + public void testSmallNSize() { + final List records = QueryInsightsTestUtils.generateQueryInsightRecords(10); + topQueriesService.setTopNSize(1); + topQueriesService.consumeRecords(records); + assertEquals(1, topQueriesService.getTopQueriesRecords(false).size()); + } + + public void testValidateTopNSize() { + assertThrows(IllegalArgumentException.class, () -> { topQueriesService.validateTopNSize(QueryInsightsSettings.MAX_N_SIZE + 1); }); + } + + public void testGetTopQueriesWhenNotEnabled() { + topQueriesService.setEnabled(false); + assertThrows(IllegalArgumentException.class, () -> { topQueriesService.getTopQueriesRecords(false); }); + } + + public void testValidateWindowSize() { + assertThrows(IllegalArgumentException.class, () -> { + topQueriesService.validateWindowSize(new TimeValue(QueryInsightsSettings.MAX_WINDOW_SIZE.getSeconds() + 1, TimeUnit.SECONDS)); + }); + assertThrows(IllegalArgumentException.class, () -> { + topQueriesService.validateWindowSize(new TimeValue(QueryInsightsSettings.MIN_WINDOW_SIZE.getSeconds() - 1, TimeUnit.SECONDS)); + }); + assertThrows(IllegalArgumentException.class, () -> { topQueriesService.validateWindowSize(new TimeValue(2, TimeUnit.DAYS)); }); + assertThrows(IllegalArgumentException.class, () -> { topQueriesService.validateWindowSize(new TimeValue(7, TimeUnit.MINUTES)); }); + } + + private static void runUntilTimeoutOrFinish(DeterministicTaskQueue deterministicTaskQueue, long duration) { + final long endTime = deterministicTaskQueue.getCurrentTimeMillis() + duration; + while (deterministicTaskQueue.getCurrentTimeMillis() < endTime + && (deterministicTaskQueue.hasRunnableTasks() || deterministicTaskQueue.hasDeferredTasks())) { + if (deterministicTaskQueue.hasDeferredTasks() && randomBoolean()) { + deterministicTaskQueue.advanceTime(); + } else if (deterministicTaskQueue.hasRunnableTasks()) { + deterministicTaskQueue.runRandomTask(); + } + } + } +}