|
| 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; |
| 10 | + |
| 11 | +import org.apache.logging.log4j.LogManager; |
| 12 | +import org.apache.logging.log4j.Logger; |
| 13 | +import org.opensearch.common.unit.TimeValue; |
| 14 | +import org.opensearch.common.util.concurrent.ThreadContext; |
| 15 | +import org.opensearch.core.tasks.TaskId; |
| 16 | +import org.opensearch.tasks.CancellableTask; |
| 17 | + |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.function.Supplier; |
| 21 | + |
| 22 | +import static org.opensearch.search.SearchService.NO_TIMEOUT; |
| 23 | + |
| 24 | +/** |
| 25 | + * Base class to define QueryGroup tasks |
| 26 | + */ |
| 27 | +public class QueryGroupTask extends CancellableTask { |
| 28 | + |
| 29 | + 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"; |
| 32 | + private String queryGroupId; |
| 33 | + |
| 34 | + public QueryGroupTask(long id, String type, String action, String description, TaskId parentTaskId, Map<String, String> headers) { |
| 35 | + this(id, type, action, description, parentTaskId, headers, NO_TIMEOUT); |
| 36 | + } |
| 37 | + |
| 38 | + public QueryGroupTask( |
| 39 | + long id, |
| 40 | + String type, |
| 41 | + String action, |
| 42 | + String description, |
| 43 | + TaskId parentTaskId, |
| 44 | + Map<String, String> headers, |
| 45 | + TimeValue cancelAfterTimeInterval |
| 46 | + ) { |
| 47 | + super(id, type, action, description, parentTaskId, headers, cancelAfterTimeInterval); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * This method should always be called after calling setQueryGroupId at least once on this object |
| 52 | + * @return task queryGroupId |
| 53 | + */ |
| 54 | + public final String getQueryGroupId() { |
| 55 | + if (queryGroupId == null) { |
| 56 | + logger.warn("QueryGroup _id can't be null, It should be set before accessing it. This is abnormal behaviour "); |
| 57 | + } |
| 58 | + return queryGroupId; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * sets the queryGroupId from threadContext into the task itself, |
| 63 | + * This method was defined since the queryGroupId can only be evaluated after task creation |
| 64 | + * @param threadContext current threadContext |
| 65 | + */ |
| 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()); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean shouldCancelChildrenOnCancellation() { |
| 74 | + return false; |
| 75 | + } |
| 76 | +} |
0 commit comments