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