|
@@ -19,16 +19,26 @@
|
|
|
|
|
|
package org.elasticsearch.cluster;
|
|
|
|
|
|
+import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
|
|
|
import org.elasticsearch.cluster.routing.ShardRouting;
|
|
|
import org.elasticsearch.common.collect.ImmutableOpenMap;
|
|
|
+import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
+import org.elasticsearch.common.io.stream.StreamOutput;
|
|
|
+import org.elasticsearch.common.io.stream.Writeable;
|
|
|
+import org.elasticsearch.common.xcontent.ToXContent;
|
|
|
+import org.elasticsearch.common.xcontent.ToXContent.Params;
|
|
|
+import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
/**
|
|
|
* ClusterInfo is an object representing a map of nodes to {@link DiskUsage}
|
|
|
* and a map of shard ids to shard sizes, see
|
|
|
* <code>InternalClusterInfoService.shardIdentifierFromRouting(String)</code>
|
|
|
* for the key used in the shardSizes map
|
|
|
*/
|
|
|
-public class ClusterInfo {
|
|
|
+public class ClusterInfo implements ToXContent, Writeable {
|
|
|
private final ImmutableOpenMap<String, DiskUsage> leastAvailableSpaceUsage;
|
|
|
private final ImmutableOpenMap<String, DiskUsage> mostAvailableSpaceUsage;
|
|
|
final ImmutableOpenMap<String, Long> shardSizes;
|
|
@@ -57,6 +67,105 @@ public class ClusterInfo {
|
|
|
this.routingToDataPath = routingToDataPath;
|
|
|
}
|
|
|
|
|
|
+ public ClusterInfo(StreamInput in) throws IOException {
|
|
|
+ int size = in.readInt();
|
|
|
+ Map<String, DiskUsage> leastMap = new HashMap<>(size);
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ leastMap.put(in.readString(), new DiskUsage(in));
|
|
|
+ }
|
|
|
+
|
|
|
+ size = in.readInt();
|
|
|
+ Map<String, DiskUsage> mostMap = new HashMap<>(size);
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ mostMap.put(in.readString(), new DiskUsage(in));
|
|
|
+ }
|
|
|
+
|
|
|
+ size = in.readInt();
|
|
|
+ Map<String, Long> sizeMap = new HashMap<>(size);
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ sizeMap.put(in.readString(), in.readLong());
|
|
|
+ }
|
|
|
+
|
|
|
+ size = in.readInt();
|
|
|
+ Map<ShardRouting, String> routingMap = new HashMap<>(size);
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ routingMap.put(new ShardRouting(in), in.readString());
|
|
|
+ }
|
|
|
+
|
|
|
+ ImmutableOpenMap.Builder<String, DiskUsage> leastBuilder = ImmutableOpenMap.builder();
|
|
|
+ this.leastAvailableSpaceUsage = leastBuilder.putAll(leastMap).build();
|
|
|
+ ImmutableOpenMap.Builder<String, DiskUsage> mostBuilder = ImmutableOpenMap.builder();
|
|
|
+ this.mostAvailableSpaceUsage = mostBuilder.putAll(mostMap).build();
|
|
|
+ ImmutableOpenMap.Builder<String, Long> sizeBuilder = ImmutableOpenMap.builder();
|
|
|
+ this.shardSizes = sizeBuilder.putAll(sizeMap).build();
|
|
|
+ ImmutableOpenMap.Builder<ShardRouting, String> routingBuilder = ImmutableOpenMap.builder();
|
|
|
+ this.routingToDataPath = routingBuilder.putAll(routingMap).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void writeTo(StreamOutput out) throws IOException {
|
|
|
+ out.writeVInt(this.leastAvailableSpaceUsage.size());
|
|
|
+ for (ObjectObjectCursor<String, DiskUsage> c : this.leastAvailableSpaceUsage) {
|
|
|
+ out.writeString(c.key);
|
|
|
+ c.value.writeTo(out);
|
|
|
+ }
|
|
|
+ out.writeVInt(this.mostAvailableSpaceUsage.size());
|
|
|
+ for (ObjectObjectCursor<String, DiskUsage> c : this.mostAvailableSpaceUsage) {
|
|
|
+ out.writeString(c.key);
|
|
|
+ c.value.writeTo(out);
|
|
|
+ }
|
|
|
+ out.writeVInt(this.shardSizes.size());
|
|
|
+ for (ObjectObjectCursor<String, Long> c : this.shardSizes) {
|
|
|
+ out.writeString(c.key);
|
|
|
+ if (c.value == null) {
|
|
|
+ out.writeLong(-1);
|
|
|
+ } else {
|
|
|
+ out.writeLong(c.value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ out.writeVInt(this.routingToDataPath.size());
|
|
|
+ for (ObjectObjectCursor<ShardRouting, String> c : this.routingToDataPath) {
|
|
|
+ c.key.writeTo(out);
|
|
|
+ out.writeString(c.value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
+ builder.startObject("nodes"); {
|
|
|
+ for (ObjectObjectCursor<String, DiskUsage> c : this.leastAvailableSpaceUsage) {
|
|
|
+ builder.startObject(c.key); { // node
|
|
|
+ builder.field("node_name", c.value.getNodeName());
|
|
|
+ builder.startObject("least_available"); {
|
|
|
+ c.value.toShortXContent(builder, params);
|
|
|
+ }
|
|
|
+ builder.endObject(); // end "least_available"
|
|
|
+ builder.startObject("most_available"); {
|
|
|
+ DiskUsage most = this.mostAvailableSpaceUsage.get(c.key);
|
|
|
+ if (most != null) {
|
|
|
+ most.toShortXContent(builder, params);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ builder.endObject(); // end "most_available"
|
|
|
+ }
|
|
|
+ builder.endObject(); // end $nodename
|
|
|
+ }
|
|
|
+ }
|
|
|
+ builder.endObject(); // end "nodes"
|
|
|
+ builder.startObject("shard_sizes"); {
|
|
|
+ for (ObjectObjectCursor<String, Long> c : this.shardSizes) {
|
|
|
+ builder.byteSizeField(c.key + "_bytes", c.key, c.value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ builder.endObject(); // end "shard_sizes"
|
|
|
+ builder.startObject("shard_paths"); {
|
|
|
+ for (ObjectObjectCursor<ShardRouting, String> c : this.routingToDataPath) {
|
|
|
+ builder.field(c.key.toString(), c.value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ builder.endObject(); // end "shard_paths"
|
|
|
+ return builder;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Returns a node id to disk usage mapping for the path that has the least available space on the node.
|
|
|
*/
|