View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  
19  package org.apache.hadoop.hbase;
20  
21  import java.io.IOException;
22  import java.util.concurrent.CountDownLatch;
23  import java.util.concurrent.TimeUnit;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.client.HBaseAdmin;
28  import org.apache.hadoop.hbase.util.RegionSplitter;
29  import org.apache.hadoop.hbase.util.RegionSplitter.SplitAlgorithm;
30  import org.junit.After;
31  import org.junit.Before;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  import static org.junit.Assert.assertTrue;
36  import static org.junit.Assert.fail;
37  
38  /**
39   * An integration test to detect regressions in HBASE-7220. Create
40   * a table with many regions and verify it completes within a
41   * reasonable amount of time.
42   * @see <a href="https://issues.apache.org/jira/browse/HBASE-7220">HBASE-7220</a>
43   */
44  @Category(IntegrationTests.class)
45  public class IntegrationTestManyRegions {
46  
47    private static final String CLASS_NAME
48      = IntegrationTestManyRegions.class.getSimpleName();
49  
50    protected static final Log LOG
51      = LogFactory.getLog(IntegrationTestManyRegions.class);
52    protected static final String TABLE_NAME = CLASS_NAME;
53    protected static final String COLUMN_NAME = "f";
54    protected static final String REGION_COUNT_KEY
55      = String.format("hbase.%s.regions", CLASS_NAME);
56    protected static final String REGIONSERVER_COUNT_KEY
57      = String.format("hbase.%s.regionServers", CLASS_NAME);
58    protected static final String TIMEOUT_MINUTES_KEY
59      = String.format("hbase.%s.timeoutMinutes", CLASS_NAME);
60  
61    protected static final int DEFAULT_REGION_COUNT = 1000;
62    protected static final int DEFAULT_REGIONSERVER_COUNT = 1;
63    // running this test on my laptop consistently takes about 2.5
64    // minutes. A timeout of 4 minutes should be reasonably safe.
65    protected static final int DEFAULT_TIMEOUT_MINUTES = 4;
66    protected static final IntegrationTestingUtility util
67      = new IntegrationTestingUtility();
68  
69    protected static final int REGION_COUNT = util.getConfiguration()
70      .getInt(REGION_COUNT_KEY, DEFAULT_REGION_COUNT);
71    protected static final int REGION_SERVER_COUNT = util.getConfiguration()
72      .getInt(REGIONSERVER_COUNT_KEY, DEFAULT_REGIONSERVER_COUNT);
73    protected static final int TIMEOUT_MINUTES = util.getConfiguration()
74      .getInt(TIMEOUT_MINUTES_KEY, DEFAULT_TIMEOUT_MINUTES);
75  
76    @Before
77    public void setUp() throws Exception {
78      LOG.info(String.format("Initializing cluster with %d region servers.",
79        REGION_SERVER_COUNT));
80      util.initializeCluster(REGION_SERVER_COUNT);
81      LOG.info("Cluster initialized");
82  
83      HBaseAdmin admin = util.getHBaseAdmin();
84      if (admin.tableExists(TABLE_NAME)) {
85        LOG.info(String.format("Deleting existing table %s.", TABLE_NAME));
86        if (admin.isTableEnabled(TABLE_NAME)) admin.disableTable(TABLE_NAME);
87        admin.deleteTable(TABLE_NAME);
88        LOG.info(String.format("Existing table %s deleted.", TABLE_NAME));
89      }
90      LOG.info("Cluster ready");
91    }
92  
93    @After
94    public void tearDown() throws IOException {
95      LOG.info("Cleaning up after test.");
96      HBaseAdmin admin = util.getHBaseAdmin();
97      if (admin.tableExists(TABLE_NAME)) {
98        if (admin.isTableEnabled(TABLE_NAME)) admin.disableTable(TABLE_NAME);
99        admin.deleteTable(TABLE_NAME);
100     }
101     LOG.info("Restoring cluster.");
102     util.restoreCluster();
103     LOG.info("Cluster restored.");
104   }
105 
106   @Test
107   public void testCreateTableWithRegions() throws Exception {
108     CountDownLatch doneSignal = new CountDownLatch(1);
109     Worker worker = new Worker(doneSignal, util.getHBaseAdmin());
110     Thread t = new Thread(worker);
111 
112     LOG.info("Launching worker thread to create the table.");
113     t.start();
114     boolean workerComplete = false;
115     workerComplete = doneSignal.await(TIMEOUT_MINUTES, TimeUnit.MINUTES);
116     if (!workerComplete) {
117       t.interrupt();
118       fail("Timeout limit expired.");
119     }
120     assertTrue("Table creation failed.", worker.isSuccess());
121   }
122 
123   private static class Worker implements Runnable {
124     private final CountDownLatch doneSignal;
125     private final HBaseAdmin admin;
126     private boolean success = false;
127 
128     public Worker(final CountDownLatch doneSignal, final HBaseAdmin admin) {
129       this.doneSignal = doneSignal;
130       this.admin = admin;
131     }
132 
133     public boolean isSuccess() {
134       return this.success;
135     }
136 
137     @Override
138     public void run() {
139       long startTime, endTime;
140       HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
141       desc.addFamily(new HColumnDescriptor(COLUMN_NAME));
142       SplitAlgorithm algo = new RegionSplitter.HexStringSplit();
143       byte[][] splits = algo.split(REGION_COUNT);
144 
145       LOG.info(String.format("Creating table %s with %d splits.",
146         TABLE_NAME, REGION_COUNT));
147       startTime = System.currentTimeMillis();
148       try {
149         admin.createTable(desc, splits);
150         endTime = System.currentTimeMillis();
151         success = true;
152         LOG.info(String.format("Pre-split table created successfully in %dms.",
153           (endTime - startTime)));
154       } catch (IOException e) {
155         LOG.error("Failed to create table", e);
156       } finally {
157         doneSignal.countDown();
158       }
159     }
160   }
161 }