diff options
author | Øyvind Grønnesby <oyving@verizonmedia.com> | 2020-09-23 12:42:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-23 12:42:41 +0200 |
commit | 8d6708dcee8a8041917bac842b45645d01dbaa25 (patch) | |
tree | 53777c108e9a512ba568fd7b7b4d9fec86a66d62 /config-model | |
parent | 4f2572fa8b1e50ca7e36b3a0fdf2baac59c6cbec (diff) |
Remove the use of optional in stream expression (#14501)
No longer use optional in validator by providing a identity value
for .reduce(). Also put throw + message in separate method.
Diffstat (limited to 'config-model')
2 files changed, 11 insertions, 8 deletions
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/application/validation/QuotaValidator.java b/config-model/src/main/java/com/yahoo/vespa/model/application/validation/QuotaValidator.java index f6108f5ac77..7cd5e6b9b07 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/application/validation/QuotaValidator.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/application/validation/QuotaValidator.java @@ -6,7 +6,6 @@ import com.yahoo.config.provision.Capacity; import com.yahoo.config.provision.ClusterSpec; import com.yahoo.vespa.model.VespaModel; -import java.util.Optional; import java.util.stream.Collectors; /** @@ -23,16 +22,15 @@ public class QuotaValidator extends Validator { } private void validateBudget(int budget, VespaModel model) { - Optional<Double> spend = model.allClusters().stream() + var spend = model.allClusters().stream() .map(clusterId -> model.provisioned().all().get(clusterId)) .map(Capacity::maxResources) .map(clusterCapacity -> clusterCapacity.nodeResources().cost() * clusterCapacity.nodes()) - .reduce(Double::sum); + .reduce(0.0, Double::sum); - if(spend.isPresent() && spend.get() > budget) - throw new IllegalArgumentException( - String.format("Hourly spend for maximum specified resources ($%.2f) exceeds budget from quota ($%d)!", - spend.get(), budget)); + if (spend > budget) { + throwBudgetExceeded(spend, budget); + } } /** Check that all clusters in the application do not exceed the quota max cluster size. */ @@ -51,4 +49,9 @@ public class QuotaValidator extends Validator { throw new IllegalArgumentException("Clusters " + clusterNames + " exceeded max cluster size of " + maxClusterSize); } } + + private void throwBudgetExceeded(double spend, double budget) { + var message = String.format("Hourly spend for maximum specified resources ($%.2f) exceeds budget from quota ($%.2f)!", spend, budget); + throw new IllegalArgumentException(message); + } } diff --git a/config-model/src/test/java/com/yahoo/vespa/model/application/validation/QuotaValidatorTest.java b/config-model/src/test/java/com/yahoo/vespa/model/application/validation/QuotaValidatorTest.java index 0a1c0dcb8d8..10199bfe6b9 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/application/validation/QuotaValidatorTest.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/application/validation/QuotaValidatorTest.java @@ -42,7 +42,7 @@ public class QuotaValidatorTest { tester.deploy(null, getServices("testCluster", 10), Environment.prod, null); fail(); } catch (RuntimeException e) { - assertEquals("Hourly spend for maximum specified resources ($1.60) exceeds budget from quota ($1)!", e.getMessage()); + assertEquals("Hourly spend for maximum specified resources ($1.60) exceeds budget from quota ($1.00)!", e.getMessage()); } } |