cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dbros...@apache.org
Subject [1/4] git commit: make sure streams get closed
Date Sun, 04 May 2014 04:22:16 GMT
Repository: cassandra
Updated Branches:
  refs/heads/trunk 425d31849 -> e5f08867e


make sure streams get closed


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

Branch: refs/heads/trunk
Commit: c454807a75a55bf7a12dbe7c641266bcaf33d6e1
Parents: 2f32783
Author: Dave Brosius <dbrosius@mebigfatguy.com>
Authored: Sun May 4 00:19:51 2014 -0400
Committer: Dave Brosius <dbrosius@mebigfatguy.com>
Committed: Sun May 4 00:19:51 2014 -0400

----------------------------------------------------------------------
 .../cassandra/locator/CloudstackSnitch.java     | 78 +++++++++++++-------
 1 file changed, 51 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/c454807a/src/java/org/apache/cassandra/locator/CloudstackSnitch.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/locator/CloudstackSnitch.java b/src/java/org/apache/cassandra/locator/CloudstackSnitch.java
index af26ef9..57c973b 100644
--- a/src/java/org/apache/cassandra/locator/CloudstackSnitch.java
+++ b/src/java/org/apache/cassandra/locator/CloudstackSnitch.java
@@ -57,7 +57,8 @@ public class CloudstackSnitch extends AbstractNetworkTopologySnitch
 
     private static final String DEFAULT_DC = "UNKNOWN-DC";
     private static final String DEFAULT_RACK = "UNKNOWN-RACK";
-    private static final String[] LEASE_FILES = {
+    private static final String[] LEASE_FILES = 
+    {
         "file:///var/lib/dhcp/dhclient.eth0.leases",
         "file:///var/lib/dhclient/dhclient.eth0.leases"
     };
@@ -71,7 +72,8 @@ public class CloudstackSnitch extends AbstractNetworkTopologySnitch
         String zone = csQueryMetadata(endpoint + ZONE_NAME_QUERY_URI);
         String zone_parts[] = zone.split("-");
 
-        if (zone_parts.length != 3) {
+        if (zone_parts.length != 3) 
+        {
             throw new ConfigurationException("CloudstackSnitch cannot handle invalid zone
format: " + zone);
         }
         csZoneDc = zone_parts[0] + "-" + zone_parts[1];
@@ -83,7 +85,8 @@ public class CloudstackSnitch extends AbstractNetworkTopologySnitch
         if (endpoint.equals(FBUtilities.getBroadcastAddress()))
             return csZoneRack;
         EndpointState state = Gossiper.instance.getEndpointStateForEndpoint(endpoint);
-        if (state == null || state.getApplicationState(ApplicationState.RACK) == null) {
+        if (state == null || state.getApplicationState(ApplicationState.RACK) == null) 
+        {
             if (savedEndpoints == null)
                 savedEndpoints = SystemTable.loadDcRackInfo();
             if (savedEndpoints.containsKey(endpoint))
@@ -98,7 +101,8 @@ public class CloudstackSnitch extends AbstractNetworkTopologySnitch
         if (endpoint.equals(FBUtilities.getBroadcastAddress()))
             return csZoneDc;
         EndpointState state = Gossiper.instance.getEndpointStateForEndpoint(endpoint);
-        if (state == null || state.getApplicationState(ApplicationState.DC) == null) {
+        if (state == null || state.getApplicationState(ApplicationState.DC) == null) 
+        {
             if (savedEndpoints == null)
                 savedEndpoints = SystemTable.loadDcRackInfo();
             if (savedEndpoints.containsKey(endpoint))
@@ -113,14 +117,19 @@ public class CloudstackSnitch extends AbstractNetworkTopologySnitch
         HttpURLConnection conn = null;
         DataInputStream is = null;
 
-        try {
+        try 
+        {
             conn = (HttpURLConnection) new URL(url).openConnection();
-        } catch (Exception e) {
+        } 
+        catch (Exception e) 
+        {
             throw new ConfigurationException("CloudstackSnitch cannot query wrong metadata
URL: " + url);
         }
-        try {
+        try 
+        {
             conn.setRequestMethod("GET");
-            if (conn.getResponseCode() != 200) {
+            if (conn.getResponseCode() != 200) 
+            {
                 throw new ConfigurationException("CloudstackSnitch was unable to query metadata.");
             }
 
@@ -129,27 +138,31 @@ public class CloudstackSnitch extends AbstractNetworkTopologySnitch
             is = new DataInputStream(new BufferedInputStream(conn.getInputStream()));
             is.readFully(b);
             return new String(b, StandardCharsets.UTF_8);
-        } finally {
+        } 
+        finally 
+        {
             FileUtils.close(is);
             conn.disconnect();
         }
-
     }
 
     String csMetadataEndpoint() throws ConfigurationException
     {
-        for (String lease_uri: LEASE_FILES) {
-            try {
+        for (String lease_uri: LEASE_FILES) 
+        {
+            try 
+            {
                 File lease_file = new File(new URI(lease_uri));
-                if (lease_file.exists()) {
+                if (lease_file.exists()) 
+                {
                     return csEndpointFromLease(lease_file);
                 }
-
-            } catch (Exception e) {
+            } 
+            catch (Exception e) 
+            {
                 continue;
             }
 
-
         }
 
         throw new ConfigurationException("No valid DHCP lease file could be found.");
@@ -163,21 +176,32 @@ public class CloudstackSnitch extends AbstractNetworkTopologySnitch
         String endpoint = null;
         Pattern identifierPattern = Pattern.compile("^[ \t]*option dhcp-server-identifier
(.*);$");
 
-        try {
+        try 
+        {
             reader = new BufferedReader(new FileReader(lease));
-        } catch (Exception e) {
-            throw new ConfigurationException("CloudstackSnitch cannot access lease file.");
-        }
-
-        while ((line = reader.readLine()) != null) {
-            Matcher matcher = identifierPattern.matcher(line);
-
-            if (matcher.find()) {
-                endpoint = matcher.group(1);
+            
+            while ((line = reader.readLine()) != null) 
+            {
+                Matcher matcher = identifierPattern.matcher(line);
+
+                if (matcher.find()) 
+                {
+                    endpoint = matcher.group(1);
+                    break;
+                }
             }
+        } 
+        catch (Exception e)  
+        {
+            throw new ConfigurationException("CloudstackSnitch cannot access lease file.");
+        } 
+        finally
+        {
+        	FileUtils.closeQuietly(reader);
         }
 
-        if (endpoint == null) {
+        if (endpoint == null) 
+        {
             throw new ConfigurationException("No metadata server could be found in lease
file.");
         }
 


Mime
View raw message