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.snapshot;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.IOException;
26  import java.net.URI;
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.List;
30  import java.util.HashSet;
31  import java.util.Set;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import org.apache.hadoop.conf.Configuration;
37  import org.apache.hadoop.fs.FileSystem;
38  import org.apache.hadoop.fs.FileStatus;
39  import org.apache.hadoop.fs.FileUtil;
40  import org.apache.hadoop.fs.FSDataOutputStream;
41  import org.apache.hadoop.fs.Path;
42  import org.apache.hadoop.hbase.HBaseTestingUtility;
43  import org.apache.hadoop.hbase.HColumnDescriptor;
44  import org.apache.hadoop.hbase.HConstants;
45  import org.apache.hadoop.hbase.HRegionInfo;
46  import org.apache.hadoop.hbase.HTableDescriptor;
47  import org.apache.hadoop.hbase.KeyValue;
48  import org.apache.hadoop.hbase.MediumTests;
49  import org.apache.hadoop.hbase.MiniHBaseCluster;
50  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
51  import org.apache.hadoop.hbase.client.HBaseAdmin;
52  import org.apache.hadoop.hbase.client.HTable;
53  import org.apache.hadoop.hbase.util.Bytes;
54  import org.apache.hadoop.hbase.util.FSUtils;
55  import org.apache.hadoop.hbase.util.Pair;
56  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
57  import org.apache.hadoop.hbase.regionserver.HRegion;
58  import org.apache.hadoop.hbase.snapshot.ExportSnapshot;
59  import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
60  import org.apache.hadoop.mapreduce.Job;
61  import org.junit.After;
62  import org.junit.AfterClass;
63  import org.junit.Before;
64  import org.junit.BeforeClass;
65  import org.junit.Test;
66  import org.junit.experimental.categories.Category;
67  
68  /**
69   * Test Export Snapshot Tool
70   */
71  @Category(MediumTests.class)
72  public class TestExportSnapshot {
73    private final Log LOG = LogFactory.getLog(getClass());
74  
75    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
76  
77    private final static byte[] FAMILY = Bytes.toBytes("cf");
78  
79    private byte[] emptySnapshotName;
80    private byte[] snapshotName;
81    private byte[] tableName;
82    private HBaseAdmin admin;
83  
84    @BeforeClass
85    public static void setUpBeforeClass() throws Exception {
86      TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
87      TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
88      TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
89      TEST_UTIL.getConfiguration().setInt("hbase.client.retries.number", 6);
90      TEST_UTIL.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true);
91      TEST_UTIL.getConfiguration().setInt("mapreduce.map.max.attempts", 10);
92      TEST_UTIL.getConfiguration().setInt("mapred.map.max.attempts", 10);
93      TEST_UTIL.startMiniCluster(3);
94    }
95  
96    @AfterClass
97    public static void tearDownAfterClass() throws Exception {
98      TEST_UTIL.shutdownMiniCluster();
99    }
100 
101   /**
102    * Create a table and take a snapshot of the table used by the export test.
103    */
104   @Before
105   public void setUp() throws Exception {
106     this.admin = TEST_UTIL.getHBaseAdmin();
107 
108     long tid = System.currentTimeMillis();
109     tableName = Bytes.toBytes("testtb-" + tid);
110     snapshotName = Bytes.toBytes("snaptb0-" + tid);
111     emptySnapshotName = Bytes.toBytes("emptySnaptb0-" + tid);
112 
113     // create Table
114     SnapshotTestingUtils.createTable(TEST_UTIL, tableName, FAMILY);
115 
116     // Take an empty snapshot
117     admin.snapshot(emptySnapshotName, tableName);
118 
119     // Add some rows
120     HTable table = new HTable(TEST_UTIL.getConfiguration(), tableName);
121     SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 500, FAMILY);
122 
123     // take a snapshot
124     admin.snapshot(snapshotName, tableName);
125   }
126 
127   @After
128   public void tearDown() throws Exception {
129     TEST_UTIL.deleteTable(tableName);
130     SnapshotTestingUtils.deleteAllSnapshots(TEST_UTIL.getHBaseAdmin());
131     SnapshotTestingUtils.deleteArchiveDirectory(TEST_UTIL);
132     admin.close();
133   }
134 
135 
136   /**
137    * Verfy the result of getBalanceSplits() method.
138    * The result are groups of files, used as input list for the "export" mappers.
139    * All the groups should have similar amount of data.
140    *
141    * The input list is a pair of file path and length.
142    * The getBalanceSplits() function sort it by length,
143    * and assign to each group a file, going back and forth through the groups.
144    */
145   @Test
146   public void testBalanceSplit() throws Exception {
147     // Create a list of files
148     List<Pair<Path, Long>> files = new ArrayList<Pair<Path, Long>>();
149     for (long i = 0; i <= 20; i++) {
150       files.add(new Pair<Path, Long>(new Path("file-" + i), i));
151     }
152 
153     // Create 5 groups (total size 210)
154     //    group 0: 20, 11, 10,  1 (total size: 42)
155     //    group 1: 19, 12,  9,  2 (total size: 42)
156     //    group 2: 18, 13,  8,  3 (total size: 42)
157     //    group 3: 17, 12,  7,  4 (total size: 42)
158     //    group 4: 16, 11,  6,  5 (total size: 42)
159     List<List<Path>> splits = ExportSnapshot.getBalancedSplits(files, 5);
160     assertEquals(5, splits.size());
161     assertEquals(Arrays.asList(new Path("file-20"), new Path("file-11"),
162       new Path("file-10"), new Path("file-1"), new Path("file-0")), splits.get(0));
163     assertEquals(Arrays.asList(new Path("file-19"), new Path("file-12"),
164       new Path("file-9"), new Path("file-2")), splits.get(1));
165     assertEquals(Arrays.asList(new Path("file-18"), new Path("file-13"),
166       new Path("file-8"), new Path("file-3")), splits.get(2));
167     assertEquals(Arrays.asList(new Path("file-17"), new Path("file-14"),
168       new Path("file-7"), new Path("file-4")), splits.get(3));
169     assertEquals(Arrays.asList(new Path("file-16"), new Path("file-15"),
170       new Path("file-6"), new Path("file-5")), splits.get(4));
171   }
172 
173   /**
174    * Verify if exported snapshot and copied files matches the original one.
175    */
176   @Test
177   public void testExportFileSystemState() throws Exception {
178     testExportFileSystemState(tableName, snapshotName, snapshotName, 2);
179   }
180 
181   @Test
182   public void testExportFileSystemStateWithSkipTmp() throws Exception {
183     TEST_UTIL.getConfiguration().setBoolean(ExportSnapshot.CONF_SKIP_TMP, true);
184     testExportFileSystemState(tableName, snapshotName, snapshotName, 2);
185   }
186 
187   @Test
188   public void testEmptyExportFileSystemState() throws Exception {
189     testExportFileSystemState(tableName, emptySnapshotName, emptySnapshotName, 1);
190   }
191 
192   @Test
193   public void testConsecutiveExports() throws Exception {
194     Path copyDir = getLocalDestinationDir();
195     testExportFileSystemState(tableName, snapshotName, snapshotName, 2, copyDir, false);
196     testExportFileSystemState(tableName, snapshotName, snapshotName, 2, copyDir, true);
197     removeExportDir(copyDir);
198   }
199 
200   @Test
201   public void testExportWithTargetName() throws Exception {
202     final byte[] targetName = Bytes.toBytes("testExportWithTargetName");
203     testExportFileSystemState(tableName, snapshotName, targetName, 2);
204   }
205 
206   /**
207    * Mock a snapshot with files in the archive dir,
208    * two regions, and one reference file.
209    */
210   @Test
211   public void testSnapshotWithRefsExportFileSystemState() throws Exception {
212     Configuration conf = TEST_UTIL.getConfiguration();
213 
214     final byte[] tableWithRefsName = Bytes.toBytes("tableWithRefs");
215     final String snapshotName = "tableWithRefs";
216     final String TEST_FAMILY = Bytes.toString(FAMILY);
217     final String TEST_HFILE = "abc";
218 
219     final SnapshotDescription sd = SnapshotDescription.newBuilder()
220         .setName(snapshotName).setTable(Bytes.toString(tableWithRefsName)).build();
221 
222     FileSystem fs = TEST_UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
223     Path rootDir = TEST_UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
224     Path archiveDir = new Path(rootDir, HConstants.HFILE_ARCHIVE_DIRECTORY);
225 
226     HTableDescriptor htd = new HTableDescriptor(tableWithRefsName);
227     htd.addFamily(new HColumnDescriptor(TEST_FAMILY));
228 
229     // First region, simple with one plain hfile.
230     HRegion r0 = HRegion.createHRegion(new HRegionInfo(htd.getName()), archiveDir,
231         conf, htd, null, true, true);
232     Path storeFile = new Path(new Path(r0.getRegionDir(), TEST_FAMILY), TEST_HFILE);
233     FSDataOutputStream out = fs.create(storeFile);
234     out.write(Bytes.toBytes("Test Data"));
235     out.close();
236     r0.close();
237 
238     // Second region, used to test the split case.
239     // This region contains a reference to the hfile in the first region.
240     HRegion r1 = HRegion.createHRegion(new HRegionInfo(htd.getName()), archiveDir,
241         conf, htd, null, true, true);
242     out = fs.create(new Path(new Path(r1.getRegionDir(), TEST_FAMILY),
243         storeFile.getName() + '.' + r0.getRegionInfo().getEncodedName()));
244     out.write(Bytes.toBytes("Test Data"));
245     out.close();
246     r1.close();
247 
248     Path tableDir = HTableDescriptor.getTableDir(archiveDir, tableWithRefsName);
249     Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
250     FileUtil.copy(fs, tableDir, fs, snapshotDir, false, conf);
251     SnapshotDescriptionUtils.writeSnapshotInfo(sd, snapshotDir, fs);
252 
253     byte[] name = Bytes.toBytes(snapshotName);
254     testExportFileSystemState(tableWithRefsName, name, name, 2);
255   }
256 
257   private void testExportFileSystemState(final byte[] tableName, final byte[] snapshotName,
258       final byte[] targetName, int filesExpected) throws Exception {
259     Path copyDir = getHdfsDestinationDir();
260     testExportFileSystemState(tableName, snapshotName, targetName, filesExpected, copyDir, false);
261     removeExportDir(copyDir);
262   }
263 
264   /**
265    * Test ExportSnapshot
266    */
267   private void testExportFileSystemState(final byte[] tableName, final byte[] snapshotName,
268       final byte[] targetName, int filesExpected, Path copyDir, boolean overwrite)
269       throws Exception {
270     URI hdfsUri = FileSystem.get(TEST_UTIL.getConfiguration()).getUri();
271     FileSystem fs = FileSystem.get(copyDir.toUri(), new Configuration());
272     copyDir = copyDir.makeQualified(fs);
273 
274     List<String> opts = new ArrayList<String>();
275     opts.add("-snapshot");
276     opts.add(Bytes.toString(snapshotName));
277     opts.add("-copy-to");
278     opts.add(copyDir.toString());
279     if (targetName != snapshotName) {
280       opts.add("-target");
281       opts.add(Bytes.toString(targetName));
282     }
283     if (overwrite) opts.add("-overwrite");
284 
285     // Export Snapshot
286     int res = ExportSnapshot.innerMain(TEST_UTIL.getConfiguration(),
287         opts.toArray(new String[opts.size()]));
288     assertEquals(0, res);
289 
290     // Verify File-System state
291     FileStatus[] rootFiles = fs.listStatus(copyDir);
292     assertEquals(filesExpected, rootFiles.length);
293     for (FileStatus fileStatus: rootFiles) {
294       String name = fileStatus.getPath().getName();
295       assertTrue(fileStatus.isDir());
296       assertTrue(name.equals(HConstants.SNAPSHOT_DIR_NAME) || name.equals(".archive"));
297     }
298 
299     // compare the snapshot metadata and verify the hfiles
300     final FileSystem hdfs = FileSystem.get(hdfsUri, TEST_UTIL.getConfiguration());
301     final Path snapshotDir = new Path(HConstants.SNAPSHOT_DIR_NAME, Bytes.toString(snapshotName));
302     final Path targetDir = new Path(HConstants.SNAPSHOT_DIR_NAME, Bytes.toString(targetName));
303     verifySnapshot(hdfs, new Path(TEST_UTIL.getDefaultRootDirPath(), snapshotDir),
304         fs, new Path(copyDir, targetDir));
305     verifyArchive(fs, copyDir, tableName, Bytes.toString(targetName));
306     FSUtils.logFileSystemState(hdfs, snapshotDir, LOG);
307   }
308 
309   /**
310    * Check that ExportSnapshot will return a failure if something fails.
311    */
312   @Test
313   public void testExportFailure() throws Exception {
314     assertEquals(1, runExportAndInjectFailures(snapshotName, false));
315   }
316 
317   /*
318    * Execute the ExportSnapshot job injecting failures
319    */
320   private int runExportAndInjectFailures(final byte[] snapshotName, boolean retry)
321       throws Exception {
322     Path copyDir = TEST_UTIL.getDataTestDir("export-" + System.currentTimeMillis());
323     URI hdfsUri = FileSystem.get(TEST_UTIL.getConfiguration()).getUri();
324     FileSystem fs = FileSystem.get(copyDir.toUri(), new Configuration());
325     copyDir = copyDir.makeQualified(fs);
326 
327     Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
328     conf.setBoolean(ExportSnapshot.CONF_TEST_FAILURE, true);
329     conf.setBoolean(ExportSnapshot.CONF_TEST_RETRY, retry);
330 
331     // Export Snapshot
332     int res = ExportSnapshot.innerMain(conf, new String[] {
333       "-snapshot", Bytes.toString(snapshotName),
334       "-copy-to", copyDir.toString()
335     });
336     return res;
337   }
338 
339   /*
340    * verify if the snapshot folder on file-system 1 match the one on file-system 2
341    */
342   private void verifySnapshot(final FileSystem fs1, final Path root1,
343       final FileSystem fs2, final Path root2) throws IOException {
344     Set<String> s = new HashSet<String>();
345     assertEquals(listFiles(fs1, root1, root1), listFiles(fs2, root2, root2));
346   }
347 
348   /*
349    * Verify if the files exists
350    */
351   private void verifyArchive(final FileSystem fs, final Path rootDir,
352       final byte[] tableName, final String snapshotName) throws IOException {
353     final Path exportedSnapshot = new Path(rootDir,
354       new Path(HConstants.SNAPSHOT_DIR_NAME, snapshotName));
355     final Path exportedArchive = new Path(rootDir, HConstants.HFILE_ARCHIVE_DIRECTORY);
356     LOG.debug(listFiles(fs, exportedArchive, exportedArchive));
357     SnapshotReferenceUtil.visitReferencedFiles(fs, exportedSnapshot,
358         new SnapshotReferenceUtil.FileVisitor() {
359         public void storeFile (final String region, final String family, final String hfile)
360             throws IOException {
361           verifyNonEmptyFile(new Path(exportedArchive,
362             new Path(Bytes.toString(tableName), new Path(region, new Path(family, hfile)))));
363         }
364 
365         public void recoveredEdits (final String region, final String logfile)
366             throws IOException {
367           verifyNonEmptyFile(new Path(exportedSnapshot,
368             new Path(Bytes.toString(tableName), new Path(region, logfile))));
369         }
370 
371         public void logFile (final String server, final String logfile)
372             throws IOException {
373           verifyNonEmptyFile(new Path(exportedSnapshot, new Path(server, logfile)));
374         }
375 
376         private void verifyNonEmptyFile(final Path path) throws IOException {
377           assertTrue(path + " should exist", fs.exists(path));
378           assertTrue(path + " should not be empty", fs.getFileStatus(path).getLen() > 0);
379         }
380     });
381 
382     // Verify Snapshot description
383     SnapshotDescription desc = SnapshotDescriptionUtils.readSnapshotInfo(fs, exportedSnapshot);
384     assertTrue(desc.getName().equals(snapshotName));
385     assertTrue(desc.getTable().equals(Bytes.toString(tableName)));
386   }
387 
388   private Set<String> listFiles(final FileSystem fs, final Path root, final Path dir)
389       throws IOException {
390     Set<String> files = new HashSet<String>();
391     int rootPrefix = root.toString().length();
392     FileStatus[] list = FSUtils.listStatus(fs, dir);
393     if (list != null) {
394       for (FileStatus fstat: list) {
395         LOG.debug(fstat.getPath());
396         if (fstat.isDir()) {
397           files.addAll(listFiles(fs, root, fstat.getPath()));
398         } else {
399           files.add(fstat.getPath().toString().substring(rootPrefix));
400         }
401       }
402     }
403     return files;
404   }
405 
406   private Path getHdfsDestinationDir() {
407     Path rootDir = TEST_UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
408     Path path = new Path(new Path(rootDir, "export-test"), "export-" + System.currentTimeMillis());
409     LOG.info("HDFS export destination path: " + path);
410     return path;
411   }
412 
413   private Path getLocalDestinationDir() {
414     Path path = TEST_UTIL.getDataTestDir("local-export-" + System.currentTimeMillis());
415     LOG.info("Local export destination path: " + path);
416     return path;
417   }
418 
419   private void removeExportDir(final Path path) throws IOException {
420     FileSystem fs = FileSystem.get(path.toUri(), new Configuration());
421     FSUtils.logFileSystemState(fs, path, LOG);
422     fs.delete(path, true);
423   }
424 }