10
10
11
11
import org .opensearch .cluster .ClusterName ;
12
12
import org .opensearch .cluster .ClusterState ;
13
+ import org .opensearch .cluster .ClusterStateUpdateTask ;
13
14
import org .opensearch .cluster .metadata .Metadata ;
14
15
import org .opensearch .cluster .metadata .QueryGroup ;
15
16
import org .opensearch .cluster .service .ClusterService ;
16
17
import org .opensearch .common .collect .Tuple ;
17
18
import org .opensearch .common .settings .ClusterSettings ;
18
19
import org .opensearch .common .settings .Settings ;
20
+ import org .opensearch .core .action .ActionListener ;
21
+ import org .opensearch .plugin .wlm .QueryGroupTestUtils ;
22
+ import org .opensearch .plugin .wlm .action .CreateQueryGroupResponse ;
19
23
import org .opensearch .search .ResourceType ;
20
24
import org .opensearch .test .OpenSearchTestCase ;
21
25
import org .opensearch .threadpool .ThreadPool ;
26
30
import java .util .List ;
27
31
import java .util .Map ;
28
32
33
+ import org .mockito .ArgumentCaptor ;
34
+
29
35
import static org .opensearch .cluster .metadata .QueryGroup .builder ;
30
36
import static org .opensearch .plugin .wlm .QueryGroupTestUtils .MEMORY_STRING ;
31
37
import static org .opensearch .plugin .wlm .QueryGroupTestUtils .MONITOR_STRING ;
34
40
import static org .opensearch .plugin .wlm .QueryGroupTestUtils ._ID_ONE ;
35
41
import static org .opensearch .plugin .wlm .QueryGroupTestUtils ._ID_TWO ;
36
42
import static org .opensearch .plugin .wlm .QueryGroupTestUtils .assertEqualQueryGroups ;
43
+ import static org .opensearch .plugin .wlm .QueryGroupTestUtils .clusterSettings ;
37
44
import static org .opensearch .plugin .wlm .QueryGroupTestUtils .clusterSettingsSet ;
38
45
import static org .opensearch .plugin .wlm .QueryGroupTestUtils .preparePersistenceServiceSetup ;
39
46
import static org .opensearch .plugin .wlm .QueryGroupTestUtils .queryGroupList ;
40
47
import static org .opensearch .plugin .wlm .QueryGroupTestUtils .queryGroupOne ;
41
48
import static org .opensearch .plugin .wlm .QueryGroupTestUtils .queryGroupTwo ;
42
49
import static org .opensearch .plugin .wlm .service .QueryGroupPersistenceService .QUERY_GROUP_COUNT_SETTING_NAME ;
50
+ import static org .opensearch .plugin .wlm .service .QueryGroupPersistenceService .SOURCE ;
51
+ import static org .mockito .ArgumentMatchers .any ;
52
+ import static org .mockito .ArgumentMatchers .eq ;
53
+ import static org .mockito .Mockito .anyString ;
54
+ import static org .mockito .Mockito .doAnswer ;
43
55
import static org .mockito .Mockito .mock ;
56
+ import static org .mockito .Mockito .times ;
57
+ import static org .mockito .Mockito .verify ;
44
58
45
59
public class QueryGroupPersistenceServiceTests extends OpenSearchTestCase {
46
60
47
61
/**
48
- * Test case to validate the creation logic of a single QueryGroup
62
+ * Test case to validate the creation logic of a QueryGroup
49
63
*/
50
64
public void testCreateQueryGroup () {
51
65
Tuple <QueryGroupPersistenceService , ClusterState > setup = preparePersistenceServiceSetup (new HashMap <>());
@@ -136,6 +150,9 @@ public void testCreateQueryGroupOverflowCount() {
136
150
assertThrows (RuntimeException .class , () -> queryGroupPersistenceService1 .saveQueryGroupInClusterState (toCreate , clusterState ));
137
151
}
138
152
153
+ /**
154
+ * Tests the invalid value of {@code node.query_group.max_count}
155
+ */
139
156
public void testInvalidMaxQueryGroupCount () {
140
157
Settings settings = Settings .builder ().put (QUERY_GROUP_COUNT_SETTING_NAME , 2 ).build ();
141
158
ClusterSettings clusterSettings = new ClusterSettings (settings , clusterSettingsSet ());
@@ -147,4 +164,84 @@ public void testInvalidMaxQueryGroupCount() {
147
164
);
148
165
assertThrows (IllegalArgumentException .class , () -> queryGroupPersistenceService .setMaxQueryGroupCount (-1 ));
149
166
}
167
+
168
+ /**
169
+ * Tests the valid value of {@code node.query_group.max_count}
170
+ */
171
+ public void testValidMaxSandboxCountSetting () {
172
+ Settings settings = Settings .builder ().put (QUERY_GROUP_COUNT_SETTING_NAME , 100 ).build ();
173
+ ClusterService clusterService = new ClusterService (settings , clusterSettings (), mock (ThreadPool .class ));
174
+ QueryGroupPersistenceService queryGroupPersistenceService = new QueryGroupPersistenceService (
175
+ clusterService ,
176
+ settings ,
177
+ clusterSettings ()
178
+ );
179
+ queryGroupPersistenceService .setMaxQueryGroupCount (50 );
180
+ assertEquals (50 , queryGroupPersistenceService .getMaxQueryGroupCount ());
181
+ }
182
+
183
+ /**
184
+ * Tests PersistInClusterStateMetadata function
185
+ */
186
+ public void testPersistInClusterStateMetadata () {
187
+ ClusterService clusterService = mock (ClusterService .class );
188
+ @ SuppressWarnings ("unchecked" )
189
+ ActionListener <CreateQueryGroupResponse > listener = mock (ActionListener .class );
190
+ QueryGroupPersistenceService queryGroupPersistenceService = new QueryGroupPersistenceService (
191
+ clusterService ,
192
+ QueryGroupTestUtils .settings (),
193
+ clusterSettings ()
194
+ );
195
+ queryGroupPersistenceService .persistInClusterStateMetadata (queryGroupOne , listener );
196
+ verify (clusterService ).submitStateUpdateTask (eq (SOURCE ), any ());
197
+ }
198
+
199
+ /**
200
+ * Tests PersistInClusterStateMetadata function with inner functions
201
+ */
202
+ public void testPersistInClusterStateMetadataInner () {
203
+ ClusterService clusterService = mock (ClusterService .class );
204
+ @ SuppressWarnings ("unchecked" )
205
+ ActionListener <CreateQueryGroupResponse > listener = mock (ActionListener .class );
206
+ QueryGroupPersistenceService queryGroupPersistenceService = new QueryGroupPersistenceService (
207
+ clusterService ,
208
+ QueryGroupTestUtils .settings (),
209
+ clusterSettings ()
210
+ );
211
+ ArgumentCaptor <ClusterStateUpdateTask > captor = ArgumentCaptor .forClass (ClusterStateUpdateTask .class );
212
+ queryGroupPersistenceService .persistInClusterStateMetadata (queryGroupOne , listener );
213
+ verify (clusterService , times (1 )).submitStateUpdateTask (eq (SOURCE ), captor .capture ());
214
+ ClusterStateUpdateTask capturedTask = captor .getValue ();
215
+ assertEquals (queryGroupPersistenceService .createQueryGroupThrottlingKey , capturedTask .getClusterManagerThrottlingKey ());
216
+
217
+ doAnswer (invocation -> {
218
+ ClusterStateUpdateTask task = invocation .getArgument (1 );
219
+ task .clusterStateProcessed (SOURCE , mock (ClusterState .class ), mock (ClusterState .class ));
220
+ return null ;
221
+ }).when (clusterService ).submitStateUpdateTask (anyString (), any ());
222
+ queryGroupPersistenceService .persistInClusterStateMetadata (queryGroupOne , listener );
223
+ verify (listener ).onResponse (any (CreateQueryGroupResponse .class ));
224
+ }
225
+
226
+ /**
227
+ * Tests PersistInClusterStateMetadata function with failure
228
+ */
229
+ public void testPersistInClusterStateMetadataFailure () {
230
+ ClusterService clusterService = mock (ClusterService .class );
231
+ @ SuppressWarnings ("unchecked" )
232
+ ActionListener <CreateQueryGroupResponse > listener = mock (ActionListener .class );
233
+ QueryGroupPersistenceService queryGroupPersistenceService = new QueryGroupPersistenceService (
234
+ clusterService ,
235
+ QueryGroupTestUtils .settings (),
236
+ clusterSettings ()
237
+ );
238
+ doAnswer (invocation -> {
239
+ ClusterStateUpdateTask task = invocation .getArgument (1 );
240
+ Exception exception = new RuntimeException ("Test Exception" );
241
+ task .onFailure (SOURCE , exception );
242
+ return null ;
243
+ }).when (clusterService ).submitStateUpdateTask (anyString (), any ());
244
+ queryGroupPersistenceService .persistInClusterStateMetadata (queryGroupOne , listener );
245
+ verify (listener ).onFailure (any (RuntimeException .class ));
246
+ }
150
247
}
0 commit comments