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 static org.junit.Assert.assertTrue;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.InvalidFamilyOperationException;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
32  import org.apache.hadoop.hbase.procedure2.ProcedureResult;
33  import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
34  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.DeleteColumnFamilyState;
35  import org.apache.hadoop.hbase.testclassification.MediumTests;
36  import org.junit.After;
37  import org.junit.AfterClass;
38  import org.junit.Before;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  @Category(MediumTests.class)
44  public class TestDeleteColumnFamilyProcedure {
45    private static final Log LOG = LogFactory.getLog(TestDeleteColumnFamilyProcedure.class);
46  
47    protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
48  
49    private static void setupConf(Configuration conf) {
50      conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
51    }
52  
53    @BeforeClass
54    public static void setupCluster() throws Exception {
55      setupConf(UTIL.getConfiguration());
56      UTIL.startMiniCluster(1);
57    }
58  
59    @AfterClass
60    public static void cleanupTest() throws Exception {
61      try {
62        UTIL.shutdownMiniCluster();
63      } catch (Exception e) {
64        LOG.warn("failure shutting down cluster", e);
65      }
66    }
67  
68    @Before
69    public void setup() throws Exception {
70      ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(getMasterProcedureExecutor(), false);
71    }
72  
73    @After
74    public void tearDown() throws Exception {
75      ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(getMasterProcedureExecutor(), false);
76      for (HTableDescriptor htd: UTIL.getHBaseAdmin().listTables()) {
77        LOG.info("Tear down, remove table=" + htd.getTableName());
78        UTIL.deleteTable(htd.getTableName());
79      }
80    }
81  
82    @Test(timeout = 60000)
83    public void testDeleteColumnFamily() throws Exception {
84      final TableName tableName = TableName.valueOf("testDeleteColumnFamily");
85      final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
86      final String cf1 = "cf1";
87      final String cf2 = "cf2";
88  
89      MasterProcedureTestingUtility.createTable(procExec, tableName, null, cf1, cf2, "f3");
90  
91      // Test 1: delete the column family that exists online
92      long procId1 =
93          procExec.submitProcedure(new DeleteColumnFamilyProcedure(procExec.getEnvironment(),
94              tableName, cf1.getBytes()));
95      // Wait the completion
96      ProcedureTestingUtility.waitProcedure(procExec, procId1);
97      ProcedureTestingUtility.assertProcNotFailed(procExec, procId1);
98  
99      MasterProcedureTestingUtility.validateColumnFamilyDeletion(UTIL.getHBaseCluster().getMaster(),
100       tableName, cf1);
101 
102     // Test 2: delete the column family that exists offline
103     UTIL.getHBaseAdmin().disableTable(tableName);
104     long procId2 =
105         procExec.submitProcedure(new DeleteColumnFamilyProcedure(procExec.getEnvironment(),
106             tableName, cf2.getBytes()));
107     // Wait the completion
108     ProcedureTestingUtility.waitProcedure(procExec, procId2);
109     ProcedureTestingUtility.assertProcNotFailed(procExec, procId2);
110   }
111 
112   @Test(timeout=60000)
113   public void testDeleteColumnFamilyTwice() throws Exception {
114     final TableName tableName = TableName.valueOf("testDeleteColumnFamilyTwice");
115     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
116 
117     final String cf2 = "cf2";
118 
119     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", cf2);
120 
121     // delete the column family that exists
122     long procId1 =
123         procExec.submitProcedure(new DeleteColumnFamilyProcedure(procExec.getEnvironment(),
124             tableName, cf2.getBytes()));
125     // Wait the completion
126     ProcedureTestingUtility.waitProcedure(procExec, procId1);
127     // First delete should succeed
128     ProcedureTestingUtility.assertProcNotFailed(procExec, procId1);
129 
130     MasterProcedureTestingUtility.validateColumnFamilyDeletion(UTIL.getHBaseCluster().getMaster(),
131       tableName, cf2);
132 
133     // delete the column family that does not exist
134     long procId2 =
135         procExec.submitProcedure(new DeleteColumnFamilyProcedure(procExec.getEnvironment(),
136             tableName, cf2.getBytes()));
137 
138     // Wait the completion
139     ProcedureTestingUtility.waitProcedure(procExec, procId2);
140 
141     // Second delete should fail with InvalidFamilyOperationException
142     ProcedureResult result = procExec.getResult(procId2);
143     assertTrue(result.isFailed());
144     LOG.debug("Delete online failed with exception: " + result.getException());
145     assertTrue(result.getException().getCause() instanceof InvalidFamilyOperationException);
146 
147     // Try again, this time with table disabled.
148     UTIL.getHBaseAdmin().disableTable(tableName);
149     long procId3 =
150         procExec.submitProcedure(new DeleteColumnFamilyProcedure(procExec.getEnvironment(),
151             tableName, cf2.getBytes()));
152     // Wait the completion
153     ProcedureTestingUtility.waitProcedure(procExec, procId3);
154     // Expect fail with InvalidFamilyOperationException
155     result = procExec.getResult(procId2);
156     assertTrue(result.isFailed());
157     LOG.debug("Delete offline failed with exception: " + result.getException());
158     assertTrue(result.getException().getCause() instanceof InvalidFamilyOperationException);
159   }
160 
161   @Test(timeout=60000)
162   public void testDeleteNonExistingColumnFamily() throws Exception {
163     final TableName tableName = TableName.valueOf("testDeleteNonExistingColumnFamily");
164     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
165 
166     final String cf3 = "cf3";
167 
168     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2");
169 
170     // delete the column family that does not exist
171     long procId1 =
172         procExec.submitProcedure(new DeleteColumnFamilyProcedure(procExec.getEnvironment(),
173             tableName, cf3.getBytes()));
174     // Wait the completion
175     ProcedureTestingUtility.waitProcedure(procExec, procId1);
176 
177     ProcedureResult result = procExec.getResult(procId1);
178     assertTrue(result.isFailed());
179     LOG.debug("Delete failed with exception: " + result.getException());
180     assertTrue(result.getException().getCause() instanceof InvalidFamilyOperationException);
181   }
182 
183   @Test(timeout=60000)
184   public void testRecoveryAndDoubleExecutionOffline() throws Exception {
185     final TableName tableName = TableName.valueOf("testRecoveryAndDoubleExecutionOffline");
186     final String cf4 = "cf4";
187 
188     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
189 
190     // create the table
191     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2", "f3", cf4);
192     UTIL.getHBaseAdmin().disableTable(tableName);
193     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
194     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
195 
196     // Start the Delete procedure && kill the executor
197     long procId =
198         procExec.submitProcedure(new DeleteColumnFamilyProcedure(procExec.getEnvironment(),
199             tableName, cf4.getBytes()));
200 
201     // Restart the executor and execute the step twice
202     int numberOfSteps = DeleteColumnFamilyState.values().length;
203     MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId, numberOfSteps,
204       DeleteColumnFamilyState.values());
205 
206     MasterProcedureTestingUtility.validateColumnFamilyDeletion(UTIL.getHBaseCluster().getMaster(),
207       tableName, cf4);
208   }
209 
210   @Test(timeout = 60000)
211   public void testRecoveryAndDoubleExecutionOnline() throws Exception {
212     final TableName tableName = TableName.valueOf("testRecoveryAndDoubleExecutionOnline");
213     final String cf5 = "cf5";
214 
215     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
216 
217     // create the table
218     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2", "f3", cf5);
219     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
220     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
221 
222     // Start the Delete procedure && kill the executor
223     long procId =
224         procExec.submitProcedure(new DeleteColumnFamilyProcedure(procExec.getEnvironment(),
225             tableName, cf5.getBytes()));
226 
227     // Restart the executor and execute the step twice
228     int numberOfSteps = DeleteColumnFamilyState.values().length;
229     MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId, numberOfSteps,
230       DeleteColumnFamilyState.values());
231 
232     MasterProcedureTestingUtility.validateColumnFamilyDeletion(UTIL.getHBaseCluster().getMaster(),
233       tableName, cf5);
234   }
235 
236   @Test(timeout = 60000)
237   public void testRollbackAndDoubleExecution() throws Exception {
238     final TableName tableName = TableName.valueOf("testRollbackAndDoubleExecution");
239     final String cf5 = "cf5";
240 
241     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
242 
243     // create the table
244     HRegionInfo[] regions = MasterProcedureTestingUtility.createTable(
245       procExec, tableName, null, "f1", "f2", "f3", cf5);
246     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
247     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
248 
249     // Start the Delete procedure && kill the executor
250     long procId = procExec.submitProcedure(
251       new DeleteColumnFamilyProcedure(procExec.getEnvironment(), tableName, cf5.getBytes()));
252 
253     // Failing before DELETE_COLUMN_FAMILY_DELETE_FS_LAYOUT we should trigger the rollback
254     // NOTE: the 1 (number before DELETE_COLUMN_FAMILY_DELETE_FS_LAYOUT step) is hardcoded,
255     //       so you have to look at this test at least once when you add a new step.
256     int numberOfSteps = 1;
257     MasterProcedureTestingUtility.testRollbackAndDoubleExecution(
258       procExec,
259       procId,
260       numberOfSteps,
261       DeleteColumnFamilyState.values());
262 
263     MasterProcedureTestingUtility.validateTableCreation(
264       UTIL.getHBaseCluster().getMaster(), tableName, regions, "f1", "f2", "f3", cf5);
265   }
266 
267   @Test(timeout = 60000)
268   public void testRollbackAndDoubleExecutionAfterPONR() throws Exception {
269     final TableName tableName = TableName.valueOf("testRollbackAndDoubleExecutionAfterPONR");
270     final String cf5 = "cf5";
271 
272     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
273 
274     // create the table
275     HRegionInfo[] regions = MasterProcedureTestingUtility.createTable(
276       procExec, tableName, null, "f1", "f2", "f3", cf5);
277     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
278     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
279 
280     // Start the Delete procedure && kill the executor
281     long procId = procExec.submitProcedure(
282       new DeleteColumnFamilyProcedure(procExec.getEnvironment(), tableName, cf5.getBytes()));
283 
284     // Failing after DELETE_COLUMN_FAMILY_DELETE_FS_LAYOUT we should not trigger the rollback.
285     // NOTE: the 4 (number of DELETE_COLUMN_FAMILY_DELETE_FS_LAYOUT + 1 step) is hardcoded,
286     //       so you have to look at this test at least once when you add a new step.
287     int numberOfSteps = 4;
288     MasterProcedureTestingUtility.testRollbackAndDoubleExecutionAfterPONR(
289       procExec,
290       procId,
291       numberOfSteps,
292       DeleteColumnFamilyState.values());
293 
294     MasterProcedureTestingUtility.validateColumnFamilyDeletion(
295       UTIL.getHBaseCluster().getMaster(), tableName, cf5);
296   }
297 
298   private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
299     return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
300   }
301 }