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