diff options
author | Harald Musum <musum@yahooinc.com> | 2022-12-07 11:32:24 +0100 |
---|---|---|
committer | Harald Musum <musum@yahooinc.com> | 2022-12-07 11:32:24 +0100 |
commit | 8218e181c78cb7c36cdc66b068487a59f9bc734e (patch) | |
tree | 07d8aff5ab011bfb178bf5e80eafd612bed60836 /flags | |
parent | 56ed8aa96318742e8309b49741aa81c214fcf01a (diff) |
Support not specifying vcpu, memoryGb and diskGb for ClusterCapacity
Diffstat (limited to 'flags')
-rw-r--r-- | flags/src/main/java/com/yahoo/vespa/flags/custom/ClusterCapacity.java | 43 | ||||
-rw-r--r-- | flags/src/test/java/com/yahoo/vespa/flags/custom/ClusterCapacityTest.java | 26 |
2 files changed, 53 insertions, 16 deletions
diff --git a/flags/src/main/java/com/yahoo/vespa/flags/custom/ClusterCapacity.java b/flags/src/main/java/com/yahoo/vespa/flags/custom/ClusterCapacity.java index 6d98c3a3283..b24d5f62174 100644 --- a/flags/src/main/java/com/yahoo/vespa/flags/custom/ClusterCapacity.java +++ b/flags/src/main/java/com/yahoo/vespa/flags/custom/ClusterCapacity.java @@ -19,37 +19,47 @@ import java.util.OptionalDouble; @JsonInclude(value = JsonInclude.Include.NON_NULL) public class ClusterCapacity { private final int count; - private final double vcpu; - private final double memoryGb; - private final double diskGb; + private final OptionalDouble vcpu; + private final OptionalDouble memoryGb; + private final OptionalDouble diskGb; private final OptionalDouble bandwidthGbps; @JsonCreator public ClusterCapacity(@JsonProperty("count") int count, - @JsonProperty("vcpu") double vcpu, - @JsonProperty("memoryGb") double memoryGb, - @JsonProperty("diskGb") double diskGb, + @JsonProperty("vcpu") Double vcpu, + @JsonProperty("memoryGb") Double memoryGb, + @JsonProperty("diskGb") Double diskGb, @JsonProperty("bandwidthGbps") Double bandwidthGbps) { this.count = (int) requireNonNegative("count", count); - this.vcpu = requireNonNegative("vcpu", vcpu); - this.memoryGb = requireNonNegative("memoryGb", memoryGb); - this.diskGb = requireNonNegative("diskGb", diskGb); + this.vcpu = vcpu == null ? OptionalDouble.empty() : OptionalDouble.of(requireNonNegative("vcpu", vcpu)); + this.memoryGb = memoryGb == null ? OptionalDouble.empty() : OptionalDouble.of(requireNonNegative("memoryGb", memoryGb)); + this.diskGb = diskGb == null ? OptionalDouble.empty() : OptionalDouble.of(requireNonNegative("diskGb", diskGb)); this.bandwidthGbps = bandwidthGbps == null ? OptionalDouble.empty() : OptionalDouble.of(bandwidthGbps); } /** Returns a new ClusterCapacity equal to {@code this}, but with the given count. */ public ClusterCapacity withCount(int count) { - return new ClusterCapacity(count, vcpu, memoryGb, diskGb, bandwidthGbpsOrNull()); + return new ClusterCapacity(count, vcpuOrNull(), memoryGbOrNull(), diskGbOrNull(), bandwidthGbpsOrNull()); } @JsonGetter("count") public int count() { return count; } - @JsonGetter("vcpu") public double vcpu() { return vcpu; } - @JsonGetter("memoryGb") public double memoryGb() { return memoryGb; } - @JsonGetter("diskGb") public double diskGb() { return diskGb; } + @JsonGetter("vcpu") public Double vcpuOrNull() { + return vcpu.isPresent() ? vcpu.getAsDouble() : null; + } + @JsonGetter("memoryGb") public Double memoryGbOrNull() { + return memoryGb.isPresent() ? memoryGb.getAsDouble() : null; + } + @JsonGetter("diskGb") public Double diskGbOrNull() { + return diskGb.isPresent() ? diskGb.getAsDouble() : null; + } @JsonGetter("bandwidthGbps") public Double bandwidthGbpsOrNull() { return bandwidthGbps.isPresent() ? bandwidthGbps.getAsDouble() : null; } + @JsonIgnore public Double vcpu() { return vcpu.orElse(0.0); } + @JsonIgnore public Double memoryGb() { return memoryGb.orElse(0.0); } + @JsonIgnore public Double diskGb() { return diskGb.orElse(0.0); } + @JsonIgnore public double bandwidthGbps() { return bandwidthGbps.orElse(1.0); } @@ -70,9 +80,9 @@ public class ClusterCapacity { if (o == null || getClass() != o.getClass()) return false; ClusterCapacity that = (ClusterCapacity) o; return count == that.count && - Double.compare(that.vcpu, vcpu) == 0 && - Double.compare(that.memoryGb, memoryGb) == 0 && - Double.compare(that.diskGb, diskGb) == 0 && + vcpu.equals(that.vcpu) && + memoryGb.equals(that.memoryGb) && + diskGb.equals(that.diskGb) && bandwidthGbps.equals(that.bandwidthGbps); } @@ -86,4 +96,5 @@ public class ClusterCapacity { throw new IllegalArgumentException("'" + name + "' must be positive, was " + value); return value; } + } diff --git a/flags/src/test/java/com/yahoo/vespa/flags/custom/ClusterCapacityTest.java b/flags/src/test/java/com/yahoo/vespa/flags/custom/ClusterCapacityTest.java index 189e3fd79c9..a0b0ad2aee2 100644 --- a/flags/src/test/java/com/yahoo/vespa/flags/custom/ClusterCapacityTest.java +++ b/flags/src/test/java/com/yahoo/vespa/flags/custom/ClusterCapacityTest.java @@ -9,6 +9,7 @@ import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertEquals; public class ClusterCapacityTest { + @Test void serialization() throws IOException { ClusterCapacity clusterCapacity = new ClusterCapacity(7, 1.2, 3.4, 5.6, null); @@ -38,4 +39,29 @@ public class ClusterCapacityTest { assertEquals(2.3, deserialized.bandwidthGbps(), 0.0001); assertEquals(7, deserialized.count()); } + + @Test + void serializationWithNoNodeResources() throws IOException { + ClusterCapacity clusterCapacity = new ClusterCapacity(7, null, null, null, null); + ObjectMapper mapper = new ObjectMapper(); + String json = mapper.writeValueAsString(clusterCapacity); + assertEquals("{\"count\":7}", json); + + ClusterCapacity deserialized = mapper.readValue(json, ClusterCapacity.class); + assertEquals(0.0, deserialized.vcpu(), 0.0001); + assertEquals(0.0, deserialized.memoryGb(), 0.0001); + assertEquals(0.0, deserialized.diskGb(), 0.0001); + assertEquals(1.0, deserialized.bandwidthGbps(), 0.0001); // 1.0 is used as fallback + assertEquals(7, deserialized.count()); + + // Test that no values for node resources is allowed in json also + String jsonWithNoValuesForNodeResources = "{\"count\":7}"; + deserialized = mapper.readValue(jsonWithNoValuesForNodeResources, ClusterCapacity.class); + assertEquals(0.0, deserialized.vcpu(), 0.0001); + assertEquals(0.0, deserialized.memoryGb(), 0.0001); + assertEquals(0.0, deserialized.diskGb(), 0.0001); + assertEquals(1.0, deserialized.bandwidthGbps(), 0.0001); // 1.0 is used as fallback + assertEquals(7, deserialized.count()); + } + }
\ No newline at end of file |