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.io.IOException;
22  import java.util.concurrent.CountDownLatch;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.MetaTableAccessor;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.HColumnDescriptor;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.HTableDescriptor;
33  import org.apache.hadoop.hbase.MiniHBaseCluster;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.client.Delete;
36  import org.apache.hadoop.hbase.client.HBaseAdmin;
37  import org.apache.hadoop.hbase.client.Result;
38  import org.apache.hadoop.hbase.client.ResultScanner;
39  import org.apache.hadoop.hbase.client.Table;
40  import org.apache.hadoop.hbase.coprocessor.BaseMasterObserver;
41  import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
42  import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
43  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
44  import org.apache.hadoop.hbase.master.HMaster;
45  import org.apache.hadoop.hbase.testclassification.MediumTests;
46  import org.apache.hadoop.hbase.util.Bytes;
47  import org.apache.hadoop.hbase.util.JVMClusterUtil;
48  import org.junit.After;
49  import org.junit.Before;
50  import org.junit.Ignore;
51  import org.junit.Test;
52  import org.junit.experimental.categories.Category;
53  
54  import static org.junit.Assert.assertEquals;
55  import static org.junit.Assert.assertTrue;
56  import static org.junit.Assert.fail;
57  
58  @Category({ MediumTests.class })
59  public class TestEnableTableHandler {
60    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
61    private static final Log LOG = LogFactory.getLog(TestEnableTableHandler.class);
62    private static final byte[] FAMILYNAME = Bytes.toBytes("fam");
63  
64    @Before
65    public void setUp() throws Exception {
66      TEST_UTIL.getConfiguration().set("hbase.balancer.tablesOnMaster", "hbase:meta");
67      TEST_UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
68        MasterSyncObserver.class.getName());
69      TEST_UTIL.startMiniCluster(1);
70    }
71  
72    @After
73    public void tearDown() throws Exception {
74      TEST_UTIL.shutdownMiniCluster();
75    }
76  
77    @Ignore
78    @Test(timeout = 300000)
79    public void testEnableTableWithNoRegionServers() throws Exception {
80      final TableName tableName = TableName.valueOf("testEnableTableWithNoRegionServers");
81      final MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
82      final HMaster m = cluster.getMaster();
83      final HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
84      final HTableDescriptor desc = new HTableDescriptor(tableName);
85      desc.addFamily(new HColumnDescriptor(FAMILYNAME));
86      admin.createTable(desc);
87      admin.disableTable(tableName);
88      TEST_UTIL.waitTableDisabled(tableName.getName());
89  
90      admin.enableTable(tableName);
91      TEST_UTIL.waitTableEnabled(tableName);
92  
93      // disable once more
94      admin.disableTable(tableName);
95  
96      TEST_UTIL.waitUntilNoRegionsInTransition(60000);
97      // now stop region servers
98      JVMClusterUtil.RegionServerThread rs = cluster.getRegionServerThreads().get(0);
99      rs.getRegionServer().stop("stop");
100     cluster.waitForRegionServerToStop(rs.getRegionServer().getServerName(), 10000);
101 
102     TEST_UTIL.waitUntilAllRegionsAssigned(TableName.META_TABLE_NAME);
103 
104     admin.enableTable(tableName);
105     assertTrue(admin.isTableEnabled(tableName));
106 
107     JVMClusterUtil.RegionServerThread rs2 = cluster.startRegionServer();
108     m.getAssignmentManager().assign(admin.getTableRegions(tableName));
109     TEST_UTIL.waitUntilAllRegionsAssigned(tableName);
110     List<HRegionInfo> onlineRegions = admin.getOnlineRegions(
111         rs2.getRegionServer().getServerName());
112     assertEquals(2, onlineRegions.size());
113     assertEquals(tableName, onlineRegions.get(1).getTable());
114   }
115 
116 
117   @Test(timeout = 300000)
118   public void testDisableTableAndRestart() throws Exception {
119     final TableName tableName = TableName.valueOf("testDisableTableAndRestart");
120     final MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
121     final HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
122     final HTableDescriptor desc = new HTableDescriptor(tableName);
123     desc.addFamily(new HColumnDescriptor(FAMILYNAME));
124     admin.createTable(desc);
125     admin.disableTable(tableName);
126     TEST_UTIL.waitTableDisabled(tableName.getName());
127 
128     TEST_UTIL.getHBaseCluster().shutdown();
129     TEST_UTIL.getHBaseCluster().waitUntilShutDown();
130 
131     TEST_UTIL.restartHBaseCluster(2);
132 
133     admin.enableTable(tableName);
134     TEST_UTIL.waitTableEnabled(tableName);
135   }
136 
137   /**
138    * We were only clearing rows that had a hregioninfo column in hbase:meta.  Mangled rows that
139    * were missing the hregioninfo because of error were being left behind messing up any
140    * subsequent table made with the same name. HBASE-12980
141    * @throws IOException
142    * @throws InterruptedException
143    */
144   @Test(timeout=60000)
145   public void testDeleteForSureClearsAllTableRowsFromMeta()
146   throws IOException, InterruptedException {
147     final TableName tableName = TableName.valueOf("testDeleteForSureClearsAllTableRowsFromMeta");
148     final HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
149     final HTableDescriptor desc = new HTableDescriptor(tableName);
150     desc.addFamily(new HColumnDescriptor(FAMILYNAME));
151     try {
152       createTable(TEST_UTIL, desc, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
153     } catch (Exception e) {
154       e.printStackTrace();
155       fail("Got an exception while creating " + tableName);
156     }
157     // Now I have a nice table, mangle it by removing the HConstants.REGIONINFO_QUALIFIER_STR
158     // content from a few of the rows.
159     try (Table metaTable = TEST_UTIL.getConnection().getTable(TableName.META_TABLE_NAME)) {
160       try (ResultScanner scanner =
161           metaTable.getScanner(MetaTableAccessor.getScanForTableName(tableName))) {
162         for (Result result : scanner) {
163           // Just delete one row.
164           Delete d = new Delete(result.getRow());
165           d.addColumn(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
166           LOG.info("Mangled: " + d);
167           metaTable.delete(d);
168           break;
169         }
170       }
171       admin.disableTable(tableName);
172       TEST_UTIL.waitTableDisabled(tableName.getName());
173       // Rely on the coprocessor based latch to make the operation synchronous.
174       try {
175         deleteTable(TEST_UTIL, tableName);
176       } catch (Exception e) {
177         e.printStackTrace();
178         fail("Got an exception while deleting " + tableName);
179       }
180       int rowCount = 0;
181       try (ResultScanner scanner =
182           metaTable.getScanner(MetaTableAccessor.getScanForTableName(tableName))) {
183         for (Result result : scanner) {
184           LOG.info("Found when none expected: " + result);
185           rowCount++;
186         }
187       }
188       assertEquals(0, rowCount);
189     }
190   }
191 
192   public  static class MasterSyncObserver extends BaseMasterObserver {
193     volatile CountDownLatch tableCreationLatch = null;
194     volatile CountDownLatch tableDeletionLatch = null;
195 
196     @Override
197     public void postCreateTableHandler(final ObserverContext<MasterCoprocessorEnvironment> ctx,
198       HTableDescriptor desc, HRegionInfo[] regions) throws IOException {
199       // the AccessController test, some times calls only and directly the postCreateTableHandler()
200       if (tableCreationLatch != null) {
201         tableCreationLatch.countDown();
202       }
203     }
204 
205     @Override
206     public void postDeleteTableHandler(final ObserverContext<MasterCoprocessorEnvironment> ctx,
207                                        TableName tableName)
208     throws IOException {
209       // the AccessController test, some times calls only and directly the postDeleteTableHandler()
210       if (tableDeletionLatch != null) {
211         tableDeletionLatch.countDown();
212       }
213     }
214   }
215 
216   public static void createTable(HBaseTestingUtility testUtil, HTableDescriptor htd,
217     byte [][] splitKeys)
218   throws Exception {
219     createTable(testUtil, testUtil.getHBaseAdmin(), htd, splitKeys);
220   }
221 
222   public static void createTable(HBaseTestingUtility testUtil, HBaseAdmin admin,
223     HTableDescriptor htd, byte [][] splitKeys)
224   throws Exception {
225     // NOTE: We need a latch because admin is not sync,
226     // so the postOp coprocessor method may be called after the admin operation returned.
227     MasterSyncObserver observer = (MasterSyncObserver)testUtil.getHBaseCluster().getMaster()
228       .getMasterCoprocessorHost().findCoprocessor(MasterSyncObserver.class.getName());
229     observer.tableCreationLatch = new CountDownLatch(1);
230     if (splitKeys != null) {
231       admin.createTable(htd, splitKeys);
232     } else {
233       admin.createTable(htd);
234     }
235     observer.tableCreationLatch.await();
236     observer.tableCreationLatch = null;
237     testUtil.waitUntilAllRegionsAssigned(htd.getTableName());
238   }
239 
240   public static void deleteTable(HBaseTestingUtility testUtil, TableName tableName)
241   throws Exception {
242     deleteTable(testUtil, testUtil.getHBaseAdmin(), tableName);
243   }
244 
245   public static void deleteTable(HBaseTestingUtility testUtil, HBaseAdmin admin,
246     TableName tableName)
247   throws Exception {
248     // NOTE: We need a latch because admin is not sync,
249     // so the postOp coprocessor method may be called after the admin operation returned.
250     MasterSyncObserver observer = (MasterSyncObserver)testUtil.getHBaseCluster().getMaster()
251       .getMasterCoprocessorHost().findCoprocessor(MasterSyncObserver.class.getName());
252     observer.tableDeletionLatch = new CountDownLatch(1);
253     try {
254       admin.disableTable(tableName);
255     } catch (Exception e) {
256       LOG.debug("Table: " + tableName + " already disabled, so just deleting it.");
257     }
258     admin.deleteTable(tableName);
259     observer.tableDeletionLatch.await();
260     observer.tableDeletionLatch = null;
261   }
262 }