|
| 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.plugin.insights.core.service; |
| 10 | + |
| 11 | +import org.opensearch.common.inject.Inject; |
| 12 | +import org.opensearch.common.lifecycle.AbstractLifecycleComponent; |
| 13 | +import org.opensearch.plugin.insights.rules.model.MetricType; |
| 14 | +import org.opensearch.plugin.insights.rules.model.SearchQueryRecord; |
| 15 | +import org.opensearch.plugin.insights.settings.QueryInsightsSettings; |
| 16 | +import org.opensearch.threadpool.Scheduler; |
| 17 | +import org.opensearch.threadpool.ThreadPool; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Comparator; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.concurrent.LinkedBlockingQueue; |
| 25 | + |
| 26 | +/** |
| 27 | + * Service responsible for gathering, analyzing, storing and exporting |
| 28 | + * information related to search queries |
| 29 | + * |
| 30 | + * @opensearch.internal |
| 31 | + */ |
| 32 | +public class QueryInsightsService extends AbstractLifecycleComponent { |
| 33 | + /** |
| 34 | + * The internal OpenSearch thread pool that execute async processing and exporting tasks |
| 35 | + */ |
| 36 | + private final ThreadPool threadPool; |
| 37 | + |
| 38 | + /** |
| 39 | + * Services to capture top n queries for different metric types |
| 40 | + */ |
| 41 | + private final Map<MetricType, TopQueriesService> topQueriesServices; |
| 42 | + |
| 43 | + /** |
| 44 | + * Flags for enabling insight data collection for different metric types |
| 45 | + */ |
| 46 | + private final Map<MetricType, Boolean> enableCollect; |
| 47 | + |
| 48 | + /** |
| 49 | + * The internal thread-safe queue to ingest the search query data and subsequently forward to processors |
| 50 | + */ |
| 51 | + private final LinkedBlockingQueue<SearchQueryRecord> queryRecordsQueue; |
| 52 | + |
| 53 | + /** |
| 54 | + * Holds a reference to delayed operation {@link Scheduler.Cancellable} so it can be cancelled when |
| 55 | + * the service closed concurrently. |
| 56 | + */ |
| 57 | + protected volatile Scheduler.Cancellable scheduledFuture; |
| 58 | + |
| 59 | + /** |
| 60 | + * Constructor of the QueryInsightsService |
| 61 | + * |
| 62 | + * @param threadPool The OpenSearch thread pool to run async tasks |
| 63 | + */ |
| 64 | + @Inject |
| 65 | + public QueryInsightsService(final ThreadPool threadPool) { |
| 66 | + enableCollect = new HashMap<>(); |
| 67 | + queryRecordsQueue = new LinkedBlockingQueue<>(QueryInsightsSettings.QUERY_RECORD_QUEUE_CAPACITY); |
| 68 | + topQueriesServices = new HashMap<>(); |
| 69 | + for (MetricType metricType : MetricType.allMetricTypes()) { |
| 70 | + enableCollect.put(metricType, false); |
| 71 | + topQueriesServices.put(metricType, new TopQueriesService(metricType)); |
| 72 | + } |
| 73 | + this.threadPool = threadPool; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Ingest the query data into in-memory stores |
| 78 | + * |
| 79 | + * @param record the record to ingest |
| 80 | + */ |
| 81 | + public boolean addRecord(final SearchQueryRecord record) { |
| 82 | + boolean shouldAdd = false; |
| 83 | + for (Map.Entry<MetricType, TopQueriesService> entry : topQueriesServices.entrySet()) { |
| 84 | + if (!enableCollect.get(entry.getKey())) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + List<SearchQueryRecord> currentSnapshot = entry.getValue().getTopQueriesCurrentSnapshot(); |
| 88 | + // skip add to top N queries store if the incoming record is smaller than the Nth record |
| 89 | + if (currentSnapshot.size() < entry.getValue().getTopNSize() |
| 90 | + || SearchQueryRecord.compare(record, currentSnapshot.get(0), entry.getKey()) > 0) { |
| 91 | + shouldAdd = true; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + if (shouldAdd) { |
| 96 | + return queryRecordsQueue.offer(record); |
| 97 | + } |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Drain the queryRecordsQueue into internal stores and services |
| 103 | + */ |
| 104 | + public void drainRecords() { |
| 105 | + final List<SearchQueryRecord> records = new ArrayList<>(); |
| 106 | + queryRecordsQueue.drainTo(records); |
| 107 | + records.sort(Comparator.comparingLong(SearchQueryRecord::getTimestamp)); |
| 108 | + for (MetricType metricType : MetricType.allMetricTypes()) { |
| 109 | + if (enableCollect.get(metricType)) { |
| 110 | + // ingest the records into topQueriesService |
| 111 | + topQueriesServices.get(metricType).consumeRecords(records); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Get the top queries service based on metricType |
| 118 | + * @param metricType {@link MetricType} |
| 119 | + * @return {@link TopQueriesService} |
| 120 | + */ |
| 121 | + public TopQueriesService getTopQueriesService(final MetricType metricType) { |
| 122 | + return topQueriesServices.get(metricType); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Set flag to enable or disable Query Insights data collection |
| 127 | + * |
| 128 | + * @param metricType {@link MetricType} |
| 129 | + * @param enable Flag to enable or disable Query Insights data collection |
| 130 | + */ |
| 131 | + public void enableCollection(final MetricType metricType, final boolean enable) { |
| 132 | + this.enableCollect.put(metricType, enable); |
| 133 | + this.topQueriesServices.get(metricType).setEnabled(enable); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Get if the Query Insights data collection is enabled for a MetricType |
| 138 | + * |
| 139 | + * @param metricType {@link MetricType} |
| 140 | + * @return if the Query Insights data collection is enabled |
| 141 | + */ |
| 142 | + public boolean isCollectionEnabled(final MetricType metricType) { |
| 143 | + return this.enableCollect.get(metricType); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Check if query insights service is enabled |
| 148 | + * |
| 149 | + * @return if query insights service is enabled |
| 150 | + */ |
| 151 | + public boolean isEnabled() { |
| 152 | + for (MetricType t : MetricType.allMetricTypes()) { |
| 153 | + if (isCollectionEnabled(t)) { |
| 154 | + return true; |
| 155 | + } |
| 156 | + } |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + protected void doStart() { |
| 162 | + if (isEnabled()) { |
| 163 | + scheduledFuture = threadPool.scheduleWithFixedDelay( |
| 164 | + this::drainRecords, |
| 165 | + QueryInsightsSettings.QUERY_RECORD_QUEUE_DRAIN_INTERVAL, |
| 166 | + QueryInsightsSettings.QUERY_INSIGHTS_EXECUTOR |
| 167 | + ); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + protected void doStop() { |
| 173 | + if (scheduledFuture != null) { |
| 174 | + scheduledFuture.cancel(); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + protected void doClose() {} |
| 180 | +} |
0 commit comments