Skip to content

Commit 5e84997

Browse files
maolingtedyu
authored andcommitted
HBASE-13468 hbase.zookeeper.quorum supports ipv6 address
Signed-off-by: tedyu <yuzhihong@gmail.com>
1 parent 6786aca commit 5e84997

5 files changed

Lines changed: 70 additions & 3 deletions

File tree

hbase-common/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@
211211
<artifactId>commons-io</artifactId>
212212
<scope>compile</scope>
213213
</dependency>
214+
<dependency>
215+
<groupId>commons-validator</groupId>
216+
<artifactId>commons-validator</artifactId>
217+
<scope>compile</scope>
218+
</dependency>
214219
<dependency>
215220
<groupId>com.google.protobuf</groupId>
216221
<artifactId>protobuf-java</artifactId>

hbase-common/src/main/java/org/apache/hadoop/hbase/zookeeper/ZKConfig.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Map.Entry;
2323
import java.util.Properties;
2424

25+
import org.apache.commons.validator.routines.InetAddressValidator;
2526
import org.apache.hadoop.conf.Configuration;
2627
import org.apache.hadoop.hbase.HConstants;
2728
import org.apache.hadoop.util.StringUtils;
@@ -146,12 +147,36 @@ public static String getZKQuorumServersString(Configuration conf) {
146147
public static String buildZKQuorumServerString(String[] serverHosts, String clientPort) {
147148
StringBuilder quorumStringBuilder = new StringBuilder();
148149
String serverHost;
150+
InetAddressValidator validator = new InetAddressValidator();
149151
for (int i = 0; i < serverHosts.length; ++i) {
150-
if (serverHosts[i].contains(":")) {
151-
serverHost = serverHosts[i]; // just use the port specified from the input
152+
if (serverHosts[i].startsWith("[")) {
153+
int index = serverHosts[i].indexOf("]");
154+
if (index < 0) {
155+
throw new IllegalArgumentException(serverHosts[i]
156+
+ " starts with '[' but has no matching ']:'");
157+
}
158+
if (index + 2 == serverHosts[i].length()) {
159+
throw new IllegalArgumentException(serverHosts[i]
160+
+ " doesn't have a port after colon");
161+
}
162+
//check the IPv6 address e.g. [2001:db8::1]
163+
String serverHostWithoutBracket = serverHosts[i].substring(1, index);
164+
if (!validator.isValidInet6Address(serverHostWithoutBracket)) {
165+
throw new IllegalArgumentException(serverHosts[i]
166+
+ " is not a valid IPv6 address");
167+
}
168+
serverHost = serverHosts[i];
169+
if ((index + 1 == serverHosts[i].length())) {
170+
serverHost = serverHosts[i] + ":" + clientPort;
171+
}
152172
} else {
153-
serverHost = serverHosts[i] + ":" + clientPort;
173+
if (serverHosts[i].contains(":")) {
174+
serverHost = serverHosts[i]; // just use the port specified from the input
175+
} else {
176+
serverHost = serverHosts[i] + ":" + clientPort;
177+
}
154178
}
179+
155180
if (i > 0) {
156181
quorumStringBuilder.append(',');
157182
}

hbase-shaded/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@
338338
</relocation>
339339

340340
<!-- org.apache.commons not including logging -->
341+
<relocation>
342+
<pattern>org.apache.commons.validator</pattern>
343+
<shadedPattern>${shaded.prefix}.org.apache.commons.validator</shadedPattern>
344+
</relocation>
341345
<relocation>
342346
<pattern>org.apache.commons.beanutils</pattern>
343347
<shadedPattern>${shaded.prefix}.org.apache.commons.beanutils</shadedPattern>

hbase-zookeeper/src/test/java/org/apache/hadoop/hbase/zookeeper/TestZKMainServer.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.hadoop.hbase.HConstants;
2929
import org.apache.hadoop.hbase.testclassification.SmallTests;
3030
import org.apache.hadoop.hbase.testclassification.ZKTests;
31+
import org.junit.Assert;
3132
import org.junit.ClassRule;
3233
import org.junit.Test;
3334
import org.junit.experimental.categories.Category;
@@ -121,5 +122,31 @@ public void testHostPortParse() {
121122
c.set("hbase.zookeeper.quorum", "example1.com:5678,example2.com:9012,example3.com");
122123
ensemble = parser.parse(c);
123124
assertEquals(ensemble, "example1.com:5678,example2.com:9012,example3.com:" + port);
125+
126+
// multiple servers(IPv6) with its own port
127+
c.set("hbase.zookeeper.quorum", "[2001:db8:1::242:ac11:2]:2181," +
128+
"[2001:db8:1::242:ac11:3]:5678");
129+
ensemble = parser.parse(c);
130+
assertEquals("[2001:db8:1::242:ac11:2]:2181," +
131+
"[2001:db8:1::242:ac11:3]:5678", ensemble);
132+
133+
// some servers(IPv6) without its own port, which will be assigned the default client port
134+
c.set("hbase.zookeeper.quorum", "[1001:db8:1::242:ac11:8], [2001:db8:1::242:df23:2]:9876," +
135+
"[2001:db8:1::242:ac11:3]:5678");
136+
ensemble = parser.parse(c);
137+
assertEquals("[1001:db8:1::242:ac11:8]:1234, [2001:db8:1::242:df23:2]:9876," +
138+
"[2001:db8:1::242:ac11:3]:5678", ensemble);
139+
140+
//a bad case
141+
try {
142+
// some servers(IPv6) with an invaild Ipv6 address in it
143+
c.set("hbase.zookeeper.quorum", "[1001:db8:1::242:ac11:8], [2001:db8:1::242:df23:2]:9876," +
144+
"[1001:db8:1::242:ac11:8:89:67]:5678");
145+
ensemble = parser.parse(c);
146+
Assert.fail("IPv6 address should be 8 groups.");
147+
} catch (IllegalArgumentException e) {
148+
//expected
149+
}
150+
124151
}
125152
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,7 @@
14661466
<audience-annotations.version>0.5.0</audience-annotations.version>
14671467
<avro.version>1.7.7</avro.version>
14681468
<commons-codec.version>1.10</commons-codec.version>
1469+
<commons-validator.version>1.6</commons-validator.version>
14691470
<!-- pretty outdated -->
14701471
<commons-io.version>2.5</commons-io.version>
14711472
<commons-lang3.version>3.6</commons-lang3.version>
@@ -1919,6 +1920,11 @@
19191920
<artifactId>commons-codec</artifactId>
19201921
<version>${commons-codec.version}</version>
19211922
</dependency>
1923+
<dependency>
1924+
<groupId>commons-validator</groupId>
1925+
<artifactId>commons-validator</artifactId>
1926+
<version>${commons-validator.version}</version>
1927+
</dependency>
19221928
<dependency>
19231929
<groupId>commons-io</groupId>
19241930
<artifactId>commons-io</artifactId>

0 commit comments

Comments
 (0)