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.junit.Test;
030
031public class CloneSnapshotFromClientAfterSplittingRegionTestBase
032    extends CloneSnapshotFromClientTestBase {
033
034  private void splitRegion(final RegionInfo regionInfo) throws IOException {
035    byte[][] splitPoints = Bytes.split(regionInfo.getStartKey(), regionInfo.getEndKey(), 1);
036    admin.split(regionInfo.getTable(), splitPoints[1]);
037  }
038
039  @Test
040  public void testCloneSnapshotAfterSplittingRegion() throws IOException, InterruptedException {
041    // Turn off the CatalogJanitor
042    admin.catalogJanitorSwitch(false);
043
044    try {
045      List<RegionInfo> regionInfos = admin.getRegions(tableName);
046      RegionReplicaUtil.removeNonDefaultRegions(regionInfos);
047
048      // Split the first region
049      splitRegion(regionInfos.get(0));
050
051      // Take a snapshot
052      admin.snapshot(snapshotName2, tableName);
053
054      // Clone the snapshot to another table
055      TableName clonedTableName =
056        TableName.valueOf(getValidMethodName() + "-" + System.currentTimeMillis());
057      admin.cloneSnapshot(snapshotName2, clonedTableName);
058      SnapshotTestingUtils.waitForTableToBeOnline(TEST_UTIL, clonedTableName);
059
060      verifyRowCount(TEST_UTIL, clonedTableName, snapshot1Rows);
061
062      RegionStates regionStates =
063        TEST_UTIL.getHBaseCluster().getMaster().getAssignmentManager().getRegionStates();
064
065      // The region count of the cloned table should be the same as the one of the original table
066      int openRegionCountOfOriginalTable =
067        regionStates.getRegionByStateOfTable(tableName).get(RegionState.State.OPEN).size();
068      int openRegionCountOfClonedTable =
069        regionStates.getRegionByStateOfTable(clonedTableName).get(RegionState.State.OPEN).size();
070      assertEquals(openRegionCountOfOriginalTable, openRegionCountOfClonedTable);
071
072      int splitRegionCountOfOriginalTable =
073        regionStates.getRegionByStateOfTable(tableName).get(RegionState.State.SPLIT).size();
074      int splitRegionCountOfClonedTable =
075        regionStates.getRegionByStateOfTable(clonedTableName).get(RegionState.State.SPLIT).size();
076      assertEquals(splitRegionCountOfOriginalTable, splitRegionCountOfClonedTable);
077
078      TEST_UTIL.deleteTable(clonedTableName);
079    } finally {
080      admin.catalogJanitorSwitch(true);
081    }
082  }
083}