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.assertFalse; 021import static org.junit.Assert.assertTrue; 022 023import java.util.List; 024import java.util.Random; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseTestingUtility; 028import org.apache.hadoop.hbase.HTableDescriptor; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.client.RegionInfo; 031import org.apache.hadoop.hbase.procedure2.Procedure; 032import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 033import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; 034import org.apache.hadoop.hbase.testclassification.MasterTests; 035import org.apache.hadoop.hbase.testclassification.MediumTests; 036import org.junit.After; 037import org.junit.AfterClass; 038import org.junit.Before; 039import org.junit.BeforeClass; 040import org.junit.ClassRule; 041import org.junit.Rule; 042import org.junit.Test; 043import org.junit.experimental.categories.Category; 044import org.junit.rules.TestName; 045import org.slf4j.Logger; 046import org.slf4j.LoggerFactory; 047 048@Category({MasterTests.class, MediumTests.class}) 049public class TestProcedureAdmin { 050 051 @ClassRule 052 public static final HBaseClassTestRule CLASS_RULE = 053 HBaseClassTestRule.forClass(TestProcedureAdmin.class); 054 055 private static final Logger LOG = LoggerFactory.getLogger(TestProcedureAdmin.class); 056 @Rule public TestName name = new TestName(); 057 058 protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 059 060 private static void setupConf(Configuration conf) { 061 conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1); 062 } 063 064 @BeforeClass 065 public static void setupCluster() throws Exception { 066 setupConf(UTIL.getConfiguration()); 067 UTIL.startMiniCluster(1); 068 } 069 070 @AfterClass 071 public static void cleanupTest() throws Exception { 072 try { 073 UTIL.shutdownMiniCluster(); 074 } catch (Exception e) { 075 LOG.warn("failure shutting down cluster", e); 076 } 077 } 078 079 @Before 080 public void setup() throws Exception { 081 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 082 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false); 083 assertTrue("expected executor to be running", procExec.isRunning()); 084 } 085 086 @After 087 public void tearDown() throws Exception { 088 assertTrue("expected executor to be running", getMasterProcedureExecutor().isRunning()); 089 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(getMasterProcedureExecutor(), false); 090 for (HTableDescriptor htd: UTIL.getAdmin().listTables()) { 091 LOG.info("Tear down, remove table=" + htd.getTableName()); 092 UTIL.deleteTable(htd.getTableName()); 093 } 094 } 095 096 @Test 097 public void testAbortProcedureSuccess() throws Exception { 098 final TableName tableName = TableName.valueOf(name.getMethodName()); 099 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 100 101 MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f"); 102 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 103 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 104 // Submit an abortable procedure 105 long procId = procExec.submitProcedure( 106 new DisableTableProcedure(procExec.getEnvironment(), tableName, false)); 107 // Wait for one step to complete 108 ProcedureTestingUtility.waitProcedure(procExec, procId); 109 110 boolean abortResult = procExec.abort(procId, true); 111 assertTrue(abortResult); 112 113 MasterProcedureTestingUtility.testRestartWithAbort(procExec, procId); 114 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 115 // Validate the disable table procedure was aborted successfully 116 MasterProcedureTestingUtility.validateTableIsEnabled( 117 UTIL.getHBaseCluster().getMaster(), 118 tableName); 119 } 120 121 @Test 122 public void testAbortProcedureFailure() throws Exception { 123 final TableName tableName = TableName.valueOf(name.getMethodName()); 124 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 125 126 RegionInfo[] regions = 127 MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f"); 128 UTIL.getAdmin().disableTable(tableName); 129 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 130 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 131 // Submit an un-abortable procedure 132 long procId = procExec.submitProcedure( 133 new DeleteTableProcedure(procExec.getEnvironment(), tableName)); 134 // Wait for a couple of steps to complete (first step "prepare" is abortable) 135 ProcedureTestingUtility.waitProcedure(procExec, procId); 136 for (int i = 0; i < 2; ++i) { 137 ProcedureTestingUtility.assertProcNotYetCompleted(procExec, procId); 138 ProcedureTestingUtility.restart(procExec); 139 ProcedureTestingUtility.waitProcedure(procExec, procId); 140 } 141 142 boolean abortResult = procExec.abort(procId, true); 143 assertFalse(abortResult); 144 145 MasterProcedureTestingUtility.testRestartWithAbort(procExec, procId); 146 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 147 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 148 // Validate the delete table procedure was not aborted 149 MasterProcedureTestingUtility.validateTableDeletion( 150 UTIL.getHBaseCluster().getMaster(), tableName); 151 } 152 153 @Test 154 public void testAbortProcedureInterruptedNotAllowed() throws Exception { 155 final TableName tableName = TableName.valueOf(name.getMethodName()); 156 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 157 158 RegionInfo[] regions = 159 MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f"); 160 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 161 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 162 // Submit a procedure 163 long procId = procExec.submitProcedure( 164 new DisableTableProcedure(procExec.getEnvironment(), tableName, true)); 165 // Wait for one step to complete 166 ProcedureTestingUtility.waitProcedure(procExec, procId); 167 168 // Set the mayInterruptIfRunning flag to false 169 boolean abortResult = procExec.abort(procId, false); 170 assertFalse(abortResult); 171 172 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false); 173 ProcedureTestingUtility.restart(procExec); 174 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 175 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 176 // Validate the delete table procedure was not aborted 177 MasterProcedureTestingUtility.validateTableIsDisabled( 178 UTIL.getHBaseCluster().getMaster(), tableName); 179 } 180 181 @Test 182 public void testAbortNonExistProcedure() throws Exception { 183 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 184 Random randomGenerator = new Random(); 185 long procId; 186 // Generate a non-existing procedure 187 do { 188 procId = randomGenerator.nextLong(); 189 } while (procExec.getResult(procId) != null); 190 191 boolean abortResult = procExec.abort(procId, true); 192 assertFalse(abortResult); 193 } 194 195 @Test 196 public void testGetProcedure() throws Exception { 197 final TableName tableName = TableName.valueOf(name.getMethodName()); 198 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 199 200 MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f"); 201 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 202 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true); 203 204 long procId = procExec.submitProcedure( 205 new DisableTableProcedure(procExec.getEnvironment(), tableName, false)); 206 // Wait for one step to complete 207 ProcedureTestingUtility.waitProcedure(procExec, procId); 208 209 List<Procedure<MasterProcedureEnv>> procedures = procExec.getProcedures(); 210 assertTrue(procedures.size() >= 1); 211 boolean found = false; 212 for (Procedure<?> proc: procedures) { 213 if (proc.getProcId() == procId) { 214 assertTrue(proc.isRunnable()); 215 found = true; 216 } else { 217 assertTrue(proc.isSuccess()); 218 } 219 } 220 assertTrue(found); 221 222 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false); 223 ProcedureTestingUtility.restart(procExec); 224 ProcedureTestingUtility.waitNoProcedureRunning(procExec); 225 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 226 procedures = procExec.getProcedures(); 227 for (Procedure proc: procedures) { 228 assertTrue(proc.isSuccess()); 229 } 230 } 231 232 private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() { 233 return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor(); 234 } 235}