Skip to content

Commit b583b61

Browse files
committed
add rejection listener unit test
Signed-off-by: Kaushal Kumar <[email protected]>
1 parent 59046ba commit b583b61

File tree

4 files changed

+72
-11
lines changed

4 files changed

+72
-11
lines changed

server/src/main/java/org/opensearch/action/search/TransportSearchAction.java

-5
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,6 @@ protected void doExecute(Task task, SearchRequest searchRequest, ActionListener<
316316
listener
317317
);
318318
}
319-
320-
if (task instanceof QueryGroupTask) {
321-
listener =
322-
}
323-
324319
executeRequest(task, searchRequest, this::searchAsyncAction, listener);
325320
}
326321

server/src/main/java/org/opensearch/wlm/QueryGroupService.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
package org.opensearch.wlm;
1010

11+
import java.util.Optional;
12+
1113
/**
12-
* This is stub at this point in time
14+
* This is stub at this point in time and will be replace by an acutal one in couple of days
1315
*/
1416
public class QueryGroupService {
1517
/**
@@ -25,8 +27,11 @@ public void requestFailedFor(final String queryGroupId) {
2527
* @param queryGroupId query group identifier
2628
* @return whether the queryGroup is contended and should reject new incoming requests
2729
*/
28-
public boolean shouldRejectFor(String queryGroupId) {
29-
if (queryGroupId == null) return false;
30-
return false;
30+
public Optional<String> shouldRejectFor(String queryGroupId) {
31+
if (queryGroupId == null) return Optional.empty();
32+
// TODO: At this point this is dummy and we need to decide whether to cancel the request based on last
33+
// reported resource usage for the queryGroup. We also need to increment the rejection count here for the
34+
// query group
35+
return Optional.of("Possible reason. ");
3136
}
3237
}

server/src/main/java/org/opensearch/wlm/listeners/QueryGroupRequestRejectionOperationListener.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.opensearch.wlm.QueryGroupService;
1616
import org.opensearch.wlm.QueryGroupTask;
1717

18+
import java.util.Optional;
19+
1820
/**
1921
* This listener is used to perform the rejections for incoming requests into a queryGroup
2022
*/
@@ -35,8 +37,9 @@ public QueryGroupRequestRejectionOperationListener(QueryGroupService queryGroupS
3537
@Override
3638
protected void onRequestStart(SearchRequestContext searchRequestContext) {
3739
final String queryGroupId = threadPool.getThreadContext().getHeader(QueryGroupTask.QUERY_GROUP_ID_HEADER);
38-
if (queryGroupService.shouldRejectFor(queryGroupId)) {
39-
throw new OpenSearchRejectedExecutionException("QueryGroup " + queryGroupId + " is already contended.");
40+
Optional<String> reason = queryGroupService.shouldRejectFor(queryGroupId);
41+
if (reason.isPresent()) {
42+
throw new OpenSearchRejectedExecutionException("QueryGroup " + queryGroupId + " is already contended." + reason.get());
4043
}
4144
}
4245
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.wlm.listeners;
10+
11+
import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException;
12+
import org.opensearch.test.OpenSearchTestCase;
13+
import org.opensearch.threadpool.TestThreadPool;
14+
import org.opensearch.threadpool.ThreadPool;
15+
import org.opensearch.wlm.QueryGroupService;
16+
import org.opensearch.wlm.QueryGroupTask;
17+
18+
import java.util.Optional;
19+
20+
import static org.mockito.Mockito.mock;
21+
import static org.mockito.Mockito.when;
22+
23+
public class QueryGroupRequestRejectionOperationListenerTests extends OpenSearchTestCase {
24+
ThreadPool testThreadPool;
25+
QueryGroupService queryGroupService;
26+
QueryGroupRequestRejectionOperationListener sut;
27+
28+
29+
public void setUp() throws Exception {
30+
super.setUp();
31+
testThreadPool = new TestThreadPool("RejectionTestThreadPool");
32+
}
33+
34+
public void tearDown() throws Exception {
35+
super.tearDown();
36+
testThreadPool.shutdown();
37+
}
38+
39+
public void testRejectionCase() {
40+
queryGroupService = mock(QueryGroupService.class);
41+
sut = new QueryGroupRequestRejectionOperationListener(queryGroupService, testThreadPool);
42+
final String testQueryGroupId = "asdgasgkajgkw3141_3rt4t";
43+
testThreadPool.getThreadContext().putHeader(QueryGroupTask.QUERY_GROUP_ID_HEADER, testQueryGroupId);
44+
when(queryGroupService.shouldRejectFor(testQueryGroupId)).thenReturn(Optional.of("Test query group is contended"));
45+
46+
assertThrows(OpenSearchRejectedExecutionException.class, () -> sut.onRequestStart(null));
47+
}
48+
49+
public void testNonRejectionCase() {
50+
queryGroupService = mock(QueryGroupService.class);
51+
sut = new QueryGroupRequestRejectionOperationListener(queryGroupService, testThreadPool);
52+
final String testQueryGroupId = "asdgasgkajgkw3141_3rt4t";
53+
testThreadPool.getThreadContext().putHeader(QueryGroupTask.QUERY_GROUP_ID_HEADER, testQueryGroupId);
54+
when(queryGroupService.shouldRejectFor(testQueryGroupId)).thenReturn(Optional.empty());
55+
56+
sut.onRequestStart(null);
57+
}
58+
}

0 commit comments

Comments
 (0)