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