HBASE-24033 Add ut for loading the corrupt recovered hfiles (#1322)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / CloneSnapshotFromClientTestBase.java
blob00cc1a04f760aba47934eb3b001c4bb96d248103
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.client;
20 import java.io.IOException;
21 import org.apache.hadoop.hbase.HBaseTestingUtility;
22 import org.apache.hadoop.hbase.HConstants;
23 import org.apache.hadoop.hbase.TableName;
24 import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
25 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
26 import org.apache.hadoop.hbase.util.Bytes;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Rule;
32 import org.junit.rules.TestName;
34 /**
35 * Base class for testing clone snapsot
37 public class CloneSnapshotFromClientTestBase {
39 protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
41 protected final byte[] FAMILY = Bytes.toBytes("cf");
43 protected String emptySnapshot;
44 protected String snapshotName0;
45 protected String snapshotName1;
46 protected String snapshotName2;
47 protected TableName tableName;
48 protected int snapshot0Rows;
49 protected int snapshot1Rows;
50 protected Admin admin;
52 @Rule
53 public TestName name = new TestName();
55 protected static void setupConfiguration() {
56 TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
57 TEST_UTIL.getConfiguration().setInt("hbase.hstore.compactionThreshold", 10);
58 TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
59 TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
60 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
61 TEST_UTIL.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true);
64 @BeforeClass
65 public static void setUpBeforeClass() throws Exception {
66 setupConfiguration();
67 TEST_UTIL.startMiniCluster(3);
70 @AfterClass
71 public static void tearDownAfterClass() throws Exception {
72 TEST_UTIL.shutdownMiniCluster();
75 protected final String getValidMethodName() {
76 return name.getMethodName().replaceAll("[^0-9A-Za-z_]", "_");
79 /**
80 * Initialize the tests with a table filled with some data and two snapshots (snapshotName0,
81 * snapshotName1) of different states. The tableName, snapshotNames and the number of rows in the
82 * snapshot are initialized.
84 @Before
85 public void setup() throws Exception {
86 this.admin = TEST_UTIL.getAdmin();
88 long tid = System.currentTimeMillis();
89 tableName = TableName.valueOf(getValidMethodName() + tid);
90 emptySnapshot = "emptySnaptb-" + tid;
91 snapshotName0 = "snaptb0-" + tid;
92 snapshotName1 = "snaptb1-" + tid;
93 snapshotName2 = "snaptb2-" + tid;
95 createTableAndSnapshots();
98 protected void createTable() throws IOException, InterruptedException {
99 SnapshotTestingUtils.createTable(TEST_UTIL, tableName, getNumReplicas(), FAMILY);
102 protected int numRowsToLoad() {
103 return 500;
106 protected int countRows(Table table) throws IOException {
107 return TEST_UTIL.countRows(table);
110 private void createTableAndSnapshots() throws Exception {
111 // create Table and disable it
112 createTable();
113 admin.disableTable(tableName);
115 // take an empty snapshot
116 admin.snapshot(emptySnapshot, tableName);
118 // enable table and insert data
119 admin.enableTable(tableName);
120 SnapshotTestingUtils.loadData(TEST_UTIL, tableName, numRowsToLoad(), FAMILY);
121 try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
122 snapshot0Rows = countRows(table);
124 admin.disableTable(tableName);
126 // take a snapshot
127 admin.snapshot(snapshotName0, tableName);
129 // enable table and insert more data
130 admin.enableTable(tableName);
131 SnapshotTestingUtils.loadData(TEST_UTIL, tableName, numRowsToLoad(), FAMILY);
132 try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
133 snapshot1Rows = countRows(table);
135 admin.disableTable(tableName);
137 // take a snapshot of the updated table
138 admin.snapshot(snapshotName1, tableName);
140 // re-enable table
141 admin.enableTable(tableName);
144 protected int getNumReplicas() {
145 return 1;
148 protected void verifyRowCount(final HBaseTestingUtility util, final TableName tableName,
149 long expectedRows) throws IOException {
150 SnapshotTestingUtils.verifyRowCount(util, tableName, expectedRows);
153 @After
154 public void tearDown() throws Exception {
155 if (admin.tableExists(tableName)) {
156 TEST_UTIL.deleteTable(tableName);
158 SnapshotTestingUtils.deleteAllSnapshots(admin);
159 SnapshotTestingUtils.deleteArchiveDirectory(TEST_UTIL);