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.hadoop.hbase.testclassification.MediumTests;
32  import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
33  import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
34  import org.junit.Before;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  import static junit.framework.Assert.assertEquals;
39  import static org.junit.Assert.*;
40  
41  /**
42   * Test for HQuorumPeer.
43   */
44  @Category(MediumTests.class)
45  public class TestHQuorumPeer {
46    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
47    private static int PORT_NO = 21818;
48    private Path dataDir;
49  
50  
51    @Before public void setup() throws IOException {
52      // Set it to a non-standard port.
53      TEST_UTIL.getConfiguration().setInt(HConstants.ZOOKEEPER_CLIENT_PORT,
54          PORT_NO);
55      this.dataDir = TEST_UTIL.getDataTestDir(this.getClass().getName());
56      FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration());
57      if (fs.exists(this.dataDir)) {
58        if (!fs.delete(this.dataDir, true)) {
59          throw new IOException("Failed cleanup of " + this.dataDir);
60        }
61      }
62      if (!fs.mkdirs(this.dataDir)) {
63        throw new IOException("Failed create of " + this.dataDir);
64      }
65    }
66  
67    @Test public void testMakeZKProps() {
68      Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
69      conf.set(HConstants.ZOOKEEPER_DATA_DIR, this.dataDir.toString());
70      Properties properties = ZKConfig.makeZKProps(conf);
71      assertEquals(dataDir.toString(), (String)properties.get("dataDir"));
72      assertEquals(Integer.valueOf(PORT_NO),
73        Integer.valueOf(properties.getProperty("clientPort")));
74      assertEquals("localhost:2888:3888", properties.get("server.0"));
75      assertEquals(null, properties.get("server.1"));
76  
77      String oldValue = conf.get(HConstants.ZOOKEEPER_QUORUM);
78      conf.set(HConstants.ZOOKEEPER_QUORUM, "a.foo.bar,b.foo.bar,c.foo.bar");
79      properties = ZKConfig.makeZKProps(conf);
80      assertEquals(dataDir.toString(), properties.get("dataDir"));
81      assertEquals(Integer.valueOf(PORT_NO),
82        Integer.valueOf(properties.getProperty("clientPort")));
83      assertEquals("a.foo.bar:2888:3888", properties.get("server.0"));
84      assertEquals("b.foo.bar:2888:3888", properties.get("server.1"));
85      assertEquals("c.foo.bar:2888:3888", properties.get("server.2"));
86      assertEquals(null, properties.get("server.3"));
87      conf.set(HConstants.ZOOKEEPER_QUORUM, oldValue);
88    }
89  
90    @Test public void testConfigInjection() throws Exception {
91      String s =
92        "dataDir=" + this.dataDir.toString() + "\n" +
93        "clientPort=2181\n" +
94        "initLimit=2\n" +
95        "syncLimit=2\n" +
96        "server.0=${hbase.master.hostname}:2888:3888\n" +
97        "server.1=server1:2888:3888\n" +
98        "server.2=server2:2888:3888\n";
99  
100     System.setProperty("hbase.master.hostname", "localhost");
101     InputStream is = new ByteArrayInputStream(s.getBytes());
102     Configuration conf = TEST_UTIL.getConfiguration();
103     Properties properties = ZKConfig.parseZooCfg(conf, is);
104 
105     assertEquals(this.dataDir.toString(), properties.get("dataDir"));
106     assertEquals(Integer.valueOf(2181),
107       Integer.valueOf(properties.getProperty("clientPort")));
108     assertEquals("localhost:2888:3888", properties.get("server.0"));
109 
110     HQuorumPeer.writeMyID(properties);
111     QuorumPeerConfig config = new QuorumPeerConfig();
112     config.parseProperties(properties);
113 
114     assertEquals(this.dataDir.toString(), config.getDataDir());
115     assertEquals(2181, config.getClientPortAddress().getPort());
116     Map<Long,QuorumServer> servers = config.getServers();
117     assertEquals(3, servers.size());
118     assertTrue(servers.containsKey(Long.valueOf(0)));
119     QuorumServer server = servers.get(Long.valueOf(0));
120     assertEquals("localhost", server.addr.getHostName());
121 
122     // Override with system property.
123     System.setProperty("hbase.master.hostname", "foo.bar");
124     is = new ByteArrayInputStream(s.getBytes());
125     properties = ZKConfig.parseZooCfg(conf, is);
126     assertEquals("foo.bar:2888:3888", properties.get("server.0"));
127 
128     config.parseProperties(properties);
129 
130     servers = config.getServers();
131     server = servers.get(Long.valueOf(0));
132     assertEquals("foo.bar", server.addr.getHostName());
133   }
134 
135   @Test public void testShouldAssignDefaultZookeeperClientPort() {
136     Configuration config = HBaseConfiguration.create();
137     config.clear();
138     Properties p = ZKConfig.makeZKProps(config);
139     assertNotNull(p);
140     assertEquals(2181, p.get("clientPort"));
141   }
142 
143 }
144