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 import java.util.Collection;
24 import java.util.HashSet;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.fs.FileSystem;
30 import org.apache.hadoop.fs.Path;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.HRegionInfo;
35 import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
36 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
37 import org.apache.hadoop.hbase.testclassification.SmallTests;
38 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
39 import org.apache.hadoop.hbase.util.FSUtils;
40 import org.junit.AfterClass;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.junit.experimental.categories.Category;
44
45
46
47
48 @Category(SmallTests.class)
49 public class TestSnapshotHFileCleaner {
50
51 private static final Log LOG = LogFactory.getLog(TestSnapshotFileCache.class);
52 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
53 private static final String TABLE_NAME_STR = "testSnapshotManifest";
54 private static final String SNAPSHOT_NAME_STR = "testSnapshotManifest-snapshot";
55 private static Path rootDir;
56 private static FileSystem fs;
57
58
59
60
61 @BeforeClass
62 public static void setup() throws Exception {
63 Configuration conf = TEST_UTIL.getConfiguration();
64 rootDir = FSUtils.getRootDir(conf);
65 fs = FileSystem.get(conf);
66 }
67
68 @AfterClass
69 public static void cleanup() throws IOException {
70
71 fs.delete(rootDir, true);
72 }
73
74 @Test
75 public void testFindsSnapshotFilesWhenCleaning() throws IOException {
76 Configuration conf = TEST_UTIL.getConfiguration();
77 FSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
78 Path rootDir = FSUtils.getRootDir(conf);
79 Path archivedHfileDir = new Path(TEST_UTIL.getDataTestDir(), HConstants.HFILE_ARCHIVE_DIRECTORY);
80
81 FileSystem fs = FileSystem.get(conf);
82 SnapshotHFileCleaner cleaner = new SnapshotHFileCleaner();
83 cleaner.setConf(conf);
84
85
86 String snapshotName = "snapshot";
87 TableName tableName = TableName.valueOf("table");
88 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
89 HRegionInfo mockRegion = new HRegionInfo(tableName);
90 Path regionSnapshotDir = new Path(snapshotDir, mockRegion.getEncodedName());
91 Path familyDir = new Path(regionSnapshotDir, "family");
92
93 String hfile = "fd1e73e8a96c486090c5cec07b4894c4";
94 Path refFile = new Path(familyDir, hfile);
95
96
97 fs.create(refFile);
98
99
100 fs.mkdirs(archivedHfileDir);
101 fs.createNewFile(new Path(archivedHfileDir, hfile));
102
103
104 assertFalse(cleaner.isFileDeletable(fs.getFileStatus(refFile)));
105 }
106
107 class SnapshotFiles implements SnapshotFileCache.SnapshotFileInspector {
108 public Collection<String> filesUnderSnapshot(final Path snapshotDir) throws IOException {
109 Collection<String> files = new HashSet<String>();
110 files.addAll(SnapshotReferenceUtil.getHFileNames(TEST_UTIL.getConfiguration(), fs, snapshotDir));
111 return files;
112 }
113 }
114
115
116
117
118
119 @Test
120 public void testCorruptedRegionManifest() throws IOException {
121 SnapshotTestingUtils.SnapshotMock
122 snapshotMock = new SnapshotTestingUtils.SnapshotMock(TEST_UTIL.getConfiguration(), fs, rootDir);
123 SnapshotTestingUtils.SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(
124 SNAPSHOT_NAME_STR, TABLE_NAME_STR);
125 builder.addRegionV2();
126 builder.corruptOneRegionManifest();
127
128 fs.delete(SnapshotDescriptionUtils.getWorkingSnapshotDir(rootDir), true);
129 }
130
131
132
133
134
135 @Test
136 public void testCorruptedDataManifest() throws IOException {
137 SnapshotTestingUtils.SnapshotMock
138 snapshotMock = new SnapshotTestingUtils.SnapshotMock(TEST_UTIL.getConfiguration(), fs, rootDir);
139 SnapshotTestingUtils.SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(
140 SNAPSHOT_NAME_STR, TABLE_NAME_STR);
141 builder.addRegionV2();
142
143 builder.consolidate();
144 builder.corruptDataManifest();
145
146 fs.delete(SnapshotDescriptionUtils.getWorkingSnapshotDir(rootDir), true);
147 }
148 }