HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / CloneSnapshotFromClientTestBase.java
blobfa44c5c35ec42a83a03e7fc727c80e2067874a40
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.HBaseTestingUtil;
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.apache.hadoop.hbase.util.EnvironmentEdgeManager;
28 import org.junit.After;
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Rule;
33 import org.junit.rules.TestName;
35 /**
36 * Base class for testing clone snapsot
38 public class CloneSnapshotFromClientTestBase {
40 protected final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
42 protected final byte[] FAMILY = Bytes.toBytes("cf");
44 protected String emptySnapshot;
45 protected String snapshotName0;
46 protected String snapshotName1;
47 protected String snapshotName2;
48 protected TableName tableName;
49 protected int snapshot0Rows;
50 protected int snapshot1Rows;
51 protected Admin admin;
53 @Rule
54 public TestName name = new TestName();
56 protected static void setupConfiguration() {
57 TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
58 TEST_UTIL.getConfiguration().setInt("hbase.hstore.compactionThreshold", 10);
59 TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
60 TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
61 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
62 TEST_UTIL.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true);
65 @BeforeClass
66 public static void setUpBeforeClass() throws Exception {
67 setupConfiguration();
68 TEST_UTIL.startMiniCluster(3);
71 @AfterClass
72 public static void tearDownAfterClass() throws Exception {
73 TEST_UTIL.shutdownMiniCluster();
76 protected final String getValidMethodName() {
77 return name.getMethodName().replaceAll("[^0-9A-Za-z_]", "_");
80 /**
81 * Initialize the tests with a table filled with some data and two snapshots (snapshotName0,
82 * snapshotName1) of different states. The tableName, snapshotNames and the number of rows in the
83 * snapshot are initialized.
85 @Before
86 public void setup() throws Exception {
87 this.admin = TEST_UTIL.getAdmin();
88 long tid = EnvironmentEdgeManager.currentTime();
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 HBaseTestingUtil 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);