HBASE-26567 Remove IndexType from ChunkCreator (#3947)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / TestHBaseTestingUtil.java
blob7076bf936126cfbad5fcf3311195e576a77fc125
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.ArgumentMatchers.anyInt;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
28 import java.io.File;
29 import java.util.List;
30 import java.util.Random;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.fs.FileSystem;
33 import org.apache.hadoop.fs.FileUtil;
34 import org.apache.hadoop.fs.Path;
35 import org.apache.hadoop.hbase.client.Get;
36 import org.apache.hadoop.hbase.client.Put;
37 import org.apache.hadoop.hbase.client.Result;
38 import org.apache.hadoop.hbase.client.Table;
39 import org.apache.hadoop.hbase.http.ssl.KeyStoreTestUtil;
40 import org.apache.hadoop.hbase.testclassification.LargeTests;
41 import org.apache.hadoop.hbase.testclassification.MiscTests;
42 import org.apache.hadoop.hbase.util.Bytes;
43 import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
44 import org.apache.hadoop.hdfs.MiniDFSCluster;
45 import org.apache.hadoop.security.ssl.SSLFactory;
46 import org.junit.ClassRule;
47 import org.junit.Rule;
48 import org.junit.Test;
49 import org.junit.experimental.categories.Category;
50 import org.junit.rules.TestName;
51 import org.mockito.Mockito;
52 import org.mockito.invocation.InvocationOnMock;
53 import org.mockito.stubbing.Answer;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 /**
58 * Test our testing utility class
60 @Category({MiscTests.class, LargeTests.class})
61 public class TestHBaseTestingUtil {
62 private static final int NUMTABLES = 1;
63 private static final int NUMROWS = 100;
64 private static final int NUMREGIONS = 10;
66 @ClassRule
67 public static final HBaseClassTestRule CLASS_RULE =
68 HBaseClassTestRule.forClass(TestHBaseTestingUtil.class);
70 private static final Logger LOG = LoggerFactory.getLogger(TestHBaseTestingUtil.class);
72 @Rule
73 public TestName name = new TestName();
75 /**
76 * Basic sanity test that spins up multiple HDFS and HBase clusters that share
77 * the same ZK ensemble. We then create the same table in both and make sure
78 * that what we insert in one place doesn't end up in the other.
79 * @throws Exception on error
81 @Test
82 public void testMultiClusters() throws Exception {
83 // Create three clusters
85 // Cluster 1.
86 HBaseTestingUtil htu1 = new HBaseTestingUtil();
87 // Set a different zk path for each cluster
88 htu1.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
89 htu1.startMiniZKCluster();
91 // Cluster 2
92 HBaseTestingUtil htu2 = new HBaseTestingUtil();
93 htu2.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
94 htu2.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
95 htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
96 htu2.setZkCluster(htu1.getZkCluster());
98 // Cluster 3; seed it with the conf from htu1 so we pickup the 'right'
99 // zk cluster config; it is set back into the config. as part of the
100 // start of minizkcluster.
101 HBaseTestingUtil htu3 = new HBaseTestingUtil();
102 htu3.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/3");
103 htu3.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
104 htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
105 htu3.setZkCluster(htu1.getZkCluster());
107 try {
108 htu1.startMiniCluster();
109 htu2.startMiniCluster();
110 htu3.startMiniCluster();
112 final TableName tableName = TableName.valueOf(name.getMethodName());
113 final byte[] FAM_NAME = Bytes.toBytes("fam");
114 final byte[] ROW = Bytes.toBytes("row");
115 final byte[] QUAL_NAME = Bytes.toBytes("qual");
116 final byte[] VALUE = Bytes.toBytes("value");
118 Table table1 = htu1.createTable(tableName, FAM_NAME);
119 Table table2 = htu2.createTable(tableName, FAM_NAME);
121 Put put = new Put(ROW);
122 put.addColumn(FAM_NAME, QUAL_NAME, VALUE);
123 table1.put(put);
125 Get get = new Get(ROW);
126 get.addColumn(FAM_NAME, QUAL_NAME);
127 Result res = table1.get(get);
128 assertEquals(1, res.size());
130 res = table2.get(get);
131 assertEquals(0, res.size());
133 table1.close();
134 table2.close();
136 } finally {
137 htu3.shutdownMiniCluster();
138 htu2.shutdownMiniCluster();
139 htu1.shutdownMiniCluster();
143 @Test public void testMiniCluster() throws Exception {
144 HBaseTestingUtil hbt = new HBaseTestingUtil();
146 SingleProcessHBaseCluster cluster = hbt.startMiniCluster();
147 try {
148 assertEquals(1, cluster.getLiveRegionServerThreads().size());
149 } finally {
150 hbt.shutdownMiniCluster();
154 @Test
155 public void testMiniClusterBindToWildcard() throws Exception {
156 HBaseTestingUtil hbt = new HBaseTestingUtil();
157 hbt.getConfiguration().set("hbase.regionserver.ipc.address", "0.0.0.0");
158 SingleProcessHBaseCluster cluster = hbt.startMiniCluster();
159 try {
160 assertEquals(1, cluster.getLiveRegionServerThreads().size());
161 } finally {
162 hbt.shutdownMiniCluster();
166 @Test
167 public void testMiniClusterWithSSLOn() throws Exception {
168 final String BASEDIR = System.getProperty("test.build.dir",
169 "target/test-dir") + "/" + TestHBaseTestingUtil.class.getSimpleName();
170 String sslConfDir = KeyStoreTestUtil.getClasspathDir(TestHBaseTestingUtil.class);
171 String keystoresDir = new File(BASEDIR).getAbsolutePath();
173 HBaseTestingUtil hbt = new HBaseTestingUtil();
174 File base = new File(BASEDIR);
175 FileUtil.fullyDelete(base);
176 base.mkdirs();
178 KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, hbt.getConfiguration(), false);
180 hbt.getConfiguration().set("hbase.ssl.enabled", "true");
181 hbt.getConfiguration().addResource(hbt.getConfiguration().get(SSLFactory.SSL_CLIENT_CONF_KEY));
182 hbt.getConfiguration().addResource(hbt.getConfiguration().get(SSLFactory.SSL_SERVER_CONF_KEY));
184 SingleProcessHBaseCluster cluster = hbt.startMiniCluster();
185 try {
186 assertEquals(1, cluster.getLiveRegionServerThreads().size());
187 } finally {
188 hbt.shutdownMiniCluster();
193 * Test that we can start and stop multiple time a cluster
194 * with the same HBaseTestingUtility.
196 @Test public void testMultipleStartStop() throws Exception{
197 HBaseTestingUtil htu1 = new HBaseTestingUtil();
198 Path foo = new Path("foo");
200 htu1.startMiniCluster();
201 htu1.getDFSCluster().getFileSystem().create(foo);
202 assertTrue(htu1.getDFSCluster().getFileSystem().exists(foo));
203 htu1.shutdownMiniCluster();
205 htu1.startMiniCluster();
206 assertFalse(htu1.getDFSCluster().getFileSystem().exists(foo));
207 htu1.getDFSCluster().getFileSystem().create(foo);
208 assertTrue(htu1.getDFSCluster().getFileSystem().exists(foo));
209 htu1.shutdownMiniCluster();
212 @Test
213 public void testMiniZooKeeperWithOneServer() throws Exception {
214 HBaseTestingUtil hbt = new HBaseTestingUtil();
215 MiniZooKeeperCluster cluster1 = hbt.startMiniZKCluster();
216 try {
217 assertEquals(0, cluster1.getBackupZooKeeperServerNum());
218 assertTrue((cluster1.killCurrentActiveZooKeeperServer() == -1));
219 } finally {
220 hbt.shutdownMiniZKCluster();
224 @Test
225 public void testMiniZooKeeperWithMultipleServers() throws Exception {
226 HBaseTestingUtil hbt = new HBaseTestingUtil();
227 // set up zookeeper cluster with 5 zk servers
228 MiniZooKeeperCluster cluster2 = hbt.startMiniZKCluster(5);
229 int defaultClientPort = 21818;
230 cluster2.setDefaultClientPort(defaultClientPort);
231 try {
232 assertEquals(4, cluster2.getBackupZooKeeperServerNum());
234 // killing the current active zk server
235 int currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
236 assertTrue(currentActivePort >= defaultClientPort);
237 // Check if the client port is returning a proper value
238 assertTrue(cluster2.getClientPort() == currentActivePort);
240 // kill another active zk server
241 currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
242 assertTrue(currentActivePort >= defaultClientPort);
243 assertTrue(cluster2.getClientPort() == currentActivePort);
244 assertEquals(2, cluster2.getBackupZooKeeperServerNum());
245 assertEquals(3, cluster2.getZooKeeperServerNum());
247 // killing the backup zk servers
248 cluster2.killOneBackupZooKeeperServer();
249 cluster2.killOneBackupZooKeeperServer();
250 assertEquals(0, cluster2.getBackupZooKeeperServerNum());
251 assertEquals(1, cluster2.getZooKeeperServerNum());
253 // killing the last zk server
254 currentActivePort = cluster2.killCurrentActiveZooKeeperServer();
255 assertTrue(currentActivePort == -1);
256 assertTrue(cluster2.getClientPort() == currentActivePort);
257 // this should do nothing.
258 cluster2.killOneBackupZooKeeperServer();
259 assertEquals(-1, cluster2.getBackupZooKeeperServerNum());
260 assertEquals(0, cluster2.getZooKeeperServerNum());
261 } finally {
262 hbt.shutdownMiniZKCluster();
266 @Test
267 public void testMiniZooKeeperWithMultipleClientPorts() throws Exception {
268 int defaultClientPort = 8888;
269 int i, j;
270 HBaseTestingUtil hbt = new HBaseTestingUtil();
272 // Test 1 - set up zookeeper cluster with same number of ZK servers and specified client ports
273 int [] clientPortList1 = {1111, 1112, 1113};
274 MiniZooKeeperCluster cluster1 = hbt.startMiniZKCluster(clientPortList1.length, clientPortList1);
275 try {
276 List<Integer> clientPortListInCluster = cluster1.getClientPortList();
278 for (i = 0; i < clientPortListInCluster.size(); i++) {
279 // cannot assert the specific port due to the port conflict in which situation
280 // it always chooses a bigger port by +1. The below is the same.
281 assertTrue(clientPortListInCluster.get(i).intValue() >= clientPortList1[i]);
283 } finally {
284 hbt.shutdownMiniZKCluster();
287 // Test 2 - set up zookeeper cluster with more ZK servers than specified client ports
288 hbt.getConfiguration().setInt("test.hbase.zookeeper.property.clientPort", defaultClientPort);
289 int [] clientPortList2 = {2222, 2223};
290 MiniZooKeeperCluster cluster2 =
291 hbt.startMiniZKCluster(clientPortList2.length + 2, clientPortList2);
293 try {
294 List<Integer> clientPortListInCluster = cluster2.getClientPortList();
296 for (i = 0, j = 0; i < clientPortListInCluster.size(); i++) {
297 if (i < clientPortList2.length) {
298 assertTrue(clientPortListInCluster.get(i).intValue() >= clientPortList2[i]);
299 } else {
300 // servers with no specified client port will use defaultClientPort or some other ports
301 // based on defaultClientPort
302 assertTrue(clientPortListInCluster.get(i).intValue() >= defaultClientPort + j);
303 j++;
306 } finally {
307 hbt.shutdownMiniZKCluster();
310 // Test 3 - set up zookeeper cluster with invalid client ports
311 hbt.getConfiguration().setInt("test.hbase.zookeeper.property.clientPort", defaultClientPort);
312 int [] clientPortList3 = {3333, -3334, 3335, 0};
313 MiniZooKeeperCluster cluster3 =
314 hbt.startMiniZKCluster(clientPortList3.length + 1, clientPortList3);
316 try {
317 List<Integer> clientPortListInCluster = cluster3.getClientPortList();
319 for (i = 0, j = 0; i < clientPortListInCluster.size(); i++) {
320 // Servers will only use valid client ports; if ports are not specified or invalid,
321 // the default port or a port based on default port will be used.
322 if (i < clientPortList3.length && clientPortList3[i] > 0) {
323 assertTrue(clientPortListInCluster.get(i).intValue() >= clientPortList3[i]);
324 } else {
325 assertTrue(clientPortListInCluster.get(i).intValue() >= defaultClientPort + j);
326 j++;
329 } finally {
330 hbt.shutdownMiniZKCluster();
333 // Test 4 - set up zookeeper cluster with default port and some other ports used
334 // This test tests that the defaultClientPort and defaultClientPort+2 are used, so
335 // the algorithm should choice defaultClientPort+1 and defaultClientPort+3 to fill
336 // out the ports for servers without ports specified.
337 hbt.getConfiguration().setInt("test.hbase.zookeeper.property.clientPort", defaultClientPort);
338 int [] clientPortList4 = {-4444, defaultClientPort+2, 4446, defaultClientPort};
339 MiniZooKeeperCluster cluster4 =
340 hbt.startMiniZKCluster(clientPortList4.length + 1, clientPortList4);
342 try {
343 List<Integer> clientPortListInCluster = cluster4.getClientPortList();
345 for (i = 0, j = 1; i < clientPortListInCluster.size(); i++) {
346 // Servers will only use valid client ports; if ports are not specified or invalid,
347 // the default port or a port based on default port will be used.
348 if (i < clientPortList4.length && clientPortList4[i] > 0) {
349 assertTrue(clientPortListInCluster.get(i).intValue() >= clientPortList4[i]);
350 } else {
351 assertTrue(clientPortListInCluster.get(i).intValue() >= defaultClientPort + j);
352 j +=2;
355 } finally {
356 hbt.shutdownMiniZKCluster();
359 // Test 5 - set up zookeeper cluster with same ports specified - fail is expected.
360 int [] clientPortList5 = {5555, 5556, 5556};
362 try {
363 MiniZooKeeperCluster cluster5 =
364 hbt.startMiniZKCluster(clientPortList5.length, clientPortList5);
365 assertTrue(cluster5.getClientPort() == -1); // expected failure
366 } catch (Exception e) {
367 // exception is acceptable
368 } finally {
369 hbt.shutdownMiniZKCluster();
373 @Test public void testMiniDFSCluster() throws Exception {
374 HBaseTestingUtil hbt = new HBaseTestingUtil();
375 MiniDFSCluster cluster = hbt.startMiniDFSCluster(null);
376 FileSystem dfs = cluster.getFileSystem();
377 Path dir = new Path("dir");
378 Path qualifiedDir = dfs.makeQualified(dir);
379 LOG.info("dir=" + dir + ", qualifiedDir=" + qualifiedDir);
380 assertFalse(dfs.exists(qualifiedDir));
381 assertTrue(dfs.mkdirs(qualifiedDir));
382 assertTrue(dfs.delete(qualifiedDir, true));
383 hbt.shutdownMiniCluster();
386 @Test public void testSetupClusterTestBuildDir() throws Exception {
387 HBaseTestingUtil hbt = new HBaseTestingUtil();
388 Path testdir = hbt.getClusterTestDir();
389 LOG.info("uuid-subdir=" + testdir);
390 FileSystem fs = hbt.getTestFileSystem();
392 assertFalse(fs.exists(testdir));
394 hbt.startMiniDFSCluster(null);
395 assertTrue(fs.exists(testdir));
397 hbt.shutdownMiniCluster();
398 assertFalse(fs.exists(testdir));
401 @Test public void testTestDir() throws Exception {
402 HBaseTestingUtil hbt = new HBaseTestingUtil();
403 Path testdir = hbt.getDataTestDir();
404 LOG.info("testdir=" + testdir);
405 FileSystem fs = hbt.getTestFileSystem();
406 assertTrue(!fs.exists(testdir));
407 assertTrue(fs.mkdirs(testdir));
408 assertTrue(hbt.cleanupTestDir());
411 @Test public void testResolvePortConflict() throws Exception {
412 // raises port conflict between 1st call and 2nd call of randomPort() by mocking Random object
413 Random random = mock(Random.class);
414 when(random.nextInt(anyInt()))
415 .thenAnswer(new Answer<Integer>() {
416 int[] numbers = { 1, 1, 2 };
417 int count = 0;
419 @Override
420 public Integer answer(InvocationOnMock invocation) {
421 int ret = numbers[count];
422 count++;
423 return ret;
427 HBaseTestingUtil.PortAllocator.AvailablePortChecker portChecker =
428 mock(HBaseTestingUtil.PortAllocator.AvailablePortChecker.class);
429 when(portChecker.available(anyInt())).thenReturn(true);
431 HBaseTestingUtil.PortAllocator portAllocator =
432 new HBaseTestingUtil.PortAllocator(random, portChecker);
434 int port1 = portAllocator.randomFreePort();
435 int port2 = portAllocator.randomFreePort();
436 assertNotEquals(port1, port2);
437 Mockito.verify(random, Mockito.times(3)).nextInt(anyInt());
440 @Test
441 public void testOverridingOfDefaultPorts() throws Exception {
443 // confirm that default port properties being overridden to random
444 Configuration defaultConfig = HBaseConfiguration.create();
445 defaultConfig.setInt(HConstants.MASTER_INFO_PORT, HConstants.DEFAULT_MASTER_INFOPORT);
446 defaultConfig.setInt(HConstants.REGIONSERVER_INFO_PORT,
447 HConstants.DEFAULT_REGIONSERVER_INFOPORT);
448 HBaseTestingUtil htu = new HBaseTestingUtil(defaultConfig);
449 try {
450 SingleProcessHBaseCluster defaultCluster = htu.startMiniCluster();
451 final String masterHostPort =
452 defaultCluster.getMaster().getServerName().getAddress().toString();
453 assertNotEquals(HConstants.DEFAULT_MASTER_INFOPORT,
454 defaultCluster.getConfiguration().getInt(HConstants.MASTER_INFO_PORT, 0));
455 assertNotEquals(HConstants.DEFAULT_REGIONSERVER_INFOPORT,
456 defaultCluster.getConfiguration().getInt(HConstants.REGIONSERVER_INFO_PORT, 0));
457 assertEquals(masterHostPort,
458 defaultCluster.getConfiguration().get(HConstants.MASTER_ADDRS_KEY));
459 } finally {
460 htu.shutdownMiniCluster();
463 // confirm that nonDefault (custom) port settings are NOT overridden
464 Configuration altConfig = HBaseConfiguration.create();
465 final int nonDefaultMasterInfoPort = 3333;
466 final int nonDefaultRegionServerPort = 4444;
467 altConfig.setInt(HConstants.MASTER_INFO_PORT, nonDefaultMasterInfoPort);
468 altConfig.setInt(HConstants.REGIONSERVER_INFO_PORT, nonDefaultRegionServerPort);
469 htu = new HBaseTestingUtil(altConfig);
470 try {
471 SingleProcessHBaseCluster customCluster = htu.startMiniCluster();
472 final String masterHostPort =
473 customCluster.getMaster().getServerName().getAddress().toString();
474 assertEquals(nonDefaultMasterInfoPort,
475 customCluster.getConfiguration().getInt(HConstants.MASTER_INFO_PORT, 0));
476 assertEquals(nonDefaultRegionServerPort,
477 customCluster.getConfiguration().getInt(HConstants.REGIONSERVER_INFO_PORT, 0));
478 assertEquals(masterHostPort,
479 customCluster.getConfiguration().get(HConstants.MASTER_ADDRS_KEY));
480 } finally {
481 htu.shutdownMiniCluster();
485 // This test demonstrates how long killHBTU takes vs. shutdownHBTU takes
486 // for realistic results, adjust NUMROWS, NUMTABLES to much larger number.
487 @Test
488 public void testKillMiniHBaseCluster() throws Exception {
490 HBaseTestingUtil htu = new HBaseTestingUtil();
491 htu.startMiniZKCluster();
493 try {
494 htu.startMiniHBaseCluster();
496 TableName tableName;
497 byte[] FAM_NAME;
499 for(int i = 0; i < NUMTABLES; i++) {
500 tableName = TableName.valueOf(name.getMethodName() + i);
501 FAM_NAME = Bytes.toBytes("fam" + i);
503 try (Table table = htu.createMultiRegionTable(tableName, FAM_NAME, NUMREGIONS)) {
504 htu.loadRandomRows(table, FAM_NAME, 100, NUMROWS);
507 } finally {
508 htu.killMiniHBaseCluster();
509 htu.shutdownMiniZKCluster();