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.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     ProcedureInfo result = procExec.getResult(procId2);
145     assertTrue(result.isFailed());
146     LOG.debug("Add failed with exception: " + result.getExceptionFullMessage());
147     assertTrue(
148       ProcedureTestingUtility.getExceptionCause(result) instanceof InvalidFamilyOperationException);
149 
150     // Do the same add the existing column family - this time offline
151     UTIL.getHBaseAdmin().disableTable(tableName);
152     long procId3 =
153         procExec.submitProcedure(new AddColumnFamilyProcedure(procExec.getEnvironment(), tableName,
154             columnDescriptor));
155     // Wait the completion
156     ProcedureTestingUtility.waitProcedure(procExec, procId3);
157 
158     // Second add should fail with InvalidFamilyOperationException
159     result = procExec.getResult(procId3);
160     assertTrue(result.isFailed());
161     LOG.debug("Add failed with exception: " + result.getExceptionFullMessage());
162     assertTrue(
163       ProcedureTestingUtility.getExceptionCause(result) instanceof InvalidFamilyOperationException);
164   }
165 
166   @Test(timeout = 60000)
167   public void testRecoveryAndDoubleExecutionOffline() throws Exception {
168     final TableName tableName = TableName.valueOf("testRecoveryAndDoubleExecutionOffline");
169     final String cf4 = "cf4";
170     final HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf4);
171     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
172     // create the table
173     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2", "f3");
174     UTIL.getHBaseAdmin().disableTable(tableName);
175 
176     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
177     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
178 
179     // Start the AddColumnFamily procedure && kill the executor
180     long procId =
181         procExec.submitProcedure(new AddColumnFamilyProcedure(procExec.getEnvironment(), tableName,
182             columnDescriptor));
183 
184     // Restart the executor and execute the step twice
185     int numberOfSteps = AddColumnFamilyState.values().length;
186     MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId, numberOfSteps,
187       AddColumnFamilyState.values());
188 
189     MasterProcedureTestingUtility.validateColumnFamilyAddition(UTIL.getHBaseCluster().getMaster(),
190       tableName, cf4);
191   }
192 
193   @Test(timeout = 60000)
194   public void testRecoveryAndDoubleExecutionOnline() throws Exception {
195     final TableName tableName = TableName.valueOf("testRecoveryAndDoubleExecutionOnline");
196     final String cf5 = "cf5";
197     final HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf5);
198     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
199     // create the table
200     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2", "f3");
201 
202     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
203     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
204 
205     // Start the AddColumnFamily procedure && kill the executor
206     long procId =
207         procExec.submitProcedure(new AddColumnFamilyProcedure(procExec.getEnvironment(), tableName,
208             columnDescriptor));
209 
210     // Restart the executor and execute the step twice
211     int numberOfSteps = AddColumnFamilyState.values().length;
212     MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId, numberOfSteps,
213       AddColumnFamilyState.values());
214 
215     MasterProcedureTestingUtility.validateColumnFamilyAddition(UTIL.getHBaseCluster().getMaster(),
216       tableName, cf5);
217   }
218 
219   @Test(timeout = 60000)
220   public void testRollbackAndDoubleExecution() throws Exception {
221     final TableName tableName = TableName.valueOf("testRollbackAndDoubleExecution");
222     final String cf6 = "cf6";
223     final HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf6);
224     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
225 
226     // create the table
227     MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2");
228     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
229     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
230 
231     // Start the AddColumnFamily procedure && kill the executor
232     long procId =
233         procExec.submitProcedure(new AddColumnFamilyProcedure(procExec.getEnvironment(), tableName,
234             columnDescriptor));
235 
236     int numberOfSteps = AddColumnFamilyState.values().length - 2; // failing in the middle of proc
237     MasterProcedureTestingUtility.testRollbackAndDoubleExecution(procExec, procId, numberOfSteps,
238       AddColumnFamilyState.values());
239 
240     MasterProcedureTestingUtility.validateColumnFamilyDeletion(UTIL.getHBaseCluster().getMaster(),
241       tableName, cf6);
242   }
243 
244   private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
245     return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
246   }
247 }