From 9325737ff613f7f14e44338b68ab657e0ed52d37 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sun, 5 Jan 2020 15:24:36 +0100 Subject: Don't apply stale zone updates --- .../edu/mimuw/cloudatlas/agent/modules/Stanik.java | 21 +++++++++++++++------ .../pl/edu/mimuw/cloudatlas/model/ValueUtils.java | 4 ++++ 2 files changed, 19 insertions(+), 6 deletions(-) (limited to 'src/main/java/pl/edu/mimuw') 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 3e5b790..1b1824f 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 @@ -34,12 +34,14 @@ public class Stanik extends Module { private ZMI hierarchy; private HashMap> queries; + private long freshnessPeriod; public Stanik() { super(ModuleType.STATE); hierarchy = new ZMI(); queries = new HashMap>(); hierarchy.getAttributes().add("timestamp", new ValueTime(0l)); + freshnessPeriod = 60 * 1000; } public void handleTyped(StanikMessage message) throws InterruptedException, InvalidMessageType { @@ -113,13 +115,20 @@ public class Stanik extends Module { public void handleUpdateAttributes(UpdateAttributesMessage message) { try { validateUpdateAttributesMessage(message); - addMissingZones(new PathName(message.getPathName())); - ZMI zone = hierarchy.findDescendant(message.getPathName()); - AttributesMap attributes = zone.getAttributes(); - if (ValueUtils.valueLower(attributes.get("timestamp"), message.getAttributes().get("timestamp"))) { - AttributesUtil.transferAttributes(message.getAttributes(), attributes); + if (!ValueUtils.valueLower( + message.getAttributes().get("timestamp"), + new ValueTime(System.currentTimeMillis() - freshnessPeriod) + )) { + addMissingZones(new PathName(message.getPathName())); + ZMI zone = hierarchy.findDescendant(message.getPathName()); + AttributesMap attributes = zone.getAttributes(); + 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"); + } } else { - System.out.println("DEBUG: not applying update with older attributes"); + System.out.println("DEBUG: not applying update with stale attributes"); } } catch (InvalidUpdateAttributesMessage e) { System.out.println("ERROR: invalid UpdateAttributesMessage " + e.getMessage()); 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 02b2ce4..3df8231 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueUtils.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueUtils.java @@ -8,4 +8,8 @@ public class ValueUtils { public static boolean valueLower(Value a, Value b) { return ((ValueBoolean) a.isLowerThan(b)).getValue(); } + + public static ValueTime currentTime() { + return new ValueTime(System.currentTimeMillis()); + } } -- cgit v1.2.3 From 89a5d7b00f71a5be805ecc7e7907e5b0c88c5f03 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sun, 5 Jan 2020 19:33:37 +0100 Subject: Fix tests --- .../pl/edu/mimuw/cloudatlas/interpreter/Main.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/main/java/pl/edu/mimuw') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java index 1e0bb4f..7419ad4 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java @@ -50,6 +50,7 @@ import pl.edu.mimuw.cloudatlas.model.ValueList; import pl.edu.mimuw.cloudatlas.model.ValueSet; 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 Main { @@ -274,29 +275,31 @@ public class Main { List list; + ValueTime time = ValueUtils.currentTime(); + ZMI root = new ZMI(); root.getAttributes().add("level", new ValueInt(0l)); root.getAttributes().add("name", new ValueString(null)); - root.getAttributes().add("timestamp", new ValueTime(10l)); + root.getAttributes().add("timestamp", time); ZMI uw = new ZMI(root); root.addSon(uw); uw.getAttributes().add("level", new ValueInt(1l)); uw.getAttributes().add("name", new ValueString("uw")); - uw.getAttributes().add("timestamp", new ValueTime(10l)); + uw.getAttributes().add("timestamp", time); ZMI pjwstk = new ZMI(root); root.addSon(pjwstk); pjwstk.getAttributes().add("level", new ValueInt(1l)); pjwstk.getAttributes().add("name", new ValueString("pjwstk")); - pjwstk.getAttributes().add("timestamp", new ValueTime(10l)); + pjwstk.getAttributes().add("timestamp", time); ZMI violet07 = new ZMI(uw); uw.addSon(violet07); violet07.getAttributes().add("level", new ValueInt(2l)); violet07.getAttributes().add("name", new ValueString("violet07")); violet07.getAttributes().add("owner", new ValueString("/uw/violet07")); - violet07.getAttributes().add("timestamp", new ValueTime("2012/11/09 18:00:00.000")); + violet07.getAttributes().add("timestamp", time); list = Arrays.asList(new Value[] { violet07Contact, khaki31Contact, khaki13Contact }); @@ -322,7 +325,7 @@ public class Main { khaki31.getAttributes().add("level", new ValueInt(2l)); khaki31.getAttributes().add("name", new ValueString("khaki31")); khaki31.getAttributes().add("owner", new ValueString("/uw/khaki31")); - khaki31.getAttributes().add("timestamp", new ValueTime("2012/11/09 20:03:00.000")); + khaki31.getAttributes().add("timestamp", time); list = Arrays.asList(new Value[] { violet08Contact }); @@ -348,7 +351,7 @@ public class Main { khaki13.getAttributes().add("level", new ValueInt(2l)); khaki13.getAttributes().add("name", new ValueString("khaki13")); khaki13.getAttributes().add("owner", new ValueString("/uw/khaki13")); - khaki13.getAttributes().add("timestamp", new ValueTime("2012/11/09 21:03:00.000")); + khaki13.getAttributes().add("timestamp", time); list = Arrays.asList(new Value[] { khaki14Contact, khaki32Contact }); @@ -372,7 +375,7 @@ public class Main { whatever01.getAttributes().add("level", new ValueInt(2l)); whatever01.getAttributes().add("name", new ValueString("whatever01")); whatever01.getAttributes().add("owner", new ValueString("/pjwstk/whatever01")); - whatever01.getAttributes().add("timestamp", new ValueTime("2012/11/09 21:12:00.000")); + whatever01.getAttributes().add("timestamp", time); list = Arrays.asList(new Value[] { whatever02Contact, }); @@ -396,7 +399,7 @@ public class Main { whatever02.getAttributes().add("level", new ValueInt(2l)); whatever02.getAttributes().add("name", new ValueString("whatever02")); whatever02.getAttributes().add("owner", new ValueString("/pjwstk/whatever02")); - whatever02.getAttributes().add("timestamp", new ValueTime("2012/11/09 21:13:00.000")); + whatever02.getAttributes().add("timestamp", time); list = Arrays.asList(new Value[] { whatever01Contact, }); -- cgit v1.2.3 From 88cc1f5da8ded831b15f4970e8877494d449a471 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Mon, 6 Jan 2020 12:02:46 +0100 Subject: Prune hierarchy of stale zones on read --- .../edu/mimuw/cloudatlas/agent/modules/Stanik.java | 44 +++++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'src/main/java/pl/edu/mimuw') 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 1b1824f..6c0f380 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 @@ -1,6 +1,8 @@ package pl.edu.mimuw.cloudatlas.agent.modules; import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map.Entry; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; @@ -19,6 +21,7 @@ import pl.edu.mimuw.cloudatlas.model.Type; import pl.edu.mimuw.cloudatlas.model.TypePrimitive; import pl.edu.mimuw.cloudatlas.model.Value; import pl.edu.mimuw.cloudatlas.model.ValueBoolean; +import pl.edu.mimuw.cloudatlas.model.ValueDuration; import pl.edu.mimuw.cloudatlas.model.ValueQuery; import pl.edu.mimuw.cloudatlas.model.ValueString; import pl.edu.mimuw.cloudatlas.model.ValueTime; @@ -36,12 +39,16 @@ public class Stanik extends Module { private HashMap> queries; private long freshnessPeriod; - public Stanik() { + public Stanik(long freshnessPeriod) { super(ModuleType.STATE); hierarchy = new ZMI(); queries = new HashMap>(); hierarchy.getAttributes().add("timestamp", new ValueTime(0l)); - freshnessPeriod = 60 * 1000; + this.freshnessPeriod = freshnessPeriod; + } + + public Stanik() { + this(60 * 1000); } public void handleTyped(StanikMessage message) throws InterruptedException, InvalidMessageType { @@ -67,6 +74,7 @@ public class Stanik extends Module { } public void handleGetState(GetStateMessage message) throws InterruptedException { + pruneHierarchy(); StateMessage response = new StateMessage( "", message.getRequestingModule(), @@ -78,6 +86,38 @@ public class Stanik extends Module { sendMessage(response); } + private void pruneHierarchy() { + ValueTime now = ValueUtils.currentTime(); + pruneZMI(hierarchy, now); + } + + private boolean pruneZMI(ZMI zmi, ValueTime time) { + Value timestamp = zmi.getAttributes().get("timestamp"); + + List sonsToRemove = new LinkedList(); + if (ValueUtils.valueLower(timestamp, time.subtract(new ValueDuration(freshnessPeriod)))) { + if (zmi.getFather() != null) { + return true; + } + } else { + for (ZMI son : zmi.getSons()) { + if (pruneZMI(son, time)) { + sonsToRemove.add(son); + } + } + } + + for (ZMI son : sonsToRemove) { + zmi.removeSon(son); + } + + if (zmi.getSons().isEmpty()) { + return true; + } + + return false; + } + public void handleRemoveZMI(RemoveZMIMessage message) { try { ZMI zmi = hierarchy.findDescendant(new PathName(message.getPathName())); -- cgit v1.2.3 From 8a44299b0072a8bedd1c0d74f92d258379c8447a Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Mon, 6 Jan 2020 12:57:33 +0100 Subject: Increase freshness threshold in tests --- src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java | 3 ++- src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'src/main/java/pl/edu/mimuw') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java index 0efa710..26f0e0b 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java @@ -46,7 +46,8 @@ public class Agent { HashMap modules = new HashMap(); modules.put(ModuleType.TIMER_SCHEDULER, new TimerScheduler(ModuleType.TIMER_SCHEDULER)); modules.put(ModuleType.RMI, new Remik()); - modules.put(ModuleType.STATE, new Stanik()); + Long freshnessPeriod = new Long(System.getProperty("freshness_period")); + modules.put(ModuleType.STATE, new Stanik(freshnessPeriod)); modules.put(ModuleType.QUERY, new Qurnik()); // TODO add modules as we implement them return modules; 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 6c0f380..6761c94 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 @@ -94,6 +94,8 @@ public class Stanik extends Module { private boolean pruneZMI(ZMI zmi, ValueTime time) { Value timestamp = zmi.getAttributes().get("timestamp"); + boolean isLeaf = zmi.getSons().isEmpty(); + List sonsToRemove = new LinkedList(); if (ValueUtils.valueLower(timestamp, time.subtract(new ValueDuration(freshnessPeriod)))) { if (zmi.getFather() != null) { @@ -111,7 +113,7 @@ public class Stanik extends Module { zmi.removeSon(son); } - if (zmi.getSons().isEmpty()) { + if (!isLeaf && zmi.getSons().isEmpty()) { return true; } -- cgit v1.2.3