Fix httpLatencyDetector rebind with urlSensor - if entityâs urlSensor already set then read value immediately
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/517f1ce3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/517f1ce3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/517f1ce3 Branch: refs/heads/master Commit: 517f1ce35e6b35cc16b81d89a704d774f596ac37 Parents: 6c880a4 Author: Aled Sage <aled.s...@gmail.com> Authored: Thu May 29 18:18:51 2014 +0100 Committer: Aled Sage <aled.s...@gmail.com> Committed: Fri May 30 10:24:40 2014 +0100 ---------------------------------------------------------------------- .../brooklyn/enricher/HttpLatencyDetector.java | 10 ++++++ .../enricher/HttpLatencyDetectorTest.java | 35 ++++++++++++++------ 2 files changed, 35 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/517f1ce3/policy/src/main/java/brooklyn/enricher/HttpLatencyDetector.java ---------------------------------------------------------------------- diff --git a/policy/src/main/java/brooklyn/enricher/HttpLatencyDetector.java b/policy/src/main/java/brooklyn/enricher/HttpLatencyDetector.java index 2614d38..0c61169 100644 --- a/policy/src/main/java/brooklyn/enricher/HttpLatencyDetector.java +++ b/policy/src/main/java/brooklyn/enricher/HttpLatencyDetector.java @@ -156,6 +156,16 @@ public class HttpLatencyDetector extends AbstractEnricher { } } }); + + // TODO would be good if subscription gave us the current value, rather than risking a race with code like this. + String currentVal = entity.getAttribute(urlSensor); + if (currentVal != null) { + Function<String, String> postProcessor = getConfig(URL_POST_PROCESSING); + String newVal = (postProcessor != null) ? postProcessor.apply(currentVal) : currentVal; + if (AtomicReferences.setIfDifferent(url, newVal)) { + log.debug("{} updated url on initial connectionon, to {}", this, newVal); + } + } } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/517f1ce3/policy/src/test/java/brooklyn/enricher/HttpLatencyDetectorTest.java ---------------------------------------------------------------------- diff --git a/policy/src/test/java/brooklyn/enricher/HttpLatencyDetectorTest.java b/policy/src/test/java/brooklyn/enricher/HttpLatencyDetectorTest.java index 25e7ed6..23631da 100644 --- a/policy/src/test/java/brooklyn/enricher/HttpLatencyDetectorTest.java +++ b/policy/src/test/java/brooklyn/enricher/HttpLatencyDetectorTest.java @@ -9,6 +9,7 @@ import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import brooklyn.entity.Entity; import brooklyn.entity.basic.ApplicationBuilder; import brooklyn.entity.basic.Entities; import brooklyn.entity.proxying.EntitySpec; @@ -66,11 +67,7 @@ public class HttpLatencyDetectorTest { .period(100, TimeUnit.MILLISECONDS) .build()); - EntityTestUtils.assertAttributeEventuallyNonNull(entity, HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_MOST_RECENT); - EntityTestUtils.assertAttributeEventuallyNonNull(entity, HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_IN_WINDOW); - - log.info("Latency to "+entity.getAttribute(TEST_URL)+" is "+entity.getAttribute(HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_MOST_RECENT)); - log.info("Mean latency to "+entity.getAttribute(TEST_URL)+" is "+entity.getAttribute(HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_IN_WINDOW)); + assertLatencyAttributesNonNull(entity); } @Test(groups="Integration") @@ -89,13 +86,23 @@ public class HttpLatencyDetectorTest { // gets value after url is set, and gets rolling average entity.setAttribute(TEST_URL, baseUrl.toString()); - EntityTestUtils.assertAttributeEventuallyNonNull(entity, HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_MOST_RECENT); - EntityTestUtils.assertAttributeEventuallyNonNull(entity, HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_IN_WINDOW); + assertLatencyAttributesNonNull(entity); + } - log.info("Latency to "+entity.getAttribute(TEST_URL)+" is "+entity.getAttribute(HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_MOST_RECENT)); - log.info("Mean latency to "+entity.getAttribute(TEST_URL)+" is "+entity.getAttribute(HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_IN_WINDOW)); + @Test(groups="Integration") + public void testGetsSensorIfAlredySetThenPolls() throws Exception { + entity.setAttribute(TEST_URL, baseUrl.toString()); + + entity.addEnricher(HttpLatencyDetector.builder() + .url(TEST_URL) + .noServiceUp() + .rollup(500, TimeUnit.MILLISECONDS) + .period(100, TimeUnit.MILLISECONDS) + .build()); + + assertLatencyAttributesNonNull(entity); } - + @Test(groups="Integration") public void testWaitsForServiceUp() throws Exception { entity.setAttribute(TestEntity.SERVICE_UP, false); @@ -112,6 +119,14 @@ public class HttpLatencyDetectorTest { // gets value after url is set, and gets rolling average entity.setAttribute(TestEntity.SERVICE_UP, true); + assertLatencyAttributesNonNull(entity); + } + + protected void assertLatencyAttributesNonNull(Entity entity) { EntityTestUtils.assertAttributeEventuallyNonNull(entity, HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_MOST_RECENT); + EntityTestUtils.assertAttributeEventuallyNonNull(entity, HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_IN_WINDOW); + + log.info("Latency to "+entity.getAttribute(TEST_URL)+" is "+entity.getAttribute(HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_MOST_RECENT)); + log.info("Mean latency to "+entity.getAttribute(TEST_URL)+" is "+entity.getAttribute(HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_IN_WINDOW)); } }