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.lang.reflect.Field;
25  import java.lang.reflect.Method;
26  import java.net.InetSocketAddress;
27  import java.util.Map;
28  import java.util.Properties;
29  
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.fs.FileSystem;
32  import org.apache.hadoop.fs.Path;
33  import org.apache.hadoop.hbase.*;
34  import org.apache.hadoop.hbase.testclassification.MediumTests;
35  import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
36  import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
37  import org.junit.Before;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  import static junit.framework.Assert.assertEquals;
42  import static org.junit.Assert.*;
43  
44  /**
45   * Test for HQuorumPeer.
46   */
47  @Category(MediumTests.class)
48  public class TestHQuorumPeer {
49    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
50    private static int PORT_NO = 21818;
51    private Path dataDir;
52  
53  
54    @Before public void setup() throws IOException {
55      // Set it to a non-standard port.
56      TEST_UTIL.getConfiguration().setInt(HConstants.ZOOKEEPER_CLIENT_PORT,
57          PORT_NO);
58      this.dataDir = TEST_UTIL.getDataTestDir(this.getClass().getName());
59      FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration());
60      if (fs.exists(this.dataDir)) {
61        if (!fs.delete(this.dataDir, true)) {
62          throw new IOException("Failed cleanup of " + this.dataDir);
63        }
64      }
65      if (!fs.mkdirs(this.dataDir)) {
66        throw new IOException("Failed create of " + this.dataDir);
67      }
68    }
69  
70    @Test public void testMakeZKProps() {
71      Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
72      conf.set(HConstants.ZOOKEEPER_DATA_DIR, this.dataDir.toString());
73      Properties properties = ZKConfig.makeZKProps(conf);
74      assertEquals(dataDir.toString(), (String)properties.get("dataDir"));
75      assertEquals(Integer.valueOf(PORT_NO),
76        Integer.valueOf(properties.getProperty("clientPort")));
77      assertEquals("localhost:2888:3888", properties.get("server.0"));
78      assertEquals(null, properties.get("server.1"));
79  
80      String oldValue = conf.get(HConstants.ZOOKEEPER_QUORUM);
81      conf.set(HConstants.ZOOKEEPER_QUORUM, "a.foo.bar,b.foo.bar,c.foo.bar");
82      properties = ZKConfig.makeZKProps(conf);
83      assertEquals(dataDir.toString(), properties.get("dataDir"));
84      assertEquals(Integer.valueOf(PORT_NO),
85        Integer.valueOf(properties.getProperty("clientPort")));
86      assertEquals("a.foo.bar:2888:3888", properties.get("server.0"));
87      assertEquals("b.foo.bar:2888:3888", properties.get("server.1"));
88      assertEquals("c.foo.bar:2888:3888", properties.get("server.2"));
89      assertEquals(null, properties.get("server.3"));
90      conf.set(HConstants.ZOOKEEPER_QUORUM, oldValue);
91    }
92  
93    @Test public void testConfigInjection() throws Exception {
94      String s =
95        "dataDir=" + this.dataDir.toString() + "\n" +
96        "clientPort=2181\n" +
97        "initLimit=2\n" +
98        "syncLimit=2\n" +
99        "server.0=${hbase.master.hostname}:2888:3888\n" +
100       "server.1=server1:2888:3888\n" +
101       "server.2=server2:2888:3888\n";
102 
103     System.setProperty("hbase.master.hostname", "localhost");
104     InputStream is = new ByteArrayInputStream(s.getBytes());
105     Configuration conf = TEST_UTIL.getConfiguration();
106     Properties properties = ZKConfig.parseZooCfg(conf, is);
107 
108     assertEquals(this.dataDir.toString(), properties.get("dataDir"));
109     assertEquals(Integer.valueOf(2181),
110       Integer.valueOf(properties.getProperty("clientPort")));
111     assertEquals("localhost:2888:3888", properties.get("server.0"));
112 
113     HQuorumPeer.writeMyID(properties);
114     QuorumPeerConfig config = new QuorumPeerConfig();
115     config.parseProperties(properties);
116 
117     assertEquals(this.dataDir.toString(), config.getDataDir().toString());
118     assertEquals(2181, config.getClientPortAddress().getPort());
119     Map<Long,QuorumServer> servers = config.getServers();
120     assertEquals(3, servers.size());
121     assertTrue(servers.containsKey(Long.valueOf(0)));
122     QuorumServer server = servers.get(Long.valueOf(0));
123     assertEquals("localhost", getHostName(server));
124 
125     // Override with system property.
126     System.setProperty("hbase.master.hostname", "foo.bar");
127     is = new ByteArrayInputStream(s.getBytes());
128     properties = ZKConfig.parseZooCfg(conf, is);
129     assertEquals("foo.bar:2888:3888", properties.get("server.0"));
130     config.parseProperties(properties);
131 
132     servers = config.getServers();
133     server = servers.get(Long.valueOf(0));
134     assertEquals("foo.bar", getHostName(server));
135   }
136 
137   // The reflection in this method is needed to smooth over internal differences
138   // between ZK 3.4 and 3.6. The type of 'addr' in 3.4 is InetSocketAddress. In
139   // 3.6 it becomes org.apache.zookeeper.server.quorum.MultipleAddresses, a set
140   // of InetSocketAddress.
141   private static String getHostName(QuorumServer server) throws Exception {
142     String hostname;
143     switch (server.addr.getClass().getName()) {
144       case "org.apache.zookeeper.server.quorum.MultipleAddresses":
145         // ZK 3.6 and up
146         Method m = server.addr.getClass().getDeclaredMethod("getOne");
147         hostname = ((InetSocketAddress)m.invoke(server.addr)).getHostName();
148         break;
149       default:
150         // ZK <= 3.5
151         Field f = server.getClass().getField("addr");
152         hostname = ((InetSocketAddress)f.get(server)).getHostName();
153         break;
154     }
155     return hostname;
156   }
157 
158   @Test public void testShouldAssignDefaultZookeeperClientPort() {
159     Configuration config = HBaseConfiguration.create();
160     config.clear();
161     Properties p = ZKConfig.makeZKProps(config);
162     assertNotNull(p);
163     assertEquals(2181, p.get("clientPort"));
164   }
165 
166   @Test
167   public void testGetZKQuorumServersString() {
168     Configuration config = new Configuration(TEST_UTIL.getConfiguration());
169     config.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, 8888);
170     config.set(HConstants.ZOOKEEPER_QUORUM, "foo:1234,bar:5678,baz,qux:9012");
171 
172     String s = ZKConfig.getZKQuorumServersString(config);
173     assertEquals("foo:1234,bar:5678,baz:8888,qux:9012", s);
174   }
175 }
176