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.HBaseTestingUtil;
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.apache.hadoop.hbase.util.EnvironmentEdgeManager;
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 clone snapsot
037 */
038public class CloneSnapshotFromClientTestBase {
039
040  protected final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
041
042  protected final byte[] FAMILY = Bytes.toBytes("cf");
043
044  protected String emptySnapshot;
045  protected String snapshotName0;
046  protected String snapshotName1;
047  protected String snapshotName2;
048  protected TableName tableName;
049  protected int snapshot0Rows;
050  protected int snapshot1Rows;
051  protected Admin admin;
052
053  @Rule
054  public TestName name = new TestName();
055
056  protected static void setupConfiguration() {
057    TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
058    TEST_UTIL.getConfiguration().setInt("hbase.hstore.compactionThreshold", 10);
059    TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
060    TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
061    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
062    TEST_UTIL.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true);
063  }
064
065  @BeforeClass
066  public static void setUpBeforeClass() throws Exception {
067    setupConfiguration();
068    TEST_UTIL.startMiniCluster(3);
069  }
070
071  @AfterClass
072  public static void tearDownAfterClass() throws Exception {
073    TEST_UTIL.shutdownMiniCluster();
074  }
075
076  protected final String getValidMethodName() {
077    return name.getMethodName().replaceAll("[^0-9A-Za-z_]", "_");
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  @Before
086  public void setup() throws Exception {
087    this.admin = TEST_UTIL.getAdmin();
088    long tid = EnvironmentEdgeManager.currentTime();
089    tableName = TableName.valueOf(getValidMethodName() + tid);
090    emptySnapshot = "emptySnaptb-" + tid;
091    snapshotName0 = "snaptb0-" + tid;
092    snapshotName1 = "snaptb1-" + tid;
093    snapshotName2 = "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 HBaseTestingUtil 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}