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.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.AddColumnFamilyState;
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 TestAddColumnFamilyProcedure {
45    private static final Log LOG = LogFactory.getLog(TestAddColumnFamilyProcedure.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 testAddColumnFamily() throws Exception {
84      final TableName tableName = TableName.valueOf("testAddColumnFamily");
85      final String cf1 = "cf1";
86      final String cf2 = "cf2";
87      final HColumnDescriptor columnDescriptor1 = new HColumnDescriptor(cf1);
88      final HColumnDescriptor columnDescriptor2 = new HColumnDescriptor(cf2);
89      final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
90  
91      MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f3");
92  
93      // Test 1: Add a column family online
94      long procId1 =
95          procExec.submitProcedure(new AddColumnFamilyProcedure(procExec.getEnvironment(), tableName,
96              columnDescriptor1));
97      // Wait the completion
98      ProcedureTestingUtility.waitProcedure(procExec, procId1);
99      ProcedureTestingUtility.assertProcNotFailed(procExec, procId1);
100 
101     MasterProcedureTestingUtility.validateColumnFamilyAddition(UTIL.getHBaseCluster().getMaster(),
102       tableName, cf1);
103 
104     // Test 2: Add a column family offline
105     UTIL.getHBaseAdmin().disableTable(tableName);
106     long procId2 =
107         procExec.submitProcedure(new AddColumnFamilyProcedure(procExec.getEnvironment(), tableName,
108             columnDescriptor2));
109     // Wait the completion
110     ProcedureTestingUtility.waitProcedure(procExec, procId2);
111     ProcedureTestingUtility.assertProcNotFailed(procExec, procId2);
112     MasterProcedureTestingUtility.validateColumnFamilyAddition(UTIL.getHBaseCluster().getMaster(),
113       tableName, cf2);
114   }
115 
116   @Test(timeout=60000)
117   public void testAddSameColumnFamilyTwice() throws Exception {
118     final TableName tableName = TableName.valueOf("testAddColumnFamilyTwice");
119     final String cf2 = "cf2";
120     final HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf2);
121 
122     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
123 
124     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1");
125 
126     // add the column family
127     long procId1 =
128         procExec.submitProcedure(new AddColumnFamilyProcedure(procExec.getEnvironment(), tableName,
129             columnDescriptor));
130     // Wait the completion
131     ProcedureTestingUtility.waitProcedure(procExec, procId1);
132     ProcedureTestingUtility.assertProcNotFailed(procExec, procId1);
133     MasterProcedureTestingUtility.validateColumnFamilyAddition(UTIL.getHBaseCluster().getMaster(),
134       tableName, cf2);
135 
136     // add the column family that exists
137     long procId2 =
138         procExec.submitProcedure(new AddColumnFamilyProcedure(procExec.getEnvironment(), tableName,
139             columnDescriptor));
140     // Wait the completion
141     ProcedureTestingUtility.waitProcedure(procExec, procId2);
142 
143     // Second add should fail with InvalidFamilyOperationException
144     ProcedureResult result = procExec.getResult(procId2);
145     assertTrue(result.isFailed());
146     LOG.debug("Add failed with exception: " + result.getException());
147     assertTrue(result.getException().getCause() instanceof InvalidFamilyOperationException);
148 
149     // Do the same add the existing column family - this time offline
150     UTIL.getHBaseAdmin().disableTable(tableName);
151     long procId3 =
152         procExec.submitProcedure(new AddColumnFamilyProcedure(procExec.getEnvironment(), tableName,
153             columnDescriptor));
154     // Wait the completion
155     ProcedureTestingUtility.waitProcedure(procExec, procId3);
156 
157     // Second add should fail with InvalidFamilyOperationException
158     result = procExec.getResult(procId3);
159     assertTrue(result.isFailed());
160     LOG.debug("Add failed with exception: " + result.getException());
161     assertTrue(result.getException().getCause() instanceof InvalidFamilyOperationException);
162   }
163 
164   @Test(timeout = 60000)
165   public void testRecoveryAndDoubleExecutionOffline() throws Exception {
166     final TableName tableName = TableName.valueOf("testRecoveryAndDoubleExecutionOffline");
167     final String cf4 = "cf4";
168     final HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf4);
169     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
170     // create the table
171     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2", "f3");
172     UTIL.getHBaseAdmin().disableTable(tableName);
173 
174     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
175     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
176 
177     // Start the AddColumnFamily procedure && kill the executor
178     long procId =
179         procExec.submitProcedure(new AddColumnFamilyProcedure(procExec.getEnvironment(), tableName,
180             columnDescriptor));
181 
182     // Restart the executor and execute the step twice
183     int numberOfSteps = AddColumnFamilyState.values().length;
184     MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId, numberOfSteps,
185       AddColumnFamilyState.values());
186 
187     MasterProcedureTestingUtility.validateColumnFamilyAddition(UTIL.getHBaseCluster().getMaster(),
188       tableName, cf4);
189   }
190 
191   @Test(timeout = 60000)
192   public void testRecoveryAndDoubleExecutionOnline() throws Exception {
193     final TableName tableName = TableName.valueOf("testRecoveryAndDoubleExecutionOnline");
194     final String cf5 = "cf5";
195     final HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf5);
196     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
197     // create the table
198     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2", "f3");
199 
200     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
201     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
202 
203     // Start the AddColumnFamily procedure && kill the executor
204     long procId =
205         procExec.submitProcedure(new AddColumnFamilyProcedure(procExec.getEnvironment(), tableName,
206             columnDescriptor));
207 
208     // Restart the executor and execute the step twice
209     int numberOfSteps = AddColumnFamilyState.values().length;
210     MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId, numberOfSteps,
211       AddColumnFamilyState.values());
212 
213     MasterProcedureTestingUtility.validateColumnFamilyAddition(UTIL.getHBaseCluster().getMaster(),
214       tableName, cf5);
215   }
216 
217   @Test(timeout = 60000)
218   public void testRollbackAndDoubleExecution() throws Exception {
219     final TableName tableName = TableName.valueOf("testRollbackAndDoubleExecution");
220     final String cf6 = "cf6";
221     final HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf6);
222     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
223 
224     // create the table
225     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2");
226     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
227     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
228 
229     // Start the AddColumnFamily procedure && kill the executor
230     long procId =
231         procExec.submitProcedure(new AddColumnFamilyProcedure(procExec.getEnvironment(), tableName,
232             columnDescriptor));
233 
234     int numberOfSteps = AddColumnFamilyState.values().length - 2; // failing in the middle of proc
235     MasterProcedureTestingUtility.testRollbackAndDoubleExecution(procExec, procId, numberOfSteps,
236       AddColumnFamilyState.values());
237 
238     MasterProcedureTestingUtility.validateColumnFamilyDeletion(UTIL.getHBaseCluster().getMaster(),
239       tableName, cf6);
240   }
241 
242   private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
243     return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
244   }
245 }