brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [29/50] brooklyn-server git commit: fixes from code review: setting interrupted flag and cleaner hostname lookup/bailout logic
Date Mon, 01 Feb 2016 17:48:59 GMT
fixes from code review: setting interrupted flag and cleaner hostname lookup/bailout logic


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

Branch: refs/heads/0.4.0
Commit: a6b00ab6d6d420352a1963b06b6ad41ea89071aa
Parents: 3b8e6e7
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Wed Oct 10 12:32:48 2012 +0100
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Wed Oct 10 12:32:48 2012 +0100

----------------------------------------------------------------------
 .../location/geo/UtraceHostGeoLookup.java       | 43 +++++++++-----------
 .../src/main/java/brooklyn/util/Exceptions.java | 16 ++++++++
 core/src/main/java/brooklyn/util/Time.java      |  4 +-
 3 files changed, 37 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a6b00ab6/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 221534c..7fb7cf8 100644
--- a/core/src/main/java/brooklyn/location/geo/UtraceHostGeoLookup.java
+++ b/core/src/main/java/brooklyn/location/geo/UtraceHostGeoLookup.java
@@ -7,15 +7,17 @@ import groovy.util.XmlParser;
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLConnection;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import brooklyn.util.Exceptions;
 import brooklyn.util.NetworkUtils;
 import brooklyn.util.ResourceUtils;
+import brooklyn.util.RuntimeInterruptedException;
 
 import com.google.common.base.Throwables;
 
@@ -54,51 +56,46 @@ Beyond this you get blacklisted and requests may time out, or return none.
         return "http://xml.utrace.de/?query="+ip.trim();
     }
 
-    static AtomicBoolean retrievingLocalExternalIp = new AtomicBoolean(false); 
-    volatile static String localExternalIp;
+    private static final AtomicBoolean retrievingLocalExternalIp = new AtomicBoolean(false);
+    private static final CountDownLatch triedLocalExternalIp = new CountDownLatch(1);
+    static volatile String localExternalIp;
+
     /** returns public IP of localhost */
-    public static synchronized String getLocalhostExternalIp() {
+    public static String getLocalhostExternalIp() {
         if (localExternalIp!=null) return localExternalIp;
 
         // do in private thread, otherwise blocks for 30s+ on dodgy network!
         // (we can skip it if someone else is doing it, we have synch lock so we'll get notified)
-        if (!retrievingLocalExternalIp.get())
+        if (retrievingLocalExternalIp.compareAndSet(false, true)) {
             new Thread(new Runnable() {
                 public void run() {
-                    if (retrievingLocalExternalIp.getAndSet(true))
-                        // someone else already trying to retrieve; caller can safely just
wait,
-                        // as they will get notified by the someone else
-                        return;
                     try {
-                        if (localExternalIp!=null)
-                            // someone else succeeded
-                            return;
                         log.debug("Looking up external IP of this host in private thread
"+Thread.currentThread());
                         localExternalIp = new ResourceUtils(HostGeoLookup.class).getResourceAsString("http://api.externalip.net/ip/").trim();
                         log.debug("Finished looking up external IP of this host in private
thread, result "+localExternalIp);
                     } catch (Throwable t) {
                         log.debug("Not able to look up external IP of this host in private
thread, probably offline ("+t+")");
                     } finally {
-                        synchronized (UtraceHostGeoLookup.class) {
-                            UtraceHostGeoLookup.class.notifyAll();        
-                            retrievingLocalExternalIp.set(false);
-                        }
+                        retrievingLocalExternalIp.set(false);
+                        triedLocalExternalIp.countDown();
                     }
                 }
             }).start();
-        
+        }
+
         try {
             // only wait 2s, so startup is fast
-            UtraceHostGeoLookup.class.wait(2000);
+            triedLocalExternalIp.await(2000, TimeUnit.MILLISECONDS);
         } catch (InterruptedException e) {
-            throw Throwables.propagate(e);
+            throw Exceptions.propagate(e);
         }
-        if (localExternalIp==null) throw 
-            Throwables.propagate(new IOException("Unable to discover external IP of local
machine; response to server timed out (thread may be ongoing)"));
-        
+        if (localExternalIp==null)  
+            throw Throwables.propagate(new IOException("Unable to discover external IP of
local machine; response to server timed out (ongoing="+retrievingLocalExternalIp+")"));
+
         log.debug("Looked up external IP of this host, result is: "+localExternalIp);
         return localExternalIp;
     }
+    
     public String getLookupUrlForLocalhost() {
         return getLookupUrlForPublicIp(getLocalhostExternalIp());
     }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a6b00ab6/core/src/main/java/brooklyn/util/Exceptions.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/Exceptions.java b/core/src/main/java/brooklyn/util/Exceptions.java
new file mode 100644
index 0000000..efda008
--- /dev/null
+++ b/core/src/main/java/brooklyn/util/Exceptions.java
@@ -0,0 +1,16 @@
+package brooklyn.util;
+
+import com.google.common.base.Throwables;
+
+public class Exceptions {
+
+    /** like guava {@link Throwables#propagate(Throwable)}, 
+     * but set interrupted if interrupted exception (why doesn't guava do this?!), 
+     * and throw {@link RuntimeInterruptedException} */
+    public static RuntimeException propagate(Throwable throwable) {
+        if (throwable instanceof InterruptedException)
+            throw new RuntimeInterruptedException((InterruptedException)throwable);
+        return Throwables.propagate(throwable);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a6b00ab6/core/src/main/java/brooklyn/util/Time.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/Time.java b/core/src/main/java/brooklyn/util/Time.java
index 0aa32b3..f49eccd 100644
--- a/core/src/main/java/brooklyn/util/Time.java
+++ b/core/src/main/java/brooklyn/util/Time.java
@@ -14,8 +14,6 @@ import java.util.concurrent.TimeUnit;
 
 import brooklyn.util.text.Strings;
 
-import com.google.common.base.Throwables;
-
 public class Time {
 
 	public static String DATE_FORMAT_PREFERRED = "yyyy-MM-dd HH:mm:ss.SSS";
@@ -152,7 +150,7 @@ public class Time {
 		try {
 			Thread.sleep(millis);
 		} catch (InterruptedException e) {
-			throw Throwables.propagate(e);
+			throw Exceptions.propagate(e);
 		}
 	}
 


Mime
View raw message