Skip to content

Commit d442f7c

Browse files
authored
Add Create QueryGroup API Logic (opensearch-project#14680)
* add logic for Create QueryGroup API Signed-off-by: Ruirui Zhang <[email protected]> * remove wildcard imports Signed-off-by: Ruirui Zhang <[email protected]> * change jvm to memeory Signed-off-by: Ruirui Zhang <[email protected]> * modify querygroup Signed-off-by: Ruirui Zhang <[email protected]> * fix javadoc and add more tests Signed-off-by: Ruirui Zhang <[email protected]> * add more tests Signed-off-by: Ruirui Zhang <[email protected]> * address comments Signed-off-by: Ruirui Zhang <[email protected]> * fix the persist logic Signed-off-by: Ruirui Zhang <[email protected]> * remove inflight checks as they are not necessary Signed-off-by: Ruirui Zhang <[email protected]> * remove persistable interface Signed-off-by: Ruirui Zhang <[email protected]> * modify QueryGroupServiceSettings Signed-off-by: Ruirui Zhang <[email protected]> * add in an action package in the plugin Signed-off-by: Ruirui Zhang <[email protected]> * modify based on commments Signed-off-by: Ruirui Zhang <[email protected]> * address comments on QueryGroupPersistenceService Signed-off-by: Ruirui Zhang <[email protected]> * address comments on persistence service Signed-off-by: Ruirui Zhang <[email protected]> * address comments Signed-off-by: Ruirui Zhang <[email protected]> * fix unit test Signed-off-by: Ruirui Zhang <[email protected]> * address comments Signed-off-by: Ruirui Zhang <[email protected]> * add IT Signed-off-by: Ruirui Zhang <[email protected]> * add coverage Signed-off-by: Ruirui Zhang <[email protected]>
1 parent 2311195 commit d442f7c

24 files changed

+1427
-81
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1010
- Add setting to ignore throttling nodes for allocation of unassigned primaries in remote restore ([#14991](https://github.com/opensearch-project/OpenSearch/pull/14991))
1111
- [Streaming Indexing] Enhance RestClient with a new streaming API support ([#14437](https://github.com/opensearch-project/OpenSearch/pull/14437))
1212
- Add basic aggregation support for derived fields ([#14618](https://github.com/opensearch-project/OpenSearch/pull/14618))
13+
- [Workload Management] Add Create QueryGroup API Logic ([#14680](https://github.com/opensearch-project/OpenSearch/pull/14680))- [Workload Management] Add Create QueryGroup API Logic ([#14680](https://github.com/opensearch-project/OpenSearch/pull/14680))
1314
- Add ThreadContextPermission for markAsSystemContext and allow core to perform the method ([#15016](https://github.com/opensearch-project/OpenSearch/pull/15016))
1415
- Add ThreadContextPermission for stashAndMergeHeaders and stashWithOrigin ([#15039](https://github.com/opensearch-project/OpenSearch/pull/15039))
1516
- [Concurrent Segment Search] Support composite aggregations with scripting ([#15072](https://github.com/opensearch-project/OpenSearch/pull/15072))
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
* Modifications Copyright OpenSearch Contributors. See
9+
* GitHub history for details.
10+
*/
11+
12+
apply plugin: 'opensearch.yaml-rest-test'
13+
apply plugin: 'opensearch.internal-cluster-test'
14+
15+
opensearchplugin {
16+
description 'OpenSearch Workload Management Plugin.'
17+
classname 'org.opensearch.plugin.wlm.WorkloadManagementPlugin'
18+
}
19+
20+
dependencies {
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.plugin.wlm;
10+
11+
import org.opensearch.action.ActionRequest;
12+
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
13+
import org.opensearch.cluster.node.DiscoveryNodes;
14+
import org.opensearch.common.settings.ClusterSettings;
15+
import org.opensearch.common.settings.IndexScopedSettings;
16+
import org.opensearch.common.settings.Setting;
17+
import org.opensearch.common.settings.Settings;
18+
import org.opensearch.common.settings.SettingsFilter;
19+
import org.opensearch.core.action.ActionResponse;
20+
import org.opensearch.plugin.wlm.action.CreateQueryGroupAction;
21+
import org.opensearch.plugin.wlm.action.TransportCreateQueryGroupAction;
22+
import org.opensearch.plugin.wlm.rest.RestCreateQueryGroupAction;
23+
import org.opensearch.plugin.wlm.service.QueryGroupPersistenceService;
24+
import org.opensearch.plugins.ActionPlugin;
25+
import org.opensearch.plugins.Plugin;
26+
import org.opensearch.rest.RestController;
27+
import org.opensearch.rest.RestHandler;
28+
29+
import java.util.List;
30+
import java.util.function.Supplier;
31+
32+
/**
33+
* Plugin class for WorkloadManagement
34+
*/
35+
public class WorkloadManagementPlugin extends Plugin implements ActionPlugin {
36+
37+
/**
38+
* Default constructor
39+
*/
40+
public WorkloadManagementPlugin() {}
41+
42+
@Override
43+
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
44+
return List.of(new ActionPlugin.ActionHandler<>(CreateQueryGroupAction.INSTANCE, TransportCreateQueryGroupAction.class));
45+
}
46+
47+
@Override
48+
public List<RestHandler> getRestHandlers(
49+
Settings settings,
50+
RestController restController,
51+
ClusterSettings clusterSettings,
52+
IndexScopedSettings indexScopedSettings,
53+
SettingsFilter settingsFilter,
54+
IndexNameExpressionResolver indexNameExpressionResolver,
55+
Supplier<DiscoveryNodes> nodesInCluster
56+
) {
57+
return List.of(new RestCreateQueryGroupAction());
58+
}
59+
60+
@Override
61+
public List<Setting<?>> getSettings() {
62+
return List.of(QueryGroupPersistenceService.MAX_QUERY_GROUP_COUNT);
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.plugin.wlm.action;
10+
11+
import org.opensearch.action.ActionType;
12+
13+
/**
14+
* Transport action to create QueryGroup
15+
*
16+
* @opensearch.experimental
17+
*/
18+
public class CreateQueryGroupAction extends ActionType<CreateQueryGroupResponse> {
19+
20+
/**
21+
* An instance of CreateQueryGroupAction
22+
*/
23+
public static final CreateQueryGroupAction INSTANCE = new CreateQueryGroupAction();
24+
25+
/**
26+
* Name for CreateQueryGroupAction
27+
*/
28+
public static final String NAME = "cluster:admin/opensearch/wlm/query_group/_create";
29+
30+
/**
31+
* Default constructor
32+
*/
33+
private CreateQueryGroupAction() {
34+
super(NAME, CreateQueryGroupResponse::new);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.plugin.wlm.action;
10+
11+
import org.opensearch.action.ActionRequest;
12+
import org.opensearch.action.ActionRequestValidationException;
13+
import org.opensearch.cluster.metadata.QueryGroup;
14+
import org.opensearch.common.UUIDs;
15+
import org.opensearch.core.common.io.stream.StreamInput;
16+
import org.opensearch.core.common.io.stream.StreamOutput;
17+
import org.opensearch.core.xcontent.XContentParser;
18+
import org.joda.time.Instant;
19+
20+
import java.io.IOException;
21+
22+
/**
23+
* A request for create QueryGroup
24+
* User input schema:
25+
* {
26+
* "name": "analytics",
27+
* "resiliency_mode": "enforced",
28+
* "resource_limits": {
29+
* "cpu" : 0.4,
30+
* "memory" : 0.2
31+
* }
32+
* }
33+
*
34+
* @opensearch.experimental
35+
*/
36+
public class CreateQueryGroupRequest extends ActionRequest {
37+
private final QueryGroup queryGroup;
38+
39+
/**
40+
* Constructor for CreateQueryGroupRequest
41+
* @param queryGroup - A {@link QueryGroup} object
42+
*/
43+
public CreateQueryGroupRequest(QueryGroup queryGroup) {
44+
this.queryGroup = queryGroup;
45+
}
46+
47+
/**
48+
* Constructor for CreateQueryGroupRequest
49+
* @param in - A {@link StreamInput} object
50+
*/
51+
public CreateQueryGroupRequest(StreamInput in) throws IOException {
52+
super(in);
53+
queryGroup = new QueryGroup(in);
54+
}
55+
56+
/**
57+
* Generate a CreateQueryGroupRequest from XContent
58+
* @param parser - A {@link XContentParser} object
59+
*/
60+
public static CreateQueryGroupRequest fromXContent(XContentParser parser) throws IOException {
61+
QueryGroup.Builder builder = QueryGroup.Builder.fromXContent(parser);
62+
return new CreateQueryGroupRequest(builder._id(UUIDs.randomBase64UUID()).updatedAt(Instant.now().getMillis()).build());
63+
}
64+
65+
@Override
66+
public ActionRequestValidationException validate() {
67+
return null;
68+
}
69+
70+
@Override
71+
public void writeTo(StreamOutput out) throws IOException {
72+
super.writeTo(out);
73+
queryGroup.writeTo(out);
74+
}
75+
76+
/**
77+
* QueryGroup getter
78+
*/
79+
public QueryGroup getQueryGroup() {
80+
return queryGroup;
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.plugin.wlm.action;
10+
11+
import org.opensearch.cluster.metadata.QueryGroup;
12+
import org.opensearch.core.action.ActionResponse;
13+
import org.opensearch.core.common.io.stream.StreamInput;
14+
import org.opensearch.core.common.io.stream.StreamOutput;
15+
import org.opensearch.core.rest.RestStatus;
16+
import org.opensearch.core.xcontent.ToXContent;
17+
import org.opensearch.core.xcontent.ToXContentObject;
18+
import org.opensearch.core.xcontent.XContentBuilder;
19+
20+
import java.io.IOException;
21+
22+
/**
23+
* Response for the create API for QueryGroup
24+
*
25+
* @opensearch.experimental
26+
*/
27+
public class CreateQueryGroupResponse extends ActionResponse implements ToXContent, ToXContentObject {
28+
private final QueryGroup queryGroup;
29+
private final RestStatus restStatus;
30+
31+
/**
32+
* Constructor for CreateQueryGroupResponse
33+
* @param queryGroup - The QueryGroup to be included in the response
34+
* @param restStatus - The restStatus for the response
35+
*/
36+
public CreateQueryGroupResponse(final QueryGroup queryGroup, RestStatus restStatus) {
37+
this.queryGroup = queryGroup;
38+
this.restStatus = restStatus;
39+
}
40+
41+
/**
42+
* Constructor for CreateQueryGroupResponse
43+
* @param in - A {@link StreamInput} object
44+
*/
45+
public CreateQueryGroupResponse(StreamInput in) throws IOException {
46+
queryGroup = new QueryGroup(in);
47+
restStatus = RestStatus.readFrom(in);
48+
}
49+
50+
@Override
51+
public void writeTo(StreamOutput out) throws IOException {
52+
queryGroup.writeTo(out);
53+
RestStatus.writeTo(out, restStatus);
54+
}
55+
56+
@Override
57+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
58+
return queryGroup.toXContent(builder, params);
59+
}
60+
61+
/**
62+
* queryGroup getter
63+
*/
64+
public QueryGroup getQueryGroup() {
65+
return queryGroup;
66+
}
67+
68+
/**
69+
* restStatus getter
70+
*/
71+
public RestStatus getRestStatus() {
72+
return restStatus;
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.plugin.wlm.action;
10+
11+
import org.opensearch.action.support.ActionFilters;
12+
import org.opensearch.action.support.HandledTransportAction;
13+
import org.opensearch.common.inject.Inject;
14+
import org.opensearch.core.action.ActionListener;
15+
import org.opensearch.plugin.wlm.service.QueryGroupPersistenceService;
16+
import org.opensearch.tasks.Task;
17+
import org.opensearch.threadpool.ThreadPool;
18+
import org.opensearch.transport.TransportService;
19+
20+
/**
21+
* Transport action to create QueryGroup
22+
*
23+
* @opensearch.experimental
24+
*/
25+
public class TransportCreateQueryGroupAction extends HandledTransportAction<CreateQueryGroupRequest, CreateQueryGroupResponse> {
26+
27+
private final ThreadPool threadPool;
28+
private final QueryGroupPersistenceService queryGroupPersistenceService;
29+
30+
/**
31+
* Constructor for TransportCreateQueryGroupAction
32+
*
33+
* @param actionName - action name
34+
* @param transportService - a {@link TransportService} object
35+
* @param actionFilters - a {@link ActionFilters} object
36+
* @param threadPool - a {@link ThreadPool} object
37+
* @param queryGroupPersistenceService - a {@link QueryGroupPersistenceService} object
38+
*/
39+
@Inject
40+
public TransportCreateQueryGroupAction(
41+
String actionName,
42+
TransportService transportService,
43+
ActionFilters actionFilters,
44+
ThreadPool threadPool,
45+
QueryGroupPersistenceService queryGroupPersistenceService
46+
) {
47+
super(CreateQueryGroupAction.NAME, transportService, actionFilters, CreateQueryGroupRequest::new);
48+
this.threadPool = threadPool;
49+
this.queryGroupPersistenceService = queryGroupPersistenceService;
50+
}
51+
52+
@Override
53+
protected void doExecute(Task task, CreateQueryGroupRequest request, ActionListener<CreateQueryGroupResponse> listener) {
54+
threadPool.executor(ThreadPool.Names.SAME)
55+
.execute(() -> queryGroupPersistenceService.persistInClusterStateMetadata(request.getQueryGroup(), listener));
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
/**
10+
* Package for the action classes of WorkloadManagementPlugin
11+
*/
12+
package org.opensearch.plugin.wlm.action;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
/**
10+
* Base package for WorkloadManagementPlugin
11+
*/
12+
package org.opensearch.plugin.wlm;

0 commit comments

Comments
 (0)