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.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      // Test that we read only from the config instance
42      // (i.e. via hbase-default.xml and hbase-site.xml)
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        // OK
69      }
70    }
71  
72    @Test
73    public void testClusterKeyWithMultiplePorts() throws Exception {
74      // server has different port than the default port
75      testKey("server1:2182", 2181, "hbase", true);
76      // multiple servers have their own port
77      testKey("server1:2182,server2:2183,server3:2184", 2181, "hbase", true);
78      // one server has no specified port, should use default port
79      testKey("server1:2182,server2,server3:2184", 2181, "hbase", true);
80      // the last server has no specified port, should use default port
81      testKey("server1:2182,server2:2183,server3", 2181, "hbase", true);
82      // multiple servers have no specified port, should use default port for those servers
83      testKey("server1:2182,server2,server3:2184,server4", 2181, "hbase", true);
84      // same server, different ports
85      testKey("server1:2182,server1:2183,server1", 2181, "hbase", true);
86      // mix of same server/different port and different server
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); // not support multiple client ports
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 }