Skip to content

Commit a4ae050

Browse files
[Manual backport 2.x] Add default index template for query insights local index (#255) (#276)
* Add default index template for query insights local index * Add auto_expand to index template settings --------- (cherry picked from commit 4b24828) Signed-off-by: Chenyang Ji <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 36cc79b commit a4ae050

File tree

9 files changed

+619
-136
lines changed

9 files changed

+619
-136
lines changed

Diff for: src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public List<Setting<?>> getSettings() {
148148
QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING,
149149
QueryInsightsSettings.TOP_N_EXPORTER_DELETE_AFTER,
150150
QueryInsightsSettings.TOP_N_EXPORTER_TYPE,
151+
QueryInsightsSettings.TOP_N_EXPORTER_TEMPLATE_PRIORITY,
151152
QueryCategorizationSettings.SEARCH_QUERY_FIELD_TYPE_CACHE_SIZE_KEY
152153
);
153154
}

Diff for: src/main/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporter.java

+225-66
Large diffs are not rendered by default.

Diff for: src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactory.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
package org.opensearch.plugin.insights.core.exporter;
1010

11+
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.DEFAULT_TEMPLATE_PRIORITY;
12+
1113
import java.io.IOException;
1214
import java.time.format.DateTimeFormatter;
1315
import java.util.HashMap;
@@ -74,20 +76,34 @@ public void validateExporterType(final String exporterType) throws IllegalArgume
7476
* @param id id of the exporter so that exporters can be retrieved and reused across services
7577
* @param indexPattern the index pattern if creating an index exporter
7678
* @param indexMapping index mapping file
79+
* @param templatePriority the priority value for the template
7780
* @return LocalIndexExporter the created exporter sink
7881
*/
79-
public LocalIndexExporter createLocalIndexExporter(String id, String indexPattern, String indexMapping) {
82+
public LocalIndexExporter createLocalIndexExporter(String id, String indexPattern, String indexMapping, long templatePriority) {
8083
LocalIndexExporter exporter = new LocalIndexExporter(
8184
client,
8285
clusterService,
8386
DateTimeFormatter.ofPattern(indexPattern, Locale.ROOT),
8487
indexMapping,
8588
id
8689
);
90+
exporter.setTemplatePriority(templatePriority);
8791
this.exporters.put(id, exporter);
8892
return exporter;
8993
}
9094

95+
/**
96+
* Create a local index exporter based on provided parameters, using default template priority
97+
*
98+
* @param id id of the exporter so that exporters can be retrieved and reused across services
99+
* @param indexPattern the index pattern if creating an index exporter
100+
* @param indexMapping index mapping file
101+
* @return LocalIndexExporter the created exporter sink
102+
*/
103+
public LocalIndexExporter createLocalIndexExporter(String id, String indexPattern, String indexMapping) {
104+
return createLocalIndexExporter(id, indexPattern, indexMapping, DEFAULT_TEMPLATE_PRIORITY);
105+
}
106+
91107
/**
92108
* Create a debug exporter based on provided parameters
93109
*
@@ -105,11 +121,14 @@ public DebugExporter createDebugExporter(String id) {
105121
*
106122
* @param exporter The exporter to update
107123
* @param indexPattern the index pattern if creating a index exporter
124+
* @param templatePriority the priority value for the template (for LocalIndexExporter)
108125
* @return QueryInsightsExporter the updated exporter sink
109126
*/
110-
public QueryInsightsExporter updateExporter(QueryInsightsExporter exporter, String indexPattern) {
127+
public QueryInsightsExporter updateExporter(QueryInsightsExporter exporter, String indexPattern, long templatePriority) {
111128
if (exporter.getClass() == LocalIndexExporter.class) {
112-
((LocalIndexExporter) exporter).setIndexPattern(DateTimeFormatter.ofPattern(indexPattern, Locale.ROOT));
129+
LocalIndexExporter localExporter = (LocalIndexExporter) exporter;
130+
localExporter.setIndexPattern(DateTimeFormatter.ofPattern(indexPattern, Locale.ROOT));
131+
localExporter.setTemplatePriority(templatePriority);
113132
}
114133
return exporter;
115134
}

Diff for: src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java

+36
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.MIN_DELETE_AFTER_VALUE;
1818
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.QUERY_INSIGHTS_EXECUTOR;
1919
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_EXPORTER_DELETE_AFTER;
20+
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_EXPORTER_TEMPLATE_PRIORITY;
2021
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_EXPORTER_TYPE;
2122
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_QUERIES_INDEX_PATTERN_GLOB;
2223

@@ -178,9 +179,12 @@ public QueryInsightsService(
178179
(this::setExporterDeleteAfterAndDelete),
179180
(this::validateExporterDeleteAfter)
180181
);
182+
clusterService.getClusterSettings()
183+
.addSettingsUpdateConsumer(TOP_N_EXPORTER_TEMPLATE_PRIORITY, this::setTemplatePriority, this::validateTemplatePriority);
181184

182185
this.setExporterDeleteAfterAndDelete(clusterService.getClusterSettings().get(TOP_N_EXPORTER_DELETE_AFTER));
183186
this.setExporterAndReaderType(SinkType.parse(clusterService.getClusterSettings().get(TOP_N_EXPORTER_TYPE)));
187+
this.setTemplatePriority(clusterService.getClusterSettings().get(TOP_N_EXPORTER_TEMPLATE_PRIORITY));
184188

185189
this.searchQueryCategorizer = SearchQueryCategorizer.getInstance(metricsRegistry);
186190
this.enableSearchQueryMetricsFeature(false);
@@ -531,6 +535,38 @@ public void validateExporterType(final String exporterType) {
531535
queryInsightsExporterFactory.validateExporterType(exporterType);
532536
}
533537

538+
/**
539+
* Set the template priority for all top queries exporters
540+
*
541+
* @param templatePriority the priority value to use for templates
542+
*/
543+
public void setTemplatePriority(final long templatePriority) {
544+
logger.info("Setting query insights index template priority to [{}]", templatePriority);
545+
final QueryInsightsExporter topQueriesExporter = queryInsightsExporterFactory.getExporter(TOP_QUERIES_EXPORTER_ID);
546+
if (topQueriesExporter != null && topQueriesExporter.getClass() == LocalIndexExporter.class) {
547+
logger.debug("Updating query insights index template priority for top queries exporter to [{}]", templatePriority);
548+
queryInsightsExporterFactory.updateExporter(
549+
topQueriesExporter,
550+
QueryInsightsSettings.DEFAULT_TOP_N_QUERIES_INDEX_PATTERN,
551+
templatePriority
552+
);
553+
}
554+
}
555+
556+
/**
557+
* Validate the template priority
558+
*
559+
* @param templatePriority the priority value to validate
560+
*/
561+
public void validateTemplatePriority(final long templatePriority) {
562+
if (templatePriority < 0) {
563+
OperationalMetricsCounter.getInstance().incrementCounter(OperationalMetric.INVALID_EXPORTER_TYPE_FAILURES);
564+
throw new IllegalArgumentException(
565+
String.format(Locale.ROOT, "Invalid template priority setting [%d], value should be a non-negative long.", templatePriority)
566+
);
567+
}
568+
}
569+
534570
@Override
535571
protected void doStart() {
536572
if (isAnyFeatureEnabled()) {

Diff for: src/main/java/org/opensearch/plugin/insights/settings/QueryInsightsSettings.java

+15
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ public class QueryInsightsSettings {
230230
* Default exporter type of top queries
231231
*/
232232
public static final String DEFAULT_TOP_QUERIES_EXPORTER_TYPE = SinkType.LOCAL_INDEX.toString();
233+
/**
234+
* Default template priority for top queries indices
235+
*/
236+
public static final long DEFAULT_TEMPLATE_PRIORITY = 1847L;
233237
/**
234238
* Default Top N local indices retention period in days
235239
*/
@@ -271,6 +275,17 @@ public class QueryInsightsSettings {
271275
*/
272276
public static final String TOP_QUERIES_INDEX_PATTERN_GLOB = "top_queries-*";
273277

278+
/**
279+
* Setting for Top N local indices template priority
280+
*/
281+
public static final Setting<Long> TOP_N_EXPORTER_TEMPLATE_PRIORITY = Setting.longSetting(
282+
TOP_N_QUERIES_EXPORTER_PREFIX + ".template_priority",
283+
DEFAULT_TEMPLATE_PRIORITY,
284+
0L, // min value is 0
285+
Setting.Property.Dynamic,
286+
Setting.Property.NodeScope
287+
);
288+
274289
/**
275290
* Get the enabled setting based on type
276291
* @param type MetricType

Diff for: src/test/java/org/opensearch/plugin/insights/QueryInsightsPluginTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public void testGetSettings() {
8181
QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING,
8282
QueryInsightsSettings.TOP_N_EXPORTER_DELETE_AFTER,
8383
QueryInsightsSettings.TOP_N_EXPORTER_TYPE,
84+
QueryInsightsSettings.TOP_N_EXPORTER_TEMPLATE_PRIORITY,
8485
QueryCategorizationSettings.SEARCH_QUERY_FIELD_TYPE_CACHE_SIZE_KEY
8586
),
8687
queryInsightsPlugin.getSettings()

Diff for: src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java

+1
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ public static void registerAllQueryInsightsSettings(ClusterSettings clusterSetti
354354
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_NAME);
355355
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_TYPE);
356356
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_EXPORTER_DELETE_AFTER);
357+
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_EXPORTER_TEMPLATE_PRIORITY);
357358
clusterSettings.registerSetting(QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING);
358359
}
359360
}

0 commit comments

Comments
 (0)