org.elasticsearch.indices.NodeIndicesStats Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* 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 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 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.indices;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.stats.CommonStats;
import org.elasticsearch.action.admin.indices.stats.IndexShardStats;
import org.elasticsearch.action.admin.indices.stats.ShardStats;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.bulk.stats.BulkStats;
import org.elasticsearch.index.cache.query.QueryCacheStats;
import org.elasticsearch.index.cache.request.RequestCacheStats;
import org.elasticsearch.index.engine.SegmentsStats;
import org.elasticsearch.index.fielddata.FieldDataStats;
import org.elasticsearch.index.flush.FlushStats;
import org.elasticsearch.index.get.GetStats;
import org.elasticsearch.index.mapper.NodeMappingStats;
import org.elasticsearch.index.merge.MergeStats;
import org.elasticsearch.index.recovery.RecoveryStats;
import org.elasticsearch.index.refresh.RefreshStats;
import org.elasticsearch.index.search.stats.SearchStats;
import org.elasticsearch.index.shard.DocsStats;
import org.elasticsearch.index.shard.IndexingStats;
import org.elasticsearch.index.shard.ShardCountStats;
import org.elasticsearch.index.store.StoreStats;
import org.elasticsearch.index.translog.TranslogStats;
import org.elasticsearch.index.warmer.WarmerStats;
import org.elasticsearch.search.suggest.completion.CompletionStats;
import org.elasticsearch.xcontent.ToXContentFragment;
import org.elasticsearch.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Global information on indices stats running on a specific node.
*/
public class NodeIndicesStats implements Writeable, ToXContentFragment {
private static final Version VERSION_SUPPORTING_STATS_BY_INDEX = Version.V_8_5_0;
private final CommonStats stats;
private final Map> statsByShard;
private final Map statsByIndex;
public NodeIndicesStats(StreamInput in) throws IOException {
stats = new CommonStats(in);
statsByShard = new HashMap<>();
int entries = in.readVInt();
for (int i = 0; i < entries; i++) {
Index index = new Index(in);
int indexShardListSize = in.readVInt();
List indexShardStats = new ArrayList<>(indexShardListSize);
for (int j = 0; j < indexShardListSize; j++) {
indexShardStats.add(new IndexShardStats(in));
}
statsByShard.put(index, indexShardStats);
}
if (in.getVersion().onOrAfter(VERSION_SUPPORTING_STATS_BY_INDEX)) {
statsByIndex = in.readMap(Index::new, CommonStats::new);
} else {
statsByIndex = new HashMap<>();
}
}
public NodeIndicesStats(CommonStats oldStats, Map statsByIndex, Map> statsByShard) {
this.statsByShard = Objects.requireNonNull(statsByShard);
this.statsByIndex = Objects.requireNonNull(statsByIndex);
// make a total common stats from old ones and current ones
this.stats = oldStats;
for (List shardStatsList : statsByShard.values()) {
for (IndexShardStats indexShardStats : shardStatsList) {
for (ShardStats shardStats : indexShardStats.getShards()) {
stats.add(shardStats.getStats());
}
}
}
for (CommonStats indexStats : statsByIndex.values()) {
stats.add(indexStats);
}
}
@Nullable
public StoreStats getStore() {
return stats.getStore();
}
@Nullable
public DocsStats getDocs() {
return stats.getDocs();
}
@Nullable
public IndexingStats getIndexing() {
return stats.getIndexing();
}
@Nullable
public GetStats getGet() {
return stats.getGet();
}
@Nullable
public SearchStats getSearch() {
return stats.getSearch();
}
@Nullable
public MergeStats getMerge() {
return stats.getMerge();
}
@Nullable
public RefreshStats getRefresh() {
return stats.getRefresh();
}
@Nullable
public FlushStats getFlush() {
return stats.getFlush();
}
@Nullable
public WarmerStats getWarmer() {
return stats.getWarmer();
}
@Nullable
public FieldDataStats getFieldData() {
return stats.getFieldData();
}
@Nullable
public QueryCacheStats getQueryCache() {
return stats.getQueryCache();
}
@Nullable
public RequestCacheStats getRequestCache() {
return stats.getRequestCache();
}
@Nullable
public CompletionStats getCompletion() {
return stats.getCompletion();
}
@Nullable
public SegmentsStats getSegments() {
return stats.getSegments();
}
@Nullable
public TranslogStats getTranslog() {
return stats.getTranslog();
}
@Nullable
public RecoveryStats getRecoveryStats() {
return stats.getRecoveryStats();
}
@Nullable
public BulkStats getBulk() {
return stats.getBulk();
}
@Nullable
public ShardCountStats getShardCount() {
return stats.getShards();
}
@Nullable
public NodeMappingStats getNodeMappingStats() {
return stats.getNodeMappings();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
stats.writeTo(out);
out.writeMap(statsByShard, (o, k) -> k.writeTo(o), StreamOutput::writeList);
if (out.getVersion().onOrAfter(VERSION_SUPPORTING_STATS_BY_INDEX)) {
out.writeMap(statsByIndex, (o, k) -> k.writeTo(o), (o, v) -> v.writeTo(o));
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NodeIndicesStats that = (NodeIndicesStats) o;
return stats.equals(that.stats) && statsByShard.equals(that.statsByShard) && statsByIndex.equals(that.statsByIndex);
}
@Override
public int hashCode() {
return Objects.hash(stats, statsByShard, statsByIndex);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
final String level = params.param("level", "node");
final boolean isLevelValid = "indices".equalsIgnoreCase(level)
|| "node".equalsIgnoreCase(level)
|| "shards".equalsIgnoreCase(level);
if (isLevelValid == false) {
throw new IllegalArgumentException("level parameter must be one of [indices] or [node] or [shards] but was [" + level + "]");
}
// "node" level
builder.startObject(Fields.INDICES);
stats.toXContent(builder, params);
if ("indices".equals(level)) {
Map indexStats = createCommonStatsByIndex();
builder.startObject(Fields.INDICES);
for (Map.Entry entry : indexStats.entrySet()) {
builder.startObject(entry.getKey().getName());
entry.getValue().toXContent(builder, params);
builder.endObject();
}
builder.endObject();
} else if ("shards".equals(level)) {
builder.startObject("shards");
for (Map.Entry> entry : statsByShard.entrySet()) {
builder.startArray(entry.getKey().getName());
for (IndexShardStats indexShardStats : entry.getValue()) {
builder.startObject().startObject(String.valueOf(indexShardStats.getShardId().getId()));
for (ShardStats shardStats : indexShardStats.getShards()) {
shardStats.toXContent(builder, params);
}
builder.endObject().endObject();
}
builder.endArray();
}
builder.endObject();
}
builder.endObject();
return builder;
}
private Map createCommonStatsByIndex() {
Map statsMap = new HashMap<>();
for (Map.Entry> entry : statsByShard.entrySet()) {
if (statsMap.containsKey(entry.getKey()) == false) {
statsMap.put(entry.getKey(), new CommonStats());
}
for (IndexShardStats indexShardStats : entry.getValue()) {
for (ShardStats shardStats : indexShardStats.getShards()) {
statsMap.get(entry.getKey()).add(shardStats.getStats());
}
}
}
for (Map.Entry entry : statsByIndex.entrySet()) {
statsMap.computeIfAbsent(entry.getKey(), k -> new CommonStats()).add(entry.getValue());
}
return statsMap;
}
public List getShardStats(Index index) {
return statsByShard.get(index);
}
static final class Fields {
static final String INDICES = "indices";
}
}