forked from aparo/opensearch-learning-to-rank
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: Johannes Peter <[email protected]>
- Loading branch information
1 parent
aced4bd
commit 244824e
Showing
11 changed files
with
681 additions
and
4 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
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
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.