Skip to content

Commit

Permalink
[7.x] Data Stream Stats API (elastic#58707) (elastic#59566)
Browse files Browse the repository at this point in the history
This API reports on statistics important for data streams, including the number of data
streams, the number of backing indices for those streams, the disk usage for each data
stream, and the maximum timestamp for each data stream
  • Loading branch information
jbaiera authored Jul 14, 2020
1 parent ed2c29f commit 5f7e7e9
Show file tree
Hide file tree
Showing 18 changed files with 1,732 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.elasticsearch.client.indices.CreateDataStreamRequest;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.DataStreamsStatsRequest;
import org.elasticsearch.client.indices.DataStreamsStatsResponse;
import org.elasticsearch.client.indices.DeleteAliasRequest;
import org.elasticsearch.client.indices.DeleteComposableIndexTemplateRequest;
import org.elasticsearch.client.indices.DeleteDataStreamRequest;
Expand Down Expand Up @@ -260,6 +262,40 @@ public Cancellable getDataStreamAsync(GetDataStreamRequest dataStreamRequest, Re
GetDataStreamResponse::fromXContent, listener, emptySet());
}

/**
* Gets statistics about one or more data streams using the Get Data Streams Stats API.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html"> Data Streams API on
* elastic.co</a>
*
* @param dataStreamsStatsRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be
* customized
* @return the response
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public DataStreamsStatsResponse dataStreamsStats(DataStreamsStatsRequest dataStreamsStatsRequest, RequestOptions options)
throws IOException {
return restHighLevelClient.performRequestAndParseEntity(dataStreamsStatsRequest, IndicesRequestConverters::dataStreamsStats,
options, DataStreamsStatsResponse::fromXContent, emptySet());
}

/**
* Asynchronously gets statistics about one or more data streams using the Get Data Streams Stats API.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html"> Data Streams API on
* elastic.co</a>
*
* @param dataStreamsStatsRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be
* customized
* @param listener the listener to be notified upon request completion
* @return cancellable that may be used to cancel the request
*/
public Cancellable dataStreamsStatsAsync(DataStreamsStatsRequest dataStreamsStatsRequest, RequestOptions options,
ActionListener<DataStreamsStatsResponse> listener) {
return restHighLevelClient.performRequestAsyncAndParseEntity(dataStreamsStatsRequest, IndicesRequestConverters::dataStreamsStats,
options, DataStreamsStatsResponse::fromXContent, listener, emptySet());
}

/**
* Creates an index using the Create Index API.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.elasticsearch.client.indices.CloseIndexRequest;
import org.elasticsearch.client.indices.CreateDataStreamRequest;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.DataStreamsStatsRequest;
import org.elasticsearch.client.indices.GetDataStreamRequest;
import org.elasticsearch.client.indices.DeleteAliasRequest;
import org.elasticsearch.client.indices.DeleteComposableIndexTemplateRequest;
Expand Down Expand Up @@ -96,6 +97,21 @@ static Request getDataStreams(GetDataStreamRequest dataStreamRequest) {
return new Request(HttpGet.METHOD_NAME, endpoint);
}

static Request dataStreamsStats(DataStreamsStatsRequest dataStreamsStatsRequest) {
String[] expressions = dataStreamsStatsRequest.indices() == null ? Strings.EMPTY_ARRAY : dataStreamsStatsRequest.indices();
final String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_data_stream")
.addCommaSeparatedPathParts(expressions)
.addPathPartAsIs("_stats")
.build();
Request request = new Request(HttpGet.METHOD_NAME, endpoint);

RequestConverters.Params parameters = new RequestConverters.Params();
parameters.withIndicesOptions(dataStreamsStatsRequest.indicesOptions());
request.addParameters(parameters.asMap());
return request;
}

static Request deleteIndex(DeleteIndexRequest deleteIndexRequest) {
String endpoint = RequestConverters.endpoint(deleteIndexRequest.indices());
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License 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.elasticsearch.client.indices;

import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.Validatable;

public class DataStreamsStatsRequest implements Validatable {

private final String[] indices;
private IndicesOptions indicesOptions = IndicesOptions.fromOptions(false, false, true, true);

public DataStreamsStatsRequest(String... indices) {
this.indices = indices;
}

public String[] indices() {
return indices;
}

public IndicesOptions indicesOptions() {
return indicesOptions;
}

public DataStreamsStatsRequest indicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = indicesOptions;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License 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.elasticsearch.client.indices;

import org.elasticsearch.client.core.BroadcastResponse;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;

public class DataStreamsStatsResponse extends BroadcastResponse {

private final int dataStreamCount;
private final int backingIndices;
private final ByteSizeValue totalStoreSize;
private final Map<String, DataStreamStats> dataStreams;

protected DataStreamsStatsResponse(Shards shards, int dataStreamCount, int backingIndices, ByteSizeValue totalStoreSize,
Map<String, DataStreamStats> dataStreams) {
super(shards);
this.dataStreamCount = dataStreamCount;
this.backingIndices = backingIndices;
this.totalStoreSize = totalStoreSize;
this.dataStreams = dataStreams;
}

private static final ParseField DATA_STREAM_COUNT = new ParseField("data_stream_count");
private static final ParseField BACKING_INDICES = new ParseField("backing_indices");
private static final ParseField TOTAL_STORE_SIZE_BYTES = new ParseField("total_store_size_bytes");
private static final ParseField DATA_STREAMS = new ParseField("data_streams");
private static final ParseField DATA_STREAM = new ParseField("data_stream");
private static final ParseField STORE_SIZE_BYTES = new ParseField("store_size_bytes");
private static final ParseField MAXIMUM_TIMESTAMP = new ParseField("maximum_timestamp");

@SuppressWarnings("unchecked")
private static final ConstructingObjectParser<DataStreamsStatsResponse, Void> PARSER = new ConstructingObjectParser<>(
"data_streams_stats", true, arg -> {
Shards shards = (Shards) arg[0];
Integer dataStreamCount = ((Integer) arg[1]);
Integer backingIndices = ((Integer) arg[2]);
ByteSizeValue totalStoreSize = ((ByteSizeValue) arg[3]);
Map<String, DataStreamStats> dataStreams = new HashMap<>();
for (DataStreamStats dataStreamStats : ((List<DataStreamStats>) arg[4])) {
dataStreams.put(dataStreamStats.dataStream, dataStreamStats);
}
return new DataStreamsStatsResponse(shards, dataStreamCount, backingIndices, totalStoreSize, dataStreams);
});

private static final ConstructingObjectParser<DataStreamStats, Void> ENTRY_PARSER = new ConstructingObjectParser<>(
"data_streams_stats.entry", true, arg -> {
String dataStream = ((String) arg[0]);
Integer backingIndices = ((Integer) arg[1]);
ByteSizeValue storeSize = ((ByteSizeValue) arg[2]);
Long maximumTimestamp = ((Long) arg[3]);
return new DataStreamStats(dataStream, backingIndices, storeSize, maximumTimestamp);
});

static {
declareShardsField(PARSER);
PARSER.declareInt(constructorArg(), DATA_STREAM_COUNT);
PARSER.declareInt(constructorArg(), BACKING_INDICES);
PARSER.declareField(constructorArg(), (p, c) -> new ByteSizeValue(p.longValue()), TOTAL_STORE_SIZE_BYTES,
ObjectParser.ValueType.VALUE);
PARSER.declareObjectArray(constructorArg(), ENTRY_PARSER, DATA_STREAMS);
ENTRY_PARSER.declareString(constructorArg(), DATA_STREAM);
ENTRY_PARSER.declareInt(constructorArg(), BACKING_INDICES);
ENTRY_PARSER.declareField(constructorArg(), (p, c) -> new ByteSizeValue(p.longValue()), STORE_SIZE_BYTES,
ObjectParser.ValueType.VALUE);
ENTRY_PARSER.declareLong(constructorArg(), MAXIMUM_TIMESTAMP);
}

public static DataStreamsStatsResponse fromXContent(final XContentParser parser) throws IOException {
return PARSER.apply(parser, null);
}

public int getDataStreamCount() {
return dataStreamCount;
}

public int getBackingIndices() {
return backingIndices;
}

public ByteSizeValue getTotalStoreSize() {
return totalStoreSize;
}

public Map<String, DataStreamStats> getDataStreams() {
return dataStreams;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
DataStreamsStatsResponse that = (DataStreamsStatsResponse) obj;
return dataStreamCount == that.dataStreamCount &&
backingIndices == that.backingIndices &&
Objects.equals(totalStoreSize, that.totalStoreSize) &&
Objects.equals(dataStreams, that.dataStreams);
}

@Override
public int hashCode() {
return Objects.hash(dataStreamCount, backingIndices, totalStoreSize, dataStreams);
}

@Override
public String toString() {
return "DataStreamsStatsResponse{" +
"dataStreamCount=" + dataStreamCount +
", backingIndices=" + backingIndices +
", totalStoreSize=" + totalStoreSize +
", dataStreams=" + dataStreams +
'}';
}

public static class DataStreamStats {

private final String dataStream;
private final int backingIndices;
private final ByteSizeValue storeSize;
private final long maximumTimestamp;

public DataStreamStats(String dataStream, int backingIndices, ByteSizeValue storeSize, long maximumTimestamp) {
this.dataStream = dataStream;
this.backingIndices = backingIndices;
this.storeSize = storeSize;
this.maximumTimestamp = maximumTimestamp;
}

public String getDataStream() {
return dataStream;
}

public int getBackingIndices() {
return backingIndices;
}

public ByteSizeValue getStoreSize() {
return storeSize;
}

public long getMaximumTimestamp() {
return maximumTimestamp;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
DataStreamStats that = (DataStreamStats) obj;
return backingIndices == that.backingIndices &&
maximumTimestamp == that.maximumTimestamp &&
Objects.equals(dataStream, that.dataStream) &&
Objects.equals(storeSize, that.storeSize);
}

@Override
public int hashCode() {
return Objects.hash(dataStream, backingIndices, storeSize, maximumTimestamp);
}

@Override
public String toString() {
return "DataStreamStats{" +
"dataStream='" + dataStream + '\'' +
", backingIndices=" + backingIndices +
", storeSize=" + storeSize +
", maximumTimestamp=" + maximumTimestamp +
'}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.DataStream;
import org.elasticsearch.client.indices.DataStreamsStatsRequest;
import org.elasticsearch.client.indices.DataStreamsStatsResponse;
import org.elasticsearch.client.indices.DataStreamsStatsResponse.DataStreamStats;
import org.elasticsearch.client.indices.DeleteAliasRequest;
import org.elasticsearch.client.indices.DeleteComposableIndexTemplateRequest;
import org.elasticsearch.client.indices.DeleteDataStreamRequest;
Expand Down Expand Up @@ -2069,6 +2072,26 @@ public void testDataStreams() throws Exception {
assertThat(dataStream.getTimeStampField(), equalTo("@timestamp"));
assertThat(dataStream.getIndices(), hasSize(1));

DataStreamsStatsRequest dataStreamsStatsRequest = new DataStreamsStatsRequest();
DataStreamsStatsResponse dataStreamsStatsResponse = execute(dataStreamsStatsRequest, indices::dataStreamsStats,
indices::dataStreamsStatsAsync);
int dataStreamsCount = dataStreamsStatsResponse.getDataStreamCount();
assertThat(dataStreamsCount, equalTo(1));
int backingIndices = dataStreamsStatsResponse.getBackingIndices();
assertThat(backingIndices, equalTo(1));
ByteSizeValue byteSizeValue = dataStreamsStatsResponse.getTotalStoreSize();
assertThat(byteSizeValue, notNullValue());
assertThat(byteSizeValue.getBytes(), not(equalTo(0L)));
Map<String, DataStreamStats> dataStreamsStats = dataStreamsStatsResponse.getDataStreams();
assertThat(dataStreamsStats, notNullValue());
assertThat(dataStreamsStats.size(), equalTo(1));
DataStreamStats dataStreamStat = dataStreamsStats.get(dataStreamName);
assertThat(dataStreamStat, notNullValue());
assertThat(dataStreamStat.getDataStream(), equalTo(dataStreamName));
assertThat(dataStreamStat.getBackingIndices(), equalTo(1));
assertThat(dataStreamStat.getMaximumTimestamp(), equalTo(0L)); // No data in here
assertThat(dataStreamStat.getStoreSize().getBytes(), not(equalTo(0L))); // but still takes up some space on disk

DeleteDataStreamRequest deleteDataStreamRequest = new DeleteDataStreamRequest(dataStreamName);
response = execute(deleteDataStreamRequest, indices::deleteDataStream, indices::deleteDataStreamAsync);
assertThat(response.isAcknowledged(), equalTo(true));
Expand Down
Loading

0 comments on commit 5f7e7e9

Please sign in to comment.