View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.snapshot;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.hbase.HBaseTestingUtility;
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.testclassification.LargeTests;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.client.Admin;
29  import org.apache.hadoop.hbase.client.Table;
30  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
31  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
32  import org.apache.hadoop.hbase.regionserver.snapshot.RegionServerSnapshotManager;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.junit.After;
35  import org.junit.AfterClass;
36  import org.junit.Before;
37  import org.junit.BeforeClass;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  /**
42   * Test clone/restore snapshots from the client
43   *
44   * TODO This is essentially a clone of TestRestoreSnapshotFromClient.  This is worth refactoring
45   * this because there will be a few more flavors of snapshots that need to run these tests.
46   */
47  @Category(LargeTests.class)
48  public class TestRestoreFlushSnapshotFromClient {
49    private static final Log LOG = LogFactory.getLog(TestRestoreFlushSnapshotFromClient.class);
50  
51    private final static HBaseTestingUtility UTIL = new HBaseTestingUtility();
52  
53    private final byte[] FAMILY = Bytes.toBytes("cf");
54  
55    private byte[] snapshotName0;
56    private byte[] snapshotName1;
57    private byte[] snapshotName2;
58    private int snapshot0Rows;
59    private int snapshot1Rows;
60    private TableName tableName;
61    private Admin admin;
62  
63    @BeforeClass
64    public static void setUpBeforeClass() throws Exception {
65      UTIL.getConfiguration().setBoolean("hbase.online.schema.update.enable", true);
66      UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
67      UTIL.getConfiguration().setInt("hbase.client.pause", 250);
68      UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
69      UTIL.getConfiguration().setBoolean(
70          "hbase.master.enabletable.roundrobin", true);
71  
72      // Enable snapshot
73      UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
74      UTIL.getConfiguration().setLong(RegionServerSnapshotManager.SNAPSHOT_TIMEOUT_MILLIS_KEY,
75        RegionServerSnapshotManager.SNAPSHOT_TIMEOUT_MILLIS_DEFAULT * 2);
76  
77      UTIL.startMiniCluster(3);
78    }
79  
80    @AfterClass
81    public static void tearDownAfterClass() throws Exception {
82      UTIL.shutdownMiniCluster();
83    }
84  
85    /**
86     * Initialize the tests with a table filled with some data
87     * and two snapshots (snapshotName0, snapshotName1) of different states.
88     * The tableName, snapshotNames and the number of rows in the snapshot are initialized.
89     */
90    @Before
91    public void setup() throws Exception {
92      this.admin = UTIL.getHBaseAdmin();
93  
94      long tid = System.currentTimeMillis();
95      tableName = TableName.valueOf("testtb-" + tid);
96      snapshotName0 = Bytes.toBytes("snaptb0-" + tid);
97      snapshotName1 = Bytes.toBytes("snaptb1-" + tid);
98      snapshotName2 = Bytes.toBytes("snaptb2-" + tid);
99  
100     // create Table and disable it
101     SnapshotTestingUtils.createTable(UTIL, tableName, FAMILY);
102     SnapshotTestingUtils.loadData(UTIL, tableName, 500, FAMILY);
103     Table table = UTIL.getConnection().getTable(tableName);
104     snapshot0Rows = UTIL.countRows(table);
105     LOG.info("=== before snapshot with 500 rows");
106     logFSTree();
107 
108     // take a snapshot
109     admin.snapshot(Bytes.toString(snapshotName0), tableName,
110         SnapshotDescription.Type.FLUSH);
111 
112     LOG.info("=== after snapshot with 500 rows");
113     logFSTree();
114 
115     // insert more data
116     SnapshotTestingUtils.loadData(UTIL, tableName, 500, FAMILY);
117     snapshot1Rows = UTIL.countRows(table);
118     LOG.info("=== before snapshot with 1000 rows");
119     logFSTree();
120 
121     // take a snapshot of the updated table
122     admin.snapshot(Bytes.toString(snapshotName1), tableName,
123         SnapshotDescription.Type.FLUSH);
124     LOG.info("=== after snapshot with 1000 rows");
125     logFSTree();
126     table.close();
127   }
128 
129   @After
130   public void tearDown() throws Exception {
131     SnapshotTestingUtils.deleteAllSnapshots(UTIL.getHBaseAdmin());
132     SnapshotTestingUtils.deleteArchiveDirectory(UTIL);
133   }
134 
135   @Test
136   public void testTakeFlushSnapshot() throws IOException {
137     // taking happens in setup.
138   }
139 
140   @Test
141   public void testRestoreSnapshot() throws IOException {
142     SnapshotTestingUtils.verifyRowCount(UTIL, tableName, snapshot1Rows);
143 
144     // Restore from snapshot-0
145     admin.disableTable(tableName);
146     admin.restoreSnapshot(snapshotName0);
147     logFSTree();
148     admin.enableTable(tableName);
149     LOG.info("=== after restore with 500 row snapshot");
150     logFSTree();
151     SnapshotTestingUtils.verifyRowCount(UTIL, tableName, snapshot0Rows);
152 
153     // Restore from snapshot-1
154     admin.disableTable(tableName);
155     admin.restoreSnapshot(snapshotName1);
156     admin.enableTable(tableName);
157     SnapshotTestingUtils.verifyRowCount(UTIL, tableName, snapshot1Rows);
158   }
159 
160   @Test(expected=SnapshotDoesNotExistException.class)
161   public void testCloneNonExistentSnapshot() throws IOException, InterruptedException {
162     String snapshotName = "random-snapshot-" + System.currentTimeMillis();
163     TableName tableName = TableName.valueOf("random-table-" + System.currentTimeMillis());
164     admin.cloneSnapshot(snapshotName, tableName);
165   }
166 
167   @Test
168   public void testCloneSnapshot() throws IOException, InterruptedException {
169     TableName clonedTableName = TableName.valueOf("clonedtb-" + System.currentTimeMillis());
170     testCloneSnapshot(clonedTableName, snapshotName0, snapshot0Rows);
171     testCloneSnapshot(clonedTableName, snapshotName1, snapshot1Rows);
172   }
173 
174   private void testCloneSnapshot(final TableName tableName, final byte[] snapshotName,
175       int snapshotRows) throws IOException, InterruptedException {
176     // create a new table from snapshot
177     admin.cloneSnapshot(snapshotName, tableName);
178     SnapshotTestingUtils.verifyRowCount(UTIL, tableName, snapshotRows);
179 
180     UTIL.deleteTable(tableName);
181   }
182 
183   @Test
184   public void testRestoreSnapshotOfCloned() throws IOException, InterruptedException {
185     TableName clonedTableName = TableName.valueOf("clonedtb-" + System.currentTimeMillis());
186     admin.cloneSnapshot(snapshotName0, clonedTableName);
187     SnapshotTestingUtils.verifyRowCount(UTIL, clonedTableName, snapshot0Rows);
188     admin.snapshot(Bytes.toString(snapshotName2), clonedTableName, SnapshotDescription.Type.FLUSH);
189     UTIL.deleteTable(clonedTableName);
190 
191     admin.cloneSnapshot(snapshotName2, clonedTableName);
192     SnapshotTestingUtils.verifyRowCount(UTIL, clonedTableName, snapshot0Rows);
193     UTIL.deleteTable(clonedTableName);
194   }
195 
196   // ==========================================================================
197   //  Helpers
198   // ==========================================================================
199   private void logFSTree() throws IOException {
200     UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem().logFileSystemState(LOG);
201   }
202 }