forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Get QueryGroup API Logic (opensearch-project#14709)
* Add Get QueryGroup API Logic Signed-off-by: Ruirui Zhang <[email protected]> * add to changelog Signed-off-by: Ruirui Zhang <[email protected]> * fix javadoc Signed-off-by: Ruirui Zhang <[email protected]> * change GetQueryGroupAction NAME and add more tests Signed-off-by: Ruirui Zhang <[email protected]> * add more unit tests Signed-off-by: Ruirui Zhang <[email protected]> * fix spotlessapply Signed-off-by: Ruirui Zhang <[email protected]> * addressed comments Signed-off-by: Ruirui Zhang <[email protected]> * incorperate comments from create api PR Signed-off-by: Ruirui Zhang <[email protected]> * use clustermanager to get the most recent querygroups Signed-off-by: Ruirui Zhang <[email protected]> * address comments Signed-off-by: Ruirui Zhang <[email protected]> * rebase with main Signed-off-by: Ruirui Zhang <[email protected]> * add IT Signed-off-by: Ruirui Zhang <[email protected]> * address comments Signed-off-by: Ruirui Zhang <[email protected]> * fix IT Signed-off-by: Ruirui Zhang <[email protected]>
- Loading branch information
Showing
16 changed files
with
738 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...rkload-management/src/main/java/org/opensearch/plugin/wlm/action/GetQueryGroupAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugin.wlm.action; | ||
|
||
import org.opensearch.action.ActionType; | ||
|
||
/** | ||
* Transport action to get QueryGroup | ||
* | ||
* @opensearch.experimental | ||
*/ | ||
public class GetQueryGroupAction extends ActionType<GetQueryGroupResponse> { | ||
|
||
/** | ||
* An instance of GetQueryGroupAction | ||
*/ | ||
public static final GetQueryGroupAction INSTANCE = new GetQueryGroupAction(); | ||
|
||
/** | ||
* Name for GetQueryGroupAction | ||
*/ | ||
public static final String NAME = "cluster:admin/opensearch/wlm/query_group/_get"; | ||
|
||
/** | ||
* Default constructor | ||
*/ | ||
private GetQueryGroupAction() { | ||
super(NAME, GetQueryGroupResponse::new); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...kload-management/src/main/java/org/opensearch/plugin/wlm/action/GetQueryGroupRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugin.wlm.action; | ||
|
||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.action.support.clustermanager.ClusterManagerNodeReadRequest; | ||
import org.opensearch.cluster.metadata.QueryGroup; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Request for get QueryGroup | ||
* | ||
* @opensearch.experimental | ||
*/ | ||
public class GetQueryGroupRequest extends ClusterManagerNodeReadRequest<GetQueryGroupRequest> { | ||
final String name; | ||
|
||
/** | ||
* Default constructor for GetQueryGroupRequest | ||
* @param name - name for the QueryGroup to get | ||
*/ | ||
public GetQueryGroupRequest(String name) { | ||
this.name = name; | ||
} | ||
|
||
/** | ||
* Constructor for GetQueryGroupRequest | ||
* @param in - A {@link StreamInput} object | ||
*/ | ||
public GetQueryGroupRequest(StreamInput in) throws IOException { | ||
super(in); | ||
name = in.readOptionalString(); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
if (name != null) { | ||
QueryGroup.validateName(name); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeOptionalString(name); | ||
} | ||
|
||
/** | ||
* Name getter | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...load-management/src/main/java/org/opensearch/plugin/wlm/action/GetQueryGroupResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugin.wlm.action; | ||
|
||
import org.opensearch.cluster.metadata.QueryGroup; | ||
import org.opensearch.core.action.ActionResponse; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.core.xcontent.ToXContent; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
/** | ||
* Response for the get API for QueryGroup | ||
* | ||
* @opensearch.experimental | ||
*/ | ||
public class GetQueryGroupResponse extends ActionResponse implements ToXContent, ToXContentObject { | ||
private final Collection<QueryGroup> queryGroups; | ||
private final RestStatus restStatus; | ||
|
||
/** | ||
* Constructor for GetQueryGroupResponse | ||
* @param queryGroups - The QueryGroup list to be fetched | ||
* @param restStatus - The rest status of the request | ||
*/ | ||
public GetQueryGroupResponse(final Collection<QueryGroup> queryGroups, RestStatus restStatus) { | ||
this.queryGroups = queryGroups; | ||
this.restStatus = restStatus; | ||
} | ||
|
||
/** | ||
* Constructor for GetQueryGroupResponse | ||
* @param in - A {@link StreamInput} object | ||
*/ | ||
public GetQueryGroupResponse(StreamInput in) throws IOException { | ||
this.queryGroups = in.readList(QueryGroup::new); | ||
restStatus = RestStatus.readFrom(in); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeCollection(queryGroups); | ||
RestStatus.writeTo(out, restStatus); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.startArray("query_groups"); | ||
for (QueryGroup group : queryGroups) { | ||
group.toXContent(builder, params); | ||
} | ||
builder.endArray(); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
/** | ||
* queryGroups getter | ||
*/ | ||
public Collection<QueryGroup> getQueryGroups() { | ||
return queryGroups; | ||
} | ||
|
||
/** | ||
* restStatus getter | ||
*/ | ||
public RestStatus getRestStatus() { | ||
return restStatus; | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...nagement/src/main/java/org/opensearch/plugin/wlm/action/TransportGetQueryGroupAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugin.wlm.action; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.ResourceNotFoundException; | ||
import org.opensearch.action.support.ActionFilters; | ||
import org.opensearch.action.support.clustermanager.TransportClusterManagerNodeReadAction; | ||
import org.opensearch.cluster.ClusterState; | ||
import org.opensearch.cluster.block.ClusterBlockException; | ||
import org.opensearch.cluster.block.ClusterBlockLevel; | ||
import org.opensearch.cluster.metadata.IndexNameExpressionResolver; | ||
import org.opensearch.cluster.metadata.QueryGroup; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.common.inject.Inject; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.plugin.wlm.service.QueryGroupPersistenceService; | ||
import org.opensearch.search.pipeline.SearchPipelineService; | ||
import org.opensearch.threadpool.ThreadPool; | ||
import org.opensearch.transport.TransportService; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
/** | ||
* Transport action to get QueryGroup | ||
* | ||
* @opensearch.experimental | ||
*/ | ||
public class TransportGetQueryGroupAction extends TransportClusterManagerNodeReadAction<GetQueryGroupRequest, GetQueryGroupResponse> { | ||
private static final Logger logger = LogManager.getLogger(SearchPipelineService.class); | ||
|
||
/** | ||
* Constructor for TransportGetQueryGroupAction | ||
* | ||
* @param clusterService - a {@link ClusterService} object | ||
* @param transportService - a {@link TransportService} object | ||
* @param actionFilters - a {@link ActionFilters} object | ||
* @param threadPool - a {@link ThreadPool} object | ||
* @param indexNameExpressionResolver - a {@link IndexNameExpressionResolver} object | ||
*/ | ||
@Inject | ||
public TransportGetQueryGroupAction( | ||
ClusterService clusterService, | ||
TransportService transportService, | ||
ActionFilters actionFilters, | ||
ThreadPool threadPool, | ||
IndexNameExpressionResolver indexNameExpressionResolver | ||
) { | ||
super( | ||
GetQueryGroupAction.NAME, | ||
transportService, | ||
clusterService, | ||
threadPool, | ||
actionFilters, | ||
GetQueryGroupRequest::new, | ||
indexNameExpressionResolver, | ||
true | ||
); | ||
} | ||
|
||
@Override | ||
protected String executor() { | ||
return ThreadPool.Names.SAME; | ||
} | ||
|
||
@Override | ||
protected GetQueryGroupResponse read(StreamInput in) throws IOException { | ||
return new GetQueryGroupResponse(in); | ||
} | ||
|
||
@Override | ||
protected ClusterBlockException checkBlock(GetQueryGroupRequest request, ClusterState state) { | ||
return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ); | ||
} | ||
|
||
@Override | ||
protected void clusterManagerOperation(GetQueryGroupRequest request, ClusterState state, ActionListener<GetQueryGroupResponse> listener) | ||
throws Exception { | ||
final String name = request.getName(); | ||
final Collection<QueryGroup> resultGroups = QueryGroupPersistenceService.getFromClusterStateMetadata(name, state); | ||
|
||
if (resultGroups.isEmpty() && name != null && !name.isEmpty()) { | ||
logger.warn("No QueryGroup exists with the provided name: {}", name); | ||
throw new ResourceNotFoundException("No QueryGroup exists with the provided name: " + name); | ||
} | ||
listener.onResponse(new GetQueryGroupResponse(resultGroups, RestStatus.OK)); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...load-management/src/main/java/org/opensearch/plugin/wlm/rest/RestGetQueryGroupAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugin.wlm.rest; | ||
|
||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.core.xcontent.ToXContent; | ||
import org.opensearch.plugin.wlm.action.GetQueryGroupAction; | ||
import org.opensearch.plugin.wlm.action.GetQueryGroupRequest; | ||
import org.opensearch.plugin.wlm.action.GetQueryGroupResponse; | ||
import org.opensearch.rest.BaseRestHandler; | ||
import org.opensearch.rest.BytesRestResponse; | ||
import org.opensearch.rest.RestChannel; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.rest.RestResponse; | ||
import org.opensearch.rest.action.RestResponseListener; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static org.opensearch.rest.RestRequest.Method.GET; | ||
|
||
/** | ||
* Rest action to get a QueryGroup0 | ||
* | ||
* @opensearch.experimental | ||
*/ | ||
public class RestGetQueryGroupAction extends BaseRestHandler { | ||
|
||
/** | ||
* Constructor for RestGetQueryGroupAction | ||
*/ | ||
public RestGetQueryGroupAction() {} | ||
|
||
@Override | ||
public String getName() { | ||
return "get_query_group"; | ||
} | ||
|
||
/** | ||
* The list of {@link Route}s that this RestHandler is responsible for handling. | ||
*/ | ||
@Override | ||
public List<Route> routes() { | ||
return List.of(new Route(GET, "_wlm/query_group/{name}"), new Route(GET, "_wlm/query_group/")); | ||
} | ||
|
||
@Override | ||
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
final GetQueryGroupRequest getQueryGroupRequest = new GetQueryGroupRequest(request.param("name")); | ||
return channel -> client.execute(GetQueryGroupAction.INSTANCE, getQueryGroupRequest, getQueryGroupResponse(channel)); | ||
} | ||
|
||
private RestResponseListener<GetQueryGroupResponse> getQueryGroupResponse(final RestChannel channel) { | ||
return new RestResponseListener<>(channel) { | ||
@Override | ||
public RestResponse buildResponse(final GetQueryGroupResponse response) throws Exception { | ||
return new BytesRestResponse(RestStatus.OK, response.toXContent(channel.newBuilder(), ToXContent.EMPTY_PARAMS)); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.