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