View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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.SmallTests;
27  import org.junit.Assert;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertTrue;
33  
34  @Category({SmallTests.class})
35  public class TestZKConfig {
36  
37    @Test
38    public void testZKConfigLoading() throws Exception {
39      Configuration conf = HBaseConfiguration.create();
40      // Test that we read only from the config instance
41      // (i.e. via hbase-default.xml and hbase-site.xml)
42      conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, 2181);
43      Properties props = ZKConfig.makeZKProps(conf);
44      assertEquals("Property client port should have been default from the HBase config",
45                          "2181",
46                          props.getProperty("clientPort"));
47    }
48  
49    @Test
50    public void testGetZooKeeperClusterKey() {
51      Configuration conf = HBaseConfiguration.create();
52      conf.set(HConstants.ZOOKEEPER_QUORUM, "\tlocalhost\n");
53      conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "3333");
54      conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "hbase");
55      String clusterKey = ZKConfig.getZooKeeperClusterKey(conf, "test");
56      assertTrue(!clusterKey.contains("\t") && !clusterKey.contains("\n"));
57      assertEquals("localhost:3333:hbase,test", clusterKey);
58    }
59  
60    @Test
61    public void testClusterKey() throws Exception {
62      testKey("server", 2181, "hbase");
63      testKey("server1,server2,server3", 2181, "hbase");
64      try {
65        ZKConfig.validateClusterKey("2181:hbase");
66      } catch (IOException ex) {
67        // OK
68      }
69    }
70  
71    @Test
72    public void testClusterKeyWithMultiplePorts() throws Exception {
73      // server has different port than the default port
74      testKey("server1:2182", 2181, "hbase", true);
75      // multiple servers have their own port
76      testKey("server1:2182,server2:2183,server3:2184", 2181, "hbase", true);
77      // one server has no specified port, should use default port
78      testKey("server1:2182,server2,server3:2184", 2181, "hbase", true);
79      // the last server has no specified port, should use default port
80      testKey("server1:2182,server2:2183,server3", 2181, "hbase", true);
81      // multiple servers have no specified port, should use default port for those servers
82      testKey("server1:2182,server2,server3:2184,server4", 2181, "hbase", true);
83      // same server, different ports
84      testKey("server1:2182,server1:2183,server1", 2181, "hbase", true);
85      // mix of same server/different port and different server
86      testKey("server1:2182,server2:2183,server1", 2181, "hbase", true);
87    }
88  
89    private void testKey(String ensemble, int port, String znode)
90        throws IOException {
91      testKey(ensemble, port, znode, false); // not support multiple client ports
92    }
93  
94    private void testKey(String ensemble, int port, String znode, Boolean multiplePortSupport)
95        throws IOException {
96      Configuration conf = new Configuration();
97      String key = ensemble+":"+port+":"+znode;
98      String ensemble2 = null;
99      ZKConfig.ZKClusterKey zkClusterKey = ZKConfig.transformClusterKey(key);
100     if (multiplePortSupport) {
101       ensemble2 = ZKConfig.standardizeZKQuorumServerString(ensemble,
102           Integer.toString(port));
103       assertEquals(ensemble2, zkClusterKey.getQuorumString());
104     }
105     else {
106       assertEquals(ensemble, zkClusterKey.getQuorumString());
107     }
108     assertEquals(port, zkClusterKey.getClientPort());
109     assertEquals(znode, zkClusterKey.getZnodeParent());
110 
111     conf = HBaseConfiguration.createClusterConf(conf, key);
112     assertEquals(zkClusterKey.getQuorumString(), conf.get(HConstants.ZOOKEEPER_QUORUM));
113     assertEquals(zkClusterKey.getClientPort(), conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, -1));
114     assertEquals(zkClusterKey.getZnodeParent(), conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT));
115 
116     String reconstructedKey = ZKConfig.getZooKeeperClusterKey(conf);
117     if (multiplePortSupport) {
118       String key2 = ensemble2 + ":" + port + ":" + znode;
119       assertEquals(key2, reconstructedKey);
120     }
121     else {
122       assertEquals(key, reconstructedKey);
123     }
124   }
125 }