From c9a84787cd8d3ebb142b11096c5693ca44cfcff2 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Mon, 30 Dec 2019 14:24:22 +0100 Subject: Implement ZMI removals in Stanik --- .../cloudatlas/agent/messages/StanikMessage.java | 1 + .../edu/mimuw/cloudatlas/agent/modules/Stanik.java | 35 ++++++++++++++++---- .../pl/edu/mimuw/cloudatlas/model/ValueUtils.java | 4 +++ .../mimuw/cloudatlas/agent/modules/StanikTest.java | 38 ++++++++++++++++++++++ .../pl/edu/mimuw/cloudatlas/model/TestUtil.java | 4 --- 5 files changed, 71 insertions(+), 11 deletions(-) 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>) queries.clone()); + StateMessage response = new StateMessage( + "", + message.getRequestingModule(), + 0, + message.getRequestId(), + hierarchy.clone(), + (HashMap>) 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 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 int iterableSize(Iterable iterable) { int count = 0; for (T attribute : iterable) { -- cgit v1.2.3 From 4def2fc74f86df8eb15ef8ecd6b0ed37e83f0803 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Mon, 30 Dec 2019 14:27:02 +0100 Subject: Add zmi removal message --- .../agent/messages/RemoveZMIMessage.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/RemoveZMIMessage.java 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; + } +} -- cgit v1.2.3