You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2014/09/10 23:42:44 UTC

[2/3] git commit: utrace now has timeout, defaulting to 3s, and preventing retry within 5m

utrace now has timeout, defaulting to 3s, and preventing retry within 5m


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/4dac3d1b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/4dac3d1b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/4dac3d1b

Branch: refs/heads/master
Commit: 4dac3d1b8321557505c921ebed80434dc9e7deba
Parents: 47178ae
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Authored: Wed Sep 10 23:30:57 2014 +0200
Committer: Alex Heneveld <al...@cloudsoftcorp.com>
Committed: Wed Sep 10 23:30:57 2014 +0200

----------------------------------------------------------------------
 .../location/geo/UtraceHostGeoLookup.java       | 48 +++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4dac3d1b/core/src/main/java/brooklyn/location/geo/UtraceHostGeoLookup.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/location/geo/UtraceHostGeoLookup.java b/core/src/main/java/brooklyn/location/geo/UtraceHostGeoLookup.java
index 5a0edcb..2560b55 100644
--- a/core/src/main/java/brooklyn/location/geo/UtraceHostGeoLookup.java
+++ b/core/src/main/java/brooklyn/location/geo/UtraceHostGeoLookup.java
@@ -25,17 +25,21 @@ import groovy.util.XmlParser;
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.MalformedURLException;
+import java.util.concurrent.atomic.AtomicReference;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import brooklyn.util.exceptions.Exceptions;
 import brooklyn.util.net.Networking;
 import brooklyn.util.time.Duration;
+import brooklyn.util.time.Durations;
 
 import com.google.common.base.Throwables;
 
 public class UtraceHostGeoLookup implements HostGeoLookup {
 
+
     /*
      * 
 http://xml.utrace.de/?query=88.198.156.18 
@@ -63,6 +67,13 @@ Beyond this you get blacklisted and requests may time out, or return none.
 (This may last for several days once blacklisting, not sure how long.)
      */
     
+    /** after failures, subsequent retries within this time interval are blocked */
+    private static final Duration RETRY_INTERVAL = Duration.FIVE_MINUTES;
+    /** requests taking longer than this period are deemed to have timed out and failed;
+     * set reasonably low so that if we are blacklisted for making too many requests,
+     * the call to get geo info does not take very long */
+    private static final Duration REQUEST_TIMEOUT = Duration.seconds(3);
+    
     public static final Logger log = LoggerFactory.getLogger(UtraceHostGeoLookup.class);
     
     public String getLookupUrlForPublicIp(String ip) {
@@ -98,11 +109,46 @@ Beyond this you get blacklisted and requests may time out, or return none.
     private static boolean LOGGED_GEO_LOOKUP_UNAVAILABLE = false;
     private static long LAST_FAILURE_UTC = -1;
     
+    /** does the {@link #retrieveHostGeoInfo(InetAddress)}, but in the background with a default timeout */
     public HostGeoInfo getHostGeoInfo(InetAddress address) throws MalformedURLException, IOException {
-        if (Duration.sinceUtc(LAST_FAILURE_UTC).compareTo(Duration.ONE_MINUTE) < 0) {
+        if (Duration.sinceUtc(LAST_FAILURE_UTC).compareTo(RETRY_INTERVAL) < 0) {
             // wait at least 60s since a failure
             return null;
         }
+        return getHostGeoInfo(address, REQUEST_TIMEOUT);
+    }
+    
+    /** does a {@link #retrieveHostGeoInfo(InetAddress)} with a timeout (returning null, interrupting, and setting failure time) */
+    public HostGeoInfo getHostGeoInfo(final InetAddress address, Duration timeout) throws MalformedURLException, IOException {
+        final AtomicReference<HostGeoInfo> result = new AtomicReference<HostGeoInfo>();
+        Thread lt = new Thread() {
+            public void run() {
+                try {
+                    result.set(retrieveHostGeoInfo(address));
+                } catch (Exception e) {
+                    throw Exceptions.propagate(e);
+                }
+            }
+        };
+        lt.start();
+
+        try {
+            Durations.join(lt, timeout);
+        } catch (InterruptedException e) {
+            throw Exceptions.propagate(e);
+        }
+        
+        if (lt.isAlive()) {
+            // interrupt and set the failure time so that subsequent attempts do not face this timeout
+            lt.interrupt();
+            LAST_FAILURE_UTC = System.currentTimeMillis();
+            log.debug("Geo info lookup for "+address+" timed out after "+timeout);
+        }
+        
+        return result.get();
+    }
+    
+    public HostGeoInfo retrieveHostGeoInfo(InetAddress address) throws MalformedURLException, IOException {
         String url = getLookupUrlFor(address);
         if (log.isDebugEnabled())
             log.debug("Geo info lookup for "+address+" at "+url);