001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertNotEquals;
023import static org.junit.Assert.assertTrue;
024import static org.mockito.ArgumentMatchers.anyInt;
025import static org.mockito.Mockito.mock;
026import static org.mockito.Mockito.when;
027
028import java.io.File;
029import java.util.List;
030import java.util.Random;
031import org.apache.hadoop.conf.Configuration;
032import org.apache.hadoop.fs.FileSystem;
033import org.apache.hadoop.fs.FileUtil;
034import org.apache.hadoop.fs.Path;
035import org.apache.hadoop.hbase.client.Get;
036import org.apache.hadoop.hbase.client.Put;
037import org.apache.hadoop.hbase.client.Result;
038import org.apache.hadoop.hbase.client.Table;
039import org.apache.hadoop.hbase.http.ssl.KeyStoreTestUtil;
040import org.apache.hadoop.hbase.testclassification.LargeTests;
041import org.apache.hadoop.hbase.testclassification.MiscTests;
042import org.apache.hadoop.hbase.util.Bytes;
043import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
044import org.apache.hadoop.hdfs.MiniDFSCluster;
045import org.apache.hadoop.security.ssl.SSLFactory;
046import org.junit.ClassRule;
047import org.junit.Rule;
048import org.junit.Test;
049import org.junit.experimental.categories.Category;
050import org.junit.rules.TestName;
051import org.mockito.Mockito;
052import org.mockito.invocation.InvocationOnMock;
053import org.mockito.stubbing.Answer;
054import org.slf4j.Logger;
055import org.slf4j.LoggerFactory;
056
057/**
058 * Test our testing utility class
059 */
060@Category({MiscTests.class, LargeTests.class})
061public class TestHBaseTestingUtil {
062  private static final int NUMTABLES = 1;
063  private static final int NUMROWS = 100;
064  private static final int NUMREGIONS = 10;
065
066  @ClassRule
067  public static final HBaseClassTestRule CLASS_RULE =
068      HBaseClassTestRule.forClass(TestHBaseTestingUtil.class);
069
070  private static final Logger LOG = LoggerFactory.getLogger(TestHBaseTestingUtil.class);
071
072  @Rule
073  public TestName name = new TestName();
074
075  /**
076   * Basic sanity test that spins up multiple HDFS and HBase clusters that share
077   * the same ZK ensemble. We then create the same table in both and make sure
078   * that what we insert in one place doesn't end up in the other.
079   * @throws Exception on error
080   */
081  @Test
082  public void testMultiClusters() throws Exception {
083    // Create three clusters
084
085    // Cluster 1.
086    HBaseTestingUtil htu1 = new HBaseTestingUtil();
087    // Set a different zk path for each cluster
088    htu1.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
089    htu1.startMiniZKCluster();
090
091    // Cluster 2
092    HBaseTestingUtil htu2 = new HBaseTestingUtil();
093    htu2.getConfiguration().set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
094    htu2.getConfiguration().set(HConstants.ZOOKEEPER_CLIENT_PORT,
095      htu1.getConfiguration().get(HConstants.ZOOKEEPER_CLIENT_PORT, "-1"));
096    htu2.setZkCluster(htu1.getZkCluster());
097
098    // Cluster 3; seed it with the conf from htu1 so we pickup the 'right'
099    // 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());
106
107    try {
108      htu1.startMiniCluster();
109      htu2.startMiniCluster();
110      htu3.startMiniCluster();
111
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");
117
118      Table table1 = htu1.createTable(tableName, FAM_NAME);
119      Table table2 = htu2.createTable(tableName, FAM_NAME);
120
121      Put put = new Put(ROW);
122      put.addColumn(FAM_NAME, QUAL_NAME, VALUE);
123      table1.put(put);
124
125      Get get = new Get(ROW);
126      get.addColumn(FAM_NAME, QUAL_NAME);
127      Result res = table1.get(get);
128      assertEquals(1, res.size());
129
130      res = table2.get(get);
131      assertEquals(0, res.size());
132
133      table1.close();
134      table2.close();
135
136    } finally {
137      htu3.shutdownMiniCluster();
138      htu2.shutdownMiniCluster();
139      htu1.shutdownMiniCluster();
140    }
141  }
142
143  @Test public void testMiniCluster() throws Exception {
144    HBaseTestingUtil hbt = new HBaseTestingUtil();
145
146    SingleProcessHBaseCluster cluster = hbt.startMiniCluster();
147    try {
148      assertEquals(1, cluster.getLiveRegionServerThreads().size());
149    } finally {
150      hbt.shutdownMiniCluster();
151    }
152  }
153
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();
163    }
164  }
165
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();
172
173    HBaseTestingUtil hbt = new HBaseTestingUtil();
174    File base = new File(BASEDIR);
175    FileUtil.fullyDelete(base);
176    base.mkdirs();
177
178    KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, hbt.getConfiguration(), false);
179
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));
183
184    SingleProcessHBaseCluster cluster = hbt.startMiniCluster();
185    try {
186      assertEquals(1, cluster.getLiveRegionServerThreads().size());
187    } finally {
188      hbt.shutdownMiniCluster();
189    }
190  }
191
192  /**
193   *  Test that we can start and stop multiple time a cluster
194   *   with the same HBaseTestingUtility.
195   */
196  @Test public void testMultipleStartStop() throws Exception{
197    HBaseTestingUtil htu1 = new HBaseTestingUtil();
198    Path foo = new Path("foo");
199
200    htu1.startMiniCluster();
201    htu1.getDFSCluster().getFileSystem().create(foo);
202    assertTrue(htu1.getDFSCluster().getFileSystem().exists(foo));
203    htu1.shutdownMiniCluster();
204
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();
210  }
211
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();
221    }
222  }
223
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());
233
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);
239
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());
246
247      // killing the backup zk servers
248      cluster2.killOneBackupZooKeeperServer();
249      cluster2.killOneBackupZooKeeperServer();
250      assertEquals(0, cluster2.getBackupZooKeeperServerNum());
251      assertEquals(1, cluster2.getZooKeeperServerNum());
252
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();
263    }
264  }
265
266  @Test
267  public void testMiniZooKeeperWithMultipleClientPorts() throws Exception {
268    int defaultClientPort = 8888;
269    int i, j;
270    HBaseTestingUtil hbt = new HBaseTestingUtil();
271
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();
277
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]);
282      }
283    } finally {
284      hbt.shutdownMiniZKCluster();
285    }
286
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);
292
293    try {
294      List<Integer> clientPortListInCluster = cluster2.getClientPortList();
295
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++;
304        }
305      }
306    } finally {
307      hbt.shutdownMiniZKCluster();
308    }
309
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);
315
316    try {
317      List<Integer> clientPortListInCluster = cluster3.getClientPortList();
318
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++;
327        }
328      }
329    } finally {
330      hbt.shutdownMiniZKCluster();
331    }
332
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);
341
342    try {
343      List<Integer> clientPortListInCluster = cluster4.getClientPortList();
344
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;
353        }
354      }
355    } finally {
356      hbt.shutdownMiniZKCluster();
357    }
358
359    // Test 5 - set up zookeeper cluster with same ports specified - fail is expected.
360    int [] clientPortList5 = {5555, 5556, 5556};
361
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();
370    }
371  }
372
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();
384  }
385
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();
391
392    assertFalse(fs.exists(testdir));
393
394    hbt.startMiniDFSCluster(null);
395    assertTrue(fs.exists(testdir));
396
397    hbt.shutdownMiniCluster();
398    assertFalse(fs.exists(testdir));
399  }
400
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());
409  }
410
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;
418
419        @Override
420        public Integer answer(InvocationOnMock invocation) {
421          int ret = numbers[count];
422          count++;
423          return ret;
424        }
425      });
426
427    HBaseTestingUtil.PortAllocator.AvailablePortChecker portChecker =
428      mock(HBaseTestingUtil.PortAllocator.AvailablePortChecker.class);
429    when(portChecker.available(anyInt())).thenReturn(true);
430
431    HBaseTestingUtil.PortAllocator portAllocator =
432      new HBaseTestingUtil.PortAllocator(random, portChecker);
433
434    int port1 = portAllocator.randomFreePort();
435    int port2 = portAllocator.randomFreePort();
436    assertNotEquals(port1, port2);
437    Mockito.verify(random, Mockito.times(3)).nextInt(anyInt());
438  }
439
440  @Test
441  public void testOverridingOfDefaultPorts() throws Exception {
442
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();
461    }
462
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();
482    }
483  }
484
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 {
489
490    HBaseTestingUtil htu = new HBaseTestingUtil();
491    htu.startMiniZKCluster();
492
493    try {
494      htu.startMiniHBaseCluster();
495
496      TableName tableName;
497      byte[] FAM_NAME;
498
499      for(int i = 0; i < NUMTABLES; i++) {
500        tableName = TableName.valueOf(name.getMethodName() + i);
501        FAM_NAME = Bytes.toBytes("fam" + i);
502
503        try (Table table = htu.createMultiRegionTable(tableName, FAM_NAME, NUMREGIONS)) {
504          htu.loadRandomRows(table, FAM_NAME, 100, NUMROWS);
505        }
506      }
507    } finally {
508      htu.killMiniHBaseCluster();
509      htu.shutdownMiniZKCluster();
510    }
511  }
512}