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.HBaseTestingUtility;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.HRegionInfo;
30 import org.apache.hadoop.hbase.SmallTests;
31 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.apache.hadoop.hbase.util.FSUtils;
34 import org.junit.AfterClass;
35 import org.junit.Test;
36 import org.junit.experimental.categories.Category;
37
38
39
40
41 @Category(SmallTests.class)
42 public class TestSnapshotHFileCleaner {
43
44 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
45
46 @AfterClass
47 public static void cleanup() throws IOException {
48 Configuration conf = TEST_UTIL.getConfiguration();
49 Path rootDir = FSUtils.getRootDir(conf);
50 FileSystem fs = FileSystem.get(conf);
51
52 fs.delete(rootDir, true);
53 }
54
55 @Test
56 public void testFindsSnapshotFilesWhenCleaning() throws IOException {
57 Configuration conf = TEST_UTIL.getConfiguration();
58 FSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
59 Path rootDir = FSUtils.getRootDir(conf);
60 Path archivedHfileDir = new Path(TEST_UTIL.getDataTestDir(), HConstants.HFILE_ARCHIVE_DIRECTORY);
61
62 FileSystem fs = FileSystem.get(conf);
63 SnapshotHFileCleaner cleaner = new SnapshotHFileCleaner();
64 cleaner.setConf(conf);
65
66
67 String snapshotName = "snapshot";
68 byte[] snapshot = Bytes.toBytes(snapshotName);
69 String table = "table";
70 byte[] tableName = Bytes.toBytes(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 }