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.master.handler;
20  
21  import java.util.List;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.MetaTableAccessor;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.HColumnDescriptor;
29  import org.apache.hadoop.hbase.HRegionInfo;
30  import org.apache.hadoop.hbase.HTableDescriptor;
31  import org.apache.hadoop.hbase.MiniHBaseCluster;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.client.HBaseAdmin;
34  import org.apache.hadoop.hbase.master.HMaster;
35  import org.apache.hadoop.hbase.testclassification.MediumTests;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.apache.hadoop.hbase.util.JVMClusterUtil;
38  import org.junit.After;
39  import org.junit.Before;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  import static org.junit.Assert.assertEquals;
44  import static org.junit.Assert.assertTrue;
45  
46  import java.io.IOException;
47  import org.apache.hadoop.hbase.client.Delete;
48  import org.apache.hadoop.hbase.client.Result;
49  import org.apache.hadoop.hbase.client.ResultScanner;
50  import org.apache.hadoop.hbase.client.Scan;
51  import org.apache.hadoop.hbase.client.Table;
52  
53  @Category({ MediumTests.class })
54  public class TestEnableTableHandler {
55    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
56    private static final Log LOG = LogFactory.getLog(TestEnableTableHandler.class);
57    private static final byte[] FAMILYNAME = Bytes.toBytes("fam");
58  
59    @Before
60    public void setUp() throws Exception {
61      TEST_UTIL.getConfiguration().set("hbase.balancer.tablesOnMaster", "hbase:meta");
62      TEST_UTIL.startMiniCluster(1);
63    }
64  
65    @After
66    public void tearDown() throws Exception {
67      TEST_UTIL.shutdownMiniCluster();
68    }
69  
70    @Test(timeout = 300000)
71    public void testEnableTableWithNoRegionServers() throws Exception {
72      final TableName tableName = TableName.valueOf("testEnableTableWithNoRegionServers");
73      final MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
74      final HMaster m = cluster.getMaster();
75      final HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
76      final HTableDescriptor desc = new HTableDescriptor(tableName);
77      desc.addFamily(new HColumnDescriptor(FAMILYNAME));
78      admin.createTable(desc);
79      admin.disableTable(tableName);
80      TEST_UTIL.waitTableDisabled(tableName.getName());
81  
82      admin.enableTable(tableName);
83      TEST_UTIL.waitTableEnabled(tableName);
84  
85      // disable once more
86      admin.disableTable(tableName);
87  
88      TEST_UTIL.waitUntilNoRegionsInTransition(60000);
89      // now stop region servers
90      JVMClusterUtil.RegionServerThread rs = cluster.getRegionServerThreads().get(0);
91      rs.getRegionServer().stop("stop");
92      cluster.waitForRegionServerToStop(rs.getRegionServer().getServerName(), 10000);
93  
94      TEST_UTIL.waitUntilAllRegionsAssigned(TableName.META_TABLE_NAME);
95  
96      admin.enableTable(tableName);
97      assertTrue(admin.isTableEnabled(tableName));
98  
99      JVMClusterUtil.RegionServerThread rs2 = cluster.startRegionServer();
100     m.getAssignmentManager().assign(admin.getTableRegions(tableName));
101     TEST_UTIL.waitUntilAllRegionsAssigned(tableName);
102     List<HRegionInfo> onlineRegions = admin.getOnlineRegions(
103         rs2.getRegionServer().getServerName());
104     assertEquals(2, onlineRegions.size());
105     assertEquals(tableName, onlineRegions.get(1).getTable());
106   }
107 
108 
109   @Test(timeout = 300000)
110   public void testDisableTableAndRestart() throws Exception {
111     final TableName tableName = TableName.valueOf("testDisableTableAndRestart");
112     final MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
113     final HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
114     final HTableDescriptor desc = new HTableDescriptor(tableName);
115     desc.addFamily(new HColumnDescriptor(FAMILYNAME));
116     admin.createTable(desc);
117     admin.disableTable(tableName);
118     TEST_UTIL.waitTableDisabled(tableName.getName());
119 
120     TEST_UTIL.getHBaseCluster().shutdown();
121     TEST_UTIL.getHBaseCluster().waitUntilShutDown();
122 
123     TEST_UTIL.restartHBaseCluster(2);
124 
125     admin.enableTable(tableName);
126     TEST_UTIL.waitTableEnabled(tableName);
127   }
128 
129   /**
130    * We were only clearing rows that had a hregioninfo column in hbase:meta.  Mangled rows that
131    * were missing the hregioninfo because of error were being left behind messing up any
132    * subsequent table made with the same name. HBASE-12980
133    * @throws IOException
134    * @throws InterruptedException
135    */
136   @Test(timeout=60000)
137   public void testDeleteForSureClearsAllTableRowsFromMeta()
138   throws IOException, InterruptedException {
139     final TableName tableName = TableName.valueOf("testDeleteForSureClearsAllTableRowsFromMeta");
140     final MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
141     final HMaster m = cluster.getMaster();
142     final HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
143     final HTableDescriptor desc = new HTableDescriptor(tableName);
144     desc.addFamily(new HColumnDescriptor(FAMILYNAME));
145     admin.createTable(desc, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
146     // Now I have a nice table, mangle it by removing the HConstants.REGIONINFO_QUALIFIER_STR
147     // content from a few of the rows.
148     Scan metaScannerForMyTable = MetaTableAccessor.getScanForTableName(tableName);
149     try (Table metaTable = TEST_UTIL.getConnection().getTable(TableName.META_TABLE_NAME)) {
150       try (ResultScanner scanner = metaTable.getScanner(metaScannerForMyTable)) {
151         for (Result result : scanner) {
152           // Just delete one row.
153           Delete d = new Delete(result.getRow());
154           d.addColumn(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
155           metaTable.delete(d);
156           break;
157         }
158       }
159       admin.disableTable(tableName);
160       TEST_UTIL.waitTableDisabled(tableName.getName());
161       // Presume this synchronous all is.
162       admin.deleteTable(tableName);
163       int rowCount = 0;
164       try (ResultScanner scanner = metaTable.getScanner(metaScannerForMyTable)) {
165         for (Result result : scanner) {
166           rowCount++;
167         }
168       }
169       assertEquals(0, rowCount);
170     }
171   }
172 }