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