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.IOException;
24  import java.io.InputStream;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.fs.FileSystem;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.HBaseConfiguration;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.HConstants;
34  import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
35  import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
36  import org.junit.Before;
37  import org.junit.Test;
38  
39  import static junit.framework.Assert.assertEquals;
40  import static org.junit.Assert.*;
41  
42  /**
43   * Test for HQuorumPeer.
44   */
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("hbase.zookeeper.property.clientPort",
54        PORT_NO);
55      this.dataDir = HBaseTestingUtility.getTestDir(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("hbase.zookeeper.property.dataDir", 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   /**
136    * Test Case for HBASE-2305
137    */
138   @Test public void testShouldAssignDefaultZookeeperClientPort() {
139     Configuration config = HBaseConfiguration.create();
140     config.clear();
141     Properties p = ZKConfig.makeZKProps(config);
142     assertNotNull(p);
143     assertEquals(2181, p.get("hbase.zookeeper.property.clientPort"));
144   }
145 }