diff options
author | Martin <marcin.j.chrzanowski@gmail.com> | 2019-12-30 14:27:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-30 14:27:03 +0100 |
commit | 71452a391ecb6620ff3a06b80da5824b88cb4ad0 (patch) | |
tree | 665e88a50176b115bd0ea5ad9706dc64a3b8bdbf | |
parent | 00b7eaac7f87f73d9492bd7f917719fd364b5731 (diff) | |
parent | 4def2fc74f86df8eb15ef8ecd6b0ed37e83f0803 (diff) |
Merge pull request #80 from m-chrzan/stanik-removal
Implement ZMI removals in Stanik
6 files changed, 93 insertions, 11 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/RemoveZMIMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/RemoveZMIMessage.java new file mode 100644 index 0000000..9330185 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/RemoveZMIMessage.java @@ -0,0 +1,22 @@ +package pl.edu.mimuw.cloudatlas.agent.messages; + +import pl.edu.mimuw.cloudatlas.model.ValueTime; + +public class RemoveZMIMessage extends StanikMessage { + private String pathName; + private ValueTime removalTimestamp; + + public RemoveZMIMessage(String messageId, long timestamp, String pathName, ValueTime removalTimestamp) { + super(messageId, timestamp, Type.REMOVE_ZMI); + this.pathName = pathName; + this.removalTimestamp = removalTimestamp; + } + + public String getPathName() { + return pathName; + } + + public ValueTime getRemovalTimestamp() { + return removalTimestamp; + } +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java index b23f6e0..d86de4a 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java @@ -6,6 +6,7 @@ import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType; public abstract class StanikMessage extends AgentMessage { public enum Type { GET_STATE, + REMOVE_ZMI, UPDATE_ATTRIBUTES, UPDATE_QUERIES } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java index 4694219..ea893d9 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java @@ -6,6 +6,7 @@ import java.util.Map.Entry; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.RemoveZMIMessage; import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage; import pl.edu.mimuw.cloudatlas.agent.messages.StanikMessage; import pl.edu.mimuw.cloudatlas.agent.messages.UpdateAttributesMessage; @@ -21,6 +22,7 @@ import pl.edu.mimuw.cloudatlas.model.ValueBoolean; import pl.edu.mimuw.cloudatlas.model.ValueQuery; import pl.edu.mimuw.cloudatlas.model.ValueString; import pl.edu.mimuw.cloudatlas.model.ValueTime; +import pl.edu.mimuw.cloudatlas.model.ValueUtils; import pl.edu.mimuw.cloudatlas.model.ZMI; public class Stanik extends Module { @@ -45,6 +47,9 @@ public class Stanik extends Module { case GET_STATE: handleGetState((GetStateMessage) message); break; + case REMOVE_ZMI: + handleRemoveZMI((RemoveZMIMessage) message); + break; case UPDATE_ATTRIBUTES: handleUpdateAttributes((UpdateAttributesMessage) message); break; @@ -57,17 +62,37 @@ public class Stanik extends Module { } public void handleGetState(GetStateMessage message) throws InterruptedException { - StateMessage response = new StateMessage("", message.getRequestingModule(), 0, message.getRequestId(), hierarchy.clone(), (HashMap<Attribute, Entry<ValueQuery, ValueTime>>) queries.clone()); + StateMessage response = new StateMessage( + "", + message.getRequestingModule(), + 0, + message.getRequestId(), + hierarchy.clone(), + (HashMap<Attribute, Entry<ValueQuery, ValueTime>>) queries.clone() + ); sendMessage(response); } + public void handleRemoveZMI(RemoveZMIMessage message) { + try { + ZMI zmi = hierarchy.findDescendant(new PathName(message.getPathName())); + if (ValueUtils.valueLower(zmi.getAttributes().getOrNull("timestamp"), message.getRemovalTimestamp())) { + zmi.getFather().removeSon(zmi); + } else { + System.out.println("DEBUG: not removing zone with fresher timestamp than removal"); + } + } catch (ZMI.NoSuchZoneException e) { + System.out.println("DEBUG: trying to remove zone that doesn't exist"); + } + } + public void handleUpdateAttributes(UpdateAttributesMessage message) { try { validateUpdateAttributesMessage(message); addMissingZones(new PathName(message.getPathName())); ZMI zone = hierarchy.findDescendant(message.getPathName()); AttributesMap attributes = zone.getAttributes(); - if (valueLower(attributes.get("timestamp"), message.getAttributes().get("timestamp"))) { + if (ValueUtils.valueLower(attributes.get("timestamp"), message.getAttributes().get("timestamp"))) { AttributesUtil.transferAttributes(message.getAttributes(), attributes); } else { System.out.println("DEBUG: not applying update with older attributes"); @@ -84,16 +109,12 @@ public class Stanik extends Module { Attribute attribute = entry.getKey(); ValueTime timestamp = entry.getValue().getValue(); Entry<ValueQuery, ValueTime> currentTimestampedQuery = queries.get(attribute); - if (currentTimestampedQuery == null || valueLower(currentTimestampedQuery.getValue(), timestamp)) { + if (currentTimestampedQuery == null || ValueUtils.valueLower(currentTimestampedQuery.getValue(), timestamp)) { queries.put(entry.getKey(), entry.getValue()); } } } - private boolean valueLower(Value a, Value b) { - return ((ValueBoolean) a.isLowerThan(b)).getValue(); - } - private void validateUpdateAttributesMessage(UpdateAttributesMessage message) throws InvalidUpdateAttributesMessage { validateZoneName(message); validateHasTimeStamp(message); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueUtils.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueUtils.java index 01a45b5..02b2ce4 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueUtils.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueUtils.java @@ -4,4 +4,8 @@ public class ValueUtils { public static boolean valueNonNullOfType(Value value, Type type) { return value != null && !value.isNull() && value.getType().isCompatible(type); } + + public static boolean valueLower(Value a, Value b) { + return ((ValueBoolean) a.isLowerThan(b)).getValue(); + } } diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java index 8958472..495a012 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java @@ -8,6 +8,7 @@ import java.util.Map.Entry; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage; import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.RemoveZMIMessage; import pl.edu.mimuw.cloudatlas.agent.messages.ResponseMessage; import pl.edu.mimuw.cloudatlas.agent.messages.UpdateAttributesMessage; import pl.edu.mimuw.cloudatlas.agent.messages.UpdateQueriesMessage; @@ -198,4 +199,41 @@ public class StanikTest { assertEquals(new ValueTime(43l), timestampedQuery4.getValue()); assertEquals(new ValueQuery("SELECT 1000 AS foo"), timestampedQuery4.getKey()); } + + @Test + public void removeZMI() throws Exception { + AttributesMap attributes = new AttributesMap(); + attributes.add("foo", new ValueInt(1337l)); + attributes.add("bar", new ValueString("baz")); + attributes.add("name", new ValueString("new")); + attributes.add("timestamp", new ValueTime(42l)); + UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/new", attributes); + stanik.handleTyped(message); + + RemoveZMIMessage removeMessage = new RemoveZMIMessage("test_msg2", 0, "/new", new ValueTime(43l)); + stanik.handleTyped(removeMessage); + boolean noSuchZone = false; + try { + stanik.getHierarchy().findDescendant("/new"); + } catch (ZMI.NoSuchZoneException e) { + noSuchZone = true; + } + assertTrue(noSuchZone); + } + + @Test + public void dontRemoveZMIIfTimestampOlder() throws Exception { + AttributesMap attributes = new AttributesMap(); + attributes.add("foo", new ValueInt(1337l)); + attributes.add("bar", new ValueString("baz")); + attributes.add("name", new ValueString("new")); + attributes.add("timestamp", new ValueTime(42l)); + UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/new", attributes); + stanik.handleTyped(message); + + RemoveZMIMessage removeMessage = new RemoveZMIMessage("test_msg2", 0, "/new", new ValueTime(41l)); + stanik.handleTyped(removeMessage); + + stanik.getHierarchy().findDescendant("/new"); + } } diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/model/TestUtil.java b/src/test/java/pl/edu/mimuw/cloudatlas/model/TestUtil.java index ed633d7..ddc61a9 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/model/TestUtil.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/model/TestUtil.java @@ -1,10 +1,6 @@ package pl.edu.mimuw.cloudatlas.model; public class TestUtil { - public static boolean valueLower(Value a, Value b) { - return ((ValueBoolean) a.isLowerThan(b)).getValue(); - } - public static <T> int iterableSize(Iterable<T> iterable) { int count = 0; for (T attribute : iterable) { |