Browse Source

Simplify cluster info related classes to records (#90080)

Ievgen Degtiarenko 3 years ago
parent
commit
fa654b9a0b

+ 8 - 52
server/src/main/java/org/elasticsearch/cluster/ClusterInfo.java

@@ -15,7 +15,6 @@ 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.unit.ByteSizeValue;
-import org.elasticsearch.common.util.set.Sets;
 import org.elasticsearch.index.shard.ShardId;
 import org.elasticsearch.index.store.StoreStats;
 import org.elasticsearch.xcontent.ToXContentFragment;
@@ -36,13 +35,13 @@ import java.util.Set;
  */
 public class ClusterInfo implements ToXContentFragment, Writeable {
 
+    public static final ClusterInfo EMPTY = new ClusterInfo();
     public static final Version DATA_SET_SIZE_SIZE_VERSION = Version.V_7_13_0;
 
     private final Map<String, DiskUsage> leastAvailableSpaceUsage;
     private final Map<String, DiskUsage> mostAvailableSpaceUsage;
     final Map<String, Long> shardSizes;
     final Map<ShardId, Long> shardDataSetSizes;
-    public static final ClusterInfo EMPTY = new ClusterInfo();
     final Map<ShardRouting, String> routingToDataPath;
     final Map<NodeAndPath, ReservedSpace> reservedSpace;
 
@@ -236,31 +235,15 @@ public class ClusterInfo implements ToXContentFragment, Writeable {
     /**
      * Represents a data path on a node
      */
-    public static class NodeAndPath implements Writeable {
-        public final String nodeId;
-        public final String path;
+    public record NodeAndPath(String nodeId, String path) implements Writeable {
 
-        public NodeAndPath(String nodeId, String path) {
-            this.nodeId = Objects.requireNonNull(nodeId);
-            this.path = Objects.requireNonNull(path);
+        public NodeAndPath {
+            Objects.requireNonNull(nodeId);
+            Objects.requireNonNull(path);
         }
 
         public NodeAndPath(StreamInput in) throws IOException {
-            this.nodeId = in.readString();
-            this.path = in.readString();
-        }
-
-        @Override
-        public boolean equals(Object o) {
-            if (this == o) return true;
-            if (o == null || getClass() != o.getClass()) return false;
-            NodeAndPath that = (NodeAndPath) o;
-            return nodeId.equals(that.nodeId) && path.equals(that.path);
-        }
-
-        @Override
-        public int hashCode() {
-            return Objects.hash(nodeId, path);
+            this(in.readString(), in.readString());
         }
 
         @Override
@@ -273,25 +256,12 @@ public class ClusterInfo implements ToXContentFragment, Writeable {
     /**
      * Represents the total amount of "reserved" space on a particular data path, together with the set of shards considered.
      */
-    public static class ReservedSpace implements Writeable {
+    public record ReservedSpace(long total, Set<ShardId> shardIds) implements Writeable {
 
         public static final ReservedSpace EMPTY = new ReservedSpace(0, new HashSet<>());
 
-        private final long total;
-        private final Set<ShardId> shardIds;
-
-        private ReservedSpace(long total, HashSet<ShardId> shardIds) {
-            this.total = total;
-            this.shardIds = shardIds;
-        }
-
         ReservedSpace(StreamInput in) throws IOException {
-            total = in.readVLong();
-            final int shardIdCount = in.readVInt();
-            shardIds = Sets.newHashSetWithExpectedSize(shardIdCount);
-            for (int i = 0; i < shardIdCount; i++) {
-                shardIds.add(new ShardId(in));
-            }
+            this(in.readVLong(), in.readSet(ShardId::new));
         }
 
         @Override
@@ -308,19 +278,6 @@ public class ClusterInfo implements ToXContentFragment, Writeable {
             return shardIds.contains(shardId);
         }
 
-        @Override
-        public boolean equals(Object o) {
-            if (this == o) return true;
-            if (o == null || getClass() != o.getClass()) return false;
-            ReservedSpace that = (ReservedSpace) o;
-            return total == that.total && shardIds.equals(that.shardIds);
-        }
-
-        @Override
-        public int hashCode() {
-            return Objects.hash(total, shardIds);
-        }
-
         void toXContent(XContentBuilder builder, Params params) throws IOException {
             builder.field("total", total);
             builder.startArray("shards");
@@ -352,5 +309,4 @@ public class ClusterInfo implements ToXContentFragment, Writeable {
             }
         }
     }
-
 }

+ 5 - 43
server/src/main/java/org/elasticsearch/cluster/DiskUsage.java

@@ -22,39 +22,19 @@ import org.elasticsearch.xcontent.ToXContentFragment;
 import org.elasticsearch.xcontent.XContentBuilder;
 
 import java.io.IOException;
-import java.util.Objects;
 
 /**
  * Encapsulation class used to represent the amount of disk used on a node.
  */
-public class DiskUsage implements ToXContentFragment, Writeable {
+public record DiskUsage(String nodeId, String nodeName, String path, long totalBytes, long freeBytes)
+    implements
+        ToXContentFragment,
+        Writeable {
 
     private static final Logger logger = LogManager.getLogger(DiskUsage.class);
 
-    final String nodeId;
-    final String nodeName;
-    final String path;
-    final long totalBytes;
-    final long freeBytes;
-
-    /**
-     * Create a new DiskUsage, if {@code totalBytes} is 0, {@link #getFreeDiskAsPercentage()}
-     * will always return 100.0% free
-     */
-    public DiskUsage(String nodeId, String nodeName, String path, long totalBytes, long freeBytes) {
-        this.nodeId = nodeId;
-        this.nodeName = nodeName;
-        this.freeBytes = freeBytes;
-        this.totalBytes = totalBytes;
-        this.path = path;
-    }
-
     public DiskUsage(StreamInput in) throws IOException {
-        this.nodeId = in.readString();
-        this.nodeName = in.readString();
-        this.path = in.readString();
-        this.totalBytes = in.readVLong();
-        this.freeBytes = in.readVLong();
+        this(in.readString(), in.readString(), in.readString(), in.readVLong(), in.readVLong());
     }
 
     @Override
@@ -124,24 +104,6 @@ public class DiskUsage implements ToXContentFragment, Writeable {
         return getTotalBytes() - getFreeBytes();
     }
 
-    @Override
-    public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
-
-        DiskUsage other = (DiskUsage) o;
-        return Objects.equals(nodeId, other.nodeId)
-            && Objects.equals(nodeName, other.nodeName)
-            && Objects.equals(totalBytes, other.totalBytes)
-            && Objects.equals(freeBytes, other.freeBytes);
-
-    }
-
-    @Override
-    public int hashCode() {
-        return Objects.hash(nodeId, nodeName, path, totalBytes, freeBytes);
-    }
-
     @Override
     public String toString() {
         return "["