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;
20  
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.FileUtil;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.client.Get;
32  import org.apache.hadoop.hbase.client.Put;
33  import org.apache.hadoop.hbase.client.Result;
34  import org.apache.hadoop.hbase.client.Table;
35  import org.apache.hadoop.hbase.testclassification.LargeTests;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
38  import org.apache.hadoop.hdfs.MiniDFSCluster;
39  import org.apache.hadoop.hbase.http.ssl.KeyStoreTestUtil;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  import java.io.File;
44  import java.util.List;
45  
46  /**
47   * Test our testing utility class
48   */
49  @Category(LargeTests.class)
50  public class TestHBaseTestingUtility {
51    private static final Log LOG = LogFactory.getLog(TestHBaseTestingUtility.class);
52  
53    /**
54     * Basic sanity test that spins up multiple HDFS and HBase clusters that share
55     * the same ZK ensemble. We then create the same table in both and make sure
56     * that what we insert in one place doesn't end up in the other.
57     * @throws Exception
58     */
59    @Test (timeout=180000)
60    public void testMultiClusters() throws Exception {
61      // Create three clusters
62  
63      // Cluster 1.
64      HBaseTestingUtility htu1 = new HBaseTestingUtility();
65      // Set a different zk path for each cluster
66      htu1.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
67      htu1.startMiniZKCluster();
68  
69      // Cluster 2
70      HBaseTestingUtility htu2 = new HBaseTestingUtility();
71      htu2.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
72      htu2.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
73        htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
74      htu2.setZkCluster(htu1.getZkCluster());
75  
76      // Cluster 3; seed it with the conf from htu1 so we pickup the 'right'
77      // zk cluster config; it is set back into the config. as part of the
78      // start of minizkcluster.
79      HBaseTestingUtility htu3 = new HBaseTestingUtility();
80      htu3.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/3");
81      htu3.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
82        htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
83      htu3.setZkCluster(htu1.getZkCluster());
84  
85      try {
86        htu1.startMiniCluster();
87        htu2.startMiniCluster();
88        htu3.startMiniCluster();
89  
90        final TableName TABLE_NAME = TableName.valueOf("test");
91        final byte[] FAM_NAME = Bytes.toBytes("fam");
92        final byte[] ROW = Bytes.toBytes("row");
93        final byte[] QUAL_NAME = Bytes.toBytes("qual");
94        final byte[] VALUE = Bytes.toBytes("value");
95  
96        Table table1 = htu1.createTable(TABLE_NAME, FAM_NAME);
97        Table table2 = htu2.createTable(TABLE_NAME, FAM_NAME);
98  
99        Put put = new Put(ROW);
100       put.add(FAM_NAME, QUAL_NAME, VALUE);
101       table1.put(put);
102 
103       Get get = new Get(ROW);
104       get.addColumn(FAM_NAME, QUAL_NAME);
105       Result res = table1.get(get);
106       assertEquals(1, res.size());
107 
108       res = table2.get(get);
109       assertEquals(0, res.size());
110 
111       table1.close();
112       table2.close();
113 
114     } finally {
115       htu3.shutdownMiniCluster();
116       htu2.shutdownMiniCluster();
117       htu1.shutdownMiniCluster();
118     }
119   }
120 
121   @Test public void testMiniCluster() throws Exception {
122     HBaseTestingUtility hbt = new HBaseTestingUtility();
123 
124     MiniHBaseCluster cluster = hbt.startMiniCluster();
125     try {
126       assertEquals(1, cluster.getLiveRegionServerThreads().size());
127     } finally {
128       hbt.shutdownMiniCluster();
129     }
130   }
131 
132   @Test
133   public void testMiniClusterBindToWildcard() throws Exception {
134     HBaseTestingUtility hbt = new HBaseTestingUtility();
135     hbt.getConfiguration().set("hbase.regionserver.ipc.address", "0.0.0.0");
136     MiniHBaseCluster cluster = hbt.startMiniCluster();
137     try {
138       assertEquals(1, cluster.getLiveRegionServerThreads().size());
139     } finally {
140       hbt.shutdownMiniCluster();
141     }
142   }
143 
144   @Test
145   public void testMiniClusterWithSSLOn() throws Exception {
146     final String BASEDIR = System.getProperty("test.build.dir",
147         "target/test-dir") + "/" + TestHBaseTestingUtility.class.getSimpleName();
148     String sslConfDir = KeyStoreTestUtil.getClasspathDir(TestHBaseTestingUtility.class);
149     String keystoresDir = new File(BASEDIR).getAbsolutePath();
150 
151     HBaseTestingUtility hbt = new HBaseTestingUtility();
152     File base = new File(BASEDIR);
153     FileUtil.fullyDelete(base);
154     base.mkdirs();
155 
156     KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, hbt.getConfiguration(), false);
157 
158     hbt.getConfiguration().set("hbase.ssl.enabled", "true");
159     hbt.getConfiguration().addResource("ssl-server.xml");
160     hbt.getConfiguration().addResource("ssl-client.xml");
161 
162     MiniHBaseCluster cluster = hbt.startMiniCluster();
163     try {
164       assertEquals(1, cluster.getLiveRegionServerThreads().size());
165     } finally {
166       hbt.shutdownMiniCluster();
167     }
168   }
169 
170   /**
171    *  Test that we can start and stop multiple time a cluster
172    *   with the same HBaseTestingUtility.
173    */
174   @Test public void testMultipleStartStop() throws Exception{
175     HBaseTestingUtility htu1 = new HBaseTestingUtility();
176     Path foo = new Path("foo");
177 
178     htu1.startMiniCluster();
179     htu1.getDFSCluster().getFileSystem().create(foo);
180     assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
181     htu1.shutdownMiniCluster();
182 
183     htu1.startMiniCluster();
184     assertFalse( htu1.getDFSCluster().getFileSystem().exists(foo));
185     htu1.getDFSCluster().getFileSystem().create(foo);
186     assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
187     htu1.shutdownMiniCluster();
188   }
189 
190   @Test
191   public void testMiniZooKeeperWithOneServer() throws Exception {
192     HBaseTestingUtility hbt = new HBaseTestingUtility();
193     MiniZooKeeperCluster cluster1 = hbt.startMiniZKCluster();
194     try {
195       assertEquals(0, cluster1.getBackupZooKeeperServerNum());
196       assertTrue((cluster1.killCurrentActiveZooKeeperServer() == -1));
197     } finally {
198       hbt.shutdownMiniZKCluster();
199     }
200   }
201 
202   @Test
203   public void testMiniZooKeeperWithMultipleServers() throws Exception {
204     HBaseTestingUtility hbt = new HBaseTestingUtility();
205     // set up zookeeper cluster with 5 zk servers
206     MiniZooKeeperCluster cluster2 = hbt.startMiniZKCluster(5);
207     int defaultClientPort = 21818;
208     cluster2.setDefaultClientPort(defaultClientPort);
209     try {
210       assertEquals(4, cluster2.getBackupZooKeeperServerNum());
211 
212       // killing the current active zk server
213       int currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
214       assertTrue(currentActivePort >= defaultClientPort);
215       // Check if the client port is returning a proper value
216       assertTrue(cluster2.getClientPort() == currentActivePort);
217 
218       // kill another active zk server
219       currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
220       assertTrue(currentActivePort >= defaultClientPort);
221       assertTrue(cluster2.getClientPort() == currentActivePort);      
222       assertEquals(2, cluster2.getBackupZooKeeperServerNum());
223       assertEquals(3, cluster2.getZooKeeperServerNum());
224 
225       // killing the backup zk servers
226       cluster2.killOneBackupZooKeeperServer();
227       cluster2.killOneBackupZooKeeperServer();
228       assertEquals(0, cluster2.getBackupZooKeeperServerNum());
229       assertEquals(1, cluster2.getZooKeeperServerNum());
230 
231       // killing the last zk server
232       currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
233       assertTrue(currentActivePort == -1);
234       assertTrue(cluster2.getClientPort() == currentActivePort);
235       // this should do nothing.
236       cluster2.killOneBackupZooKeeperServer();
237       assertEquals(-1, cluster2.getBackupZooKeeperServerNum());
238       assertEquals(0, cluster2.getZooKeeperServerNum());
239     } finally {
240       hbt.shutdownMiniZKCluster();
241     }
242   }
243 
244   @Test
245   public void testMiniZooKeeperWithMultipleClientPorts() throws Exception {
246     int defaultClientPort = 8888;
247     int i, j;
248     HBaseTestingUtility hbt = new HBaseTestingUtility();
249 
250     // Test 1 - set up zookeeper cluster with same number of ZK servers and specified client ports
251     int [] clientPortList1 = {1111, 1112, 1113};
252     MiniZooKeeperCluster cluster1 = hbt.startMiniZKCluster(clientPortList1.length, clientPortList1);
253     try {
254       List<Integer> clientPortListInCluster = cluster1.getClientPortList();
255 
256       for (i = 0; i < clientPortListInCluster.size(); i++) {
257         assertEquals(clientPortListInCluster.get(i).intValue(), clientPortList1[i]);
258       }
259     } finally {
260       hbt.shutdownMiniZKCluster();
261     }
262 
263     // Test 2 - set up zookeeper cluster with more ZK servers than specified client ports
264     hbt.getConfiguration().setInt("test.hbase.zookeeper.property.clientPort", defaultClientPort);
265     int [] clientPortList2 = {2222, 2223};
266     MiniZooKeeperCluster cluster2 =
267         hbt.startMiniZKCluster(clientPortList2.length + 2, clientPortList2);
268 
269     try {
270       List<Integer> clientPortListInCluster = cluster2.getClientPortList();
271 
272       for (i = 0, j = 0; i < clientPortListInCluster.size(); i++) {
273         if (i < clientPortList2.length) {
274           assertEquals(clientPortListInCluster.get(i).intValue(), clientPortList2[i]);
275         } else {
276           // servers with no specified client port will use defaultClientPort or some other ports
277           // based on defaultClientPort
278           assertEquals(clientPortListInCluster.get(i).intValue(), defaultClientPort + j);
279           j++;
280         }
281       }
282     } finally {
283       hbt.shutdownMiniZKCluster();
284     }
285 
286     // Test 3 - set up zookeeper cluster with invalid client ports
287     hbt.getConfiguration().setInt("test.hbase.zookeeper.property.clientPort", defaultClientPort);
288     int [] clientPortList3 = {3333, -3334, 3335, 0};
289     MiniZooKeeperCluster cluster3 =
290         hbt.startMiniZKCluster(clientPortList3.length + 1, clientPortList3);
291 
292     try {
293       List<Integer> clientPortListInCluster = cluster3.getClientPortList();
294 
295       for (i = 0, j = 0; i < clientPortListInCluster.size(); i++) {
296         // Servers will only use valid client ports; if ports are not specified or invalid,
297         // the default port or a port based on default port will be used.
298         if (i < clientPortList3.length && clientPortList3[i] > 0) {
299           assertEquals(clientPortListInCluster.get(i).intValue(), clientPortList3[i]);
300         } else {
301           assertEquals(clientPortListInCluster.get(i).intValue(), defaultClientPort + j);
302           j++;
303         }
304       }
305     } finally {
306       hbt.shutdownMiniZKCluster();
307     }
308 
309     // Test 4 - set up zookeeper cluster with default port and some other ports used
310     // This test tests that the defaultClientPort and defaultClientPort+2 are used, so
311     // the algorithm should choice defaultClientPort+1 and defaultClientPort+3 to fill
312     // out the ports for servers without ports specified.
313     hbt.getConfiguration().setInt("test.hbase.zookeeper.property.clientPort", defaultClientPort);
314     int [] clientPortList4 = {-4444, defaultClientPort+2, 4446, defaultClientPort};
315     MiniZooKeeperCluster cluster4 =
316         hbt.startMiniZKCluster(clientPortList4.length + 1, clientPortList4);
317 
318     try {
319       List<Integer> clientPortListInCluster = cluster4.getClientPortList();
320 
321       for (i = 0, j = 1; i < clientPortListInCluster.size(); i++) {
322         // Servers will only use valid client ports; if ports are not specified or invalid,
323         // the default port or a port based on default port will be used.
324         if (i < clientPortList4.length && clientPortList4[i] > 0) {
325           assertEquals(clientPortListInCluster.get(i).intValue(), clientPortList4[i]);
326         } else {
327           assertEquals(clientPortListInCluster.get(i).intValue(), defaultClientPort + j);
328           j +=2;
329         }
330       }
331     } finally {
332       hbt.shutdownMiniZKCluster();
333     }
334 
335     // Test 5 - set up zookeeper cluster with same ports specified - fail is expected.
336     int [] clientPortList5 = {5555, 5556, 5556};
337 
338     try {
339       MiniZooKeeperCluster cluster5 =
340           hbt.startMiniZKCluster(clientPortList5.length, clientPortList5);
341       assertTrue(cluster5.getClientPort() == -1); // expected failure
342     } catch (Exception e) {
343       // exception is acceptable
344     } finally {
345       hbt.shutdownMiniZKCluster();
346     }
347   }
348 
349   @Test public void testMiniDFSCluster() throws Exception {
350     HBaseTestingUtility hbt = new HBaseTestingUtility();
351     MiniDFSCluster cluster = hbt.startMiniDFSCluster(null);
352     FileSystem dfs = cluster.getFileSystem();
353     Path dir = new Path("dir");
354     Path qualifiedDir = dfs.makeQualified(dir);
355     LOG.info("dir=" + dir + ", qualifiedDir=" + qualifiedDir);
356     assertFalse(dfs.exists(qualifiedDir));
357     assertTrue(dfs.mkdirs(qualifiedDir));
358     assertTrue(dfs.delete(qualifiedDir, true));
359     hbt.shutdownMiniCluster();
360   }
361 
362   @Test public void testSetupClusterTestBuildDir() throws Exception {
363     HBaseTestingUtility hbt = new HBaseTestingUtility();
364     Path testdir = hbt.getClusterTestDir();
365     LOG.info("uuid-subdir=" + testdir);
366     FileSystem fs = hbt.getTestFileSystem();
367 
368     assertFalse(fs.exists(testdir));
369 
370     hbt.startMiniDFSCluster(null);
371     assertTrue(fs.exists(testdir));
372 
373     hbt.shutdownMiniCluster();
374     assertFalse(fs.exists(testdir));
375   }
376 
377   @Test public void testTestDir() throws Exception {
378     HBaseTestingUtility hbt = new HBaseTestingUtility();
379     Path testdir = hbt.getDataTestDir();
380     LOG.info("testdir=" + testdir);
381     FileSystem fs = hbt.getTestFileSystem();
382     assertTrue(!fs.exists(testdir));
383     assertTrue(fs.mkdirs(testdir));
384     assertTrue(hbt.cleanupTestDir());
385   }
386 
387 }
388