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