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 static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.Collection;
28  import java.util.List;
29  import java.util.Set;
30  import java.util.HashSet;
31  import java.util.TreeSet;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.hadoop.fs.FileStatus;
36  import org.apache.hadoop.fs.FileSystem;
37  import org.apache.hadoop.fs.Path;
38  import org.apache.hadoop.fs.PathFilter;
39  import org.apache.hadoop.hbase.HBaseTestingUtility;
40  import org.apache.hadoop.hbase.HColumnDescriptor;
41  import org.apache.hadoop.hbase.HConstants;
42  import org.apache.hadoop.hbase.HRegionInfo;
43  import org.apache.hadoop.hbase.HTableDescriptor;
44  import org.apache.hadoop.hbase.TableName;
45  import org.apache.hadoop.hbase.TableNotEnabledException;
46  import org.apache.hadoop.hbase.client.Durability;
47  import org.apache.hadoop.hbase.client.HBaseAdmin;
48  import org.apache.hadoop.hbase.client.HTable;
49  import org.apache.hadoop.hbase.client.Put;
50  import org.apache.hadoop.hbase.io.HFileLink;
51  import org.apache.hadoop.hbase.master.HMaster;
52  import org.apache.hadoop.hbase.master.MasterFileSystem;
53  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
54  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
55  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsSnapshotDoneRequest;
56  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsSnapshotDoneResponse;
57  import org.apache.hadoop.hbase.regionserver.HRegion;
58  import org.apache.hadoop.hbase.regionserver.HRegionFileSystem;
59  import org.apache.hadoop.hbase.regionserver.HRegionServer;
60  import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
61  import org.apache.hadoop.hbase.util.Bytes;
62  import org.apache.hadoop.hbase.util.FSTableDescriptors;
63  import org.apache.hadoop.hbase.util.FSUtils;
64  import org.apache.hadoop.hbase.util.FSVisitor;
65  import org.apache.hadoop.hbase.util.MD5Hash;
66  import org.junit.Assert;
67  
68  import com.google.protobuf.ServiceException;
69  
70  /**
71   * Utilities class for snapshots
72   */
73  public class SnapshotTestingUtils {
74  
75    private static final Log LOG = LogFactory.getLog(SnapshotTestingUtils.class);
76    private static byte[] KEYS = Bytes.toBytes("0123456789");
77  
78    /**
79     * Assert that we don't have any snapshots lists
80     *
81     * @throws IOException
82     *           if the admin operation fails
83     */
84    public static void assertNoSnapshots(HBaseAdmin admin) throws IOException {
85      assertEquals("Have some previous snapshots", 0, admin.listSnapshots()
86          .size());
87    }
88  
89    /**
90     * Make sure that there is only one snapshot returned from the master and its
91     * name and table match the passed in parameters.
92     */
93    public static List<SnapshotDescription> assertExistsMatchingSnapshot(
94        HBaseAdmin admin, String snapshotName, TableName tableName)
95        throws IOException {
96      // list the snapshot
97      List<SnapshotDescription> snapshots = admin.listSnapshots();
98  
99      List<SnapshotDescription> returnedSnapshots = new ArrayList<SnapshotDescription>();
100     for (SnapshotDescription sd : snapshots) {
101       if (snapshotName.equals(sd.getName()) &&
102           tableName.equals(TableName.valueOf(sd.getTable()))) {
103         returnedSnapshots.add(sd);
104       }
105     }
106 
107     Assert.assertTrue("No matching snapshots found.", returnedSnapshots.size()>0);
108     return returnedSnapshots;
109   }
110 
111   /**
112    * Make sure that there is only one snapshot returned from the master
113    */
114   public static void assertOneSnapshotThatMatches(HBaseAdmin admin,
115       SnapshotDescription snapshot) throws IOException {
116     assertOneSnapshotThatMatches(admin, snapshot.getName(),
117         TableName.valueOf(snapshot.getTable()));
118   }
119 
120   /**
121    * Make sure that there is only one snapshot returned from the master and its
122    * name and table match the passed in parameters.
123    */
124   public static List<SnapshotDescription> assertOneSnapshotThatMatches(
125       HBaseAdmin admin, String snapshotName, TableName tableName)
126       throws IOException {
127     // list the snapshot
128     List<SnapshotDescription> snapshots = admin.listSnapshots();
129 
130     assertEquals("Should only have 1 snapshot", 1, snapshots.size());
131     assertEquals(snapshotName, snapshots.get(0).getName());
132     assertEquals(tableName, TableName.valueOf(snapshots.get(0).getTable()));
133 
134     return snapshots;
135   }
136 
137   /**
138    * Make sure that there is only one snapshot returned from the master and its
139    * name and table match the passed in parameters.
140    */
141   public static List<SnapshotDescription> assertOneSnapshotThatMatches(
142       HBaseAdmin admin, byte[] snapshot, TableName tableName) throws IOException {
143     return assertOneSnapshotThatMatches(admin, Bytes.toString(snapshot),
144         tableName);
145   }
146 
147   /**
148    * Confirm that the snapshot contains references to all the files that should
149    * be in the snapshot.
150    */
151   public static void confirmSnapshotValid(
152       SnapshotDescription snapshotDescriptor, TableName tableName,
153       byte[] testFamily, Path rootDir, HBaseAdmin admin, FileSystem fs,
154       boolean requireLogs, Path logsDir, Set<String> snapshotServers)
155       throws IOException {
156     ArrayList nonEmptyTestFamilies = new ArrayList(1);
157     nonEmptyTestFamilies.add(testFamily);
158     confirmSnapshotValid(snapshotDescriptor, tableName,
159       nonEmptyTestFamilies, null, rootDir, admin, fs, requireLogs,
160       logsDir, snapshotServers);
161   }
162 
163   /**
164    * Confirm that the snapshot has no references files but only metadata.
165    */
166   public static void confirmEmptySnapshotValid(
167       SnapshotDescription snapshotDescriptor, TableName tableName,
168       byte[] testFamily, Path rootDir, HBaseAdmin admin, FileSystem fs,
169       boolean requireLogs, Path logsDir, Set<String> snapshotServers)
170       throws IOException {
171     ArrayList emptyTestFamilies = new ArrayList(1);
172     emptyTestFamilies.add(testFamily);
173     confirmSnapshotValid(snapshotDescriptor, tableName,
174       null, emptyTestFamilies, rootDir, admin, fs, requireLogs,
175       logsDir, snapshotServers);
176   }
177 
178   /**
179    * Confirm that the snapshot contains references to all the files that should
180    * be in the snapshot. This method also perform some redundant check like
181    * the existence of the snapshotinfo or the regioninfo which are done always
182    * by the MasterSnapshotVerifier, at the end of the snapshot operation.
183    */
184   public static void confirmSnapshotValid(
185       SnapshotDescription snapshotDescriptor, TableName tableName,
186       List<byte[]> nonEmptyTestFamilies, List<byte[]> emptyTestFamilies,
187       Path rootDir, HBaseAdmin admin, FileSystem fs, boolean requireLogs,
188       Path logsDir, Set<String> snapshotServers) throws IOException {
189     // check snapshot dir
190     Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(
191         snapshotDescriptor, rootDir);
192     assertTrue(fs.exists(snapshotDir));
193 
194     // check snapshot info
195     Path snapshotinfo = new Path(snapshotDir, SnapshotDescriptionUtils.SNAPSHOTINFO_FILE);
196     assertTrue(fs.exists(snapshotinfo));
197 
198     // check the logs dir
199     if (requireLogs) {
200       TakeSnapshotUtils.verifyAllLogsGotReferenced(fs, logsDir,
201           snapshotServers, snapshotDescriptor, new Path(snapshotDir,
202               HConstants.HREGION_LOGDIR_NAME));
203     }
204 
205     // check the table info
206     HTableDescriptor desc = FSTableDescriptors.getTableDescriptorFromFs(fs, rootDir, tableName);
207     HTableDescriptor snapshotDesc = FSTableDescriptors.getTableDescriptorFromFs(fs, snapshotDir);
208     assertEquals(desc, snapshotDesc);
209 
210     // Extract regions and families with store files
211     final Set<String> snapshotRegions = new HashSet<String>();
212     final Set<byte[]> snapshotFamilies = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
213     FSVisitor.visitTableStoreFiles(fs, snapshotDir, new FSVisitor.StoreFileVisitor() {
214       public void storeFile(final String region, final String family, final String hfileName)
215           throws IOException {
216         snapshotRegions.add(region);
217         snapshotFamilies.add(Bytes.toBytes(family));
218       }
219     });
220 
221     // Verify that there are store files in the specified families
222     if (nonEmptyTestFamilies != null) {
223       for (final byte[] familyName: nonEmptyTestFamilies) {
224         assertTrue(snapshotFamilies.contains(familyName));
225       }
226     }
227 
228     // Verify that there are no store files in the specified families
229     if (emptyTestFamilies != null) {
230       for (final byte[] familyName: emptyTestFamilies) {
231         assertFalse(snapshotFamilies.contains(familyName));
232       }
233     }
234 
235     // Avoid checking regions if the request is for an empty snapshot
236     if ((nonEmptyTestFamilies == null || nonEmptyTestFamilies.size() == 0) &&
237         (emptyTestFamilies != null && emptyTestFamilies.size() > 0)) {
238       assertEquals(0, snapshotRegions.size());
239       return;
240     }
241 
242     // check the region snapshot for all the regions
243     List<HRegionInfo> regions = admin.getTableRegions(tableName);
244     assertEquals(regions.size(), snapshotRegions.size());
245 
246     // Verify Regions
247     for (HRegionInfo info : regions) {
248       String regionName = info.getEncodedName();
249       assertTrue(snapshotRegions.contains(regionName));
250 
251       Path regionDir = new Path(snapshotDir, regionName);
252       HRegionInfo snapshotRegionInfo = HRegionFileSystem.loadRegionInfoFileContent(fs, regionDir);
253       assertEquals(info, snapshotRegionInfo);
254     }
255   }
256 
257   /**
258    * Helper method for testing async snapshot operations. Just waits for the
259    * given snapshot to complete on the server by repeatedly checking the master.
260    *
261    * @param master: the master running the snapshot
262    * @param snapshot: the snapshot to check
263    * @param sleep: amount to sleep between checks to see if the snapshot is done
264    * @throws ServiceException if the snapshot fails
265    */
266   public static void waitForSnapshotToComplete(HMaster master,
267       SnapshotDescription snapshot, long sleep) throws ServiceException {
268     final IsSnapshotDoneRequest request = IsSnapshotDoneRequest.newBuilder()
269         .setSnapshot(snapshot).build();
270     IsSnapshotDoneResponse done = IsSnapshotDoneResponse.newBuilder()
271         .buildPartial();
272     while (!done.getDone()) {
273       done = master.isSnapshotDone(null, request);
274       try {
275         Thread.sleep(sleep);
276       } catch (InterruptedException e) {
277         throw new ServiceException(e);
278       }
279     }
280   }
281 
282   /*
283    * Take snapshot with maximum of numTries attempts, ignoring CorruptedSnapshotException
284    * except for the last CorruptedSnapshotException
285    */
286   public static void snapshot(HBaseAdmin admin,
287       final String snapshotName, final String tableName,
288       SnapshotDescription.Type type, int numTries) throws IOException {
289     int tries = 0;
290     CorruptedSnapshotException lastEx = null;
291     while (tries++ < numTries) {
292       try {
293         admin.snapshot(snapshotName, tableName, type);
294         return;
295       } catch (CorruptedSnapshotException cse) {
296         LOG.warn("Got CorruptedSnapshotException", cse);
297         lastEx = cse;
298       }
299     }
300     throw lastEx;
301   }
302 
303   public static void cleanupSnapshot(HBaseAdmin admin, byte[] tableName)
304       throws IOException {
305     SnapshotTestingUtils.cleanupSnapshot(admin, Bytes.toString(tableName));
306   }
307 
308   public static void cleanupSnapshot(HBaseAdmin admin, String snapshotName)
309       throws IOException {
310     // delete the taken snapshot
311     admin.deleteSnapshot(snapshotName);
312     assertNoSnapshots(admin);
313   }
314 
315   /**
316    * Expect the snapshot to throw an error when checking if the snapshot is
317    * complete
318    *
319    * @param master master to check
320    * @param snapshot the {@link SnapshotDescription} request to pass to the master
321    * @param clazz expected exception from the master
322    */
323   public static void expectSnapshotDoneException(HMaster master,
324       IsSnapshotDoneRequest snapshot,
325       Class<? extends HBaseSnapshotException> clazz) {
326     try {
327       master.isSnapshotDone(null, snapshot);
328       Assert.fail("didn't fail to lookup a snapshot");
329     } catch (ServiceException se) {
330       try {
331         throw ProtobufUtil.getRemoteException(se);
332       } catch (HBaseSnapshotException e) {
333         assertEquals("Threw wrong snapshot exception!", clazz, e.getClass());
334       } catch (Throwable t) {
335         Assert.fail("Threw an unexpected exception:" + t);
336       }
337     }
338   }
339 
340   /**
341    * List all the HFiles in the given table
342    *
343    * @param fs: FileSystem where the table lives
344    * @param tableDir directory of the table
345    * @return array of the current HFiles in the table (could be a zero-length array)
346    * @throws IOException on unexecpted error reading the FS
347    */
348   public static Path[] listHFiles(final FileSystem fs, final Path tableDir)
349       throws IOException {
350     final ArrayList<Path> hfiles = new ArrayList<Path>();
351     FSVisitor.visitTableStoreFiles(fs, tableDir, new FSVisitor.StoreFileVisitor() {
352       public void storeFile(final String region, final String family, final String hfileName)
353           throws IOException {
354         hfiles.add(new Path(tableDir, new Path(region, new Path(family, hfileName))));
355       }
356     });
357     return hfiles.toArray(new Path[hfiles.size()]);
358   }
359 
360   /**
361    * Take a snapshot of the specified table and verify that the given family is
362    * not empty. Note that this will leave the table disabled
363    * in the case of an offline snapshot.
364    */
365   public static void createSnapshotAndValidate(HBaseAdmin admin,
366       TableName tableName, String familyName, String snapshotNameString,
367       Path rootDir, FileSystem fs, boolean onlineSnapshot)
368       throws Exception {
369     ArrayList<byte[]> nonEmptyFamilyNames = new ArrayList<byte[]>(1);
370     nonEmptyFamilyNames.add(Bytes.toBytes(familyName));
371     createSnapshotAndValidate(admin, tableName, nonEmptyFamilyNames, /* emptyFamilyNames= */ null,
372                               snapshotNameString, rootDir, fs, onlineSnapshot);
373   }
374 
375   /**
376    * Take a snapshot of the specified table and verify the given families.
377    * Note that this will leave the table disabled in the case of an offline snapshot.
378    */
379   public static void createSnapshotAndValidate(HBaseAdmin admin,
380       TableName tableName, List<byte[]> nonEmptyFamilyNames, List<byte[]> emptyFamilyNames,
381       String snapshotNameString, Path rootDir, FileSystem fs, boolean onlineSnapshot)
382         throws Exception {
383     if (!onlineSnapshot) {
384       try {
385         admin.disableTable(tableName);
386       } catch (TableNotEnabledException tne) {
387         LOG.info("In attempting to disable " + tableName + " it turns out that the this table is " +
388             "already disabled.");
389       }
390     }
391     admin.snapshot(snapshotNameString, tableName);
392 
393     List<SnapshotDescription> snapshots = SnapshotTestingUtils.assertExistsMatchingSnapshot(admin,
394       snapshotNameString, tableName);
395     if (snapshots == null || snapshots.size() != 1) {
396       Assert.fail("Incorrect number of snapshots for table " + tableName);
397     }
398 
399     SnapshotTestingUtils.confirmSnapshotValid(snapshots.get(0), tableName, nonEmptyFamilyNames,
400       emptyFamilyNames, rootDir, admin, fs, false,
401       new Path(rootDir, HConstants.HREGION_LOGDIR_NAME), null);
402   }
403 
404   /**
405    * Corrupt the specified snapshot by deleting some files.
406    *
407    * @param util {@link HBaseTestingUtility}
408    * @param snapshotName name of the snapshot to corrupt
409    * @return array of the corrupted HFiles
410    * @throws IOException on unexecpted error reading the FS
411    */
412   public static ArrayList corruptSnapshot(final HBaseTestingUtility util, final String snapshotName)
413       throws IOException {
414     final MasterFileSystem mfs = util.getHBaseCluster().getMaster().getMasterFileSystem();
415     final FileSystem fs = mfs.getFileSystem();
416 
417     Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName,
418                                                                         mfs.getRootDir());
419     SnapshotDescription snapshotDesc = SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotDir);
420     final TableName table = TableName.valueOf(snapshotDesc.getTable());
421 
422     final ArrayList corruptedFiles = new ArrayList();
423     SnapshotReferenceUtil.visitTableStoreFiles(fs, snapshotDir, new FSVisitor.StoreFileVisitor() {
424       public void storeFile (final String region, final String family, final String hfile)
425           throws IOException {
426         HFileLink link = HFileLink.create(util.getConfiguration(), table, region, family, hfile);
427         if (corruptedFiles.size() % 2 == 0) {
428           fs.delete(link.getAvailablePath(fs));
429           corruptedFiles.add(hfile);
430         }
431       }
432     });
433 
434     assertTrue(corruptedFiles.size() > 0);
435     return corruptedFiles;
436   }
437 
438   // ==========================================================================
439   //  Table Helpers
440   // ==========================================================================
441   public static void waitForTableToBeOnline(final HBaseTestingUtility util,
442                                             final TableName tableName)
443       throws IOException, InterruptedException {
444     HRegionServer rs = util.getRSForFirstRegionInTable(tableName);
445     List<HRegion> onlineRegions = rs.getOnlineRegions(tableName);
446     for (HRegion region : onlineRegions) {
447       region.waitForFlushesAndCompactions();
448     }
449     util.getHBaseAdmin().isTableAvailable(tableName);
450   }
451 
452   public static void createTable(final HBaseTestingUtility util, final TableName tableName,
453       final byte[]... families) throws IOException, InterruptedException {
454     HTableDescriptor htd = new HTableDescriptor(tableName);
455     for (byte[] family: families) {
456       HColumnDescriptor hcd = new HColumnDescriptor(family);
457       htd.addFamily(hcd);
458     }
459     byte[][] splitKeys = new byte[KEYS.length-2][];
460     for (int i = 0; i < splitKeys.length; ++i) {
461       splitKeys[i] = new byte[] { KEYS[i+1] };
462     }
463     util.getHBaseAdmin().createTable(htd, splitKeys);
464     waitForTableToBeOnline(util, tableName);
465     assertEquals(KEYS.length-1, util.getHBaseAdmin().getTableRegions(tableName).size());
466   }
467 
468   public static void loadData(final HBaseTestingUtility util, final TableName tableName, int rows,
469       byte[]... families) throws IOException, InterruptedException {
470     loadData(util, new HTable(util.getConfiguration(), tableName), rows, families);
471   }
472 
473   public static void loadData(final HBaseTestingUtility util, final HTable table, int rows,
474       byte[]... families) throws IOException, InterruptedException {
475     table.setAutoFlush(false, true);
476 
477     // Ensure one row per region
478     assertTrue(rows >= KEYS.length);
479     for (byte k0: KEYS) {
480       byte[] k = new byte[] { k0 };
481       byte[] value = Bytes.add(Bytes.toBytes(System.currentTimeMillis()), k);
482       byte[] key = Bytes.add(k, Bytes.toBytes(MD5Hash.getMD5AsHex(value)));
483       putData(table, families, key, value);
484       rows--;
485     }
486 
487     // Add other extra rows. more rows, more files
488     while (rows-- > 0) {
489       byte[] value = Bytes.add(Bytes.toBytes(System.currentTimeMillis()), Bytes.toBytes(rows));
490       byte[] key = Bytes.toBytes(MD5Hash.getMD5AsHex(value));
491       putData(table, families, key, value);
492     }
493     table.flushCommits();
494 
495     waitForTableToBeOnline(util, table.getName());
496   }
497 
498   private static void putData(final HTable table, final byte[][] families,
499       final byte[] key, final byte[] value) throws IOException {
500     byte[] q = Bytes.toBytes("q");
501     Put put = new Put(key);
502     put.setDurability(Durability.SKIP_WAL);
503     for (byte[] family: families) {
504       put.add(family, q, value);
505     }
506     table.put(put);
507   }
508 
509   public static void deleteAllSnapshots(final HBaseAdmin admin)
510       throws IOException {
511     // Delete all the snapshots
512     for (SnapshotDescription snapshot: admin.listSnapshots()) {
513       admin.deleteSnapshot(snapshot.getName());
514     }
515     SnapshotTestingUtils.assertNoSnapshots(admin);
516   }
517 
518   public static void deleteArchiveDirectory(final HBaseTestingUtility util)
519       throws IOException {
520     // Ensure the archiver to be empty
521     MasterFileSystem mfs = util.getMiniHBaseCluster().getMaster().getMasterFileSystem();
522     Path archiveDir = new Path(mfs.getRootDir(), HConstants.HFILE_ARCHIVE_DIRECTORY);
523     mfs.getFileSystem().delete(archiveDir, true);
524   }
525 
526   public static void verifyRowCount(final HBaseTestingUtility util, final TableName tableName,
527       long expectedRows) throws IOException {
528     HTable table = new HTable(util.getConfiguration(), tableName);
529     try {
530       assertEquals(expectedRows, util.countRows(table));
531     } finally {
532       table.close();
533     }
534   }
535 }