forked from aparo/opensearch-learning-to-rank
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: #87 implemented rest endpoint to make stats available #90
Merged
sstults
merged 3 commits into
opensearch-project:main
from
JohannesDaniel:87-ltrstats-rest-endpoint
Dec 18, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
164 changes: 164 additions & 0 deletions
164
src/main/java/org/opensearch/ltr/rest/RestStatsLTRAction.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,164 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package org.opensearch.ltr.rest; | ||
|
||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.ltr.stats.LTRStats; | ||
import org.opensearch.ltr.transport.LTRStatsAction; | ||
import org.opensearch.ltr.transport.LTRStatsRequest; | ||
import org.opensearch.rest.BaseRestHandler; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.rest.action.RestActions; | ||
|
||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.o19s.es.ltr.LtrQueryParserPlugin.LTR_BASE_URI; | ||
import static com.o19s.es.ltr.LtrQueryParserPlugin.LTR_LEGACY_BASE_URI; | ||
|
||
/** | ||
* Provide an API to get information on the plugin usage and | ||
* performance, such as | ||
* <ul> | ||
* <li>statistics on plugin's cache performance</li> | ||
* <li>statistics on indices used to store features, feature sets and model definitions.</li> | ||
* <li>information on overall plugin status</li> | ||
* </ul> | ||
*/ | ||
public class RestStatsLTRAction extends BaseRestHandler { | ||
private static final String NAME = "learning_to_rank_stats"; | ||
private final LTRStats ltrStats; | ||
|
||
public RestStatsLTRAction(final LTRStats ltrStats) { | ||
this.ltrStats = ltrStats; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return List.of(); | ||
} | ||
|
||
@Override | ||
public List<ReplacedRoute> replacedRoutes() { | ||
return List.of( | ||
new ReplacedRoute( | ||
RestRequest.Method.GET, | ||
String.format(Locale.ROOT, "%s%s", LTR_BASE_URI, "/{nodeId}/stats/"), | ||
RestRequest.Method.GET, | ||
String.format(Locale.ROOT, "%s%s", LTR_LEGACY_BASE_URI, "/{nodeId}/stats/") | ||
), | ||
new ReplacedRoute( | ||
RestRequest.Method.GET, | ||
String.format(Locale.ROOT, "%s%s", LTR_BASE_URI, "/{nodeId}/stats/{stat}"), | ||
RestRequest.Method.GET, | ||
String.format(Locale.ROOT, "%s%s", LTR_LEGACY_BASE_URI, "/{nodeId}/stats/{stat}") | ||
), | ||
new ReplacedRoute( | ||
RestRequest.Method.GET, | ||
String.format(Locale.ROOT, "%s%s", LTR_BASE_URI, "/stats/"), | ||
RestRequest.Method.GET, | ||
String.format(Locale.ROOT, "%s%s", LTR_LEGACY_BASE_URI, "/stats/") | ||
), | ||
new ReplacedRoute( | ||
RestRequest.Method.GET, | ||
String.format(Locale.ROOT, "%s%s", LTR_BASE_URI, "/stats/{stat}"), | ||
RestRequest.Method.GET, | ||
String.format(Locale.ROOT, "%s%s", LTR_LEGACY_BASE_URI, "/stats/{stat}") | ||
) | ||
); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings({"rawtypes", "unchecked"}) | ||
protected RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { | ||
final LTRStatsRequest ltrStatsRequest = getRequest(request); | ||
return (channel) -> client.execute(LTRStatsAction.INSTANCE, | ||
ltrStatsRequest, | ||
new RestActions.NodesResponseRestListener(channel)); | ||
} | ||
|
||
/** | ||
* Creates a LTRStatsRequest from a RestRequest | ||
* | ||
* @param request RestRequest | ||
* @return LTRStatsRequest | ||
*/ | ||
private LTRStatsRequest getRequest(final RestRequest request) { | ||
final LTRStatsRequest ltrStatsRequest = new LTRStatsRequest( | ||
splitCommaSeparatedParam(request, "nodeId") | ||
); | ||
ltrStatsRequest.timeout(request.param("timeout")); | ||
|
||
final List<String> requestedStats = List.of(splitCommaSeparatedParam(request, "stat")); | ||
|
||
final Set<String> validStats = ltrStats.getStats().keySet(); | ||
|
||
if (isAllStatsRequested(requestedStats)) { | ||
ltrStatsRequest.addAll(validStats); | ||
|
||
} else { | ||
ltrStatsRequest.addAll(getStatsToBeRetrieved(request, validStats, requestedStats)); | ||
} | ||
|
||
return ltrStatsRequest; | ||
} | ||
|
||
private Set<String> getStatsToBeRetrieved( | ||
final RestRequest request, | ||
final Set<String> validStats, | ||
final List<String> requestedStats) { | ||
|
||
if (requestedStats.contains(LTRStatsRequest.ALL_STATS_KEY)) { | ||
throw new IllegalArgumentException(String.format(Locale.ROOT, "Request %s contains both %s and individual stats", | ||
request.path(), LTRStatsRequest.ALL_STATS_KEY)); | ||
} | ||
|
||
final Set<String> invalidStats = requestedStats.stream() | ||
.filter(s -> !validStats.contains(s)) | ||
.collect(Collectors.toSet()); | ||
|
||
if (!invalidStats.isEmpty()) { | ||
throw new IllegalArgumentException( | ||
unrecognized(request, invalidStats, new HashSet<>(requestedStats), "stat")); | ||
} | ||
|
||
return new HashSet<>(requestedStats); | ||
} | ||
|
||
private boolean isAllStatsRequested(final List<String> requestedStats) { | ||
return requestedStats.isEmpty() | ||
|| (requestedStats.size() == 1 && requestedStats.contains(LTRStatsRequest.ALL_STATS_KEY)); | ||
} | ||
|
||
private String[] splitCommaSeparatedParam(final RestRequest request, final String paramName) { | ||
final String param = request.param(paramName); | ||
|
||
if (param == null) { | ||
return new String[0]; | ||
} else { | ||
return param.split(","); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
|
@@ -75,7 +76,7 @@ public String getLtrStoreHealthStatus(String storeName) { | |
clusterService.state().getRoutingTable().index(storeName) | ||
); | ||
|
||
return indexHealth.getStatus().name().toLowerCase(); | ||
return indexHealth.getStatus().name().toLowerCase(Locale.ROOT); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default as this is displayed |
||
|
||
/** | ||
|
38 changes: 38 additions & 0 deletions
38
src/main/java/org/opensearch/ltr/transport/LTRStatsAction.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,38 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package org.opensearch.ltr.transport; | ||
|
||
import org.opensearch.action.ActionRequestBuilder; | ||
import org.opensearch.action.ActionType; | ||
import org.opensearch.client.OpenSearchClient; | ||
|
||
public class LTRStatsAction extends ActionType<LTRStatsNodesResponse> { | ||
public static final String NAME = "cluster:admin/ltr/stats"; | ||
public static final LTRStatsAction INSTANCE = new LTRStatsAction(); | ||
|
||
protected LTRStatsAction() { | ||
super(NAME, LTRStatsNodesResponse::new); | ||
} | ||
|
||
public static class LTRStatsRequestBuilder | ||
extends ActionRequestBuilder<LTRStatsRequest, LTRStatsNodesResponse> { | ||
private static final String[] nodeIds = null; | ||
|
||
protected LTRStatsRequestBuilder(final OpenSearchClient client) { | ||
super(client, INSTANCE, new LTRStatsRequest(nodeIds)); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/org/opensearch/ltr/transport/LTRStatsNodeRequest.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,48 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package org.opensearch.ltr.transport; | ||
|
||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.transport.TransportRequest; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* LTRStatsNodeRequest to get a node stat | ||
*/ | ||
public class LTRStatsNodeRequest extends TransportRequest { | ||
private final LTRStatsRequest ltrStatsRequest; | ||
|
||
public LTRStatsNodeRequest(final LTRStatsRequest ltrStatsRequest) { | ||
this.ltrStatsRequest = ltrStatsRequest; | ||
} | ||
|
||
public LTRStatsNodeRequest(final StreamInput in) throws IOException { | ||
super(in); | ||
ltrStatsRequest = new LTRStatsRequest(in); | ||
} | ||
|
||
public LTRStatsRequest getLTRStatsNodesRequest() { | ||
return ltrStatsRequest; | ||
} | ||
|
||
@Override | ||
public void writeTo(final StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
ltrStatsRequest.writeTo(out); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/org/opensearch/ltr/transport/LTRStatsNodeResponse.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,63 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package org.opensearch.ltr.transport; | ||
|
||
import org.opensearch.action.support.nodes.BaseNodeResponse; | ||
import org.opensearch.cluster.node.DiscoveryNode; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.xcontent.ToXContentFragment; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class LTRStatsNodeResponse extends BaseNodeResponse implements ToXContentFragment { | ||
|
||
private final Map<String, Object> statsMap; | ||
|
||
LTRStatsNodeResponse(final StreamInput in) throws IOException { | ||
super(in); | ||
this.statsMap = in.readMap(StreamInput::readString, StreamInput::readGenericValue); | ||
} | ||
|
||
LTRStatsNodeResponse(final DiscoveryNode node, final Map<String, Object> statsToValues) { | ||
super(node); | ||
this.statsMap = statsToValues; | ||
} | ||
|
||
public static LTRStatsNodeResponse readStats(final StreamInput in) throws IOException { | ||
return new LTRStatsNodeResponse(in); | ||
} | ||
|
||
public Map<String, Object> getStatsMap() { | ||
return statsMap; | ||
} | ||
|
||
@Override | ||
public void writeTo(final StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeMap(statsMap, StreamOutput::writeString, StreamOutput::writeGenericValue); | ||
} | ||
|
||
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException { | ||
for (Map.Entry<String, Object> stat : statsMap.entrySet()) { | ||
builder.field(stat.getKey(), stat.getValue()); | ||
} | ||
|
||
return builder; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for this line.
Why do we use default locale here https://github.com/opensearch-project/opensearch-learning-to-rank-base/blob/main/src/main/java/com/o19s/es/termstat/TermStatQueryBuilder.java#L154-L155