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