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.TableName;
27  import org.apache.hadoop.hbase.client.Admin;
28  import org.apache.hadoop.hbase.client.HTable;
29  import org.apache.hadoop.hbase.master.MasterFileSystem;
30  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
31  import org.apache.hadoop.hbase.mob.MobConstants;
32  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
33  import org.apache.hadoop.hbase.regionserver.snapshot.RegionServerSnapshotManager;
34  import org.apache.hadoop.hbase.testclassification.LargeTests;
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.apache.hadoop.hbase.util.FSUtils;
37  import org.junit.After;
38  import org.junit.AfterClass;
39  import org.junit.Before;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  /**
45   * Test clone/restore snapshots from the client
46   *
47   * TODO This is essentially a clone of TestRestoreSnapshotFromClient.  This is worth refactoring
48   * this because there will be a few more flavors of snapshots that need to run these tests.
49   */
50  @Category(LargeTests.class)
51  public class TestMobRestoreFlushSnapshotFromClient {
52    final Log LOG = LogFactory.getLog(getClass());
53  
54    private final static HBaseTestingUtility UTIL = new HBaseTestingUtility();
55  
56    private final byte[] FAMILY = Bytes.toBytes("cf");
57  
58    private byte[] snapshotName0;
59    private byte[] snapshotName1;
60    private byte[] snapshotName2;
61    private int snapshot0Rows;
62    private int snapshot1Rows;
63    private TableName tableName;
64    private Admin admin;
65  
66    @BeforeClass
67    public static void setUpBeforeClass() throws Exception {
68      UTIL.getConfiguration().setBoolean("hbase.online.schema.update.enable", true);
69      UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
70      UTIL.getConfiguration().setInt("hbase.client.pause", 250);
71      UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
72      UTIL.getConfiguration().setBoolean(
73          "hbase.master.enabletable.roundrobin", true);
74  
75      // Enable snapshot
76      UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
77      UTIL.getConfiguration().setLong(RegionServerSnapshotManager.SNAPSHOT_TIMEOUT_MILLIS_KEY,
78        RegionServerSnapshotManager.SNAPSHOT_TIMEOUT_MILLIS_DEFAULT * 2);
79  
80      UTIL.getConfiguration().setInt(MobConstants.MOB_FILE_CACHE_SIZE_KEY, 0);
81      UTIL.getConfiguration().setInt("hfile.format.version", 3);
82  
83      UTIL.startMiniCluster(3);
84    }
85  
86    @AfterClass
87    public static void tearDownAfterClass() throws Exception {
88      UTIL.shutdownMiniCluster();
89    }
90  
91    /**
92     * Initialize the tests with a table filled with some data
93     * and two snapshots (snapshotName0, snapshotName1) of different states.
94     * The tableName, snapshotNames and the number of rows in the snapshot are initialized.
95     */
96    @Before
97    public void setup() throws Exception {
98      this.admin = UTIL.getHBaseAdmin();
99  
100     long tid = System.currentTimeMillis();
101     tableName = TableName.valueOf("testtb-" + tid);
102     snapshotName0 = Bytes.toBytes("snaptb0-" + tid);
103     snapshotName1 = Bytes.toBytes("snaptb1-" + tid);
104     snapshotName2 = Bytes.toBytes("snaptb2-" + tid);
105 
106     // create Table
107     MobSnapshotTestingUtils.createMobTable(UTIL, tableName, 1, FAMILY);
108 
109     HTable table = new HTable(UTIL.getConfiguration(), tableName);
110     SnapshotTestingUtils.loadData(UTIL, tableName, 500, FAMILY);
111     snapshot0Rows = MobSnapshotTestingUtils.countMobRows(table);
112     LOG.info("=== before snapshot with 500 rows");
113     logFSTree();
114 
115     // take a snapshot
116     admin.snapshot(Bytes.toString(snapshotName0), tableName,
117         SnapshotDescription.Type.FLUSH);
118 
119     LOG.info("=== after snapshot with 500 rows");
120     logFSTree();
121 
122     // insert more data
123     SnapshotTestingUtils.loadData(UTIL, tableName, 500, FAMILY);
124     snapshot1Rows = MobSnapshotTestingUtils.countMobRows(table);
125     LOG.info("=== before snapshot with 1000 rows");
126     logFSTree();
127 
128     // take a snapshot of the updated table
129     admin.snapshot(Bytes.toString(snapshotName1), tableName,
130         SnapshotDescription.Type.FLUSH);
131     LOG.info("=== after snapshot with 1000 rows");
132     logFSTree();
133     table.close();
134   }
135 
136   @After
137   public void tearDown() throws Exception {
138     SnapshotTestingUtils.deleteAllSnapshots(UTIL.getHBaseAdmin());
139     SnapshotTestingUtils.deleteArchiveDirectory(UTIL);
140   }
141 
142   @Test
143   public void testTakeFlushSnapshot() throws IOException {
144     // taking happens in setup.
145   }
146 
147   @Test
148   public void testRestoreSnapshot() throws IOException {
149     MobSnapshotTestingUtils.verifyMobRowCount(UTIL, tableName, snapshot1Rows);
150 
151     // Restore from snapshot-0
152     admin.disableTable(tableName);
153     admin.restoreSnapshot(snapshotName0);
154     logFSTree();
155     admin.enableTable(tableName);
156     LOG.info("=== after restore with 500 row snapshot");
157     logFSTree();
158     MobSnapshotTestingUtils.verifyMobRowCount(UTIL, tableName, snapshot0Rows);
159 
160     // Restore from snapshot-1
161     admin.disableTable(tableName);
162     admin.restoreSnapshot(snapshotName1);
163     admin.enableTable(tableName);
164     MobSnapshotTestingUtils.verifyMobRowCount(UTIL, tableName, snapshot1Rows);
165   }
166 
167   @Test(expected=SnapshotDoesNotExistException.class)
168   public void testCloneNonExistentSnapshot() throws IOException, InterruptedException {
169     String snapshotName = "random-snapshot-" + System.currentTimeMillis();
170     TableName tableName = TableName.valueOf("random-table-" + System.currentTimeMillis());
171     admin.cloneSnapshot(snapshotName, tableName);
172   }
173 
174   @Test
175   public void testCloneSnapshot() throws IOException, InterruptedException {
176     TableName clonedTableName = TableName.valueOf("clonedtb-" + System.currentTimeMillis());
177     testCloneSnapshot(clonedTableName, snapshotName0, snapshot0Rows);
178     testCloneSnapshot(clonedTableName, snapshotName1, snapshot1Rows);
179   }
180 
181   private void testCloneSnapshot(final TableName tableName, final byte[] snapshotName,
182       int snapshotRows) throws IOException, InterruptedException {
183     // create a new table from snapshot
184     admin.cloneSnapshot(snapshotName, tableName);
185     MobSnapshotTestingUtils.verifyMobRowCount(UTIL, tableName, snapshotRows);
186 
187     UTIL.deleteTable(tableName);
188   }
189 
190   @Test
191   public void testRestoreSnapshotOfCloned() throws IOException, InterruptedException {
192     TableName clonedTableName = TableName.valueOf("clonedtb-" + System.currentTimeMillis());
193     admin.cloneSnapshot(snapshotName0, clonedTableName);
194     MobSnapshotTestingUtils.verifyMobRowCount(UTIL, clonedTableName, snapshot0Rows);
195     admin.snapshot(Bytes.toString(snapshotName2), clonedTableName, SnapshotDescription.Type.FLUSH);
196     UTIL.deleteTable(clonedTableName);
197 
198     admin.cloneSnapshot(snapshotName2, clonedTableName);
199     MobSnapshotTestingUtils.verifyMobRowCount(UTIL, clonedTableName, snapshot0Rows);
200     UTIL.deleteTable(clonedTableName);
201   }
202 
203   // ==========================================================================
204   //  Helpers
205   // ==========================================================================
206   private void logFSTree() throws IOException {
207     MasterFileSystem mfs = UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem();
208     FSUtils.logFileSystemState(mfs.getFileSystem(), mfs.getRootDir(), LOG);
209   }
210 }