13
13
import org .opensearch .action .admin .cluster .node .info .NodesInfoRequest ;
14
14
import org .opensearch .action .admin .cluster .node .info .NodesInfoResponse ;
15
15
import org .opensearch .action .admin .cluster .node .info .PluginsAndModules ;
16
+ import org .opensearch .action .admin .cluster .settings .ClusterUpdateSettingsRequest ;
16
17
import org .opensearch .action .index .IndexResponse ;
17
18
import org .opensearch .action .search .SearchResponse ;
18
19
import org .opensearch .common .settings .Settings ;
19
20
import org .opensearch .index .query .QueryBuilders ;
20
21
import org .opensearch .plugin .insights .rules .action .top_queries .TopQueriesAction ;
21
22
import org .opensearch .plugin .insights .rules .action .top_queries .TopQueriesRequest ;
22
23
import org .opensearch .plugin .insights .rules .action .top_queries .TopQueriesResponse ;
24
+ import org .opensearch .plugin .insights .rules .model .MetricType ;
23
25
import org .opensearch .plugins .Plugin ;
24
26
import org .opensearch .plugins .PluginInfo ;
25
27
import org .opensearch .test .OpenSearchIntegTestCase ;
28
30
import java .util .Arrays ;
29
31
import java .util .Collection ;
30
32
import java .util .List ;
33
+ import java .util .concurrent .ExecutionException ;
31
34
import java .util .function .Function ;
32
35
import java .util .stream .Collectors ;
33
36
import java .util .stream .Stream ;
@@ -74,15 +77,69 @@ public void testQueryInsightPluginInstalled() {
74
77
* Test get top queries when feature disabled
75
78
*/
76
79
public void testGetTopQueriesWhenFeatureDisabled () {
77
- TopQueriesRequest request = new TopQueriesRequest ();
80
+ TopQueriesRequest request = new TopQueriesRequest (MetricType . LATENCY );
78
81
TopQueriesResponse response = OpenSearchIntegTestCase .client ().execute (TopQueriesAction .INSTANCE , request ).actionGet ();
79
82
Assert .assertNotEquals (0 , response .failures ().size ());
80
83
Assert .assertEquals (
81
- "Cannot get query data when query insight feature is not enabled." ,
84
+ "Cannot get query data when query insight feature is not enabled for MetricType [latency] ." ,
82
85
response .failures ().get (0 ).getCause ().getCause ().getMessage ()
83
86
);
84
87
}
85
88
89
+ /**
90
+ * Test update top query record when feature enabled
91
+ */
92
+ public void testUpdateRecordWhenFeatureEnabled () throws ExecutionException , InterruptedException {
93
+ Settings commonSettings = Settings .builder ().put (TOP_N_LATENCY_QUERIES_ENABLED .getKey (), "false" ).build ();
94
+
95
+ logger .info ("--> starting nodes for query insight testing" );
96
+ List <String > nodes = internalCluster ().startNodes (TOTAL_NUMBER_OF_NODES , Settings .builder ().put (commonSettings ).build ());
97
+
98
+ logger .info ("--> waiting for nodes to form a cluster" );
99
+ ClusterHealthResponse health = client ().admin ().cluster ().prepareHealth ().setWaitForNodes ("2" ).execute ().actionGet ();
100
+ assertFalse (health .isTimedOut ());
101
+
102
+ assertAcked (
103
+ prepareCreate ("test" ).setSettings (Settings .builder ().put ("index.number_of_shards" , 2 ).put ("index.number_of_replicas" , 2 ))
104
+ );
105
+ ensureStableCluster (2 );
106
+ logger .info ("--> creating indices for query insight testing" );
107
+ for (int i = 0 ; i < 5 ; i ++) {
108
+ IndexResponse response = client ().prepareIndex ("test_" + i ).setId ("" + i ).setSource ("field_" + i , "value_" + i ).get ();
109
+ assertEquals ("CREATED" , response .status ().toString ());
110
+ }
111
+ // making search requests to get top queries
112
+ for (int i = 0 ; i < TOTAL_SEARCH_REQUESTS ; i ++) {
113
+ SearchResponse searchResponse = internalCluster ().client (randomFrom (nodes ))
114
+ .prepareSearch ()
115
+ .setQuery (QueryBuilders .matchAllQuery ())
116
+ .get ();
117
+ assertEquals (searchResponse .getFailedShards (), 0 );
118
+ }
119
+
120
+ TopQueriesRequest request = new TopQueriesRequest (MetricType .LATENCY );
121
+ TopQueriesResponse response = OpenSearchIntegTestCase .client ().execute (TopQueriesAction .INSTANCE , request ).actionGet ();
122
+ Assert .assertNotEquals (0 , response .failures ().size ());
123
+ Assert .assertEquals (
124
+ "Cannot get query data when query insight feature is not enabled for MetricType [latency]." ,
125
+ response .failures ().get (0 ).getCause ().getCause ().getMessage ()
126
+ );
127
+
128
+ ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest ().persistentSettings (
129
+ Settings .builder ().put (TOP_N_LATENCY_QUERIES_ENABLED .getKey (), "true" ).build ()
130
+ );
131
+ assertAcked (internalCluster ().client ().admin ().cluster ().updateSettings (updateSettingsRequest ).get ());
132
+ TopQueriesRequest request2 = new TopQueriesRequest (MetricType .LATENCY );
133
+ TopQueriesResponse response2 = OpenSearchIntegTestCase .client ().execute (TopQueriesAction .INSTANCE , request2 ).actionGet ();
134
+ Assert .assertEquals (0 , response2 .failures ().size ());
135
+ Assert .assertEquals (TOTAL_NUMBER_OF_NODES , response2 .getNodes ().size ());
136
+ for (int i = 0 ; i < TOTAL_NUMBER_OF_NODES ; i ++) {
137
+ Assert .assertEquals (0 , response2 .getNodes ().get (i ).getTopQueriesRecord ().size ());
138
+ }
139
+
140
+ internalCluster ().stopAllNodes ();
141
+ }
142
+
86
143
/**
87
144
* Test get top queries when feature enabled
88
145
*/
@@ -93,7 +150,7 @@ public void testGetTopQueriesWhenFeatureEnabled() {
93
150
.put (TOP_N_LATENCY_QUERIES_WINDOW_SIZE .getKey (), "600s" )
94
151
.build ();
95
152
96
- logger .info ("--> starting 2 nodes for query insight testing" );
153
+ logger .info ("--> starting nodes for query insight testing" );
97
154
List <String > nodes = internalCluster ().startNodes (TOTAL_NUMBER_OF_NODES , Settings .builder ().put (commonSettings ).build ());
98
155
99
156
logger .info ("--> waiting for nodes to form a cluster" );
@@ -118,11 +175,11 @@ public void testGetTopQueriesWhenFeatureEnabled() {
118
175
assertEquals (searchResponse .getFailedShards (), 0 );
119
176
}
120
177
121
- TopQueriesRequest request = new TopQueriesRequest ();
178
+ TopQueriesRequest request = new TopQueriesRequest (MetricType . LATENCY );
122
179
TopQueriesResponse response = OpenSearchIntegTestCase .client ().execute (TopQueriesAction .INSTANCE , request ).actionGet ();
123
180
Assert .assertEquals (0 , response .failures ().size ());
124
181
Assert .assertEquals (TOTAL_NUMBER_OF_NODES , response .getNodes ().size ());
125
- Assert .assertEquals (TOTAL_SEARCH_REQUESTS , response .getNodes ().stream ().mapToInt (o -> o .getLatencyRecords ().size ()).sum ());
182
+ Assert .assertEquals (TOTAL_SEARCH_REQUESTS , response .getNodes ().stream ().mapToInt (o -> o .getTopQueriesRecord ().size ()).sum ());
126
183
127
184
internalCluster ().stopAllNodes ();
128
185
}
@@ -137,7 +194,7 @@ public void testGetTopQueriesWithSmallTopN() {
137
194
.put (TOP_N_LATENCY_QUERIES_WINDOW_SIZE .getKey (), "600s" )
138
195
.build ();
139
196
140
- logger .info ("--> starting 2 nodes for query insight testing" );
197
+ logger .info ("--> starting nodes for query insight testing" );
141
198
List <String > nodes = internalCluster ().startNodes (TOTAL_NUMBER_OF_NODES , Settings .builder ().put (commonSettings ).build ());
142
199
143
200
logger .info ("--> waiting for nodes to form a cluster" );
@@ -162,12 +219,11 @@ public void testGetTopQueriesWithSmallTopN() {
162
219
assertEquals (searchResponse .getFailedShards (), 0 );
163
220
}
164
221
165
- TopQueriesRequest request = new TopQueriesRequest ();
222
+ TopQueriesRequest request = new TopQueriesRequest (MetricType . LATENCY );
166
223
TopQueriesResponse response = OpenSearchIntegTestCase .client ().execute (TopQueriesAction .INSTANCE , request ).actionGet ();
167
224
Assert .assertEquals (0 , response .failures ().size ());
168
225
Assert .assertEquals (TOTAL_NUMBER_OF_NODES , response .getNodes ().size ());
169
- // TODO: this should be 1 after changing to cluster level top N.
170
- Assert .assertEquals (2 , response .getNodes ().stream ().mapToInt (o -> o .getLatencyRecords ().size ()).sum ());
226
+ Assert .assertEquals (2 , response .getNodes ().stream ().mapToInt (o -> o .getTopQueriesRecord ().size ()).sum ());
171
227
172
228
internalCluster ().stopAllNodes ();
173
229
}
@@ -179,10 +235,10 @@ public void testGetTopQueriesWithSmallWindowSize() {
179
235
Settings commonSettings = Settings .builder ()
180
236
.put (TOP_N_LATENCY_QUERIES_ENABLED .getKey (), "true" )
181
237
.put (TOP_N_LATENCY_QUERIES_SIZE .getKey (), "100" )
182
- .put (TOP_N_LATENCY_QUERIES_WINDOW_SIZE .getKey (), "0ms " )
238
+ .put (TOP_N_LATENCY_QUERIES_WINDOW_SIZE .getKey (), "1m " )
183
239
.build ();
184
240
185
- logger .info ("--> starting 2 nodes for query insight testing" );
241
+ logger .info ("--> starting nodes for query insight testing" );
186
242
List <String > nodes = internalCluster ().startNodes (TOTAL_NUMBER_OF_NODES , Settings .builder ().put (commonSettings ).build ());
187
243
188
244
logger .info ("--> waiting for nodes to form a cluster" );
@@ -207,11 +263,10 @@ public void testGetTopQueriesWithSmallWindowSize() {
207
263
assertEquals (searchResponse .getFailedShards (), 0 );
208
264
}
209
265
210
- TopQueriesRequest request = new TopQueriesRequest ();
266
+ TopQueriesRequest request = new TopQueriesRequest (MetricType . LATENCY );
211
267
TopQueriesResponse response = OpenSearchIntegTestCase .client ().execute (TopQueriesAction .INSTANCE , request ).actionGet ();
212
268
Assert .assertEquals (0 , response .failures ().size ());
213
269
Assert .assertEquals (TOTAL_NUMBER_OF_NODES , response .getNodes ().size ());
214
- Assert .assertEquals (0 , response .getNodes ().stream ().mapToInt (o -> o .getLatencyRecords ().size ()).sum ());
215
270
216
271
internalCluster ().stopAllNodes ();
217
272
}
0 commit comments