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.master.procedure;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.HBaseTestingUtility;
25  import org.apache.hadoop.hbase.HTableDescriptor;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.TableNotDisabledException;
29  import org.apache.hadoop.hbase.TableNotFoundException;
30  import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
31  import org.apache.hadoop.hbase.procedure2.ProcedureResult;
32  import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
33  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.TruncateTableState;
34  import org.apache.hadoop.hbase.testclassification.MediumTests;
35  import org.apache.hadoop.hbase.util.Bytes;
36  
37  import org.junit.After;
38  import org.junit.AfterClass;
39  import org.junit.Before;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  import static org.junit.Assert.assertEquals;
45  import static org.junit.Assert.assertFalse;
46  import static org.junit.Assert.assertTrue;
47  import static org.junit.Assert.fail;
48  
49  @Category(MediumTests.class)
50  public class TestTruncateTableProcedure {
51    private static final Log LOG = LogFactory.getLog(TestTruncateTableProcedure.class);
52  
53    protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
54  
55    private static void setupConf(Configuration conf) {
56      conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
57    }
58  
59    @BeforeClass
60    public static void setupCluster() throws Exception {
61      setupConf(UTIL.getConfiguration());
62      UTIL.startMiniCluster(1);
63    }
64  
65    @AfterClass
66    public static void cleanupTest() throws Exception {
67      try {
68        UTIL.shutdownMiniCluster();
69      } catch (Exception e) {
70        LOG.warn("failure shutting down cluster", e);
71      }
72    }
73  
74    @Before
75    public void setup() throws Exception {
76      final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
77      ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
78      assertTrue("expected executor to be running", procExec.isRunning());
79    }
80  
81    @After
82    public void tearDown() throws Exception {
83      ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(getMasterProcedureExecutor(), false);
84      for (HTableDescriptor htd: UTIL.getHBaseAdmin().listTables()) {
85        LOG.info("Tear down, remove table=" + htd.getTableName());
86        UTIL.deleteTable(htd.getTableName());
87      }
88    }
89  
90    @Test(timeout=60000)
91    public void testTruncateNotExistentTable() throws Exception {
92      final TableName tableName = TableName.valueOf("testTruncateNotExistentTable");
93  
94      final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
95      long procId = ProcedureTestingUtility.submitAndWait(procExec,
96          new TruncateTableProcedure(procExec.getEnvironment(), tableName, true));
97  
98      // Second delete should fail with TableNotFound
99      ProcedureResult result = procExec.getResult(procId);
100     assertTrue(result.isFailed());
101     LOG.debug("Truncate failed with exception: " + result.getException());
102     assertTrue(result.getException().getCause() instanceof TableNotFoundException);
103   }
104 
105   @Test(timeout=60000)
106   public void testTruncateNotDisabledTable() throws Exception {
107     final TableName tableName = TableName.valueOf("testTruncateNotDisabledTable");
108 
109     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
110     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f");
111 
112     long procId = ProcedureTestingUtility.submitAndWait(procExec,
113         new TruncateTableProcedure(procExec.getEnvironment(), tableName, false));
114 
115     // Second delete should fail with TableNotDisabled
116     ProcedureResult result = procExec.getResult(procId);
117     assertTrue(result.isFailed());
118     LOG.debug("Truncate failed with exception: " + result.getException());
119     assertTrue(result.getException().getCause() instanceof TableNotDisabledException);
120   }
121 
122   @Test(timeout=60000)
123   public void testSimpleTruncatePreserveSplits() throws Exception {
124     final TableName tableName = TableName.valueOf("testSimpleTruncatePreserveSplits");
125     testSimpleTruncate(tableName, true);
126   }
127 
128   @Test(timeout=60000)
129   public void testSimpleTruncateNoPreserveSplits() throws Exception {
130     final TableName tableName = TableName.valueOf("testSimpleTruncateNoPreserveSplits");
131     testSimpleTruncate(tableName, false);
132   }
133 
134   private void testSimpleTruncate(final TableName tableName, final boolean preserveSplits)
135       throws Exception {
136     final String[] families = new String[] { "f1", "f2" };
137     final byte[][] splitKeys = new byte[][] {
138       Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c")
139     };
140 
141     HRegionInfo[] regions = MasterProcedureTestingUtility.createTable(
142       getMasterProcedureExecutor(), tableName, splitKeys, families);
143     // load and verify that there are rows in the table
144     MasterProcedureTestingUtility.loadData(
145       UTIL.getConnection(), tableName, 100, splitKeys, families);
146     assertEquals(100, UTIL.countRows(tableName));
147     // disable the table
148     UTIL.getHBaseAdmin().disableTable(tableName);
149 
150     // truncate the table
151     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
152     long procId = ProcedureTestingUtility.submitAndWait(procExec,
153       new TruncateTableProcedure(procExec.getEnvironment(), tableName, preserveSplits));
154     ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
155 
156     UTIL.waitUntilAllRegionsAssigned(tableName);
157 
158     // validate the table regions and layout
159     if (preserveSplits) {
160       assertEquals(1 + splitKeys.length, UTIL.getHBaseAdmin().getTableRegions(tableName).size());
161     } else {
162       regions = UTIL.getHBaseAdmin().getTableRegions(tableName).toArray(new HRegionInfo[1]);
163       assertEquals(1, regions.length);
164     }
165     MasterProcedureTestingUtility.validateTableCreation(
166       UTIL.getHBaseCluster().getMaster(), tableName, regions, families);
167 
168     // verify that there are no rows in the table
169     assertEquals(0, UTIL.countRows(tableName));
170 
171     // verify that the table is read/writable
172     MasterProcedureTestingUtility.loadData(
173       UTIL.getConnection(), tableName, 50, splitKeys, families);
174     assertEquals(50, UTIL.countRows(tableName));
175   }
176 
177   @Test(timeout=60000)
178   public void testRecoveryAndDoubleExecutionPreserveSplits() throws Exception {
179     final TableName tableName = TableName.valueOf("testRecoveryAndDoubleExecutionPreserveSplits");
180     testRecoveryAndDoubleExecution(tableName, true);
181   }
182 
183   @Test(timeout=60000)
184   public void testRecoveryAndDoubleExecutionNoPreserveSplits() throws Exception {
185     final TableName tableName = TableName.valueOf("testRecoveryAndDoubleExecutionNoPreserveSplits");
186     testRecoveryAndDoubleExecution(tableName, false);
187   }
188 
189   private void testRecoveryAndDoubleExecution(final TableName tableName,
190       final boolean preserveSplits) throws Exception {
191     final String[] families = new String[] { "f1", "f2" };
192 
193     // create the table
194     final byte[][] splitKeys = new byte[][] {
195       Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c")
196     };
197     HRegionInfo[] regions = MasterProcedureTestingUtility.createTable(
198       getMasterProcedureExecutor(), tableName, splitKeys, families);
199     // load and verify that there are rows in the table
200     MasterProcedureTestingUtility.loadData(
201       UTIL.getConnection(), tableName, 100, splitKeys, families);
202     assertEquals(100, UTIL.countRows(tableName));
203     // disable the table
204     UTIL.getHBaseAdmin().disableTable(tableName);
205 
206     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
207     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
208     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
209 
210     // Start the Truncate procedure && kill the executor
211     long procId = procExec.submitProcedure(
212       new TruncateTableProcedure(procExec.getEnvironment(), tableName, preserveSplits));
213 
214     // Restart the executor and execute the step twice
215     // NOTE: the 7 (number of TruncateTableState steps) is hardcoded,
216     //       so you have to look at this test at least once when you add a new step.
217     MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(
218       procExec, procId, 7, TruncateTableState.values());
219 
220     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
221     UTIL.waitUntilAllRegionsAssigned(tableName);
222 
223     // validate the table regions and layout
224     if (preserveSplits) {
225       assertEquals(1 + splitKeys.length, UTIL.getHBaseAdmin().getTableRegions(tableName).size());
226     } else {
227       regions = UTIL.getHBaseAdmin().getTableRegions(tableName).toArray(new HRegionInfo[1]);
228       assertEquals(1, regions.length);
229     }
230     MasterProcedureTestingUtility.validateTableCreation(
231       UTIL.getHBaseCluster().getMaster(), tableName, regions, families);
232 
233     // verify that there are no rows in the table
234     assertEquals(0, UTIL.countRows(tableName));
235 
236     // verify that the table is read/writable
237     MasterProcedureTestingUtility.loadData(
238       UTIL.getConnection(), tableName, 50, splitKeys, families);
239     assertEquals(50, UTIL.countRows(tableName));
240   }
241 
242   private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
243     return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
244   }
245 }