Skip to content

Commit

Permalink
rebase from master to pick up query insights plugin changes
Browse files Browse the repository at this point in the history
Signed-off-by: Chenyang Ji <cyji@amazon.com>
  • Loading branch information
ansjcy committed Feb 6, 2024
1 parent 409fc92 commit 1895133
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testGetTopQueriesWhenFeatureDisabled() {
TopQueriesResponse response = OpenSearchIntegTestCase.client().execute(TopQueriesAction.INSTANCE, request).actionGet();
Assert.assertNotEquals(0, response.failures().size());
Assert.assertEquals(
"Cannot get top n queries when [search.top_n_queries.latency.enabled] is not enabled.",
"Cannot get top n queries for [latency] when it is not enabled.",
response.failures().get(0).getCause().getCause().getMessage()
);
}
Expand Down Expand Up @@ -121,7 +121,7 @@ public void testUpdateRecordWhenFeatureDisabledThenEnabled() throws ExecutionExc
TopQueriesResponse response = OpenSearchIntegTestCase.client().execute(TopQueriesAction.INSTANCE, request).actionGet();
Assert.assertNotEquals(0, response.failures().size());
Assert.assertEquals(
"Cannot get top n queries when [search.top_n_queries.latency.enabled] is not enabled.",
"Cannot get top n queries for [latency] when it is not enabled.",
response.failures().get(0).getCause().getCause().getMessage()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.env.Environment;
import org.opensearch.env.NodeEnvironment;
import org.opensearch.plugin.insights.core.listener.QueryInsightsListener;
import org.opensearch.plugin.insights.core.service.QueryInsightsService;
import org.opensearch.plugin.insights.rules.action.top_queries.TopQueriesAction;
import org.opensearch.plugin.insights.rules.resthandler.top_queries.RestTopQueriesAction;
import org.opensearch.plugin.insights.rules.transport.top_queries.TransportTopQueriesAction;
import org.opensearch.plugin.insights.settings.QueryInsightsSettings;
import org.opensearch.plugins.ActionPlugin;
import org.opensearch.plugins.Plugin;
Expand Down Expand Up @@ -67,7 +71,7 @@ public Collection<Object> createComponents(
) {
// create top n queries service
final QueryInsightsService queryInsightsService = new QueryInsightsService(threadPool);
return List.of(queryInsightsService);
return List.of(queryInsightsService, new QueryInsightsListener(clusterService, queryInsightsService));
}

@Override
Expand All @@ -92,12 +96,12 @@ public List<RestHandler> getRestHandlers(
final IndexNameExpressionResolver indexNameExpressionResolver,
final Supplier<DiscoveryNodes> nodesInCluster
) {
return List.of();
return List.of(new RestTopQueriesAction());
}

@Override
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
return List.of();
return List.of(new ActionPlugin.ActionHandler<>(TopQueriesAction.INSTANCE, TransportTopQueriesAction.class));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.plugin.insights.core.listener.QueryInsightsListener;
import org.opensearch.plugin.insights.core.service.QueryInsightsService;
import org.opensearch.plugin.insights.rules.action.top_queries.TopQueriesAction;
import org.opensearch.plugin.insights.rules.resthandler.top_queries.RestTopQueriesAction;
import org.opensearch.plugin.insights.settings.QueryInsightsSettings;
import org.opensearch.plugins.ActionPlugin;
import org.opensearch.rest.RestHandler;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.threadpool.ExecutorBuilder;
import org.opensearch.threadpool.ScalingExecutorBuilder;
import org.opensearch.threadpool.ThreadPool;
import org.junit.Before;

Expand Down Expand Up @@ -74,18 +79,29 @@ public void testCreateComponent() {
null,
null
);
assertEquals(1, components.size());
assertEquals(2, components.size());
assertTrue(components.get(0) instanceof QueryInsightsService);
assertTrue(components.get(1) instanceof QueryInsightsListener);
}

public void testGetExecutorBuilders() {
Settings.Builder settingsBuilder = Settings.builder();
Settings settings = settingsBuilder.build();
List<ExecutorBuilder<?>> executorBuilders = queryInsightsPlugin.getExecutorBuilders(settings);
assertEquals(1, executorBuilders.size());
assertTrue(executorBuilders.get(0) instanceof ScalingExecutorBuilder);
}

public void testGetRestHandlers() {
List<RestHandler> components = queryInsightsPlugin.getRestHandlers(Settings.EMPTY, null, null, null, null, null, null);
assertEquals(0, components.size());
assertEquals(1, components.size());
assertTrue(components.get(0) instanceof RestTopQueriesAction);
}

public void testGetActions() {
List<ActionPlugin.ActionHandler<? extends ActionRequest, ? extends ActionResponse>> components = queryInsightsPlugin.getActions();
assertEquals(0, components.size());
assertEquals(1, components.size());
assertTrue(components.get(0).getAction() instanceof TopQueriesAction);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
package org.opensearch.plugin.insights;

import org.opensearch.action.search.SearchType;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.util.Maps;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.plugin.insights.rules.action.top_queries.TopQueries;
import org.opensearch.plugin.insights.rules.model.Attribute;
import org.opensearch.plugin.insights.rules.model.MetricType;
import org.opensearch.plugin.insights.rules.model.SearchQueryRecord;
import org.opensearch.test.VersionUtils;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -26,7 +29,11 @@
import java.util.Set;
import java.util.TreeSet;

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.opensearch.test.OpenSearchTestCase.buildNewFakeTransportAddress;
import static org.opensearch.test.OpenSearchTestCase.random;
import static org.opensearch.test.OpenSearchTestCase.randomAlphaOfLengthBetween;
import static org.opensearch.test.OpenSearchTestCase.randomArray;
import static org.opensearch.test.OpenSearchTestCase.randomDouble;
Expand Down Expand Up @@ -79,6 +86,33 @@ public static List<SearchQueryRecord> generateQueryInsightRecords(int lower, int
return records;
}

public static TopQueries createRandomTopQueries() {
DiscoveryNode node = new DiscoveryNode(
"node_for_top_queries_test",
buildNewFakeTransportAddress(),
emptyMap(),
emptySet(),
VersionUtils.randomVersion(random())
);
List<SearchQueryRecord> records = generateQueryInsightRecords(10);

return new TopQueries(node, records);
}

public static TopQueries createFixedTopQueries() {
DiscoveryNode node = new DiscoveryNode(
"node_for_top_queries_test",
buildNewFakeTransportAddress(),
emptyMap(),
emptySet(),
VersionUtils.randomVersion(random())
);
List<SearchQueryRecord> records = new ArrayList<>();
records.add(createFixedSearchQueryRecord());

return new TopQueries(node, records);
}

public static SearchQueryRecord createFixedSearchQueryRecord() {
long timestamp = 1706574180000L;
Map<MetricType, Number> measurements = Map.of(MetricType.LATENCY, 1L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
package org.opensearch.plugin.insights.rules.resthandler.top_queries;

import org.opensearch.plugin.insights.rules.action.top_queries.TopQueriesRequest;
import org.opensearch.rest.RestHandler;
import org.opensearch.rest.RestRequest;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.rest.FakeRestRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

Expand All @@ -24,7 +26,6 @@ public class RestTopQueriesActionTests extends OpenSearchTestCase {
public void testEmptyNodeIdsValidType() {
Map<String, String> params = new HashMap<>();
params.put("type", randomFrom(ALLOWED_METRICS));

RestRequest restRequest = buildRestRequest(params);
TopQueriesRequest actual = RestTopQueriesAction.prepareRequest(restRequest);
assertEquals(0, actual.nodesIds().length);
Expand Down Expand Up @@ -53,6 +54,13 @@ public void testInValidType() {
);
}

public void testGetRoutes() {
RestTopQueriesAction action = new RestTopQueriesAction();
List<RestHandler.Route> routes = action.routes();
assertEquals(2, routes.size());
assertEquals("query_insights_top_queries_action", action.getName());
}

private FakeRestRequest buildRestRequest(Map<String, String> params) {
return new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.GET)
.withPath("/_insights/top_queries")
Expand Down

0 comments on commit 1895133

Please sign in to comment.