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.snapshot;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.Admin;
027import org.apache.hadoop.hbase.client.SnapshotType;
028import org.apache.hadoop.hbase.client.Table;
029import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
030import org.apache.hadoop.hbase.regionserver.snapshot.RegionServerSnapshotManager;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.apache.hadoop.hbase.testclassification.RegionServerTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
035import org.junit.After;
036import org.junit.AfterClass;
037import org.junit.Before;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045/**
046 * Test clone/restore snapshots from the client
047 *
048 * TODO This is essentially a clone of TestRestoreSnapshotFromClient.  This is worth refactoring
049 * this because there will be a few more flavors of snapshots that need to run these tests.
050 */
051@Category({RegionServerTests.class, MediumTests.class})
052public class TestRestoreFlushSnapshotFromClient {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestRestoreFlushSnapshotFromClient.class);
057
058  private static final Logger LOG =
059      LoggerFactory.getLogger(TestRestoreFlushSnapshotFromClient.class);
060
061  protected final static HBaseTestingUtil UTIL = new HBaseTestingUtil();
062
063  protected final byte[] FAMILY = Bytes.toBytes("cf");
064
065  protected String snapshotName0;
066  protected String snapshotName1;
067  protected String snapshotName2;
068  protected int snapshot0Rows;
069  protected int snapshot1Rows;
070  protected TableName tableName;
071  protected Admin admin;
072
073  @BeforeClass
074  public static void setupCluster() throws Exception {
075    setupConf(UTIL.getConfiguration());
076    UTIL.startMiniCluster(3);
077  }
078
079  protected static void setupConf(Configuration conf) {
080    UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
081    UTIL.getConfiguration().setInt("hbase.client.pause", 250);
082    UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
083    UTIL.getConfiguration().setBoolean(
084        "hbase.master.enabletable.roundrobin", true);
085
086    // Enable snapshot
087    UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
088    UTIL.getConfiguration().setLong(RegionServerSnapshotManager.SNAPSHOT_TIMEOUT_MILLIS_KEY,
089      RegionServerSnapshotManager.SNAPSHOT_TIMEOUT_MILLIS_DEFAULT * 2);
090  }
091
092  @AfterClass
093  public static void tearDownAfterClass() throws Exception {
094    UTIL.shutdownMiniCluster();
095  }
096
097  protected void createTable() throws Exception {
098    SnapshotTestingUtils.createTable(UTIL, tableName, FAMILY);
099  }
100
101  /**
102   * Initialize the tests with a table filled with some data
103   * and two snapshots (snapshotName0, snapshotName1) of different states.
104   * The tableName, snapshotNames and the number of rows in the snapshot are initialized.
105   */
106  @Before
107  public void setup() throws Exception {
108    this.admin = UTIL.getAdmin();
109
110    long tid = EnvironmentEdgeManager.currentTime();
111    tableName = TableName.valueOf("testtb-" + tid);
112    snapshotName0 = "snaptb0-" + tid;
113    snapshotName1 = "snaptb1-" + tid;
114    snapshotName2 = "snaptb2-" + tid;
115
116    // create Table and disable it
117    createTable();
118    SnapshotTestingUtils.loadData(UTIL, tableName, 500, FAMILY);
119    Table table = UTIL.getConnection().getTable(tableName);
120    snapshot0Rows = countRows(table);
121    LOG.info("=== before snapshot with 500 rows");
122    logFSTree();
123
124    // take a snapshot
125    admin.snapshot(snapshotName0, tableName, SnapshotType.FLUSH);
126
127    LOG.info("=== after snapshot with 500 rows");
128    logFSTree();
129
130    // insert more data
131    SnapshotTestingUtils.loadData(UTIL, tableName, 500, FAMILY);
132    snapshot1Rows = countRows(table);
133    LOG.info("=== before snapshot with 1000 rows");
134    logFSTree();
135
136    // take a snapshot of the updated table
137    admin.snapshot(snapshotName1, tableName, SnapshotType.FLUSH);
138    LOG.info("=== after snapshot with 1000 rows");
139    logFSTree();
140    table.close();
141  }
142
143  @After
144  public void tearDown() throws Exception {
145    SnapshotTestingUtils.deleteAllSnapshots(UTIL.getAdmin());
146    SnapshotTestingUtils.deleteArchiveDirectory(UTIL);
147  }
148
149  @Test
150  public void testTakeFlushSnapshot() throws IOException {
151    // taking happens in setup.
152  }
153
154  @Test
155  public void testRestoreSnapshot() throws IOException {
156    verifyRowCount(UTIL, tableName, snapshot1Rows);
157
158    // Restore from snapshot-0
159    admin.disableTable(tableName);
160    admin.restoreSnapshot(snapshotName0);
161    logFSTree();
162    admin.enableTable(tableName);
163    LOG.info("=== after restore with 500 row snapshot");
164    logFSTree();
165    verifyRowCount(UTIL, tableName, snapshot0Rows);
166
167    // Restore from snapshot-1
168    admin.disableTable(tableName);
169    admin.restoreSnapshot(snapshotName1);
170    admin.enableTable(tableName);
171    verifyRowCount(UTIL, tableName, snapshot1Rows);
172  }
173
174  @Test(expected=SnapshotDoesNotExistException.class)
175  public void testCloneNonExistentSnapshot() throws IOException, InterruptedException {
176    String snapshotName = "random-snapshot-" + EnvironmentEdgeManager.currentTime();
177    TableName tableName = TableName.valueOf("random-table-" +
178      EnvironmentEdgeManager.currentTime());
179    admin.cloneSnapshot(snapshotName, tableName);
180  }
181
182  @Test
183  public void testCloneSnapshot() throws IOException, InterruptedException {
184    TableName clonedTableName = TableName.valueOf("clonedtb-" +
185      EnvironmentEdgeManager.currentTime());
186    testCloneSnapshot(clonedTableName, snapshotName0, snapshot0Rows);
187    testCloneSnapshot(clonedTableName, snapshotName1, snapshot1Rows);
188  }
189
190  private void testCloneSnapshot(final TableName tableName, final String snapshotName,
191      int snapshotRows) throws IOException, InterruptedException {
192    // create a new table from snapshot
193    admin.cloneSnapshot(snapshotName, tableName);
194    verifyRowCount(UTIL, tableName, snapshotRows);
195
196    UTIL.deleteTable(tableName);
197  }
198
199  @Test
200  public void testRestoreSnapshotOfCloned() throws IOException, InterruptedException {
201    TableName clonedTableName = TableName.valueOf("clonedtb-" +
202      EnvironmentEdgeManager.currentTime());
203    admin.cloneSnapshot(snapshotName0, clonedTableName);
204    verifyRowCount(UTIL, clonedTableName, snapshot0Rows);
205    admin.snapshot(snapshotName2, clonedTableName, SnapshotType.FLUSH);
206    UTIL.deleteTable(clonedTableName);
207
208    admin.cloneSnapshot(snapshotName2, clonedTableName);
209    verifyRowCount(UTIL, clonedTableName, snapshot0Rows);
210    UTIL.deleteTable(clonedTableName);
211  }
212
213  // ==========================================================================
214  //  Helpers
215  // ==========================================================================
216  private void logFSTree() throws IOException {
217    UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem().logFileSystemState(LOG);
218  }
219
220  protected void verifyRowCount(final HBaseTestingUtil util, final TableName tableName,
221      long expectedRows) throws IOException {
222    SnapshotTestingUtils.verifyRowCount(util, tableName, expectedRows);
223  }
224
225  protected int countRows(final Table table, final byte[]... families) throws IOException {
226    return UTIL.countRows(table, families);
227  }
228}