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  
19  package org.apache.hadoop.hbase.client;
20  
21  import java.util.List;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.fs.FileSystem;
27  import org.apache.hadoop.fs.Path;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.HColumnDescriptor;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.testclassification.LargeTests;
35  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
36  import org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy;
37  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.junit.After;
40  import org.junit.AfterClass;
41  import org.junit.Assert;
42  import org.junit.Before;
43  import org.junit.BeforeClass;
44  import org.junit.Ignore;
45  import org.junit.Test;
46  import org.junit.experimental.categories.Category;
47  
48  /**
49   * Test to verify that the cloned table is independent of the table from which it was cloned
50   */
51  @Category(LargeTests.class)
52  public class TestSnapshotCloneIndependence {
53    private static final Log LOG = LogFactory.getLog(TestSnapshotCloneIndependence.class);
54  
55    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
56  
57    private static final int NUM_RS = 2;
58    private static final String STRING_TABLE_NAME = "test";
59    private static final String TEST_FAM_STR = "fam";
60    private static final byte[] TEST_FAM = Bytes.toBytes(TEST_FAM_STR);
61    private static final TableName TABLE_NAME = TableName.valueOf(STRING_TABLE_NAME);
62    private static final int CLEANER_INTERVAL = 10;
63  
64    /**
65     * Setup the config for the cluster and start it
66     * @throws Exception on failure
67     */
68    @BeforeClass
69    public static void setupCluster() throws Exception {
70      setupConf(UTIL.getConfiguration());
71      UTIL.startMiniCluster(NUM_RS);
72    }
73  
74    private static void setupConf(Configuration conf) {
75      // enable snapshot support
76      conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
77      // disable the ui
78      conf.setInt("hbase.regionsever.info.port", -1);
79      // change the flush size to a small amount, regulating number of store files
80      conf.setInt("hbase.hregion.memstore.flush.size", 25000);
81      // so make sure we get a compaction when doing a load, but keep around
82      // some files in the store
83      conf.setInt("hbase.hstore.compaction.min", 10);
84      conf.setInt("hbase.hstore.compactionThreshold", 10);
85      // block writes if we get to 12 store files
86      conf.setInt("hbase.hstore.blockingStoreFiles", 12);
87      conf.setInt("hbase.regionserver.msginterval", 100);
88      conf.setBoolean("hbase.master.enabletable.roundrobin", true);
89      // Avoid potentially aggressive splitting which would cause snapshot to fail
90      conf.set(HConstants.HBASE_REGION_SPLIT_POLICY_KEY,
91        ConstantSizeRegionSplitPolicy.class.getName());
92      // Execute cleaner frequently to induce failures
93      conf.setInt("hbase.master.cleaner.interval", CLEANER_INTERVAL);
94      conf.setInt("hbase.master.hfilecleaner.plugins.snapshot.period", CLEANER_INTERVAL);
95      // Effectively disable TimeToLiveHFileCleaner. Don't want to fully disable it because that
96      // will even trigger races between creating the directory containing back references and
97      // the back reference itself.
98      conf.setInt("hbase.master.hfilecleaner.ttl", CLEANER_INTERVAL);
99    }
100 
101   @Before
102   public void setup() throws Exception {
103     UTIL.createTable(TABLE_NAME, TEST_FAM);
104   }
105 
106   @After
107   public void tearDown() throws Exception {
108     UTIL.deleteTable(TABLE_NAME);
109     SnapshotTestingUtils.deleteAllSnapshots(UTIL.getHBaseAdmin());
110     SnapshotTestingUtils.deleteArchiveDirectory(UTIL);
111   }
112 
113   @AfterClass
114   public static void cleanupTest() throws Exception {
115     try {
116       UTIL.shutdownMiniCluster();
117     } catch (Exception e) {
118       LOG.warn("failure shutting down cluster", e);
119     }
120   }
121 
122   /**
123    * Verify that adding data to the cloned table will not affect the original, and vice-versa when
124    * it is taken as an online snapshot.
125    */
126   @Test (timeout=300000)
127   public void testOnlineSnapshotAppendIndependent() throws Exception {
128     runTestSnapshotAppendIndependent(true);
129   }
130 
131   /**
132    * Verify that adding data to the cloned table will not affect the original, and vice-versa when
133    * it is taken as an offline snapshot.
134    */
135   @Test (timeout=300000)
136   public void testOfflineSnapshotAppendIndependent() throws Exception {
137     runTestSnapshotAppendIndependent(false);
138   }
139 
140   /**
141    * Verify that adding metadata to the cloned table will not affect the original, and vice-versa
142    * when it is taken as an online snapshot.
143    */
144   @Test (timeout=300000)
145   public void testOnlineSnapshotMetadataChangesIndependent() throws Exception {
146     runTestSnapshotMetadataChangesIndependent(true);
147   }
148 
149   /**
150    * Verify that adding netadata to the cloned table will not affect the original, and vice-versa
151    * when is taken as an online snapshot.
152    */
153   @Test (timeout=300000)
154   public void testOfflineSnapshotMetadataChangesIndependent() throws Exception {
155     runTestSnapshotMetadataChangesIndependent(false);
156   }
157 
158   /**
159    * Verify that region operations, in this case splitting a region, are independent between the
160    * cloned table and the original.
161    */
162   @Test (timeout=300000)
163   public void testOfflineSnapshotRegionOperationsIndependent() throws Exception {
164     runTestRegionOperationsIndependent(false);
165   }
166 
167   /**
168    * Verify that region operations, in this case splitting a region, are independent between the
169    * cloned table and the original.
170    */
171   @Test (timeout=300000)
172   public void testOnlineSnapshotRegionOperationsIndependent() throws Exception {
173     runTestRegionOperationsIndependent(true);
174   }
175 
176   @Test (timeout=300000)
177   public void testOfflineSnapshotDeleteIndependent() throws Exception {
178     runTestSnapshotDeleteIndependent(false);
179   }
180 
181   @Ignore
182   @Test (timeout=300000)
183   public void testOnlineSnapshotDeleteIndependent() throws Exception {
184     runTestSnapshotDeleteIndependent(true);
185   }
186 
187   private static void waitOnSplit(final HTable t, int originalCount) throws Exception {
188     for (int i = 0; i < 200; i++) {
189       try {
190         Thread.sleep(50);
191       } catch (InterruptedException e) {
192         // Restore the interrupted status
193         Thread.currentThread().interrupt();
194       }
195       if (t.getAllRegionLocations().size() > originalCount) {
196         return;
197       }
198     }
199     throw new Exception("Split did not increase the number of regions");
200   }
201 
202   /*
203    * Take a snapshot of a table, add data, and verify that this only
204    * affects one table
205    * @param online - Whether the table is online or not during the snapshot
206    */
207   private void runTestSnapshotAppendIndependent(boolean online) throws Exception {
208     FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
209     Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
210 
211     Admin admin = UTIL.getHBaseAdmin();
212     final long startTime = System.currentTimeMillis();
213     final TableName localTableName =
214         TableName.valueOf(STRING_TABLE_NAME + startTime);
215 
216     try (Table original = UTIL.createTable(localTableName, TEST_FAM)) {
217       UTIL.loadTable(original, TEST_FAM);
218       final int origTableRowCount = UTIL.countRows(original);
219 
220       // Take a snapshot
221       final String snapshotNameAsString = "snapshot_" + localTableName;
222       byte[] snapshotName = Bytes.toBytes(snapshotNameAsString);
223 
224       SnapshotTestingUtils.createSnapshotAndValidate(admin, localTableName, TEST_FAM_STR,
225         snapshotNameAsString, rootDir, fs, online);
226 
227       if (!online) {
228         admin.enableTable(localTableName);
229       }
230       TableName cloneTableName = TableName.valueOf("test-clone-" + localTableName);
231       admin.cloneSnapshot(snapshotName, cloneTableName);
232 
233       try (Table clonedTable = new HTable(UTIL.getConfiguration(), cloneTableName)){
234         final int clonedTableRowCount = UTIL.countRows(clonedTable);
235 
236         Assert.assertEquals(
237           "The line counts of original and cloned tables do not match after clone. ",
238           origTableRowCount, clonedTableRowCount);
239 
240         // Attempt to add data to the test
241         final String rowKey = "new-row-" + System.currentTimeMillis();
242 
243         Put p = new Put(Bytes.toBytes(rowKey));
244         p.add(TEST_FAM, Bytes.toBytes("someQualifier"), Bytes.toBytes("someString"));
245         original.put(p);
246 
247         // Verify that it is not present in the original table
248         Assert.assertEquals("The row count of the original table was not modified by the put",
249           origTableRowCount + 1, UTIL.countRows(original));
250         Assert.assertEquals(
251           "The row count of the cloned table changed as a result of addition to the original",
252           clonedTableRowCount, UTIL.countRows(clonedTable));
253 
254         p = new Put(Bytes.toBytes(rowKey));
255         p.add(TEST_FAM, Bytes.toBytes("someQualifier"), Bytes.toBytes("someString"));
256         clonedTable.put(p);
257 
258         // Verify that the new family is not in the restored table's description
259         Assert.assertEquals(
260           "The row count of the original table was modified by the put to the clone",
261           origTableRowCount + 1, UTIL.countRows(original));
262         Assert.assertEquals("The row count of the cloned table was not modified by the put",
263           clonedTableRowCount + 1, UTIL.countRows(clonedTable));
264       }
265     }
266   }
267 
268   /*
269    * Take a snapshot of a table, do a split, and verify that this only affects one table
270    * @param online - Whether the table is online or not during the snapshot
271    */
272   private void runTestRegionOperationsIndependent(boolean online) throws Exception {
273     FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
274     Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
275 
276     // Create a table
277     Admin admin = UTIL.getHBaseAdmin();
278     final long startTime = System.currentTimeMillis();
279     final TableName localTableName =
280         TableName.valueOf(STRING_TABLE_NAME + startTime);
281     HTable original = UTIL.createTable(localTableName, TEST_FAM);
282     UTIL.loadTable(original, TEST_FAM);
283     final int loadedTableCount = UTIL.countRows(original);
284     System.out.println("Original table has: " + loadedTableCount + " rows");
285 
286     final String snapshotNameAsString = "snapshot_" + localTableName;
287 
288     // Create a snapshot
289     SnapshotTestingUtils.createSnapshotAndValidate(admin, localTableName, TEST_FAM_STR,
290       snapshotNameAsString, rootDir, fs, online);
291 
292     if (!online) {
293       admin.enableTable(localTableName);
294     }
295 
296     TableName cloneTableName = TableName.valueOf("test-clone-" + localTableName);
297 
298     // Clone the snapshot
299     byte[] snapshotName = Bytes.toBytes(snapshotNameAsString);
300     admin.cloneSnapshot(snapshotName, cloneTableName);
301 
302     // Verify that region information is the same pre-split
303     original.clearRegionCache();
304     List<HRegionInfo> originalTableHRegions = admin.getTableRegions(localTableName);
305 
306     final int originalRegionCount = originalTableHRegions.size();
307     final int cloneTableRegionCount = admin.getTableRegions(cloneTableName).size();
308     Assert.assertEquals(
309       "The number of regions in the cloned table is different than in the original table.",
310       originalRegionCount, cloneTableRegionCount);
311 
312     // Split a region on the parent table
313     admin.splitRegion(originalTableHRegions.get(0).getRegionName());
314     waitOnSplit(original, originalRegionCount);
315 
316     // Verify that the cloned table region is not split
317     final int cloneTableRegionCount2 = admin.getTableRegions(cloneTableName).size();
318     Assert.assertEquals(
319       "The number of regions in the cloned table changed though none of its regions were split.",
320       cloneTableRegionCount, cloneTableRegionCount2);
321   }
322 
323   /*
324    * Take a snapshot of a table, add metadata, and verify that this only
325    * affects one table
326    * @param online - Whether the table is online or not during the snapshot
327    */
328   private void runTestSnapshotMetadataChangesIndependent(boolean online) throws Exception {
329     FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
330     Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
331 
332     // Create a table
333     Admin admin = UTIL.getHBaseAdmin();
334     final long startTime = System.currentTimeMillis();
335     final TableName localTableName =
336         TableName.valueOf(STRING_TABLE_NAME + startTime);
337     HTable original = UTIL.createTable(localTableName, TEST_FAM);
338     UTIL.loadTable(original, TEST_FAM);
339 
340     final String snapshotNameAsString = "snapshot_" + localTableName;
341 
342     // Create a snapshot
343     SnapshotTestingUtils.createSnapshotAndValidate(admin, localTableName, TEST_FAM_STR,
344       snapshotNameAsString, rootDir, fs, online);
345 
346     if (!online) {
347       admin.enableTable(localTableName);
348     }
349     TableName cloneTableName = TableName.valueOf("test-clone-" + localTableName);
350 
351     // Clone the snapshot
352     byte[] snapshotName = Bytes.toBytes(snapshotNameAsString);
353     admin.cloneSnapshot(snapshotName, cloneTableName);
354 
355     // Add a new column family to the original table
356     byte[] TEST_FAM_2 = Bytes.toBytes("fam2");
357     HColumnDescriptor hcd = new HColumnDescriptor(TEST_FAM_2);
358 
359     admin.disableTable(localTableName);
360     admin.addColumn(localTableName, hcd);
361 
362     // Verify that it is not in the snapshot
363     admin.enableTable(localTableName);
364 
365     // get a description of the cloned table
366     // get a list of its families
367     // assert that the family is there
368     HTableDescriptor originalTableDescriptor = original.getTableDescriptor();
369     HTableDescriptor clonedTableDescriptor = admin.getTableDescriptor(cloneTableName);
370 
371     Assert.assertTrue("The original family was not found. There is something wrong. ",
372       originalTableDescriptor.hasFamily(TEST_FAM));
373     Assert.assertTrue("The original family was not found in the clone. There is something wrong. ",
374       clonedTableDescriptor.hasFamily(TEST_FAM));
375 
376     Assert.assertTrue("The new family was not found. ",
377       originalTableDescriptor.hasFamily(TEST_FAM_2));
378     Assert.assertTrue("The new family was not found. ",
379       !clonedTableDescriptor.hasFamily(TEST_FAM_2));
380   }
381 
382   /*
383    * Take a snapshot of a table, add data, and verify that deleting the snapshot does not affect
384    * either table.
385    * @param online - Whether the table is online or not during the snapshot
386    */
387   private void runTestSnapshotDeleteIndependent(boolean online) throws Exception {
388     FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
389     Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
390 
391     final Admin admin = UTIL.getHBaseAdmin();
392     final long startTime = System.currentTimeMillis();
393     final TableName localTableName =
394         TableName.valueOf(STRING_TABLE_NAME + startTime);
395 
396     try (Table original = UTIL.createTable(localTableName, TEST_FAM)) {
397       UTIL.loadTable(original, TEST_FAM);
398     }
399 
400     // Take a snapshot
401     final String snapshotNameAsString = "snapshot_" + localTableName;
402     byte[] snapshotName = Bytes.toBytes(snapshotNameAsString);
403 
404     SnapshotTestingUtils.createSnapshotAndValidate(admin, localTableName, TEST_FAM_STR,
405         snapshotNameAsString, rootDir, fs, online);
406 
407     if (!online) {
408       admin.enableTable(localTableName);
409     }
410     TableName cloneTableName = TableName.valueOf("test-clone-" + localTableName);
411     admin.cloneSnapshot(snapshotName, cloneTableName);
412 
413     // Ensure the original table does not reference the HFiles anymore
414     admin.majorCompact(localTableName);
415 
416     // Deleting the snapshot used to break the cloned table by deleting in-use HFiles
417     admin.deleteSnapshot(snapshotName);
418 
419     // Wait for cleaner run and DFS heartbeats so that anything that is deletable is fully deleted
420     Thread.sleep(10000);
421 
422     try (Table original = UTIL.getConnection().getTable(localTableName)) {
423       try (Table clonedTable = UTIL.getConnection().getTable(cloneTableName)) {
424         // Verify that all regions of both tables are readable
425         final int origTableRowCount = UTIL.countRows(original);
426         final int clonedTableRowCount = UTIL.countRows(clonedTable);
427         Assert.assertEquals(origTableRowCount, clonedTableRowCount);
428       }
429     }
430   }
431 }