View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.zookeeper;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.*;
31  import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
32  import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
33  import org.junit.Before;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  import static junit.framework.Assert.assertEquals;
38  import static org.junit.Assert.*;
39  
40  /**
41   * Test for HQuorumPeer.
42   */
43  @Category(MediumTests.class)
44  public class TestHQuorumPeer {
45    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46    private static int PORT_NO = 21818;
47    private Path dataDir;
48  
49  
50    @Before public void setup() throws IOException {
51      // Set it to a non-standard port.
52      TEST_UTIL.getConfiguration().setInt(HConstants.ZOOKEEPER_CLIENT_PORT,
53          PORT_NO);
54      this.dataDir = TEST_UTIL.getDataTestDir(this.getClass().getName());
55      FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration());
56      if (fs.exists(this.dataDir)) {
57        if (!fs.delete(this.dataDir, true)) {
58          throw new IOException("Failed cleanup of " + this.dataDir);
59        }
60      }
61      if (!fs.mkdirs(this.dataDir)) {
62        throw new IOException("Failed create of " + this.dataDir);
63      }
64    }
65  
66    @Test public void testMakeZKProps() {
67      Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
68      conf.set(HConstants.ZOOKEEPER_DATA_DIR, this.dataDir.toString());
69      Properties properties = ZKConfig.makeZKProps(conf);
70      assertEquals(dataDir.toString(), (String)properties.get("dataDir"));
71      assertEquals(Integer.valueOf(PORT_NO),
72        Integer.valueOf(properties.getProperty("clientPort")));
73      assertEquals("localhost:2888:3888", properties.get("server.0"));
74      assertEquals(null, properties.get("server.1"));
75  
76      String oldValue = conf.get(HConstants.ZOOKEEPER_QUORUM);
77      conf.set(HConstants.ZOOKEEPER_QUORUM, "a.foo.bar,b.foo.bar,c.foo.bar");
78      properties = ZKConfig.makeZKProps(conf);
79      assertEquals(dataDir.toString(), properties.get("dataDir"));
80      assertEquals(Integer.valueOf(PORT_NO),
81        Integer.valueOf(properties.getProperty("clientPort")));
82      assertEquals("a.foo.bar:2888:3888", properties.get("server.0"));
83      assertEquals("b.foo.bar:2888:3888", properties.get("server.1"));
84      assertEquals("c.foo.bar:2888:3888", properties.get("server.2"));
85      assertEquals(null, properties.get("server.3"));
86      conf.set(HConstants.ZOOKEEPER_QUORUM, oldValue);
87    }
88  
89    @Test public void testConfigInjection() throws Exception {
90      String s =
91        "dataDir=" + this.dataDir.toString() + "\n" +
92        "clientPort=2181\n" +
93        "initLimit=2\n" +
94        "syncLimit=2\n" +
95        "server.0=${hbase.master.hostname}:2888:3888\n" +
96        "server.1=server1:2888:3888\n" +
97        "server.2=server2:2888:3888\n";
98  
99      System.setProperty("hbase.master.hostname", "localhost");
100     InputStream is = new ByteArrayInputStream(s.getBytes());
101     Configuration conf = TEST_UTIL.getConfiguration();
102     Properties properties = ZKConfig.parseZooCfg(conf, is);
103 
104     assertEquals(this.dataDir.toString(), properties.get("dataDir"));
105     assertEquals(Integer.valueOf(2181),
106       Integer.valueOf(properties.getProperty("clientPort")));
107     assertEquals("localhost:2888:3888", properties.get("server.0"));
108 
109     HQuorumPeer.writeMyID(properties);
110     QuorumPeerConfig config = new QuorumPeerConfig();
111     config.parseProperties(properties);
112 
113     assertEquals(this.dataDir.toString(), config.getDataDir());
114     assertEquals(2181, config.getClientPortAddress().getPort());
115     Map<Long,QuorumServer> servers = config.getServers();
116     assertEquals(3, servers.size());
117     assertTrue(servers.containsKey(Long.valueOf(0)));
118     QuorumServer server = servers.get(Long.valueOf(0));
119     assertEquals("localhost", server.addr.getHostName());
120 
121     // Override with system property.
122     System.setProperty("hbase.master.hostname", "foo.bar");
123     is = new ByteArrayInputStream(s.getBytes());
124     properties = ZKConfig.parseZooCfg(conf, is);
125     assertEquals("foo.bar:2888:3888", properties.get("server.0"));
126 
127     config.parseProperties(properties);
128 
129     servers = config.getServers();
130     server = servers.get(Long.valueOf(0));
131     assertEquals("foo.bar", server.addr.getHostName());
132   }
133 
134   @Test public void testShouldAssignDefaultZookeeperClientPort() {
135     Configuration config = HBaseConfiguration.create();
136     config.clear();
137     Properties p = ZKConfig.makeZKProps(config);
138     assertNotNull(p);
139     assertEquals(2181, p.get("clientPort"));
140   }
141 
142 }
143