diff options
author | Jon Bratseth <bratseth@verizonmedia.com> | 2020-03-26 11:11:29 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@verizonmedia.com> | 2020-03-26 11:15:28 +0100 |
commit | c70cc1e1e3bbfde0ecbe2b712dc7b65beb228dd3 (patch) | |
tree | d42629e789961172a251911502f97221cbe14450 /config-provisioning | |
parent | 3e5be1fb5216c057c2d631149cb2cf3a20fde674 (diff) |
Validate size
Diffstat (limited to 'config-provisioning')
3 files changed, 59 insertions, 0 deletions
diff --git a/config-provisioning/src/main/java/com/yahoo/config/provision/Capacity.java b/config-provisioning/src/main/java/com/yahoo/config/provision/Capacity.java index 37bea40e932..48b4e9d91bc 100644 --- a/config-provisioning/src/main/java/com/yahoo/config/provision/Capacity.java +++ b/config-provisioning/src/main/java/com/yahoo/config/provision/Capacity.java @@ -21,6 +21,9 @@ public final class Capacity { private final NodeType type; private Capacity(ClusterResources min, ClusterResources max, boolean required, boolean canFail, NodeType type) { + if (max.smallerThan(min)) + throw new IllegalArgumentException("The max capacity must be larger than the min capacity, but got min " + + min + " and max " + max); this.min = min; this.max = max; this.required = required; diff --git a/config-provisioning/src/main/java/com/yahoo/config/provision/ClusterResources.java b/config-provisioning/src/main/java/com/yahoo/config/provision/ClusterResources.java index 49b635aa859..11873bc908c 100644 --- a/config-provisioning/src/main/java/com/yahoo/config/provision/ClusterResources.java +++ b/config-provisioning/src/main/java/com/yahoo/config/provision/ClusterResources.java @@ -36,6 +36,14 @@ public class ClusterResources { public ClusterResources with(NodeResources resources) { return new ClusterResources(nodes, groups, resources); } public ClusterResources withGroups(int groups) { return new ClusterResources(nodes, groups, nodeResources); } + /** Returns true if this is smaller than the given resources in any dimension */ + public boolean smallerThan(ClusterResources other) { + if (this.nodes < other.nodes) return true; + if (this.groups < other.groups) return true; + if ( ! this.nodeResources.justNumbers().satisfies(other.nodeResources.justNumbers())) return true; + return false; + } + @Override public boolean equals(Object o) { if (o == this) return true; diff --git a/config-provisioning/src/test/java/com/yahoo/config/provision/CapacityTest.java b/config-provisioning/src/test/java/com/yahoo/config/provision/CapacityTest.java new file mode 100644 index 00000000000..326ed7317f6 --- /dev/null +++ b/config-provisioning/src/test/java/com/yahoo/config/provision/CapacityTest.java @@ -0,0 +1,48 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.provision; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * @author bratseth + */ +public class CapacityTest { + + @Test + public void testCapacityValidation() { + // Equal min and max is allowed + Capacity.from(new ClusterResources(4, 2, new NodeResources(1,2,3,4)), + new ClusterResources(4, 2, new NodeResources(1,2,3,4)), + false, true); + assertValidationFailure(new ClusterResources(4, 2, new NodeResources(1,2,3,4)), + new ClusterResources(2, 2, new NodeResources(1,2,3,4))); + assertValidationFailure(new ClusterResources(4, 4, new NodeResources(1,2,3,4)), + new ClusterResources(4, 2, new NodeResources(1,2,3,4))); + assertValidationFailure(new ClusterResources(4, 2, new NodeResources(2,2,3,4)), + new ClusterResources(4, 2, new NodeResources(1,2,3,4))); + assertValidationFailure(new ClusterResources(4, 2, new NodeResources(1,3,3,4)), + new ClusterResources(4, 2, new NodeResources(1,2,3,4))); + assertValidationFailure(new ClusterResources(4, 2, new NodeResources(1,2,4,4)), + new ClusterResources(4, 2, new NodeResources(1,2,3,4))); + assertValidationFailure(new ClusterResources(4, 2, new NodeResources(1,2,3,5)), + new ClusterResources(4, 2, new NodeResources(1,2,3,4))); + // It's enough than one dimension is smaller also when the others are larger + assertValidationFailure(new ClusterResources(4, 2, new NodeResources(1,2,3,4)), + new ClusterResources(8, 4, new NodeResources(2,1,6,8))); + } + + private void assertValidationFailure(ClusterResources min, ClusterResources max) { + try { + Capacity.from(min, max, false, true); + fail("Expected exception with min " + min + " and max " + max); + } + catch (IllegalArgumentException e) { + assertEquals("The max capacity must be larger than the min capacity, but got min " + min + " and max " + max, + e.getMessage()); + } + } + +} |