|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.core; |
| 8 | + |
| 9 | +import org.elasticsearch.Version; |
| 10 | +import org.elasticsearch.common.Strings; |
| 11 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 12 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 13 | +import org.elasticsearch.common.io.stream.Writeable; |
| 14 | +import org.elasticsearch.common.unit.ByteSizeValue; |
| 15 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 16 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Objects; |
| 22 | + |
| 23 | +/** |
| 24 | + * {@link DataTiersFeatureSetUsage} represents the xpack usage for data tiers. |
| 25 | + * This includes things like the number of nodes per tier, indices, shards, etc. |
| 26 | + * See {@link TierSpecificStats} for the stats that are tracked on a per-tier |
| 27 | + * basis. |
| 28 | + */ |
| 29 | +public class DataTiersFeatureSetUsage extends XPackFeatureSet.Usage { |
| 30 | + private final Map<String, TierSpecificStats> tierStats; |
| 31 | + |
| 32 | + public DataTiersFeatureSetUsage(StreamInput in) throws IOException { |
| 33 | + super(in); |
| 34 | + this.tierStats = in.readMap(StreamInput::readString, TierSpecificStats::new); |
| 35 | + } |
| 36 | + |
| 37 | + public DataTiersFeatureSetUsage(Map<String, TierSpecificStats> tierStats) { |
| 38 | + super(XPackField.DATA_TIERS, true, true); |
| 39 | + this.tierStats = tierStats; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Version getMinimalSupportedVersion() { |
| 44 | + return Version.V_7_10_0; |
| 45 | + } |
| 46 | + |
| 47 | + public Map<String, TierSpecificStats> getTierStats() { |
| 48 | + return Collections.unmodifiableMap(tierStats); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void writeTo(StreamOutput out) throws IOException { |
| 53 | + super.writeTo(out); |
| 54 | + out.writeMap(tierStats, StreamOutput::writeString, (o, v) -> v.writeTo(o)); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected void innerXContent(XContentBuilder builder, Params params) throws IOException { |
| 59 | + super.innerXContent(builder, params); |
| 60 | + for (Map.Entry<String, TierSpecificStats> tierStats : tierStats.entrySet()) { |
| 61 | + builder.field(tierStats.getKey(), tierStats.getValue()); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public int hashCode() { |
| 67 | + return Objects.hash(tierStats); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean equals(Object obj) { |
| 72 | + if (obj == null) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + if (getClass() != obj.getClass()) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + DataTiersFeatureSetUsage other = (DataTiersFeatureSetUsage) obj; |
| 79 | + return Objects.equals(available, other.available) && |
| 80 | + Objects.equals(enabled, other.enabled) && |
| 81 | + Objects.equals(tierStats, other.tierStats); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public String toString() { |
| 86 | + return Strings.toString(this); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * {@link TierSpecificStats} represents statistics about nodes in a single |
| 91 | + * tier, for example, how many nodes there are, the index count, shard |
| 92 | + * count, etc. |
| 93 | + */ |
| 94 | + public static class TierSpecificStats implements Writeable, ToXContentObject { |
| 95 | + |
| 96 | + public final int nodeCount; |
| 97 | + public final int indexCount; |
| 98 | + public final int totalShardCount; |
| 99 | + public final int primaryShardCount; |
| 100 | + public final long docCount; |
| 101 | + public final long totalByteCount; |
| 102 | + public final long primaryByteCount; |
| 103 | + public final long primaryByteCountMedian; |
| 104 | + public final long primaryShardBytesMAD; |
| 105 | + |
| 106 | + public TierSpecificStats(StreamInput in) throws IOException { |
| 107 | + this.nodeCount = in.readVInt(); |
| 108 | + this.indexCount = in.readVInt(); |
| 109 | + this.totalShardCount = in.readVInt(); |
| 110 | + this.primaryShardCount = in.readVInt(); |
| 111 | + this.docCount = in.readVLong(); |
| 112 | + this.totalByteCount = in.readVLong(); |
| 113 | + this.primaryByteCount = in.readVLong(); |
| 114 | + this.primaryByteCountMedian = in.readVLong(); |
| 115 | + this.primaryShardBytesMAD = in.readVLong(); |
| 116 | + } |
| 117 | + |
| 118 | + public TierSpecificStats(int nodeCount, int indexCount, int totalShardCount, int primaryShardCount, long docCount, |
| 119 | + long totalByteCount, long primaryByteCount, long primaryByteCountMedian, long primaryShardBytesMAD) { |
| 120 | + this.nodeCount = nodeCount; |
| 121 | + this.indexCount = indexCount; |
| 122 | + this.totalShardCount = totalShardCount; |
| 123 | + this.primaryShardCount = primaryShardCount; |
| 124 | + this.docCount = docCount; |
| 125 | + this.totalByteCount = totalByteCount; |
| 126 | + this.primaryByteCount = primaryByteCount; |
| 127 | + this.primaryByteCountMedian = primaryByteCountMedian; |
| 128 | + this.primaryShardBytesMAD = primaryShardBytesMAD; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void writeTo(StreamOutput out) throws IOException { |
| 133 | + out.writeVInt(this.nodeCount); |
| 134 | + out.writeVInt(this.indexCount); |
| 135 | + out.writeVInt(this.totalShardCount); |
| 136 | + out.writeVInt(this.primaryShardCount); |
| 137 | + out.writeVLong(this.docCount); |
| 138 | + out.writeVLong(this.totalByteCount); |
| 139 | + out.writeVLong(this.primaryByteCount); |
| 140 | + out.writeVLong(this.primaryByteCountMedian); |
| 141 | + out.writeVLong(this.primaryShardBytesMAD); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 146 | + builder.startObject(); |
| 147 | + builder.field("node_count", nodeCount); |
| 148 | + builder.field("index_count", indexCount); |
| 149 | + builder.field("total_shard_count", totalShardCount); |
| 150 | + builder.field("primary_shard_count", primaryShardCount); |
| 151 | + builder.field("doc_count", docCount); |
| 152 | + builder.humanReadableField("total_size_bytes", "total_size", new ByteSizeValue(totalByteCount)); |
| 153 | + builder.humanReadableField("primary_size_bytes", "primary_size", new ByteSizeValue(primaryByteCount)); |
| 154 | + builder.humanReadableField("primary_shard_size_avg_bytes", "primary_shard_size_avg", |
| 155 | + new ByteSizeValue(primaryShardCount == 0 ? 0 : (primaryByteCount / primaryShardCount))); |
| 156 | + builder.humanReadableField("primary_shard_size_median_bytes", "primary_shard_size_median_bytes", |
| 157 | + new ByteSizeValue(primaryByteCountMedian)); |
| 158 | + builder.humanReadableField("primary_shard_size_mad_bytes", "primary_shard_size_mad_bytes", |
| 159 | + new ByteSizeValue(primaryShardBytesMAD)); |
| 160 | + builder.endObject(); |
| 161 | + return builder; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public int hashCode() { |
| 166 | + return Objects.hash(this.nodeCount, this.indexCount, this.totalShardCount, this.primaryShardCount, this.totalByteCount, |
| 167 | + this.primaryByteCount, this.docCount, this.primaryByteCountMedian, this.primaryShardBytesMAD); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public boolean equals(Object obj) { |
| 172 | + if (obj == null) { |
| 173 | + return false; |
| 174 | + } |
| 175 | + if (getClass() != obj.getClass()) { |
| 176 | + return false; |
| 177 | + } |
| 178 | + TierSpecificStats other = (TierSpecificStats) obj; |
| 179 | + return nodeCount == other.nodeCount && |
| 180 | + indexCount == other.indexCount && |
| 181 | + totalShardCount == other.totalShardCount && |
| 182 | + primaryShardCount == other.primaryShardCount && |
| 183 | + docCount == other.docCount && |
| 184 | + totalByteCount == other.totalByteCount && |
| 185 | + primaryByteCount == other.primaryByteCount && |
| 186 | + primaryByteCountMedian == other.primaryByteCountMedian && |
| 187 | + primaryShardBytesMAD == other.primaryShardBytesMAD; |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public String toString() { |
| 192 | + return Strings.toString(this); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments