summaryrefslogtreecommitdiffstats
path: root/vdslib
diff options
context:
space:
mode:
authorArnstein Ressem <aressem@gmail.com>2021-03-12 13:58:06 +0100
committerGitHub <noreply@github.com>2021-03-12 13:58:06 +0100
commit0394a2a4430ee34ea7cafe03c98592199eda492f (patch)
tree65acd8c44c09f0e00c03e85751fff733d95bccb9 /vdslib
parent5fc288ac15612f400879f7278d588541fe8257a4 (diff)
Revert "GC unused DiskState"
Diffstat (limited to 'vdslib')
-rw-r--r--vdslib/src/main/java/com/yahoo/vdslib/state/DiskState.java135
-rw-r--r--vdslib/src/test/java/com/yahoo/vdslib/state/DiskStateTestCase.java116
2 files changed, 251 insertions, 0 deletions
diff --git a/vdslib/src/main/java/com/yahoo/vdslib/state/DiskState.java b/vdslib/src/main/java/com/yahoo/vdslib/state/DiskState.java
new file mode 100644
index 00000000000..1a05f8ab565
--- /dev/null
+++ b/vdslib/src/main/java/com/yahoo/vdslib/state/DiskState.java
@@ -0,0 +1,135 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.vdslib.state;
+
+import com.yahoo.text.StringUtilities;
+
+import java.util.StringTokenizer;
+import java.text.ParseException;
+
+/**
+ *
+ */
+public class DiskState implements Cloneable {
+ private State state = State.UP;
+ private String description = "";
+ private double capacity = 1.0;
+
+ public DiskState() {}
+ public DiskState(State s) {
+ setState(s);
+ }
+ public DiskState(State s, String description, double capacity) {
+ setState(s); // Set via set methods, so we can have illegal argument checks only one place
+ setCapacity(capacity);
+ setDescription(description);
+ }
+ public DiskState clone() {
+ try{
+ return (DiskState) super.clone();
+ } catch (CloneNotSupportedException e) {
+ assert(false); // Should not happen
+ return null;
+ }
+ }
+
+ public DiskState(String serialized) throws ParseException {
+ StringTokenizer st = new StringTokenizer(serialized, " \t\f\r\n");
+ while (st.hasMoreTokens()) {
+ String token = st.nextToken();
+ int index = token.indexOf(':');
+ if (index < 0) {
+ throw new ParseException("Token " + token + " does not contain ':': " + serialized, 0);
+ }
+ String key = token.substring(0, index);
+ String value = token.substring(index + 1);
+ if (key.length() > 0) switch (key.charAt(0)) {
+ case 's':
+ if (key.length() > 1) break;
+ setState(State.get(value));
+ continue;
+ case 'c':
+ if (key.length() > 1) break;
+ try{
+ setCapacity(Double.valueOf(value));
+ } catch (Exception e) {
+ throw new ParseException("Illegal disk capacity '" + value + "'. Capacity must be a positive floating point number", 0);
+ }
+ continue;
+ case 'm':
+ if (key.length() > 1) break;
+ description = StringUtilities.unescape(value);
+ continue;
+ default:
+ break;
+ }
+ // Ignore unknown tokens
+ }
+ }
+
+ public String serialize(String prefix, boolean includeDescription) {
+ boolean empty = true;
+ StringBuilder sb = new StringBuilder();
+ if (!state.equals(State.UP) || prefix.length() < 2) {
+ sb.append(prefix).append("s:").append(state.serialize());
+ empty = false;
+ }
+ if (Math.abs(capacity - 1.0) > 0.000000001) {
+ if (empty) { empty = false; } else { sb.append(' '); }
+ sb.append(prefix).append("c:").append(capacity);
+ }
+ if (includeDescription && description.length() > 0) {
+ if (!empty) { sb.append(' '); }
+ sb.append(prefix).append("m:").append(StringUtilities.escape(description, ' '));
+ }
+ return sb.toString();
+ }
+
+ public State getState() { return state; }
+ public double getCapacity() { return capacity; }
+ public String getDescription() { return description; }
+
+ public void setState(State s) {
+ if (!s.validDiskState()) {
+ throw new IllegalArgumentException("State " + s + " is not a valid disk state.");
+ }
+ state = s;
+ }
+ public void setCapacity(double capacity) {
+ if (capacity < 0) {
+ throw new IllegalArgumentException("Negative capacity makes no sense.");
+ }
+ this.capacity = capacity;
+ }
+ public void setDescription(String desc) { description = desc; }
+
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("DiskState(").append(state.serialize());
+ if (Math.abs(capacity - 1.0) > 0.00000001) {
+ sb.append(", capacity ").append(capacity);
+ }
+ if (description.length() > 0) {
+ sb.append(": ").append(description);
+ }
+ sb.append(")");
+ return sb.toString();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (!(o instanceof DiskState)) { return false; }
+ DiskState other = (DiskState) o;
+ if (state.equals(other.state)
+ && Math.abs(capacity - other.capacity) < 0.00000001)
+ {
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ // NOTE: capacity cannot be part of the hashCode
+ return state.hashCode();
+ }
+}
diff --git a/vdslib/src/test/java/com/yahoo/vdslib/state/DiskStateTestCase.java b/vdslib/src/test/java/com/yahoo/vdslib/state/DiskStateTestCase.java
new file mode 100644
index 00000000000..bdc3be3dc70
--- /dev/null
+++ b/vdslib/src/test/java/com/yahoo/vdslib/state/DiskStateTestCase.java
@@ -0,0 +1,116 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.vdslib.state;
+
+import org.junit.Test;
+
+import java.text.ParseException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class DiskStateTestCase {
+
+ private static final double delta = 0.0000000001;
+
+ @Test
+ public void testEquals() {
+ DiskState d1 = new DiskState(State.UP, "", 1);
+ DiskState d2 = new DiskState(State.UP, "", 2);
+ DiskState d3 = new DiskState(State.DOWN, "Failed disk", 1);
+ DiskState d4 = new DiskState(State.DOWN, "IO error", 1);
+ DiskState d5 = new DiskState(State.UP, "", 1);
+ DiskState d6 = new DiskState(State.UP, "", 2);
+ DiskState d7 = new DiskState(State.DOWN, "Failed disk", 1);
+ DiskState d8 = new DiskState(State.DOWN, "IO error", 1);
+
+ assertEquals(d1, d5);
+ assertEquals(d2, d6);
+ assertEquals(d3, d7);
+ assertEquals(d4, d8);
+
+ assertFalse(d1.equals(d2));
+ assertFalse(d1.equals(d3));
+ assertFalse(d1.equals(d4));
+
+ assertFalse(d2.equals(d1));
+ assertFalse(d2.equals(d3));
+ assertFalse(d2.equals(d4));
+
+ assertFalse(d3.equals(d1));
+ assertFalse(d3.equals(d2));
+ assertEquals(d3, d4);
+
+ assertFalse(d4.equals(d1));
+ assertFalse(d4.equals(d2));
+ assertEquals(d4, d3);
+
+ assertFalse(d1.equals("class not instance of Node"));
+ }
+
+ @Test
+ public void testSerialization() throws ParseException {
+ DiskState d = new DiskState();
+ DiskState other = new DiskState(d.serialize("", true));
+ assertEquals(d, other);
+ assertEquals(d.toString(), other.toString());
+ assertEquals(State.UP, other.getState());
+ assertEquals(1.0, other.getCapacity(), delta);
+ assertEquals("", other.getDescription());
+ assertEquals("s:u", d.serialize("", false));
+ assertEquals("s:u", d.serialize("", true));
+ assertEquals("", d.serialize(".0.", false));
+ assertEquals("", d.serialize(".0.", true));
+
+ assertEquals(d, new DiskState(": s:u sbadkey:somevalue cbadkey:somevalue mbadkey:somevalue unknwonkey:somevalue"));
+
+ d = new DiskState(State.UP, "Slow disk", 1.0);
+ other = new DiskState(d.serialize("", true));
+ assertEquals(d, other);
+ assertEquals(d.toString(), other.toString());
+ assertEquals(State.UP, other.getState());
+ assertEquals(1.0, other.getCapacity(), delta);
+ assertEquals("Slow disk", other.getDescription());
+ assertEquals("s:u", d.serialize("", false));
+ assertEquals("s:u m:Slow\\x20disk", d.serialize("", true));
+ assertEquals("", d.serialize(".0.", false));
+ assertEquals(".0.m:Slow\\x20disk", d.serialize(".0.", true));
+
+ d = new DiskState(State.DOWN, "Failed disk", 2.0);
+ other = new DiskState(d.serialize("", true));
+ assertEquals(d, other);
+ assertEquals(d.toString(), other.toString());
+ assertEquals(State.DOWN, other.getState());
+ assertEquals(2.0, other.getCapacity(), delta);
+ assertEquals("Failed disk", other.getDescription());
+ assertEquals("s:d c:2.0", d.serialize("", false));
+ assertEquals("s:d c:2.0 m:Failed\\x20disk", d.serialize("", true));
+ assertEquals(".0.s:d .0.c:2.0", d.serialize(".0.", false));
+ assertEquals(".0.s:d .0.c:2.0 .0.m:Failed\\x20disk", d.serialize(".0.", true));
+
+ try {
+ new DiskState(State.MAINTENANCE);
+ assertTrue("Method expected to throw IllegalArgumentException", false);
+ } catch (IllegalArgumentException e) {
+ assertEquals("State " + State.MAINTENANCE + " is not a valid disk state.", e.getMessage());
+ }
+ try {
+ new DiskState(State.UP, "", -1);
+ assertTrue("Method expected to throw IllegalArgumentException", false);
+ } catch (IllegalArgumentException e) {
+ assertEquals("Negative capacity makes no sense.", e.getMessage());
+ }
+ try {
+ new DiskState("nocolon");
+ assertTrue("Method expected to throw ParseException", false);
+ } catch (ParseException e) {
+ assertEquals("Token nocolon does not contain ':': nocolon", e.getMessage());
+ }
+ try {
+ new DiskState("s:d c:badvalue");
+ assertTrue("Method expected to throw ParseException", false);
+ } catch (ParseException e) {
+ assertEquals("Illegal disk capacity 'badvalue'. Capacity must be a positive floating point number", e.getMessage());
+ }
+ }
+}