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