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.conf.Configuration; 022import org.apache.hadoop.hbase.HBaseTestingUtil; 023import org.apache.hadoop.hbase.HConstants; 024import org.apache.hadoop.hbase.TableName; 025import org.apache.hadoop.hbase.master.snapshot.SnapshotManager; 026import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils; 027import org.apache.hadoop.hbase.util.Bytes; 028import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 029import org.junit.After; 030import org.junit.AfterClass; 031import org.junit.Before; 032import org.junit.BeforeClass; 033import org.junit.Rule; 034import org.junit.rules.TestName; 035 036/** 037 * Base class for testing restore snapshot 038 */ 039public class RestoreSnapshotFromClientTestBase { 040 protected final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 041 042 protected final byte[] FAMILY = Bytes.toBytes("cf"); 043 protected final byte[] TEST_FAMILY2 = Bytes.toBytes("cf2"); 044 045 protected TableName tableName; 046 protected String emptySnapshot; 047 protected String snapshotName0; 048 protected String snapshotName1; 049 protected String snapshotName2; 050 protected int snapshot0Rows; 051 protected int snapshot1Rows; 052 protected Admin admin; 053 054 @Rule 055 public TestName name = new TestName(); 056 057 @BeforeClass 058 public static void setupCluster() throws Exception { 059 setupConf(TEST_UTIL.getConfiguration()); 060 TEST_UTIL.startMiniCluster(3); 061 } 062 063 protected static void setupConf(Configuration conf) { 064 TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true); 065 TEST_UTIL.getConfiguration().setInt("hbase.hstore.compactionThreshold", 10); 066 TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100); 067 TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250); 068 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6); 069 TEST_UTIL.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true); 070 } 071 072 @AfterClass 073 public static void tearDownAfterClass() throws Exception { 074 TEST_UTIL.shutdownMiniCluster(); 075 } 076 077 /** 078 * Initialize the tests with a table filled with some data and two snapshots (snapshotName0, 079 * snapshotName1) of different states. The tableName, snapshotNames and the number of rows in the 080 * snapshot are initialized. 081 */ 082 @Before 083 public void setup() throws Exception { 084 this.admin = TEST_UTIL.getAdmin(); 085 long tid = EnvironmentEdgeManager.currentTime(); 086 tableName = TableName.valueOf(getValidMethodName() + "-" + tid); 087 emptySnapshot = "emptySnaptb-" + tid; 088 snapshotName0 = "snaptb0-" + tid; 089 snapshotName1 = "snaptb1-" + tid; 090 snapshotName2 = "snaptb2-" + tid; 091 092 // create Table and disable it 093 createTable(); 094 admin.disableTable(tableName); 095 096 // take an empty snapshot 097 admin.snapshot(emptySnapshot, tableName); 098 099 // enable table and insert data 100 admin.enableTable(tableName); 101 SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 500, FAMILY); 102 try (Table table = TEST_UTIL.getConnection().getTable(tableName)) { 103 snapshot0Rows = countRows(table); 104 } 105 admin.disableTable(tableName); 106 107 // take a snapshot 108 admin.snapshot(snapshotName0, tableName); 109 110 // enable table and insert more data 111 admin.enableTable(tableName); 112 SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 500, FAMILY); 113 try (Table table = TEST_UTIL.getConnection().getTable(tableName)) { 114 snapshot1Rows = countRows(table); 115 } 116 } 117 118 protected void createTable() throws Exception { 119 SnapshotTestingUtils.createTable(TEST_UTIL, tableName, getNumReplicas(), FAMILY); 120 } 121 122 @After 123 public void tearDown() throws Exception { 124 TEST_UTIL.deleteTable(tableName); 125 SnapshotTestingUtils.deleteAllSnapshots(TEST_UTIL.getAdmin()); 126 SnapshotTestingUtils.deleteArchiveDirectory(TEST_UTIL); 127 } 128 129 protected int getNumReplicas() { 130 return 1; 131 } 132 133 protected int countRows(Table table, byte[]... families) throws IOException { 134 return TEST_UTIL.countRows(table, families); 135 } 136 137 protected void verifyRowCount(HBaseTestingUtil util, TableName tableName, long expectedRows) 138 throws IOException { 139 SnapshotTestingUtils.verifyRowCount(util, tableName, expectedRows); 140 } 141 142 protected final void splitRegion(RegionInfo regionInfo) throws IOException { 143 byte[][] splitPoints = Bytes.split(regionInfo.getStartKey(), regionInfo.getEndKey(), 1); 144 admin.split(regionInfo.getTable(), splitPoints[1]); 145 } 146 147 protected final String getValidMethodName() { 148 return name.getMethodName().replaceAll("[^0-9A-Za-z_]", "_"); 149 } 150}