1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.master.snapshot;
19
20 import static org.junit.Assert.assertFalse;
21
22 import java.io.IOException;
23
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.fs.FileSystem;
26 import org.apache.hadoop.fs.Path;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.HBaseTestingUtility;
29 import org.apache.hadoop.hbase.HConstants;
30 import org.apache.hadoop.hbase.HRegionInfo;
31 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
32 import org.apache.hadoop.hbase.testclassification.SmallTests;
33 import org.apache.hadoop.hbase.util.Bytes;
34 import org.apache.hadoop.hbase.util.FSUtils;
35 import org.junit.AfterClass;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
38
39
40
41
42 @Category(SmallTests.class)
43 public class TestSnapshotHFileCleaner {
44
45 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46
47 @AfterClass
48 public static void cleanup() throws IOException {
49 Configuration conf = TEST_UTIL.getConfiguration();
50 Path rootDir = FSUtils.getRootDir(conf);
51 FileSystem fs = FileSystem.get(conf);
52
53 fs.delete(rootDir, true);
54 }
55
56 @Test
57 public void testFindsSnapshotFilesWhenCleaning() throws IOException {
58 Configuration conf = TEST_UTIL.getConfiguration();
59 FSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
60 Path rootDir = FSUtils.getRootDir(conf);
61 Path archivedHfileDir = new Path(TEST_UTIL.getDataTestDir(), HConstants.HFILE_ARCHIVE_DIRECTORY);
62
63 FileSystem fs = FileSystem.get(conf);
64 SnapshotHFileCleaner cleaner = new SnapshotHFileCleaner();
65 cleaner.setConf(conf);
66
67
68 String snapshotName = "snapshot";
69 byte[] snapshot = Bytes.toBytes(snapshotName);
70 TableName tableName = TableName.valueOf("table");
71 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
72 HRegionInfo mockRegion = new HRegionInfo(tableName);
73 Path regionSnapshotDir = new Path(snapshotDir, mockRegion.getEncodedName());
74 Path familyDir = new Path(regionSnapshotDir, "family");
75
76 String hfile = "fd1e73e8a96c486090c5cec07b4894c4";
77 Path refFile = new Path(familyDir, hfile);
78
79
80 fs.create(refFile);
81
82
83 fs.mkdirs(archivedHfileDir);
84 fs.createNewFile(new Path(archivedHfileDir, hfile));
85
86
87 assertFalse(cleaner.isFileDeletable(fs.getFileStatus(refFile)));
88 }
89 }