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", "2181",
050      props.getProperty("clientPort"));
051  }
052
053  @Test
054  public void testGetZooKeeperClusterKey() {
055    Configuration conf = HBaseConfiguration.create();
056    conf.set(HConstants.ZOOKEEPER_QUORUM, "\tlocalhost\n");
057    conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "3333");
058    conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "hbase");
059    String clusterKey = ZKConfig.getZooKeeperClusterKey(conf, "test");
060    assertTrue(!clusterKey.contains("\t") && !clusterKey.contains("\n"));
061    assertEquals("localhost:3333:hbase,test", clusterKey);
062  }
063
064  @Test
065  public void testClusterKey() throws Exception {
066    testKey("server", 2181, "/hbase");
067    testKey("server1,server2,server3", 2181, "/hbase");
068    try {
069      ZKConfig.validateClusterKey("2181:/hbase");
070    } catch (IOException ex) {
071      // OK
072    }
073  }
074
075  @Test
076  public void testClusterKeyWithMultiplePorts() throws Exception {
077    // server has different port than the default port
078    testKey("server1:2182", 2181, "/hbase", true);
079    // multiple servers have their own port
080    testKey("server1:2182,server2:2183,server3:2184", 2181, "/hbase", true);
081    // one server has no specified port, should use default port
082    testKey("server1:2182,server2,server3:2184", 2181, "/hbase", true);
083    // the last server has no specified port, should use default port
084    testKey("server1:2182,server2:2183,server3", 2181, "/hbase", true);
085    // multiple servers have no specified port, should use default port for those servers
086    testKey("server1:2182,server2,server3:2184,server4", 2181, "/hbase", true);
087    // same server, different ports
088    testKey("server1:2182,server1:2183,server1", 2181, "/hbase", true);
089    // mix of same server/different port and different server
090    testKey("server1:2182,server2:2183,server1", 2181, "/hbase", true);
091  }
092
093  private void testKey(String ensemble, int port, String znode) throws IOException {
094    testKey(ensemble, port, znode, false); // not support multiple client ports
095  }
096
097  private void testKey(String ensemble, int port, String znode, Boolean multiplePortSupport)
098    throws IOException {
099    Configuration conf = new Configuration();
100    String key = ensemble + ":" + port + ":" + znode;
101    String ensemble2 = null;
102    ZKConfig.ZKClusterKey zkClusterKey = ZKConfig.transformClusterKey(key);
103    if (multiplePortSupport) {
104      ensemble2 = ZKConfig.standardizeZKQuorumServerString(ensemble, Integer.toString(port));
105      assertEquals(ensemble2, zkClusterKey.getQuorumString());
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    } else {
122      assertEquals(key, reconstructedKey);
123    }
124  }
125}