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.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import java.io.IOException;
27 import java.util.*;
28 import java.util.concurrent.atomic.AtomicInteger;
29
30 import com.google.common.collect.Iterables;
31 import com.google.common.collect.Lists;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.hadoop.fs.FileStatus;
35 import org.apache.hadoop.fs.FileSystem;
36 import org.apache.hadoop.fs.Path;
37 import org.apache.hadoop.hbase.HBaseTestingUtility;
38 import org.apache.hadoop.hbase.HRegionInfo;
39 import org.apache.hadoop.hbase.protobuf.generated.SnapshotProtos;
40 import org.apache.hadoop.hbase.testclassification.MediumTests;
41 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
42 import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
43 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils.SnapshotMock;
44 import org.apache.hadoop.hbase.util.FSUtils;
45 import org.junit.After;
46 import org.junit.AfterClass;
47 import org.junit.BeforeClass;
48 import org.junit.Ignore;
49 import org.junit.Test;
50 import org.junit.experimental.categories.Category;
51
52
53
54
55 @Category(MediumTests.class)
56 public class TestSnapshotFileCache {
57
58 private static final Log LOG = LogFactory.getLog(TestSnapshotFileCache.class);
59 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
60 private static FileSystem fs;
61 private static Path rootDir;
62
63 @BeforeClass
64 public static void startCluster() throws Exception {
65 UTIL.startMiniDFSCluster(1);
66 fs = UTIL.getDFSCluster().getFileSystem();
67 rootDir = UTIL.getDefaultRootDirPath();
68 }
69
70 @AfterClass
71 public static void stopCluster() throws Exception {
72 UTIL.shutdownMiniDFSCluster();
73 }
74
75 @After
76 public void cleanupFiles() throws Exception {
77
78 Path snapshotDir = SnapshotDescriptionUtils.getSnapshotsDir(rootDir);
79 fs.delete(snapshotDir, true);
80 }
81
82 @Test(timeout = 10000000)
83 @Ignore("See HBASE-19275")
84 public void testLoadAndDelete() throws IOException {
85
86 long period = Long.MAX_VALUE;
87 SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
88 "test-snapshot-file-cache-refresh", new SnapshotFiles());
89
90 createAndTestSnapshotV1(cache, "snapshot1a", false, true);
91 createAndTestSnapshotV1(cache, "snapshot1b", true, true);
92
93 createAndTestSnapshotV2(cache, "snapshot2a", false, true);
94 createAndTestSnapshotV2(cache, "snapshot2b", true, true);
95 }
96
97 @Test
98 @Ignore("See HBASE-19275")
99 public void testReloadModifiedDirectory() throws IOException {
100
101 long period = Long.MAX_VALUE;
102 SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
103 "test-snapshot-file-cache-refresh", new SnapshotFiles());
104
105 createAndTestSnapshotV1(cache, "snapshot1", false, true);
106
107 createAndTestSnapshotV1(cache, "snapshot1", false, false);
108
109 createAndTestSnapshotV2(cache, "snapshot2", false, true);
110
111 createAndTestSnapshotV2(cache, "snapshot2", false, false);
112 }
113
114 @Test
115 public void testSnapshotTempDirReload() throws IOException {
116 long period = Long.MAX_VALUE;
117
118 SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
119 "test-snapshot-file-cache-refresh", new SnapshotFiles());
120
121
122 createAndTestSnapshotV1(cache, "snapshot0v1", false, false);
123 createAndTestSnapshotV1(cache, "snapshot0v2", false, false);
124
125
126 createAndTestSnapshotV2(cache, "snapshot1", true, false);
127
128
129 createAndTestSnapshotV2(cache, "snapshot2", true, false);
130 }
131
132 @Test
133 public void testWeNeverCacheTmpDirAndLoadIt() throws Exception {
134
135 final AtomicInteger count = new AtomicInteger(0);
136
137 long period = Long.MAX_VALUE;
138 SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
139 "test-snapshot-file-cache-refresh", new SnapshotFiles()) {
140 @Override
141 List<String> getSnapshotsInProgress(final SnapshotManager snapshotManager)
142 throws IOException {
143 List<String> result = super.getSnapshotsInProgress(snapshotManager);
144 count.incrementAndGet();
145 return result;
146 }
147
148 @Override public void triggerCacheRefreshForTesting() {
149 super.triggerCacheRefreshForTesting();
150 }
151 };
152
153 SnapshotMock.SnapshotBuilder complete =
154 createAndTestSnapshotV1(cache, "snapshot", false, false);
155
156 int countBeforeCheck = count.get();
157
158 FSUtils.logFileSystemState(fs, rootDir, LOG);
159
160 List<FileStatus> allStoreFiles = getStoreFilesForSnapshot(complete);
161 Iterable<FileStatus> deletableFiles = cache.getUnreferencedFiles(allStoreFiles, null);
162 assertTrue(Iterables.isEmpty(deletableFiles));
163
164 assertEquals(0, count.get() - countBeforeCheck);
165
166
167
168 FileStatus randomFile = mockStoreFile(UUID.randomUUID().toString());
169 allStoreFiles.add(randomFile);
170 deletableFiles = cache.getUnreferencedFiles(allStoreFiles, null);
171 assertEquals(randomFile, Iterables.getOnlyElement(deletableFiles));
172 assertEquals(1, count.get() - countBeforeCheck);
173 }
174
175 private List<FileStatus> getStoreFilesForSnapshot(SnapshotMock.SnapshotBuilder builder)
176 throws IOException {
177 final List<FileStatus> allStoreFiles = Lists.newArrayList();
178 SnapshotReferenceUtil
179 .visitReferencedFiles(UTIL.getConfiguration(), fs, builder.getSnapshotsDir(),
180 new SnapshotReferenceUtil.SnapshotVisitor() {
181 @Override public void storeFile(HRegionInfo regionInfo, String familyName,
182 SnapshotProtos.SnapshotRegionManifest.StoreFile storeFile) throws IOException {
183 FileStatus status = mockStoreFile(storeFile.getName());
184 allStoreFiles.add(status);
185 }
186 });
187 return allStoreFiles;
188 }
189
190 private FileStatus mockStoreFile(String storeFileName) {
191 FileStatus status = mock(FileStatus.class);
192 Path path = mock(Path.class);
193 when(path.getName()).thenReturn(storeFileName);
194 when(status.getPath()).thenReturn(path);
195 return status;
196 }
197
198 class SnapshotFiles implements SnapshotFileCache.SnapshotFileInspector {
199 public Collection<String> filesUnderSnapshot(final Path snapshotDir) throws IOException {
200 Collection<String> files = new HashSet<String>();
201 files.addAll(SnapshotReferenceUtil.getHFileNames(UTIL.getConfiguration(), fs, snapshotDir));
202 return files;
203 }
204 };
205
206 private SnapshotMock.SnapshotBuilder createAndTestSnapshotV1(final SnapshotFileCache cache,
207 final String name, final boolean tmp, final boolean removeOnExit) throws IOException {
208 SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
209 SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV1(name, name);
210 createAndTestSnapshot(cache, builder, tmp, removeOnExit);
211 return builder;
212 }
213
214 private void createAndTestSnapshotV2(final SnapshotFileCache cache, final String name,
215 final boolean tmp, final boolean removeOnExit) throws IOException {
216 SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
217 SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(name, name);
218 createAndTestSnapshot(cache, builder, tmp, removeOnExit);
219 }
220
221 private void createAndTestSnapshot(final SnapshotFileCache cache,
222 final SnapshotMock.SnapshotBuilder builder,
223 final boolean tmp, final boolean removeOnExit) throws IOException {
224 List<Path> files = new ArrayList<Path>();
225 for (int i = 0; i < 3; ++i) {
226 for (Path filePath: builder.addRegion()) {
227 if (tmp) {
228
229 FSUtils.logFileSystemState(fs, rootDir, LOG);
230 assertFalse("Cache didn't find " + filePath,
231 contains(getNonSnapshotFiles(cache, filePath), filePath));
232 }
233 files.add(filePath);
234 }
235 }
236
237
238 if (!tmp) {
239 builder.commit();
240 }
241
242
243 for (Path path: files) {
244 assertFalse("Cache didn't find " + path, contains(getNonSnapshotFiles(cache, path), path));
245 }
246
247 FSUtils.logFileSystemState(fs, rootDir, LOG);
248 if (removeOnExit) {
249 LOG.debug("Deleting snapshot.");
250 fs.delete(builder.getSnapshotsDir(), true);
251 FSUtils.logFileSystemState(fs, rootDir, LOG);
252
253
254 for (Path filePath: files) {
255 assertFalse("Cache didn't find " + filePath,
256 contains(getNonSnapshotFiles(cache, filePath), filePath));
257 }
258
259
260 cache.triggerCacheRefreshForTesting();
261
262
263 for (Path filePath: files) {
264 assertFalse("Cache found '" + filePath + "', but it shouldn't have.",
265 contains(getNonSnapshotFiles(cache, filePath), filePath));
266 }
267 }
268 }
269
270 private static boolean contains(Iterable<FileStatus> files, Path filePath) {
271 for (FileStatus status: files) {
272 if (filePath.equals(status.getPath())) {
273 return true;
274 }
275 }
276 return false;
277 }
278
279 private static Iterable<FileStatus> getNonSnapshotFiles(SnapshotFileCache cache, Path storeFile)
280 throws IOException {
281 return cache.getUnreferencedFiles(
282 Arrays.asList(FSUtils.listStatus(fs, storeFile.getParent())), null
283 );
284 }
285 }