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