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 java.util.stream.Stream;
022import org.apache.hadoop.hbase.HBaseTestingUtil;
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.apache.hadoop.hbase.util.EnvironmentEdgeManager;
029import org.junit.jupiter.api.AfterAll;
030import org.junit.jupiter.api.AfterEach;
031import org.junit.jupiter.api.BeforeEach;
032import org.junit.jupiter.api.TestInfo;
033import org.junit.jupiter.params.provider.Arguments;
034
035/**
036 * Base class for testing clone snapshot
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  protected int numReplicas;
054
055  private String testName;
056
057  protected CloneSnapshotFromClientTestBase(int numReplicas) {
058    this.numReplicas = numReplicas;
059  }
060
061  public static Stream<Arguments> parameters() {
062    return Stream.of(Arguments.of(1), Arguments.of(3));
063  }
064
065  protected static void setupConfiguration() {
066    TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
067    TEST_UTIL.getConfiguration().setInt("hbase.hstore.compactionThreshold", 10);
068    TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
069    TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
070    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
071    TEST_UTIL.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true);
072    TEST_UTIL.getConfiguration().setBoolean("hbase.regionserver.compaction.enabled", false);
073  }
074
075  @AfterAll
076  public static void tearDownAfterClass() throws Exception {
077    TEST_UTIL.shutdownMiniCluster();
078  }
079
080  protected final String getValidMethodName() {
081    return testName;
082  }
083
084  /**
085   * Initialize the tests with a table filled with some data and two snapshots (snapshotName0,
086   * snapshotName1) of different states. The tableName, snapshotNames and the number of rows in the
087   * snapshot are initialized.
088   */
089  @BeforeEach
090  public void setUp(TestInfo testInfo) throws Exception {
091    this.admin = TEST_UTIL.getAdmin();
092    long tid = EnvironmentEdgeManager.currentTime();
093    testName = testInfo.getTestMethod().get().getName()
094      + testInfo.getDisplayName().replaceAll("[^0-9A-Za-z_]", "_");
095    tableName = TableName.valueOf(getValidMethodName() + tid);
096    initSnapshotNames(tid);
097    createTableAndSnapshots();
098  }
099
100  protected void initSnapshotNames(long tid) {
101    emptySnapshot = "emptySnaptb-" + tid;
102    snapshotName0 = "snaptb0-" + tid;
103    snapshotName1 = "snaptb1-" + tid;
104    snapshotName2 = "snaptb2-" + tid;
105  }
106
107  protected void createTable() throws IOException, InterruptedException {
108    SnapshotTestingUtils.createTable(TEST_UTIL, tableName, numReplicas, FAMILY);
109  }
110
111  protected int numRowsToLoad() {
112    return 500;
113  }
114
115  protected int countRows(Table table) throws IOException {
116    return HBaseTestingUtil.countRows(table);
117  }
118
119  protected void createTableAndSnapshots() throws Exception {
120    // create Table and disable it
121    createTable();
122    admin.disableTable(tableName);
123
124    // take an empty snapshot
125    admin.snapshot(emptySnapshot, tableName);
126
127    // enable table and insert data
128    admin.enableTable(tableName);
129    SnapshotTestingUtils.loadData(TEST_UTIL, tableName, numRowsToLoad(), FAMILY);
130    try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
131      snapshot0Rows = countRows(table);
132    }
133    admin.disableTable(tableName);
134
135    // take a snapshot
136    admin.snapshot(snapshotName0, tableName);
137
138    // enable table and insert more data
139    admin.enableTable(tableName);
140    SnapshotTestingUtils.loadData(TEST_UTIL, tableName, numRowsToLoad(), FAMILY);
141    try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
142      snapshot1Rows = countRows(table);
143    }
144    admin.disableTable(tableName);
145
146    // take a snapshot of the updated table
147    admin.snapshot(snapshotName1, tableName);
148
149    // re-enable table
150    admin.enableTable(tableName);
151  }
152
153  protected void verifyRowCount(final HBaseTestingUtil util, final TableName tableName,
154    long expectedRows) throws IOException {
155    SnapshotTestingUtils.verifyRowCount(util, tableName, expectedRows);
156  }
157
158  @AfterEach
159  public void tearDown() throws Exception {
160    if (admin.tableExists(tableName)) {
161      TEST_UTIL.deleteTable(tableName);
162    }
163    SnapshotTestingUtils.deleteAllSnapshots(admin);
164    SnapshotTestingUtils.deleteArchiveDirectory(TEST_UTIL);
165  }
166}