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