diff options
Diffstat (limited to 'config-provisioning/src/main/java/com/yahoo/config/provision/NodeResources.java')
-rw-r--r-- | config-provisioning/src/main/java/com/yahoo/config/provision/NodeResources.java | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/config-provisioning/src/main/java/com/yahoo/config/provision/NodeResources.java b/config-provisioning/src/main/java/com/yahoo/config/provision/NodeResources.java index b887a2a93e6..aa30c1373cb 100644 --- a/config-provisioning/src/main/java/com/yahoo/config/provision/NodeResources.java +++ b/config-provisioning/src/main/java/com/yahoo/config/provision/NodeResources.java @@ -17,6 +17,7 @@ public class NodeResources { private static final double diskUnitCost = 0.0003; private static final NodeResources zero = new NodeResources(0, 0, 0, 0); + private static final NodeResources unspecified = new NodeResources(0, 0, 0, 0); public enum DiskSpeed { @@ -125,31 +126,37 @@ public class NodeResources { } public NodeResources withVcpu(double vcpu) { + ensureSpecified(); if (vcpu == this.vcpu) return this; return new NodeResources(vcpu, memoryGb, diskGb, bandwidthGbps, diskSpeed, storageType); } public NodeResources withMemoryGb(double memoryGb) { + ensureSpecified(); if (memoryGb == this.memoryGb) return this; return new NodeResources(vcpu, memoryGb, diskGb, bandwidthGbps, diskSpeed, storageType); } public NodeResources withDiskGb(double diskGb) { + ensureSpecified(); if (diskGb == this.diskGb) return this; return new NodeResources(vcpu, memoryGb, diskGb, bandwidthGbps, diskSpeed, storageType); } public NodeResources withBandwidthGbps(double bandwidthGbps) { + ensureSpecified(); if (bandwidthGbps == this.bandwidthGbps) return this; return new NodeResources(vcpu, memoryGb, diskGb, bandwidthGbps, diskSpeed, storageType); } public NodeResources with(DiskSpeed diskSpeed) { + ensureSpecified(); if (diskSpeed == this.diskSpeed) return this; return new NodeResources(vcpu, memoryGb, diskGb, bandwidthGbps, diskSpeed, storageType); } public NodeResources with(StorageType storageType) { + ensureSpecified(); if (storageType == this.storageType) return this; return new NodeResources(vcpu, memoryGb, diskGb, bandwidthGbps, diskSpeed, storageType); } @@ -165,6 +172,8 @@ public class NodeResources { } public NodeResources subtract(NodeResources other) { + ensureSpecified(); + other.ensureSpecified(); if ( ! this.isInterchangeableWith(other)) throw new IllegalArgumentException(this + " and " + other + " are not interchangeable"); return new NodeResources(vcpu - other.vcpu, @@ -176,6 +185,7 @@ public class NodeResources { } public NodeResources add(NodeResources other) { + ensureSpecified(); if ( ! this.isInterchangeableWith(other)) throw new IllegalArgumentException(this + " and " + other + " are not interchangeable"); return new NodeResources(vcpu + other.vcpu, @@ -187,6 +197,8 @@ public class NodeResources { } private boolean isInterchangeableWith(NodeResources other) { + ensureSpecified(); + other.ensureSpecified(); if (this.diskSpeed != DiskSpeed.any && other.diskSpeed != DiskSpeed.any && this.diskSpeed != other.diskSpeed) return false; if (this.storageType != StorageType.any && other.storageType != StorageType.any && this.storageType != other.storageType) @@ -248,6 +260,8 @@ public class NodeResources { /** Returns true if all the resources of this are the same or larger than the given resources */ public boolean satisfies(NodeResources other) { + ensureSpecified(); + other.ensureSpecified(); if (this.vcpu < other.vcpu) return false; if (this.memoryGb < other.memoryGb) return false; if (this.diskGb < other.diskGb) return false; @@ -266,6 +280,8 @@ public class NodeResources { /** Returns true if all the resources of this are the same as or compatible with the given resources */ public boolean compatibleWith(NodeResources other) { + ensureSpecified(); + other.ensureSpecified(); if ( ! equal(this.vcpu, other.vcpu)) return false; if ( ! equal(this.memoryGb, other.memoryGb)) return false; if ( ! equal(this.diskGb, other.diskGb)) return false; @@ -276,9 +292,14 @@ public class NodeResources { return true; } - public static NodeResources unspecified() { return zero; } + public static NodeResources unspecified() { return unspecified; } - public boolean isUnspecified() { return this.equals(zero); } + public boolean isUnspecified() { return this == unspecified; } + + private void ensureSpecified() { + if (isUnspecified()) + throw new IllegalStateException("Cannot perform this on unspecified resources"); + } // Returns squared euclidean distance of the relevant numerical values of two node resources public double distanceTo(NodeResources other) { |