HBASE-2806 DNS hiccups cause uncaught NPE in HServerAddress#getBindAddress

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@960650 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2010-07-05 17:47:10 +00:00
parent 03933720fa
commit 2993d97c97
2 changed files with 41 additions and 22 deletions

View File

@ -426,6 +426,8 @@ Release 0.21.0 - Unreleased
HBASE-2707 Can't recover from a dead ROOT server if any exceptions happens
during log splitting
HBASE-2501 Refactor StoreFile Code
HBASE-2806 DNS hiccups cause uncaught NPE in HServerAddress#getBindAddress
(Benoit Sigoure via Stack)
IMPROVEMENTS
HBASE-1760 Cleanup TODOs in HTable

View File

@ -19,12 +19,14 @@
*/
package org.apache.hadoop.hbase;
import org.apache.hadoop.io.*;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.InetAddress;
/**
* HServerAddress is a "label" for a HBase server made of host and port number.
@ -39,13 +41,14 @@ public class HServerAddress implements WritableComparable<HServerAddress> {
}
/**
* Construct a HServerAddress from an InetSocketAddress
* Construct an instance from an {@link InetSocketAddress}.
* @param address InetSocketAddress of server
*/
public HServerAddress(InetSocketAddress address) {
this.address = address;
this.stringValue = address.getAddress().getHostAddress() + ":" +
address.getPort();
checkBindAddressCanBeResolved();
}
/**
@ -53,14 +56,14 @@ public class HServerAddress implements WritableComparable<HServerAddress> {
*/
public HServerAddress(String hostAndPort) {
int colonIndex = hostAndPort.lastIndexOf(':');
if(colonIndex < 0) {
if (colonIndex < 0) {
throw new IllegalArgumentException("Not a host:port pair: " + hostAndPort);
}
String host = hostAndPort.substring(0, colonIndex);
int port =
Integer.valueOf(hostAndPort.substring(colonIndex + 1)).intValue();
int port = Integer.parseInt(hostAndPort.substring(colonIndex + 1));
this.address = new InetSocketAddress(host, port);
this.stringValue = hostAndPort;
checkBindAddressCanBeResolved();
}
/**
@ -70,38 +73,53 @@ public class HServerAddress implements WritableComparable<HServerAddress> {
public HServerAddress(String bindAddress, int port) {
this.address = new InetSocketAddress(bindAddress, port);
this.stringValue = bindAddress + ":" + port;
checkBindAddressCanBeResolved();
}
/**
* Copy-constructor
*
* Copy-constructor.
* @param other HServerAddress to copy from
*/
public HServerAddress(HServerAddress other) {
String bindAddress = other.getBindAddress();
int port = other.getPort();
this.address = new InetSocketAddress(bindAddress, port);
stringValue = bindAddress + ":" + port;
stringValue = other.stringValue;
checkBindAddressCanBeResolved();
}
/** @return Bind address */
public String getBindAddress() {
return this.address.getAddress().getHostAddress();
final InetAddress addr = address.getAddress();
if (addr != null) {
return addr.getHostAddress();
} else {
LogFactory.getLog(HServerAddress.class).error("Could not resolve the"
+ " DNS name of " + stringValue);
return null;
}
}
private checkBindAddressCanBeResolved() {
if (getBindAddress() == null) {
throw new IllegalArgumentException("Could not resolve the"
+ " DNS name of " + stringValue);
}
}
/** @return Port number */
public int getPort() {
return this.address.getPort();
return address.getPort();
}
/** @return Hostname */
public String getHostname() {
return this.address.getHostName();
return address.getHostName();
}
/** @return The InetSocketAddress */
public InetSocketAddress getInetSocketAddress() {
return this.address;
return address;
}
/**
@ -109,7 +127,7 @@ public class HServerAddress implements WritableComparable<HServerAddress> {
*/
@Override
public String toString() {
return (this.stringValue == null ? "" : this.stringValue);
return stringValue == null ? "" : stringValue;
}
@Override
@ -123,13 +141,13 @@ public class HServerAddress implements WritableComparable<HServerAddress> {
if (getClass() != o.getClass()) {
return false;
}
return this.compareTo((HServerAddress)o) == 0;
return compareTo((HServerAddress) o) == 0;
}
@Override
public int hashCode() {
int result = this.address.hashCode();
result ^= this.stringValue.hashCode();
int result = address.hashCode();
result ^= stringValue.hashCode();
return result;
}
@ -141,13 +159,13 @@ public class HServerAddress implements WritableComparable<HServerAddress> {
String bindAddress = in.readUTF();
int port = in.readInt();
if(bindAddress == null || bindAddress.length() == 0) {
if (bindAddress == null || bindAddress.length() == 0) {
address = null;
stringValue = null;
} else {
address = new InetSocketAddress(bindAddress, port);
stringValue = bindAddress + ":" + port;
checkBindAddressCanBeResolved();
}
}
@ -155,7 +173,6 @@ public class HServerAddress implements WritableComparable<HServerAddress> {
if (address == null) {
out.writeUTF("");
out.writeInt(0);
} else {
out.writeUTF(address.getAddress().getHostAddress());
out.writeInt(address.getPort());
@ -170,7 +187,7 @@ public class HServerAddress implements WritableComparable<HServerAddress> {
// Addresses as Strings may not compare though address is for the one
// server with only difference being that one address has hostname
// resolved whereas other only has IP.
if (this.address.equals(o.address)) return 0;
return this.toString().compareTo(o.toString());
if (address.equals(o.address)) return 0;
return toString().compareTo(o.toString());
}
}
}