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:26 UTC
[brooklyn-server] 02/02: fix NPEs in HttpToolResponse on error response, and LatencyDetector fails entity if gets a response not in 200-399
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;