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 static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import java.util.List;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.master.RegionState;
026import org.apache.hadoop.hbase.master.assignment.RegionStates;
027import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
030import org.junit.Test;
031
032public class CloneSnapshotFromClientAfterSplittingRegionTestBase
033    extends CloneSnapshotFromClientTestBase {
034
035  private void splitRegion(final RegionInfo regionInfo) throws IOException {
036    byte[][] splitPoints = Bytes.split(regionInfo.getStartKey(), regionInfo.getEndKey(), 1);
037    admin.split(regionInfo.getTable(), splitPoints[1]);
038  }
039
040  @Test
041  public void testCloneSnapshotAfterSplittingRegion() throws IOException, InterruptedException {
042    // Turn off the CatalogJanitor
043    admin.catalogJanitorSwitch(false);
044
045    try {
046      List<RegionInfo> regionInfos = admin.getRegions(tableName);
047      RegionReplicaUtil.removeNonDefaultRegions(regionInfos);
048
049      // Split the first region
050      splitRegion(regionInfos.get(0));
051
052      // Take a snapshot
053      admin.snapshot(snapshotName2, tableName);
054
055      // Clone the snapshot to another table
056      TableName clonedTableName =
057        TableName.valueOf(getValidMethodName() + "-" + EnvironmentEdgeManager.currentTime());
058      admin.cloneSnapshot(snapshotName2, clonedTableName);
059      SnapshotTestingUtils.waitForTableToBeOnline(TEST_UTIL, clonedTableName);
060
061      verifyRowCount(TEST_UTIL, clonedTableName, snapshot1Rows);
062
063      RegionStates regionStates =
064        TEST_UTIL.getHBaseCluster().getMaster().getAssignmentManager().getRegionStates();
065
066      // The region count of the cloned table should be the same as the one of the original table
067      int openRegionCountOfOriginalTable =
068        regionStates.getRegionByStateOfTable(tableName).get(RegionState.State.OPEN).size();
069      int openRegionCountOfClonedTable =
070        regionStates.getRegionByStateOfTable(clonedTableName).get(RegionState.State.OPEN).size();
071      assertEquals(openRegionCountOfOriginalTable, openRegionCountOfClonedTable);
072
073      int splitRegionCountOfOriginalTable =
074        regionStates.getRegionByStateOfTable(tableName).get(RegionState.State.SPLIT).size();
075      int splitRegionCountOfClonedTable =
076        regionStates.getRegionByStateOfTable(clonedTableName).get(RegionState.State.SPLIT).size();
077      assertEquals(splitRegionCountOfOriginalTable, splitRegionCountOfClonedTable);
078
079      TEST_UTIL.deleteTable(clonedTableName);
080    } finally {
081      admin.catalogJanitorSwitch(true);
082    }
083  }
084
085  @Test
086  public void testCloneSnapshotBeforeSplittingRegionAndDroppingTable()
087    throws IOException, InterruptedException {
088    // Turn off the CatalogJanitor
089    admin.catalogJanitorSwitch(false);
090
091    try {
092      // Take a snapshot
093      admin.snapshot(snapshotName2, tableName);
094
095      // Clone the snapshot to another table
096      TableName clonedTableName =
097        TableName.valueOf(getValidMethodName() + "-" + EnvironmentEdgeManager.currentTime());
098      admin.cloneSnapshot(snapshotName2, clonedTableName);
099      SnapshotTestingUtils.waitForTableToBeOnline(TEST_UTIL, clonedTableName);
100
101      // Split the first region of the original table
102      List<RegionInfo> regionInfos = admin.getRegions(tableName);
103      RegionReplicaUtil.removeNonDefaultRegions(regionInfos);
104      splitRegion(regionInfos.get(0));
105
106      // Drop the original table
107      admin.disableTable(tableName);
108      admin.deleteTable(tableName);
109
110      // Disable and enable the cloned table. This should be successful
111      admin.disableTable(clonedTableName);
112      admin.enableTable(clonedTableName);
113      SnapshotTestingUtils.waitForTableToBeOnline(TEST_UTIL, clonedTableName);
114
115      verifyRowCount(TEST_UTIL, clonedTableName, snapshot1Rows);
116    } finally {
117      admin.catalogJanitorSwitch(true);
118    }
119  }
120}