-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Random sampling get stats #136040
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
Open
masseyke
wants to merge
15
commits into
elastic:main
Choose a base branch
from
masseyke:random-sampling-get-stats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Random sampling get stats #136040
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5771f4c
Adding an API to get random sampling stats
masseyke 2a74f97
merging main
masseyke 8da2d81
removing junk code
masseyke 83b4568
Merge branch 'main' into random-sampling-get-stats
masseyke 4a76c55
adding integration test
masseyke b8994cf
fixing unit tests
masseyke 4540c8e
Merge branch 'random-sampling-get-stats' of github.com:masseyke/elast…
masseyke 20d70c8
adding yaml rest test
masseyke 24f35bc
adding a unit test
masseyke aa121a7
cleanup
masseyke d00d124
Merge branch 'main' into random-sampling-get-stats
masseyke 863e510
copilot-suggested changes
masseyke 1d3db4c
making RestGetSampleStatsAction cancellable
masseyke 032d2cb
using SamplingService.getSamplingConfiguration()
masseyke 04a99c4
marking the rest action as serverless internal
masseyke 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
31 changes: 31 additions & 0 deletions
31
rest-api-spec/src/main/resources/rest-api-spec/api/indices.get_sample_stats.json
This file contains hidden or 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,31 @@ | ||
{ | ||
"indices.get_sample_stats": { | ||
"documentation": { | ||
"url": "https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-get-sample", | ||
"description": "Get stats about a random sample of ingested data" | ||
}, | ||
"stability": "experimental", | ||
"visibility": "public", | ||
"headers": { | ||
"accept": [ | ||
"application/json" | ||
] | ||
}, | ||
"url": { | ||
"paths": [ | ||
{ | ||
"path": "/{index}/_sample/stats", | ||
"methods": [ | ||
"GET" | ||
], | ||
"parts": { | ||
"index": { | ||
"type": "string", | ||
"description": "The name of a data stream or index" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.get_sample_stats/10_basic.yml
This file contains hidden or 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,19 @@ | ||
--- | ||
"Test get sample stats for index with no sample config": | ||
- requires: | ||
cluster_features: [ "random_sampling" ] | ||
reason: requires feature 'random_sampling' to get random samples | ||
|
||
- do: | ||
indices.get_sample_stats: | ||
index: non_existent | ||
catch: missing | ||
|
||
- do: | ||
indices.create: | ||
index: no_config | ||
|
||
- do: | ||
indices.get_sample_stats: | ||
index: no_config | ||
catch: missing |
This file contains hidden or 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 hidden or 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
234 changes: 234 additions & 0 deletions
234
...r/src/main/java/org/elasticsearch/action/admin/indices/sampling/GetSampleStatsAction.java
This file contains hidden or 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,234 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.action.admin.indices.sampling; | ||
|
||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.action.ActionType; | ||
import org.elasticsearch.action.FailedNodeException; | ||
import org.elasticsearch.action.IndicesRequest; | ||
import org.elasticsearch.action.support.IndicesOptions; | ||
import org.elasticsearch.action.support.nodes.BaseNodeResponse; | ||
import org.elasticsearch.action.support.nodes.BaseNodesRequest; | ||
import org.elasticsearch.action.support.nodes.BaseNodesResponse; | ||
import org.elasticsearch.cluster.ClusterName; | ||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.ingest.SamplingService; | ||
import org.elasticsearch.tasks.CancellableTask; | ||
import org.elasticsearch.tasks.Task; | ||
import org.elasticsearch.tasks.TaskId; | ||
import org.elasticsearch.transport.AbstractTransportRequest; | ||
import org.elasticsearch.xcontent.ToXContentObject; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class GetSampleStatsAction extends ActionType<GetSampleStatsAction.Response> { | ||
|
||
public static final GetSampleStatsAction INSTANCE = new GetSampleStatsAction(); | ||
public static final String NAME = "indices:admin/sample/stats"; | ||
|
||
private GetSampleStatsAction() { | ||
super(NAME); | ||
} | ||
|
||
public static class Request extends BaseNodesRequest implements IndicesRequest.Replaceable { | ||
private String indexName; | ||
|
||
public Request(String indexName) { | ||
super((String[]) null); | ||
this.indexName = indexName; | ||
} | ||
|
||
@Override | ||
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) { | ||
return new CancellableTask(id, type, action, "get sample stats", parentTaskId, headers); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
if (this.indices().length != 1) { | ||
return new ActionRequestValidationException(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public IndicesRequest indices(String... indices) { | ||
assert indices.length == 1 : "GetSampleStatsAction only supports a single index name"; | ||
this.indexName = indices[0]; | ||
return this; | ||
} | ||
|
||
@Override | ||
public String[] indices() { | ||
return new String[] { indexName }; | ||
} | ||
|
||
@Override | ||
public IndicesOptions indicesOptions() { | ||
return IndicesOptions.STRICT_SINGLE_INDEX_NO_EXPAND_FORBID_CLOSED_ALLOW_SELECTORS; | ||
} | ||
} | ||
|
||
public static class NodeRequest extends AbstractTransportRequest implements IndicesRequest { | ||
private final String indexName; | ||
|
||
public NodeRequest(String indexName) { | ||
this.indexName = indexName; | ||
} | ||
|
||
public NodeRequest(StreamInput in) throws IOException { | ||
super(in); | ||
this.indexName = in.readString(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeString(indexName); | ||
} | ||
|
||
public String getIndexName() { | ||
return indexName; | ||
} | ||
|
||
@Override | ||
public String[] indices() { | ||
return new String[] { indexName }; | ||
} | ||
|
||
@Override | ||
public IndicesOptions indicesOptions() { | ||
return IndicesOptions.STRICT_SINGLE_INDEX_NO_EXPAND_FORBID_CLOSED_ALLOW_SELECTORS; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
NodeRequest other = (NodeRequest) o; | ||
return Objects.equals(indexName, other.indexName); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(indexName); | ||
} | ||
} | ||
|
||
public static class Response extends BaseNodesResponse<GetSampleStatsAction.NodeResponse> implements Writeable, ToXContentObject { | ||
final int maxSize; | ||
|
||
public Response(StreamInput in) throws IOException { | ||
super(in); | ||
maxSize = in.readInt(); | ||
} | ||
|
||
public Response( | ||
ClusterName clusterName, | ||
List<GetSampleStatsAction.NodeResponse> nodes, | ||
List<FailedNodeException> failures, | ||
int maxSize | ||
) { | ||
super(clusterName, nodes, failures); | ||
this.maxSize = maxSize; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeInt(maxSize); | ||
} | ||
|
||
public SamplingService.SampleStats getSampleStats() { | ||
SamplingService.SampleStats rawStats = getRawSampleStats(); | ||
return rawStats.adjustForMaxSize(maxSize); | ||
} | ||
|
||
private SamplingService.SampleStats getRawSampleStats() { | ||
return getNodes().stream() | ||
.map(NodeResponse::getSampleStats) | ||
.filter(Objects::nonNull) | ||
.reduce(SamplingService.SampleStats::combine) | ||
.orElse(new SamplingService.SampleStats()); | ||
} | ||
|
||
@Override | ||
protected List<GetSampleStatsAction.NodeResponse> readNodesFrom(StreamInput in) throws IOException { | ||
return in.readCollectionAsList(GetSampleStatsAction.NodeResponse::new); | ||
} | ||
|
||
@Override | ||
protected void writeNodesTo(StreamOutput out, List<GetSampleStatsAction.NodeResponse> nodes) throws IOException { | ||
out.writeCollection(nodes); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Response other = (Response) o; | ||
return Objects.equals(getNodes(), other.getNodes()) && maxSize == other.maxSize; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getNodes(), maxSize); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
return getSampleStats().toXContent(builder, params); | ||
} | ||
} | ||
|
||
public static class NodeResponse extends BaseNodeResponse { | ||
private final SamplingService.SampleStats sampleStats; | ||
|
||
protected NodeResponse(StreamInput in) throws IOException { | ||
super(in); | ||
sampleStats = new SamplingService.SampleStats(in); | ||
} | ||
|
||
protected NodeResponse(DiscoveryNode node, SamplingService.SampleStats sampleStats) { | ||
super(node); | ||
this.sampleStats = sampleStats; | ||
} | ||
|
||
public SamplingService.SampleStats getSampleStats() { | ||
return sampleStats; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
sampleStats.writeTo(out); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
GetSampleStatsAction.NodeResponse other = (GetSampleStatsAction.NodeResponse) o; | ||
return getNode().equals(other.getNode()) && sampleStats.equals(other.sampleStats); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getNode(), sampleStats); | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.