You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by db...@apache.org on 2014/05/04 06:20:55 UTC
git commit: make sure streams get closed
Repository: cassandra
Updated Branches:
refs/heads/cassandra-1.2 2f32783bd -> c454807a7
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/cassandra-1.2
Commit: c454807a75a55bf7a12dbe7c641266bcaf33d6e1
Parents: 2f32783
Author: Dave Brosius <db...@mebigfatguy.com>
Authored: Sun May 4 00:19:51 2014 -0400
Committer: Dave Brosius <db...@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.");
}