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.HColumnDescriptor;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.InvalidFamilyOperationException;
30  import org.apache.hadoop.hbase.ProcedureInfo;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
33  import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
34  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.ModifyColumnFamilyState;
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 TestModifyColumnFamilyProcedure {
45    private static final Log LOG = LogFactory.getLog(TestModifyColumnFamilyProcedure.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 testModifyColumnFamily() throws Exception {
84      final TableName tableName = TableName.valueOf("testModifyColumnFamily");
85      final String cf1 = "cf1";
86      final HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf1);
87      int oldBlockSize = columnDescriptor.getBlocksize();
88      int newBlockSize = 3 * oldBlockSize;
89  
90      final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
91  
92      MasterProcedureTestingUtility.createTable(procExec, tableName, null, cf1, "f2");
93  
94      // Test 1: modify the column family online
95      columnDescriptor.setBlocksize(newBlockSize);
96      long procId1 = procExec.submitProcedure(
97        new ModifyColumnFamilyProcedure(procExec.getEnvironment(), tableName, columnDescriptor));
98      // Wait the completion
99      ProcedureTestingUtility.waitProcedure(procExec, procId1);
100     ProcedureTestingUtility.assertProcNotFailed(procExec, procId1);
101     MasterProcedureTestingUtility.validateColumnFamilyModification(UTIL.getHBaseCluster()
102         .getMaster(), tableName, cf1, columnDescriptor);
103 
104     // Test 2: modify the column family offline
105     UTIL.getHBaseAdmin().disableTable(tableName);
106     columnDescriptor.setBlocksize(newBlockSize * 2);
107     long procId2 = procExec.submitProcedure(
108       new ModifyColumnFamilyProcedure(procExec.getEnvironment(), tableName, columnDescriptor));
109     // Wait the completion
110     ProcedureTestingUtility.waitProcedure(procExec, procId2);
111     ProcedureTestingUtility.assertProcNotFailed(procExec, procId2);
112     MasterProcedureTestingUtility.validateColumnFamilyModification(UTIL.getHBaseCluster()
113         .getMaster(), tableName, cf1, columnDescriptor);
114   }
115 
116   @Test(timeout=60000)
117   public void testModifyNonExistingColumnFamily() throws Exception {
118     final TableName tableName = TableName.valueOf("testModifyExistingColumnFamily");
119     final String cf2 = "cf2";
120     final HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf2);
121     int oldBlockSize = columnDescriptor.getBlocksize();
122     int newBlockSize = 2 * oldBlockSize;
123 
124     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
125 
126     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1");
127 
128     // Modify the column family that does not exist
129     columnDescriptor.setBlocksize(newBlockSize);
130     long procId1 = procExec.submitProcedure(
131       new ModifyColumnFamilyProcedure(procExec.getEnvironment(), tableName, columnDescriptor));
132     // Wait the completion
133     ProcedureTestingUtility.waitProcedure(procExec, procId1);
134 
135     ProcedureInfo result = procExec.getResult(procId1);
136     assertTrue(result.isFailed());
137     LOG.debug("Modify failed with exception: " + result.getExceptionFullMessage());
138     assertTrue(
139       ProcedureTestingUtility.getExceptionCause(result) instanceof InvalidFamilyOperationException);
140   }
141 
142   @Test(timeout=60000)
143   public void testRecoveryAndDoubleExecutionOffline() throws Exception {
144     final TableName tableName = TableName.valueOf("testRecoveryAndDoubleExecutionOffline");
145     final String cf3 = "cf3";
146     final HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf3);
147     int oldBlockSize = columnDescriptor.getBlocksize();
148     int newBlockSize = 4 * oldBlockSize;
149 
150     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
151 
152     // create the table
153     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2", cf3);
154     UTIL.getHBaseAdmin().disableTable(tableName);
155     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
156     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
157 
158     // Start the Modify procedure && kill the executor
159     columnDescriptor.setBlocksize(newBlockSize);
160     long procId = procExec.submitProcedure(
161       new ModifyColumnFamilyProcedure(procExec.getEnvironment(), tableName, columnDescriptor));
162 
163     // Restart the executor and execute the step twice
164     int numberOfSteps = ModifyColumnFamilyState.values().length;
165     MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(
166       procExec,
167       procId,
168       numberOfSteps,
169       ModifyColumnFamilyState.values());
170 
171     MasterProcedureTestingUtility.validateColumnFamilyModification(UTIL.getHBaseCluster()
172         .getMaster(), tableName, cf3, columnDescriptor);
173   }
174 
175   @Test(timeout = 60000)
176   public void testRecoveryAndDoubleExecutionOnline() throws Exception {
177     final TableName tableName = TableName.valueOf("testRecoveryAndDoubleExecutionOnline");
178     final String cf4 = "cf4";
179     final HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf4);
180     int oldBlockSize = columnDescriptor.getBlocksize();
181     int newBlockSize = 4 * oldBlockSize;
182 
183     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
184 
185     // create the table
186     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2", cf4);
187     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
188     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
189 
190     // Start the Modify procedure && kill the executor
191     columnDescriptor.setBlocksize(newBlockSize);
192     long procId = procExec.submitProcedure(
193       new ModifyColumnFamilyProcedure(procExec.getEnvironment(), tableName, columnDescriptor));
194 
195     // Restart the executor and execute the step twice
196     int numberOfSteps = ModifyColumnFamilyState.values().length;
197     MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId, numberOfSteps,
198       ModifyColumnFamilyState.values());
199 
200     MasterProcedureTestingUtility.validateColumnFamilyModification(UTIL.getHBaseCluster()
201         .getMaster(), tableName, cf4, columnDescriptor);
202   }
203 
204   @Test(timeout = 60000)
205   public void testRollbackAndDoubleExecution() throws Exception {
206     final TableName tableName = TableName.valueOf("testRollbackAndDoubleExecution");
207     final String cf3 = "cf3";
208     final HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf3);
209     int oldBlockSize = columnDescriptor.getBlocksize();
210     int newBlockSize = 4 * oldBlockSize;
211 
212     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
213 
214     // create the table
215     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2", cf3);
216     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
217     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
218 
219     // Start the Modify procedure && kill the executor
220     columnDescriptor.setBlocksize(newBlockSize);
221     long procId = procExec.submitProcedure(
222       new ModifyColumnFamilyProcedure(procExec.getEnvironment(), tableName, columnDescriptor));
223 
224     // Failing in the middle of proc
225     int numberOfSteps = ModifyColumnFamilyState.values().length - 2;
226     MasterProcedureTestingUtility.testRollbackAndDoubleExecution(
227       procExec,
228       procId,
229       numberOfSteps,
230       ModifyColumnFamilyState.values());
231   }
232 
233   private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
234     return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
235   }
236 }