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.HBaseTestingUtility;
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.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 restore snapshot
037 */
038public class RestoreSnapshotFromClientTestBase {
039  protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
040
041  protected final byte[] FAMILY = Bytes.toBytes("cf");
042  protected final byte[] TEST_FAMILY2 = Bytes.toBytes("cf2");
043
044  protected TableName tableName;
045  protected byte[] emptySnapshot;
046  protected byte[] snapshotName0;
047  protected byte[] snapshotName1;
048  protected byte[] snapshotName2;
049  protected int snapshot0Rows;
050  protected int snapshot1Rows;
051  protected Admin admin;
052
053  @Rule
054  public TestName name = new TestName();
055
056  @BeforeClass
057  public static void setupCluster() throws Exception {
058    setupConf(TEST_UTIL.getConfiguration());
059    TEST_UTIL.startMiniCluster(3);
060  }
061
062  protected static void setupConf(Configuration conf) {
063    TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
064    TEST_UTIL.getConfiguration().setInt("hbase.hstore.compactionThreshold", 10);
065    TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
066    TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
067    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
068    TEST_UTIL.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true);
069  }
070
071  @AfterClass
072  public static void tearDownAfterClass() throws Exception {
073    TEST_UTIL.shutdownMiniCluster();
074  }
075
076  /**
077   * Initialize the tests with a table filled with some data and two snapshots (snapshotName0,
078   * snapshotName1) of different states. The tableName, snapshotNames and the number of rows in the
079   * snapshot are initialized.
080   */
081  @Before
082  public void setup() throws Exception {
083    this.admin = TEST_UTIL.getAdmin();
084
085    long tid = System.currentTimeMillis();
086    tableName = TableName.valueOf(getValidMethodName() + "-" + tid);
087    emptySnapshot = Bytes.toBytes("emptySnaptb-" + tid);
088    snapshotName0 = Bytes.toBytes("snaptb0-" + tid);
089    snapshotName1 = Bytes.toBytes("snaptb1-" + tid);
090    snapshotName2 = Bytes.toBytes("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(HBaseTestingUtility 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}