diff options
author | Jon Marius Venstad <jonmv@users.noreply.github.com> | 2023-12-18 15:04:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-18 15:04:39 +0100 |
commit | c594e1ce3a1bb59fbe97f81d17a8c67361d7994c (patch) | |
tree | e1c64368225d6bd6142f2d799a8a1d604fab9fec /config-model/src/main/java | |
parent | 6adad88bd950d1af73a676752639d8c9cd4a74c1 (diff) | |
parent | 45fc6575935c2188131d6b8e310570cb0c56bbde (diff) |
Merge pull request #29694 from vespa-engine/jonmv/more-careful-resource-parsing
More careful resource parsing
Diffstat (limited to 'config-model/src/main/java')
-rw-r--r-- | config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/NodesSpecification.java | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/NodesSpecification.java b/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/NodesSpecification.java index baf752cb4be..93d65426b61 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/NodesSpecification.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/NodesSpecification.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.Function; +import java.util.function.ToDoubleFunction; import java.util.logging.Level; /** @@ -313,10 +314,10 @@ public class NodesSpecification { } private static Pair<NodeResources, NodeResources> nodeResourcesFromResourcesElement(ModelElement element) { - Pair<Double, Double> vcpu = toRange(element.stringAttribute("vcpu"), .0, Double::parseDouble); - Pair<Double, Double> memory = toRange(element.stringAttribute("memory"), .0, s -> parseGbAmount(s, "B")); - Pair<Double, Double> disk = toRange(element.stringAttribute("disk"), .0, s -> parseGbAmount(s, "B")); - Pair<Double, Double> bandwith = toRange(element.stringAttribute("bandwidth"), .3, s -> parseGbAmount(s, "BPS")); + Pair<Double, Double> vcpu = toRange("vcpu", element, .0, Double::parseDouble); + Pair<Double, Double> memory = toRange("memory", element, .0, s -> parseGbAmount(s, "B")); + Pair<Double, Double> disk = toRange("disk", element, .0, s -> parseGbAmount(s, "B")); + Pair<Double, Double> bandwith = toRange("bandwidth", element, .3, s -> parseGbAmount(s, "BPS")); NodeResources.DiskSpeed diskSpeed = parseOptionalDiskSpeed(element.stringAttribute("disk-speed")); NodeResources.StorageType storageType = parseOptionalStorageType(element.stringAttribute("storage-type")); NodeResources.Architecture architecture = parseOptionalArchitecture(element.stringAttribute("architecture")); @@ -342,7 +343,7 @@ public class NodesSpecification { if (byteAmount.endsWith(unit)) byteAmount = byteAmount.substring(0, byteAmount.length() - unit.length()); - double multiplier = Math.pow(1000, -3); + double multiplier = -1; if (byteAmount.endsWith("K")) multiplier = Math.pow(1000, -2); else if (byteAmount.endsWith("M")) @@ -360,7 +361,11 @@ public class NodesSpecification { else if (byteAmount.endsWith("Y")) multiplier = Math.pow(1000, 5); - byteAmount = byteAmount.substring(0, byteAmount.length() -1 ).strip(); + if (multiplier == -1) + multiplier = Math.pow(1000, -3); + else + byteAmount = byteAmount.substring(0, byteAmount.length() -1).strip(); + try { return Double.parseDouble(byteAmount) * multiplier; } @@ -477,20 +482,27 @@ public class NodesSpecification { } /** Parses a value ("value") or value range ("[min-value, max-value]") */ - private static <T> Pair<T, T> toRange(String s, T defaultValue, Function<String, T> valueParser) { + private static Pair<Double, Double> toRange(String name, ModelElement element, double defaultValue, ToDoubleFunction<String> valueParser) { + String s = element.stringAttribute(name); try { + Pair<Double, Double> pair; if (s == null) return new Pair<>(defaultValue, defaultValue); s = s.trim(); if (s.startsWith("[") && s.endsWith("]")) { String[] numbers = s.substring(1, s.length() - 1).split(","); if (numbers.length != 2) throw new IllegalArgumentException(); - return new Pair<>(valueParser.apply(numbers[0].trim()), valueParser.apply(numbers[1].trim())); + pair = new Pair<>(valueParser.applyAsDouble(numbers[0].trim()), valueParser.applyAsDouble(numbers[1].trim())); + if (pair.getFirst() > pair.getSecond()) + throw new IllegalArgumentException("first value cannot be larger than second value"); } else { - return new Pair<>(valueParser.apply(s), valueParser.apply(s)); + pair = new Pair<>(valueParser.applyAsDouble(s), valueParser.applyAsDouble(s)); } + if (pair.getFirst() < 0 || pair.getSecond() < 0) + throw new IllegalArgumentException("values cannot be negative"); + return pair; } catch (IllegalArgumentException e) { - throw new IllegalArgumentException("Expected a number or range on the form [min, max], but got '" + s + "'", e); + throw new IllegalArgumentException("Expected a number or range on the form [min, max] for node resource '" + name + "', but got '" + s + "'", e); } } |