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.Path;
30  import org.apache.hadoop.hbase.client.Get;
31  import org.apache.hadoop.hbase.client.HTable;
32  import org.apache.hadoop.hbase.client.Put;
33  import org.apache.hadoop.hbase.client.Result;
34  import org.apache.hadoop.hbase.testclassification.LargeTests;
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
37  import org.apache.hadoop.hdfs.MiniDFSCluster;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  /**
42   * Test our testing utility class
43   */
44  @Category(LargeTests.class)
45  public class TestHBaseTestingUtility {
46    private final Log LOG = LogFactory.getLog(this.getClass());
47  
48    /**
49     * Basic sanity test that spins up multiple HDFS and HBase clusters that share
50     * the same ZK ensemble. We then create the same table in both and make sure
51     * that what we insert in one place doesn't end up in the other.
52     * @throws Exception
53     */
54    @Test (timeout=180000)
55    public void testMultiClusters() throws Exception {
56      // Create three clusters
57  
58      // Cluster 1.
59      HBaseTestingUtility htu1 = new HBaseTestingUtility();
60      // Set a different zk path for each cluster
61      htu1.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
62      htu1.startMiniZKCluster();
63  
64      // Cluster 2
65      HBaseTestingUtility htu2 = new HBaseTestingUtility();
66      htu2.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
67      htu2.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
68        htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
69      htu2.setZkCluster(htu1.getZkCluster());
70  
71      // Cluster 3; seed it with the conf from htu1 so we pickup the 'right'
72      // zk cluster config; it is set back into the config. as part of the
73      // start of minizkcluster.
74      HBaseTestingUtility htu3 = new HBaseTestingUtility();
75      htu3.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/3");
76      htu3.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
77        htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
78      htu3.setZkCluster(htu1.getZkCluster());
79  
80      try {
81        htu1.startMiniCluster();
82        htu2.startMiniCluster();
83        htu3.startMiniCluster();
84  
85        final byte[] TABLE_NAME = Bytes.toBytes("test");
86        final byte[] FAM_NAME = Bytes.toBytes("fam");
87        final byte[] ROW = Bytes.toBytes("row");
88        final byte[] QUAL_NAME = Bytes.toBytes("qual");
89        final byte[] VALUE = Bytes.toBytes("value");
90  
91        HTable table1 = htu1.createTable(TABLE_NAME, FAM_NAME);
92        HTable table2 = htu2.createTable(TABLE_NAME, FAM_NAME);
93  
94        Put put = new Put(ROW);
95        put.add(FAM_NAME, QUAL_NAME, VALUE);
96        table1.put(put);
97  
98        Get get = new Get(ROW);
99        get.addColumn(FAM_NAME, QUAL_NAME);
100       Result res = table1.get(get);
101       assertEquals(1, res.size());
102 
103       res = table2.get(get);
104       assertEquals(0, res.size());
105 
106       table1.close();
107       table2.close();
108 
109     } finally {
110       htu3.shutdownMiniCluster();
111       htu2.shutdownMiniCluster();
112       htu1.shutdownMiniCluster();
113     }
114   }
115 
116   @Test public void testMiniCluster() throws Exception {
117     HBaseTestingUtility hbt = new HBaseTestingUtility();
118 
119     MiniHBaseCluster cluster = hbt.startMiniCluster();
120     try {
121       assertEquals(1, cluster.getLiveRegionServerThreads().size());
122     } finally {
123       hbt.shutdownMiniCluster();
124     }
125   }
126 
127   /**
128    *  Test that we can start and stop multiple time a cluster
129    *   with the same HBaseTestingUtility.
130    */
131   @Test public void testMultipleStartStop() throws Exception{
132     HBaseTestingUtility htu1 = new HBaseTestingUtility();
133     Path foo = new Path("foo");
134 
135     htu1.startMiniCluster();
136     htu1.getDFSCluster().getFileSystem().create(foo);
137     assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
138     htu1.shutdownMiniCluster();
139 
140     htu1.startMiniCluster();
141     assertFalse( htu1.getDFSCluster().getFileSystem().exists(foo));
142     htu1.getDFSCluster().getFileSystem().create(foo);
143     assertTrue( htu1.getDFSCluster().getFileSystem().exists(foo));
144     htu1.shutdownMiniCluster();
145   }
146 
147 
148   @Test public void testMiniZooKeeper() throws Exception {
149     HBaseTestingUtility hbt = new HBaseTestingUtility();
150     MiniZooKeeperCluster cluster1 = hbt.startMiniZKCluster();
151     try {
152       assertEquals(0, cluster1.getBackupZooKeeperServerNum());
153       assertTrue((cluster1.killCurrentActiveZooKeeperServer() == -1));
154     } finally {
155       hbt.shutdownMiniZKCluster();
156     }
157 
158     // set up zookeeper cluster with 5 zk servers
159     MiniZooKeeperCluster cluster2 = hbt.startMiniZKCluster(5);
160     int defaultClientPort = 21818;
161     cluster2.setDefaultClientPort(defaultClientPort);
162     try {
163       assertEquals(4, cluster2.getBackupZooKeeperServerNum());
164 
165       // killing the current active zk server
166       assertTrue((cluster2.killCurrentActiveZooKeeperServer() >= defaultClientPort));
167       assertTrue((cluster2.killCurrentActiveZooKeeperServer() >= defaultClientPort));
168       assertEquals(2, cluster2.getBackupZooKeeperServerNum());
169       assertEquals(3, cluster2.getZooKeeperServerNum());
170 
171       // killing the backup zk servers
172       cluster2.killOneBackupZooKeeperServer();
173       cluster2.killOneBackupZooKeeperServer();
174       assertEquals(0, cluster2.getBackupZooKeeperServerNum());
175       assertEquals(1, cluster2.getZooKeeperServerNum());
176 
177       // killing the last zk server
178       assertTrue((cluster2.killCurrentActiveZooKeeperServer() == -1));
179       // this should do nothing.
180       cluster2.killOneBackupZooKeeperServer();
181       assertEquals(-1, cluster2.getBackupZooKeeperServerNum());
182       assertEquals(0, cluster2.getZooKeeperServerNum());
183     } finally {
184       hbt.shutdownMiniZKCluster();
185     }
186   }
187 
188   @Test public void testMiniDFSCluster() throws Exception {
189     HBaseTestingUtility hbt = new HBaseTestingUtility();
190     MiniDFSCluster cluster = hbt.startMiniDFSCluster(null);
191     FileSystem dfs = cluster.getFileSystem();
192     Path dir = new Path("dir");
193     Path qualifiedDir = dfs.makeQualified(dir);
194     LOG.info("dir=" + dir + ", qualifiedDir=" + qualifiedDir);
195     assertFalse(dfs.exists(qualifiedDir));
196     assertTrue(dfs.mkdirs(qualifiedDir));
197     assertTrue(dfs.delete(qualifiedDir, true));
198     hbt.shutdownMiniCluster();
199   }
200 
201   @Test public void testSetupClusterTestBuildDir() throws Exception {
202     HBaseTestingUtility hbt = new HBaseTestingUtility();
203     Path testdir = hbt.getClusterTestDir();
204     LOG.info("uuid-subdir=" + testdir);
205     FileSystem fs = hbt.getTestFileSystem();
206 
207     assertFalse(fs.exists(testdir));
208 
209     hbt.startMiniDFSCluster(null);
210     assertTrue(fs.exists(testdir));
211 
212     hbt.shutdownMiniCluster();
213     assertFalse(fs.exists(testdir));
214   }
215 
216   @Test public void testTestDir() throws Exception {
217     HBaseTestingUtility hbt = new HBaseTestingUtility();
218     Path testdir = hbt.getDataTestDir();
219     LOG.info("testdir=" + testdir);
220     FileSystem fs = hbt.getTestFileSystem();
221     assertTrue(!fs.exists(testdir));
222     assertTrue(fs.mkdirs(testdir));
223     assertTrue(hbt.cleanupTestDir());
224   }
225 
226 }
227