1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.zookeeper;
19
20 import java.io.IOException;
21 import java.util.Properties;
22
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.HBaseConfiguration;
25 import org.apache.hadoop.hbase.HConstants;
26 import org.apache.hadoop.hbase.testclassification.MiscTests;
27 import org.apache.hadoop.hbase.testclassification.SmallTests;
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.junit.experimental.categories.Category;
31
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertTrue;
34
35 @Category({MiscTests.class, SmallTests.class})
36 public class TestZKConfig {
37
38 @Test
39 public void testZKConfigLoading() throws Exception {
40 Configuration conf = HBaseConfiguration.create();
41
42
43 conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, 2181);
44 Properties props = ZKConfig.makeZKProps(conf);
45 assertEquals("Property client port should have been default from the HBase config",
46 "2181",
47 props.getProperty("clientPort"));
48 }
49
50 @Test
51 public void testGetZooKeeperClusterKey() {
52 Configuration conf = HBaseConfiguration.create();
53 conf.set(HConstants.ZOOKEEPER_QUORUM, "\tlocalhost\n");
54 conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "3333");
55 conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "hbase");
56 String clusterKey = ZKConfig.getZooKeeperClusterKey(conf, "test");
57 assertTrue(!clusterKey.contains("\t") && !clusterKey.contains("\n"));
58 assertEquals("localhost:3333:hbase,test", clusterKey);
59 }
60
61 @Test
62 public void testClusterKey() throws Exception {
63 testKey("server", 2181, "hbase");
64 testKey("server1,server2,server3", 2181, "hbase");
65 try {
66 ZKConfig.validateClusterKey("2181:hbase");
67 } catch (IOException ex) {
68
69 }
70 }
71
72 @Test
73 public void testClusterKeyWithMultiplePorts() throws Exception {
74
75 testKey("server1:2182", 2181, "hbase", true);
76
77 testKey("server1:2182,server2:2183,server3:2184", 2181, "hbase", true);
78
79 testKey("server1:2182,server2,server3:2184", 2181, "hbase", true);
80
81 testKey("server1:2182,server2:2183,server3", 2181, "hbase", true);
82
83 testKey("server1:2182,server2,server3:2184,server4", 2181, "hbase", true);
84
85 testKey("server1:2182,server1:2183,server1", 2181, "hbase", true);
86
87 testKey("server1:2182,server2:2183,server1", 2181, "hbase", true);
88 }
89
90 private void testKey(String ensemble, int port, String znode)
91 throws IOException {
92 testKey(ensemble, port, znode, false);
93 }
94
95 private void testKey(String ensemble, int port, String znode, Boolean multiplePortSupport)
96 throws IOException {
97 Configuration conf = new Configuration();
98 String key = ensemble+":"+port+":"+znode;
99 String ensemble2 = null;
100 ZKConfig.ZKClusterKey zkClusterKey = ZKConfig.transformClusterKey(key);
101 if (multiplePortSupport) {
102 ensemble2 = ZKConfig.standardizeZKQuorumServerString(ensemble,
103 Integer.toString(port));
104 assertEquals(ensemble2, zkClusterKey.getQuorumString());
105 }
106 else {
107 assertEquals(ensemble, zkClusterKey.getQuorumString());
108 }
109 assertEquals(port, zkClusterKey.getClientPort());
110 assertEquals(znode, zkClusterKey.getZnodeParent());
111
112 conf = HBaseConfiguration.createClusterConf(conf, key);
113 assertEquals(zkClusterKey.getQuorumString(), conf.get(HConstants.ZOOKEEPER_QUORUM));
114 assertEquals(zkClusterKey.getClientPort(), conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, -1));
115 assertEquals(zkClusterKey.getZnodeParent(), conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT));
116
117 String reconstructedKey = ZKConfig.getZooKeeperClusterKey(conf);
118 if (multiplePortSupport) {
119 String key2 = ensemble2 + ":" + port + ":" + znode;
120 assertEquals(key2, reconstructedKey);
121 }
122 else {
123 assertEquals(key, reconstructedKey);
124 }
125 }
126 }