Skip to content

Commit 4940dcf

Browse files
committed
address comments
Signed-off-by: Kaushal Kumar <ravi.kaushal97@gmail.com>
1 parent 31ca5cc commit 4940dcf

File tree

3 files changed

+14
-28
lines changed

3 files changed

+14
-28
lines changed

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

Lines changed: 0 additions & 19 deletions
This file was deleted.

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import org.opensearch.tasks.CancellableTask;
1717

1818
import java.util.Map;
19+
import java.util.Optional;
20+
import java.util.function.Supplier;
1921

2022
import static org.opensearch.search.SearchService.NO_TIMEOUT;
2123

@@ -25,6 +27,8 @@
2527
public class QueryGroupTask extends CancellableTask {
2628

2729
private static final Logger logger = LogManager.getLogger(QueryGroupTask.class);
30+
public static final String QUERY_GROUP_ID_HEADER = "queryGroupId";
31+
public static final Supplier<String> DEFAULT_QUERY_GROUP_ID_SUPPLIER = () -> "DEFAULT_QUERY_GROUP";
2832
private String queryGroupId;
2933

3034
public QueryGroupTask(long id, String type, String action, String description, TaskId parentTaskId, Map<String, String> headers) {
@@ -47,7 +51,7 @@ public QueryGroupTask(
4751
* This method should always be called after calling setQueryGroupId at least once on this object
4852
* @return task queryGroupId
4953
*/
50-
public String getQueryGroupId() {
54+
public final String getQueryGroupId() {
5155
if (queryGroupId == null) {
5256
logger.warn("QueryGroup _id can't be null, It should be set before accessing it. This is abnormal behaviour ");
5357
}
@@ -59,12 +63,10 @@ public String getQueryGroupId() {
5963
* This method was defined since the queryGroupId can only be evaluated after task creation
6064
* @param threadContext current threadContext
6165
*/
62-
public void setQueryGroupId(final ThreadContext threadContext) {
63-
this.queryGroupId = QueryGroupConstants.DEFAULT_QUERY_GROUP_ID_SUPPLIER.get();
64-
65-
if (threadContext != null && threadContext.getHeader(QueryGroupConstants.QUERY_GROUP_ID_HEADER) != null) {
66-
this.queryGroupId = threadContext.getHeader(QueryGroupConstants.QUERY_GROUP_ID_HEADER);
67-
}
66+
public final void setQueryGroupId(final ThreadContext threadContext) {
67+
this.queryGroupId = Optional.ofNullable(threadContext)
68+
.map(threadContext1 -> threadContext1.getHeader(QUERY_GROUP_ID_HEADER))
69+
.orElse(DEFAULT_QUERY_GROUP_ID_SUPPLIER.get());
6870
}
6971

7072
@Override

server/src/test/java/org/opensearch/wlm/QueryGroupTaskTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
import java.util.Collections;
1616

17+
import static org.opensearch.wlm.QueryGroupTask.DEFAULT_QUERY_GROUP_ID_SUPPLIER;
18+
import static org.opensearch.wlm.QueryGroupTask.QUERY_GROUP_ID_HEADER;
19+
1720
public class QueryGroupTaskTests extends OpenSearchTestCase {
1821
private ThreadPool threadPool;
1922
private QueryGroupTask sut;
@@ -31,9 +34,9 @@ public void tearDown() throws Exception {
3134

3235
public void testSuccessfulSetQueryGroupId() {
3336
sut.setQueryGroupId(threadPool.getThreadContext());
34-
assertEquals(QueryGroupConstants.DEFAULT_QUERY_GROUP_ID_SUPPLIER.get(), sut.getQueryGroupId());
37+
assertEquals(DEFAULT_QUERY_GROUP_ID_SUPPLIER.get(), sut.getQueryGroupId());
3538

36-
threadPool.getThreadContext().putHeader(QueryGroupConstants.QUERY_GROUP_ID_HEADER, "akfanglkaglknag2332");
39+
threadPool.getThreadContext().putHeader(QUERY_GROUP_ID_HEADER, "akfanglkaglknag2332");
3740

3841
sut.setQueryGroupId(threadPool.getThreadContext());
3942
assertEquals("akfanglkaglknag2332", sut.getQueryGroupId());

0 commit comments

Comments
 (0)