Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
Signed-off-by: Kaushal Kumar <ravi.kaushal97@gmail.com>
  • Loading branch information
kaushalmahi12 committed Jul 30, 2024
1 parent 31ca5cc commit 4940dcf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
19 changes: 0 additions & 19 deletions server/src/main/java/org/opensearch/wlm/QueryGroupConstants.java

This file was deleted.

16 changes: 9 additions & 7 deletions server/src/main/java/org/opensearch/wlm/QueryGroupTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.opensearch.tasks.CancellableTask;

import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

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

Expand All @@ -25,6 +27,8 @@
public class QueryGroupTask extends CancellableTask {

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

public QueryGroupTask(long id, String type, String action, String description, TaskId parentTaskId, Map<String, String> headers) {
Expand All @@ -47,7 +51,7 @@ public QueryGroupTask(
* This method should always be called after calling setQueryGroupId at least once on this object
* @return task queryGroupId
*/
public String getQueryGroupId() {
public final String getQueryGroupId() {
if (queryGroupId == null) {
logger.warn("QueryGroup _id can't be null, It should be set before accessing it. This is abnormal behaviour ");
}
Expand All @@ -59,12 +63,10 @@ public String getQueryGroupId() {
* This method was defined since the queryGroupId can only be evaluated after task creation
* @param threadContext current threadContext
*/
public void setQueryGroupId(final ThreadContext threadContext) {
this.queryGroupId = QueryGroupConstants.DEFAULT_QUERY_GROUP_ID_SUPPLIER.get();

if (threadContext != null && threadContext.getHeader(QueryGroupConstants.QUERY_GROUP_ID_HEADER) != null) {
this.queryGroupId = threadContext.getHeader(QueryGroupConstants.QUERY_GROUP_ID_HEADER);
}
public final void setQueryGroupId(final ThreadContext threadContext) {
this.queryGroupId = Optional.ofNullable(threadContext)
.map(threadContext1 -> threadContext1.getHeader(QUERY_GROUP_ID_HEADER))
.orElse(DEFAULT_QUERY_GROUP_ID_SUPPLIER.get());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import java.util.Collections;

import static org.opensearch.wlm.QueryGroupTask.DEFAULT_QUERY_GROUP_ID_SUPPLIER;
import static org.opensearch.wlm.QueryGroupTask.QUERY_GROUP_ID_HEADER;

public class QueryGroupTaskTests extends OpenSearchTestCase {
private ThreadPool threadPool;
private QueryGroupTask sut;
Expand All @@ -31,9 +34,9 @@ public void tearDown() throws Exception {

public void testSuccessfulSetQueryGroupId() {
sut.setQueryGroupId(threadPool.getThreadContext());
assertEquals(QueryGroupConstants.DEFAULT_QUERY_GROUP_ID_SUPPLIER.get(), sut.getQueryGroupId());
assertEquals(DEFAULT_QUERY_GROUP_ID_SUPPLIER.get(), sut.getQueryGroupId());

threadPool.getThreadContext().putHeader(QueryGroupConstants.QUERY_GROUP_ID_HEADER, "akfanglkaglknag2332");
threadPool.getThreadContext().putHeader(QUERY_GROUP_ID_HEADER, "akfanglkaglknag2332");

sut.setQueryGroupId(threadPool.getThreadContext());
assertEquals("akfanglkaglknag2332", sut.getQueryGroupId());
Expand Down

0 comments on commit 4940dcf

Please sign in to comment.