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.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertNotNull;
023import static org.junit.Assert.assertNull;
024import static org.junit.Assert.assertTrue;
025import static org.junit.Assert.fail;
026
027import java.util.ArrayList;
028import java.util.List;
029
030import org.apache.hadoop.fs.FileSystem;
031import org.apache.hadoop.fs.FileUtil;
032import org.apache.hadoop.fs.Path;
033import org.apache.hadoop.hbase.HBaseClassTestRule;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.TableNotDisabledException;
036import org.apache.hadoop.hbase.TableNotFoundException;
037import org.apache.hadoop.hbase.client.RegionInfo;
038import org.apache.hadoop.hbase.client.Table;
039import org.apache.hadoop.hbase.master.MasterFileSystem;
040import org.apache.hadoop.hbase.procedure2.Procedure;
041import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
042import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
043import org.apache.hadoop.hbase.regionserver.HRegion;
044import org.apache.hadoop.hbase.testclassification.MasterTests;
045import org.apache.hadoop.hbase.testclassification.MediumTests;
046import org.apache.hadoop.hbase.util.Bytes;
047import org.apache.hadoop.hbase.util.FSUtils;
048import org.apache.hadoop.hbase.util.HFileArchiveTestingUtil;
049import org.junit.ClassRule;
050import org.junit.Rule;
051import org.junit.Test;
052import org.junit.experimental.categories.Category;
053import org.junit.rules.TestName;
054import org.slf4j.Logger;
055import org.slf4j.LoggerFactory;
056
057
058@Category({MasterTests.class, MediumTests.class})
059public class TestDeleteTableProcedure extends TestTableDDLProcedureBase {
060
061  @ClassRule
062  public static final HBaseClassTestRule CLASS_RULE =
063      HBaseClassTestRule.forClass(TestDeleteTableProcedure.class);
064
065  private static final Logger LOG = LoggerFactory.getLogger(TestDeleteTableProcedure.class);
066  @Rule public TestName name = new TestName();
067
068  @Test(expected=TableNotFoundException.class)
069  public void testDeleteNotExistentTable() throws Exception {
070    final TableName tableName = TableName.valueOf(name.getMethodName());
071
072    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
073    ProcedurePrepareLatch latch = new ProcedurePrepareLatch.CompatibilityLatch();
074    long procId = ProcedureTestingUtility.submitAndWait(procExec,
075        new DeleteTableProcedure(procExec.getEnvironment(), tableName, latch));
076    latch.await();
077  }
078
079  @Test(expected=TableNotDisabledException.class)
080  public void testDeleteNotDisabledTable() throws Exception {
081    final TableName tableName = TableName.valueOf(name.getMethodName());
082
083    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
084    MasterProcedureTestingUtility.createTable(procExec, tableName, null, "f");
085
086    ProcedurePrepareLatch latch = new ProcedurePrepareLatch.CompatibilityLatch();
087    long procId = ProcedureTestingUtility.submitAndWait(procExec,
088        new DeleteTableProcedure(procExec.getEnvironment(), tableName, latch));
089    latch.await();
090  }
091
092  @Test
093  public void testDeleteDeletedTable() throws Exception {
094    final TableName tableName = TableName.valueOf(name.getMethodName());
095    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
096
097    RegionInfo[] regions = MasterProcedureTestingUtility.createTable(
098      procExec, tableName, null, "f");
099    UTIL.getAdmin().disableTable(tableName);
100
101    // delete the table (that exists)
102    long procId1 = procExec.submitProcedure(
103        new DeleteTableProcedure(procExec.getEnvironment(), tableName));
104    // delete the table (that will no longer exist)
105    long procId2 = procExec.submitProcedure(
106        new DeleteTableProcedure(procExec.getEnvironment(), tableName));
107
108    // Wait the completion
109    ProcedureTestingUtility.waitProcedure(procExec, procId1);
110    ProcedureTestingUtility.waitProcedure(procExec, procId2);
111
112    // First delete should succeed
113    ProcedureTestingUtility.assertProcNotFailed(procExec, procId1);
114    MasterProcedureTestingUtility.validateTableDeletion(getMaster(), tableName);
115
116    // Second delete should fail with TableNotFound
117    Procedure<?> result = procExec.getResult(procId2);
118    assertTrue(result.isFailed());
119    LOG.debug("Delete failed with exception: " + result.getException());
120    assertTrue(ProcedureTestingUtility.getExceptionCause(result) instanceof TableNotFoundException);
121  }
122
123  @Test
124  public void testSimpleDelete() throws Exception {
125    final TableName tableName = TableName.valueOf(name.getMethodName());
126    final byte[][] splitKeys = null;
127    testSimpleDelete(tableName, splitKeys);
128  }
129
130  @Test
131  public void testSimpleDeleteWithSplits() throws Exception {
132    final TableName tableName = TableName.valueOf(name.getMethodName());
133    final byte[][] splitKeys = new byte[][] {
134      Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c")
135    };
136    testSimpleDelete(tableName, splitKeys);
137  }
138
139  @Test
140  public void testDeleteFromMeta() throws Exception {
141    final TableName tableName = TableName.valueOf(name.getMethodName());
142    RegionInfo[] regions = MasterProcedureTestingUtility.createTable(
143      getMasterProcedureExecutor(), tableName, null, "f1", "f2");
144    List<RegionInfo> regionsList = new ArrayList<>();
145    UTIL.getAdmin().disableTable(tableName);
146    MasterProcedureEnv procedureEnv = getMasterProcedureExecutor().getEnvironment();
147    assertNotNull("Table should be on TableDescriptors cache.",
148      procedureEnv.getMasterServices().getTableDescriptors().get(tableName));
149    DeleteTableProcedure.deleteFromMeta(procedureEnv, tableName, regionsList);
150    assertNull("Table shouldn't be on TableDescriptors anymore.",
151      procedureEnv.getMasterServices().getTableDescriptors().get(tableName));
152  }
153
154  private void testSimpleDelete(final TableName tableName, byte[][] splitKeys) throws Exception {
155    RegionInfo[] regions = MasterProcedureTestingUtility.createTable(
156      getMasterProcedureExecutor(), tableName, splitKeys, "f1", "f2");
157    UTIL.getAdmin().disableTable(tableName);
158
159    // delete the table
160    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
161    long procId = ProcedureTestingUtility.submitAndWait(procExec,
162      new DeleteTableProcedure(procExec.getEnvironment(), tableName));
163    ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
164    MasterProcedureTestingUtility.validateTableDeletion(getMaster(), tableName);
165  }
166
167  @Test
168  public void testRecoveryAndDoubleExecution() throws Exception {
169    final TableName tableName = TableName.valueOf(name.getMethodName());
170
171    // create the table
172    byte[][] splitKeys = null;
173    RegionInfo[] regions = MasterProcedureTestingUtility.createTable(
174      getMasterProcedureExecutor(), tableName, splitKeys, "f1", "f2");
175    UTIL.getAdmin().disableTable(tableName);
176
177    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
178    ProcedureTestingUtility.waitNoProcedureRunning(procExec);
179    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
180
181    // Start the Delete procedure && kill the executor
182    long procId = procExec.submitProcedure(
183      new DeleteTableProcedure(procExec.getEnvironment(), tableName));
184
185    // Restart the executor and execute the step twice
186    MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId);
187
188    MasterProcedureTestingUtility.validateTableDeletion(getMaster(), tableName);
189  }
190
191  @Test
192  public void testDeleteWhenTempDirIsNotEmpty() throws Exception {
193    final TableName tableName = TableName.valueOf(name.getMethodName());
194    final String FAM = "fam";
195    final byte[][] splitKeys = new byte[][] {
196      Bytes.toBytes("b"), Bytes.toBytes("c"), Bytes.toBytes("d")
197    };
198
199    // create the table
200    MasterProcedureTestingUtility.createTable(
201      getMasterProcedureExecutor(), tableName, splitKeys, FAM);
202
203    // get the current store files for the regions
204    List<HRegion> regions = UTIL.getHBaseCluster().getRegions(tableName);
205    // make sure we have 4 regions serving this table
206    assertEquals(4, regions.size());
207
208    // load the table
209    try (Table table = UTIL.getConnection().getTable(tableName)) {
210      UTIL.loadTable(table, Bytes.toBytes(FAM));
211    }
212
213    // disable the table so that we can manipulate the files
214    UTIL.getAdmin().disableTable(tableName);
215
216    final MasterFileSystem masterFileSystem =
217      UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem();
218    final Path tableDir = FSUtils.getTableDir(masterFileSystem.getRootDir(), tableName);
219    final Path tempDir = masterFileSystem.getTempDir();
220    final Path tempTableDir = FSUtils.getTableDir(tempDir, tableName);
221    final FileSystem fs = masterFileSystem.getFileSystem();
222
223    // copy the table to the temporary directory to make sure the temp directory is not empty
224    if (!FileUtil.copy(fs, tableDir, fs, tempTableDir, false, UTIL.getConfiguration())) {
225      fail();
226    }
227
228    // delete the table
229    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
230    long procId = ProcedureTestingUtility.submitAndWait(procExec,
231      new DeleteTableProcedure(procExec.getEnvironment(), tableName));
232    ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
233    MasterProcedureTestingUtility.validateTableDeletion(getMaster(), tableName);
234
235    // check if the temporary directory is deleted
236    assertFalse(fs.exists(tempTableDir));
237
238    // check for the existence of the archive directory
239    for (HRegion region : regions) {
240      Path archiveDir = HFileArchiveTestingUtil.getRegionArchiveDir(UTIL.getConfiguration(),
241        region);
242      assertTrue(fs.exists(archiveDir));
243    }
244  }
245}