001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.master.procedure; 019 020import static org.junit.Assert.assertTrue; 021 022import org.apache.hadoop.hbase.HBaseClassTestRule; 023import org.apache.hadoop.hbase.TableName; 024import org.apache.hadoop.hbase.TableNotDisabledException; 025import org.apache.hadoop.hbase.procedure2.Procedure; 026import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 027import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; 028import org.apache.hadoop.hbase.testclassification.MasterTests; 029import org.apache.hadoop.hbase.testclassification.MediumTests; 030import org.apache.hadoop.hbase.util.Bytes; 031import org.junit.Assert; 032import org.junit.ClassRule; 033import org.junit.Rule; 034import org.junit.Test; 035import org.junit.experimental.categories.Category; 036import org.junit.rules.TestName; 037import org.slf4j.Logger; 038import org.slf4j.LoggerFactory; 039 040@Category({MasterTests.class, MediumTests.class}) 041public class TestEnableTableProcedure extends TestTableDDLProcedureBase { 042 043 @ClassRule 044 public static final HBaseClassTestRule CLASS_RULE = 045 HBaseClassTestRule.forClass(TestEnableTableProcedure.class); 046 047 private static final Logger LOG = LoggerFactory.getLogger(TestEnableTableProcedure.class); 048 @Rule public TestName name = new TestName(); 049 050 @Test 051 public void testEnableTable() throws Exception { 052 final TableName tableName = TableName.valueOf(name.getMethodName()); 053 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 054 055 MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2"); 056 UTIL.getAdmin().disableTable(tableName); 057 058 // Enable the table 059 long procId = procExec.submitProcedure( 060 new EnableTableProcedure(procExec.getEnvironment(), tableName, false)); 061 // Wait the completion 062 ProcedureTestingUtility.waitProcedure(procExec, procId); 063 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 064 MasterProcedureTestingUtility.validateTableIsEnabled(getMaster(), tableName); 065 } 066 067 @Test(expected=TableNotDisabledException.class) 068 public void testEnableNonDisabledTable() throws Exception { 069 final TableName tableName = TableName.valueOf(name.getMethodName()); 070 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 071 072 MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f1", "f2"); 073 074 // Enable the table - expect failure 075 long procId1 = procExec.submitProcedure( 076 new EnableTableProcedure(procExec.getEnvironment(), tableName, false)); 077 ProcedureTestingUtility.waitProcedure(procExec, procId1); 078 079 Procedure<?> result = procExec.getResult(procId1); 080 assertTrue(result.isFailed()); 081 LOG.debug("Enable failed with exception: " + result.getException()); 082 assertTrue( 083 ProcedureTestingUtility.getExceptionCause(result) instanceof TableNotDisabledException); 084 085 // Enable the table with skipping table state check flag (simulate recovery scenario) 086 long procId2 = procExec.submitProcedure( 087 new EnableTableProcedure(procExec.getEnvironment(), tableName, true)); 088 // Wait the completion 089 ProcedureTestingUtility.waitProcedure(procExec, procId2); 090 ProcedureTestingUtility.assertProcNotFailed(procExec, procId2); 091 092 // Enable the table - expect failure from ProcedurePrepareLatch 093 final ProcedurePrepareLatch prepareLatch = new ProcedurePrepareLatch.CompatibilityLatch(); 094 long procId3 = procExec.submitProcedure( 095 new EnableTableProcedure(procExec.getEnvironment(), tableName, false, prepareLatch)); 096 prepareLatch.await(); 097 Assert.fail("Enable should throw exception through latch."); 098 } 099 100 @Test 101 public void testRecoveryAndDoubleExecution() throws Exception { 102 final TableName tableName = TableName.valueOf(name.getMethodName()); 103 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 104 105 final byte[][] splitKeys = new byte[][] { 106 Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") 107 }; 108 MasterProcedureTestingUtility.createTable(procExec, tableName, splitKeys, "f1", "f2"); 109 UTIL.getAdmin().disableTable(tableName); 110 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 111 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 112 113 // Start the Enable procedure && kill the executor 114 long procId = procExec.submitProcedure( 115 new EnableTableProcedure(procExec.getEnvironment(), tableName, false)); 116 117 // Restart the executor and execute the step twice 118 MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId); 119 120 MasterProcedureTestingUtility.validateTableIsEnabled(getMaster(), tableName); 121 } 122 123 @Test 124 public void testRollbackAndDoubleExecution() throws Exception { 125 final TableName tableName = TableName.valueOf(name.getMethodName()); 126 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 127 128 final byte[][] splitKeys = new byte[][] { 129 Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") 130 }; 131 MasterProcedureTestingUtility.createTable(procExec, tableName, splitKeys, "f1", "f2"); 132 UTIL.getAdmin().disableTable(tableName); 133 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 134 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 135 136 // Start the Enable procedure && kill the executor 137 long procId = procExec.submitProcedure( 138 new EnableTableProcedure(procExec.getEnvironment(), tableName, false)); 139 140 int lastStep = 3; // fail before ENABLE_TABLE_SET_ENABLING_TABLE_STATE 141 MasterProcedureTestingUtility.testRollbackAndDoubleExecution(procExec, procId, lastStep); 142 MasterProcedureTestingUtility.validateTableIsDisabled(getMaster(), tableName); 143 } 144}