HBASE-13468 hbase.zookeeper.quorum supports ipv6 address

Signed-off-by: tedyu <yuzhihong@gmail.com>
This commit is contained in:
maoling 2018-10-12 17:15:41 +08:00 committed by tedyu
parent 6786aca666
commit 5e84997f2f
5 changed files with 70 additions and 3 deletions

View File

@ -211,6 +211,11 @@
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>com.google.protobuf</groupId> <groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId> <artifactId>protobuf-java</artifactId>

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Properties; import java.util.Properties;
import org.apache.commons.validator.routines.InetAddressValidator;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.StringUtils;
@ -146,12 +147,36 @@ public final class ZKConfig {
public static String buildZKQuorumServerString(String[] serverHosts, String clientPort) { public static String buildZKQuorumServerString(String[] serverHosts, String clientPort) {
StringBuilder quorumStringBuilder = new StringBuilder(); StringBuilder quorumStringBuilder = new StringBuilder();
String serverHost; String serverHost;
InetAddressValidator validator = new InetAddressValidator();
for (int i = 0; i < serverHosts.length; ++i) { for (int i = 0; i < serverHosts.length; ++i) {
if (serverHosts[i].contains(":")) { if (serverHosts[i].startsWith("[")) {
serverHost = serverHosts[i]; // just use the port specified from the input int index = serverHosts[i].indexOf("]");
if (index < 0) {
throw new IllegalArgumentException(serverHosts[i]
+ " starts with '[' but has no matching ']:'");
}
if (index + 2 == serverHosts[i].length()) {
throw new IllegalArgumentException(serverHosts[i]
+ " doesn't have a port after colon");
}
//check the IPv6 address e.g. [2001:db8::1]
String serverHostWithoutBracket = serverHosts[i].substring(1, index);
if (!validator.isValidInet6Address(serverHostWithoutBracket)) {
throw new IllegalArgumentException(serverHosts[i]
+ " is not a valid IPv6 address");
}
serverHost = serverHosts[i];
if ((index + 1 == serverHosts[i].length())) {
serverHost = serverHosts[i] + ":" + clientPort;
}
} else { } else {
serverHost = serverHosts[i] + ":" + clientPort; if (serverHosts[i].contains(":")) {
serverHost = serverHosts[i]; // just use the port specified from the input
} else {
serverHost = serverHosts[i] + ":" + clientPort;
}
} }
if (i > 0) { if (i > 0) {
quorumStringBuilder.append(','); quorumStringBuilder.append(',');
} }

View File

@ -338,6 +338,10 @@
</relocation> </relocation>
<!-- org.apache.commons not including logging --> <!-- org.apache.commons not including logging -->
<relocation>
<pattern>org.apache.commons.validator</pattern>
<shadedPattern>${shaded.prefix}.org.apache.commons.validator</shadedPattern>
</relocation>
<relocation> <relocation>
<pattern>org.apache.commons.beanutils</pattern> <pattern>org.apache.commons.beanutils</pattern>
<shadedPattern>${shaded.prefix}.org.apache.commons.beanutils</shadedPattern> <shadedPattern>${shaded.prefix}.org.apache.commons.beanutils</shadedPattern>

View File

@ -28,6 +28,7 @@ import org.apache.hadoop.hbase.HBaseZKTestingUtility;
import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.testclassification.SmallTests; import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.hbase.testclassification.ZKTests; import org.apache.hadoop.hbase.testclassification.ZKTests;
import org.junit.Assert;
import org.junit.ClassRule; import org.junit.ClassRule;
import org.junit.Test; import org.junit.Test;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@ -121,5 +122,31 @@ public class TestZKMainServer {
c.set("hbase.zookeeper.quorum", "example1.com:5678,example2.com:9012,example3.com"); c.set("hbase.zookeeper.quorum", "example1.com:5678,example2.com:9012,example3.com");
ensemble = parser.parse(c); ensemble = parser.parse(c);
assertEquals(ensemble, "example1.com:5678,example2.com:9012,example3.com:" + port); assertEquals(ensemble, "example1.com:5678,example2.com:9012,example3.com:" + port);
// multiple servers(IPv6) with its own port
c.set("hbase.zookeeper.quorum", "[2001:db8:1::242:ac11:2]:2181," +
"[2001:db8:1::242:ac11:3]:5678");
ensemble = parser.parse(c);
assertEquals("[2001:db8:1::242:ac11:2]:2181," +
"[2001:db8:1::242:ac11:3]:5678", ensemble);
// some servers(IPv6) without its own port, which will be assigned the default client port
c.set("hbase.zookeeper.quorum", "[1001:db8:1::242:ac11:8], [2001:db8:1::242:df23:2]:9876," +
"[2001:db8:1::242:ac11:3]:5678");
ensemble = parser.parse(c);
assertEquals("[1001:db8:1::242:ac11:8]:1234, [2001:db8:1::242:df23:2]:9876," +
"[2001:db8:1::242:ac11:3]:5678", ensemble);
//a bad case
try {
// some servers(IPv6) with an invaild Ipv6 address in it
c.set("hbase.zookeeper.quorum", "[1001:db8:1::242:ac11:8], [2001:db8:1::242:df23:2]:9876," +
"[1001:db8:1::242:ac11:8:89:67]:5678");
ensemble = parser.parse(c);
Assert.fail("IPv6 address should be 8 groups.");
} catch (IllegalArgumentException e) {
//expected
}
} }
} }

View File

@ -1466,6 +1466,7 @@
<audience-annotations.version>0.5.0</audience-annotations.version> <audience-annotations.version>0.5.0</audience-annotations.version>
<avro.version>1.7.7</avro.version> <avro.version>1.7.7</avro.version>
<commons-codec.version>1.10</commons-codec.version> <commons-codec.version>1.10</commons-codec.version>
<commons-validator.version>1.6</commons-validator.version>
<!-- pretty outdated --> <!-- pretty outdated -->
<commons-io.version>2.5</commons-io.version> <commons-io.version>2.5</commons-io.version>
<commons-lang3.version>3.6</commons-lang3.version> <commons-lang3.version>3.6</commons-lang3.version>
@ -1919,6 +1920,11 @@
<artifactId>commons-codec</artifactId> <artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version> <version>${commons-codec.version}</version>
</dependency> </dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>${commons-validator.version}</version>
</dependency>
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>