You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2022/07/15 12:44:24 UTC

[brooklyn-server] branch master updated (ac8a5b28c6 -> 2b1bf4a488)

This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git


    from ac8a5b28c6 allow groups change policy to invoke things when a member joins
     new 6668764a44 fix latency computation
     new 2b1bf4a488 fix NPEs in HttpToolResponse on error response, and LatencyDetector fails entity if gets a response not in 200-399

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/brooklyn/feed/http/HttpFeed.java    |  6 ++--
 .../policy/enricher/HttpLatencyDetector.java       | 32 ++++++++++++++++++++--
 .../brooklyn/util/http/HttpToolResponse.java       |  4 +--
 3 files changed, 34 insertions(+), 8 deletions(-)


[brooklyn-server] 01/02: fix latency computation

Posted by he...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit 6668764a445131a826f82a70e12fce2900683631
Author: Alex Heneveld <al...@cloudsoft.io>
AuthorDate: Fri Jul 15 13:03:10 2022 +0100

    fix latency computation
---
 core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java b/core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java
index bd62ef602f..c1897173a2 100644
--- a/core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java
+++ b/core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java
@@ -416,6 +416,7 @@ public class HttpFeed extends AbstractFeed {
                                 pollInfo.credentials.get().getPassword());
                     }
 
+                    final long startTime = System.currentTimeMillis();
                     HttpResponse response =  pollInfo.httpExecutor.execute(new HttpRequest.Builder()
                             .headers(pollInfo.headers)
                             .uri(pollInfo.uriProvider.get())
@@ -428,7 +429,7 @@ public class HttpFeed extends AbstractFeed {
                                     .laxRedirect(true)
                                     .build())
                             .build());
-                    return createHttpToolRespose(response);
+                    return createHttpToolRespose(response, startTime);
                 }};
                 getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler<HttpToolResponse>(handlers), minPeriod);
         }
@@ -441,7 +442,7 @@ public class HttpFeed extends AbstractFeed {
     }
 
     @SuppressWarnings("unchecked")
-    private HttpToolResponse createHttpToolRespose(HttpResponse response) throws IOException {
+    private HttpToolResponse createHttpToolRespose(HttpResponse response, long startTime) throws IOException {
         int responseCode = response.code();
 
         Map<String,? extends List<String>> headers = (Map<String, List<String>>) (Map<?, ?>) response.headers().asMap();
@@ -449,7 +450,6 @@ public class HttpFeed extends AbstractFeed {
         byte[] content = null;
         final long durationMillisOfFirstResponse;
         final long durationMillisOfFullContent;
-        final long startTime = System.currentTimeMillis();
 
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         durationMillisOfFirstResponse = Duration.sinceUtc(startTime).toMilliseconds();


[brooklyn-server] 02/02: fix NPEs in HttpToolResponse on error response, and LatencyDetector fails entity if gets a response not in 200-399

Posted by he...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit 2b1bf4a488974f9b52ef9d2c3ef437c3f0727f62
Author: Alex Heneveld <al...@cloudsoft.io>
AuthorDate: Fri Jul 15 13:33:21 2022 +0100

    fix NPEs in HttpToolResponse on error response, and LatencyDetector fails entity if gets a response not in 200-399
---
 .../policy/enricher/HttpLatencyDetector.java       | 32 ++++++++++++++++++++--
 .../brooklyn/util/http/HttpToolResponse.java       |  4 +--
 2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/policy/src/main/java/org/apache/brooklyn/policy/enricher/HttpLatencyDetector.java b/policy/src/main/java/org/apache/brooklyn/policy/enricher/HttpLatencyDetector.java
index 8d695d2b06..95d29b628e 100644
--- a/policy/src/main/java/org/apache/brooklyn/policy/enricher/HttpLatencyDetector.java
+++ b/policy/src/main/java/org/apache/brooklyn/policy/enricher/HttpLatencyDetector.java
@@ -38,6 +38,7 @@ import org.apache.brooklyn.api.sensor.SensorEventListener;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.enricher.AbstractEnricher;
+import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
 import org.apache.brooklyn.core.entity.trait.Startable;
 import org.apache.brooklyn.core.sensor.Sensors;
 import org.apache.brooklyn.feed.http.HttpFeed;
@@ -46,12 +47,14 @@ import org.apache.brooklyn.feed.http.HttpValueFunctions;
 import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.core.flags.SetFromFlag;
 import org.apache.brooklyn.util.guava.Functionals;
+import org.apache.brooklyn.util.http.HttpToolResponse;
 import org.apache.brooklyn.util.javalang.AtomicReferences;
 import org.apache.brooklyn.util.javalang.Boxing;
 import org.apache.brooklyn.util.javalang.JavaClassNames;
 import org.apache.brooklyn.util.math.MathFunctions;
 import org.apache.brooklyn.util.net.Urls;
 import org.apache.brooklyn.util.text.StringFunctions;
+import org.apache.brooklyn.util.text.Strings;
 import org.apache.brooklyn.util.time.Duration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -60,6 +63,8 @@ import com.google.common.base.Function;
 import com.google.common.base.Suppliers;
 import com.google.common.reflect.TypeToken;
 
+import javax.annotation.Nullable;
+
 /**
  * An Enricher which computes latency in accessing a URL. 
  * See comments on the methods in the static {@link #builder()} this exposes.
@@ -143,7 +148,7 @@ public class HttpLatencyDetector extends AbstractEnricher implements Enricher {
                 .period(getConfig(PERIOD))
                 .baseUri(Suppliers.compose(Urls.stringToUriFunction(), AtomicReferences.supplier(url)))
                 .poll(new HttpPollConfig<Double>(REQUEST_LATENCY_IN_SECONDS_MOST_RECENT)
-                        .onResult(Functionals.chain(HttpValueFunctions.latency(), MathFunctions.divide(1000.0d)))
+                        .onResult(new ComputeLatencyAndRecordError())
                         .setOnException(null))
                 .suspended()
                 .build();
@@ -153,6 +158,27 @@ public class HttpLatencyDetector extends AbstractEnricher implements Enricher {
                 (getConfig(URL)!=null ? getConfig(URL) : getConfig(URL_SENSOR));
     }
 
+    class ComputeLatencyAndRecordError implements Function<HttpToolResponse, Double> {
+        @Override
+        public @Nullable Double apply(@Nullable HttpToolResponse input) {
+            entity.sensors().set(Sensors.newSensor(Integer.class, "web.request.latencyDetector.lastCode"), input.getResponseCode());
+            if (input.getResponseCode() >= 200 && input.getResponseCode()<=399) {
+                entity.sensors().set(Sensors.newSensor(String.class, "web.request.latencyDetector.lastCodeError"), null);
+                ServiceStateLogic.ServiceProblemsLogic.clearProblemsIndicator(entity, "web.request.latencyDetector");
+            } else {
+                String msg = Strings.firstNonBlank(input.getReasonPhrase(), "Error, response code " + input.getResponseCode());
+                entity.sensors().set(Sensors.newSensor(String.class, "web.request.latencyDetector.lastCodeError"), msg);
+                ServiceStateLogic.ServiceProblemsLogic.updateProblemsIndicator(entity, "web.request.latencyDetector", msg);
+            }
+            return Functionals.chain(HttpValueFunctions.latency(), MathFunctions.divide(1000.0d)).apply(input);
+        }
+
+        @Override
+        public boolean equals(@Nullable Object object) {
+            return false;
+        }
+    }
+
     protected void startSubscriptions(EntityLocal entity) {
         if (getConfig(REQUIRE_SERVICE_UP)) {
             subscriptions().subscribe(entity, Startable.SERVICE_UP, new SensorEventListener<Boolean>() {
@@ -308,7 +334,7 @@ public class HttpLatencyDetector extends AbstractEnricher implements Enricher {
 
         /**
          * Returns the detector. note that callers should then add this to the entity,
-         * typically using {@link Entity#addEnricher(Enricher)}.
+         * typically using {@link Entity#enrichers()} add.
          * 
          * @deprecated since 0.12.0; instead use {@link #buildSpec()} or directly use {@link EnricherSpec}
          */
@@ -326,7 +352,7 @@ public class HttpLatencyDetector extends AbstractEnricher implements Enricher {
         
         /**
          * Returns the detector. note that callers should then add this to the entity,
-         * typically using {@link Entity#addEnricher(EnricherSpec)}
+         * typically using {@link Entity#enrichers()} add
          * 
          * @see {@link EnricherSpec}
          */
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/http/HttpToolResponse.java b/utils/common/src/main/java/org/apache/brooklyn/util/http/HttpToolResponse.java
index 75b92167cd..de694f32f3 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/http/HttpToolResponse.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/http/HttpToolResponse.java
@@ -102,7 +102,7 @@ public class HttpToolResponse {
     
     public int getResponseCode() {
         synchronized (mutex) {
-            if (responseCode == 0) {
+            if (responseCode == 0 && response!=null) {
                 responseCode = response.getStatusLine().getStatusCode();
             }
         }
@@ -112,7 +112,7 @@ public class HttpToolResponse {
     public String getReasonPhrase() {
         synchronized (mutex) {
             if (reasonPhrase == null) {
-                reasonPhrase = response.getStatusLine().getReasonPhrase();
+                if (response!=null && response.getStatusLine()!=null) reasonPhrase = response.getStatusLine().getReasonPhrase();
             }
         }
         return reasonPhrase;