001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.zookeeper;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.Properties;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseConfiguration;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035@Category({MiscTests.class, SmallTests.class})
036public class TestZKConfig {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040      HBaseClassTestRule.forClass(TestZKConfig.class);
041
042  @Test
043  public void testZKConfigLoading() throws Exception {
044    Configuration conf = HBaseConfiguration.create();
045    // Test that we read only from the config instance
046    // (i.e. via hbase-default.xml and hbase-site.xml)
047    conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, 2181);
048    Properties props = ZKConfig.makeZKProps(conf);
049    assertEquals("Property client port should have been default from the HBase config",
050                        "2181",
051                        props.getProperty("clientPort"));
052  }
053
054  @Test
055  public void testGetZooKeeperClusterKey() {
056    Configuration conf = HBaseConfiguration.create();
057    conf.set(HConstants.ZOOKEEPER_QUORUM, "\tlocalhost\n");
058    conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "3333");
059    conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "hbase");
060    String clusterKey = ZKConfig.getZooKeeperClusterKey(conf, "test");
061    assertTrue(!clusterKey.contains("\t") && !clusterKey.contains("\n"));
062    assertEquals("localhost:3333:hbase,test", clusterKey);
063  }
064
065  @Test
066  public void testClusterKey() throws Exception {
067    testKey("server", 2181, "/hbase");
068    testKey("server1,server2,server3", 2181, "/hbase");
069    try {
070      ZKConfig.validateClusterKey("2181:/hbase");
071    } catch (IOException ex) {
072      // OK
073    }
074  }
075
076  @Test
077  public void testClusterKeyWithMultiplePorts() throws Exception {
078    // server has different port than the default port
079    testKey("server1:2182", 2181, "/hbase", true);
080    // multiple servers have their own port
081    testKey("server1:2182,server2:2183,server3:2184", 2181, "/hbase", true);
082    // one server has no specified port, should use default port
083    testKey("server1:2182,server2,server3:2184", 2181, "/hbase", true);
084    // the last server has no specified port, should use default port
085    testKey("server1:2182,server2:2183,server3", 2181, "/hbase", true);
086    // multiple servers have no specified port, should use default port for those servers
087    testKey("server1:2182,server2,server3:2184,server4", 2181, "/hbase", true);
088    // same server, different ports
089    testKey("server1:2182,server1:2183,server1", 2181, "/hbase", true);
090    // mix of same server/different port and different server
091    testKey("server1:2182,server2:2183,server1", 2181, "/hbase", true);
092  }
093
094  private void testKey(String ensemble, int port, String znode)
095      throws IOException {
096    testKey(ensemble, port, znode, false); // not support multiple client ports
097  }
098
099  private void testKey(String ensemble, int port, String znode, Boolean multiplePortSupport)
100      throws IOException {
101    Configuration conf = new Configuration();
102    String key = ensemble+":"+port+":"+znode;
103    String ensemble2 = null;
104    ZKConfig.ZKClusterKey zkClusterKey = ZKConfig.transformClusterKey(key);
105    if (multiplePortSupport) {
106      ensemble2 = ZKConfig.standardizeZKQuorumServerString(ensemble,
107          Integer.toString(port));
108      assertEquals(ensemble2, zkClusterKey.getQuorumString());
109    }
110    else {
111      assertEquals(ensemble, zkClusterKey.getQuorumString());
112    }
113    assertEquals(port, zkClusterKey.getClientPort());
114    assertEquals(znode, zkClusterKey.getZnodeParent());
115
116    conf = HBaseConfiguration.createClusterConf(conf, key);
117    assertEquals(zkClusterKey.getQuorumString(), conf.get(HConstants.ZOOKEEPER_QUORUM));
118    assertEquals(zkClusterKey.getClientPort(), conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, -1));
119    assertEquals(zkClusterKey.getZnodeParent(), conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT));
120
121    String reconstructedKey = ZKConfig.getZooKeeperClusterKey(conf);
122    if (multiplePortSupport) {
123      String key2 = ensemble2 + ":" + port + ":" + znode;
124      assertEquals(key2, reconstructedKey);
125    }
126    else {
127      assertEquals(key, reconstructedKey);
128    }
129  }
130}