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.client;
019
020import java.io.IOException;
021import org.apache.hadoop.hbase.TableName;
022import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
023import org.junit.Test;
024
025public class CloneSnapshotFromClientCloneLinksAfterDeleteTestBase
026    extends CloneSnapshotFromClientTestBase {
027
028  /**
029   * Verify that tables created from the snapshot are still alive after source table deletion.
030   */
031  @Test
032  public void testCloneLinksAfterDelete() throws IOException, InterruptedException {
033    // Clone a table from the first snapshot
034    final TableName clonedTableName =
035      TableName.valueOf(getValidMethodName() + "1-" + EnvironmentEdgeManager.currentTime());
036    admin.cloneSnapshot(snapshotName0, clonedTableName);
037    verifyRowCount(TEST_UTIL, clonedTableName, snapshot0Rows);
038
039    // Take a snapshot of this cloned table.
040    admin.disableTable(clonedTableName);
041    admin.snapshot(snapshotName2, clonedTableName);
042
043    // Clone the snapshot of the cloned table
044    final TableName clonedTableName2 =
045      TableName.valueOf(getValidMethodName() + "2-" + EnvironmentEdgeManager.currentTime());
046    admin.cloneSnapshot(snapshotName2, clonedTableName2);
047    verifyRowCount(TEST_UTIL, clonedTableName2, snapshot0Rows);
048    admin.disableTable(clonedTableName2);
049
050    // Remove the original table
051    TEST_UTIL.deleteTable(tableName);
052    waitCleanerRun();
053
054    // Verify the first cloned table
055    admin.enableTable(clonedTableName);
056    verifyRowCount(TEST_UTIL, clonedTableName, snapshot0Rows);
057
058    // Verify the second cloned table
059    admin.enableTable(clonedTableName2);
060    verifyRowCount(TEST_UTIL, clonedTableName2, snapshot0Rows);
061    admin.disableTable(clonedTableName2);
062
063    // Delete the first cloned table
064    TEST_UTIL.deleteTable(clonedTableName);
065    waitCleanerRun();
066
067    // Verify the second cloned table
068    admin.enableTable(clonedTableName2);
069    verifyRowCount(TEST_UTIL, clonedTableName2, snapshot0Rows);
070
071    // Clone a new table from cloned
072    final TableName clonedTableName3 =
073      TableName.valueOf(getValidMethodName() + "3-" + EnvironmentEdgeManager.currentTime());
074    admin.cloneSnapshot(snapshotName2, clonedTableName3);
075    verifyRowCount(TEST_UTIL, clonedTableName3, snapshot0Rows);
076
077    // Delete the cloned tables
078    TEST_UTIL.deleteTable(clonedTableName2);
079    TEST_UTIL.deleteTable(clonedTableName3);
080    admin.deleteSnapshot(snapshotName2);
081  }
082
083  private void waitCleanerRun() throws InterruptedException {
084    TEST_UTIL.getMiniHBaseCluster().getMaster().getHFileCleaner().choreForTesting();
085  }
086}