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.*;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.fs.FileStatus;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.testclassification.MediumTests;
32 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
33 import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
34 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils.SnapshotMock;
35 import org.apache.hadoop.hbase.util.FSUtils;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Ignore;
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42
43
44
45
46 @Category(MediumTests.class)
47 public class TestSnapshotFileCache {
48
49 private static final Log LOG = LogFactory.getLog(TestSnapshotFileCache.class);
50 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
51
52 private static final long PERIOD = Long.MAX_VALUE;
53 private static FileSystem fs;
54 private static Path rootDir;
55 private static Path snapshotDir;
56
57 @BeforeClass
58 public static void startCluster() throws Exception {
59 UTIL.startMiniDFSCluster(1);
60 fs = UTIL.getDFSCluster().getFileSystem();
61 rootDir = UTIL.getDefaultRootDirPath();
62 snapshotDir = SnapshotDescriptionUtils.getSnapshotsDir(rootDir);
63 }
64
65 @AfterClass
66 public static void stopCluster() throws Exception {
67 UTIL.shutdownMiniDFSCluster();
68 }
69
70 @After
71 public void cleanupFiles() throws Exception {
72
73 fs.delete(snapshotDir, true);
74 }
75
76 @Test(timeout = 10000000)
77 @Ignore("See HBASE-19275")
78 public void testLoadAndDelete() throws IOException {
79 SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, PERIOD, 10000000,
80 "test-snapshot-file-cache-refresh", new SnapshotFiles());
81
82 createAndTestSnapshotV1(cache, "snapshot1a", false, true, false);
83
84 createAndTestSnapshotV2(cache, "snapshot2a", false, true, false);
85 }
86
87 @Test
88 @Ignore("See HBASE-19275")
89 public void testReloadModifiedDirectory() throws IOException {
90 SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, PERIOD, 10000000,
91 "test-snapshot-file-cache-refresh", new SnapshotFiles());
92
93 createAndTestSnapshotV1(cache, "snapshot1", false, true, false);
94
95 createAndTestSnapshotV1(cache, "snapshot1", false, false, false);
96
97 createAndTestSnapshotV2(cache, "snapshot2", false, true, false);
98
99 createAndTestSnapshotV2(cache, "snapshot2", false, false, false);
100 }
101
102 @Test
103 public void testSnapshotTempDirReload() throws IOException {
104 SnapshotFileCache cache =
105 new SnapshotFileCache(fs, rootDir, PERIOD, 10000000, "test-snapshot-file-cache-refresh", new SnapshotFiles());
106
107
108 createAndTestSnapshotV1(cache, "snapshot0v1", false, false, false);
109 createAndTestSnapshotV1(cache, "snapshot0v2", false, false, false);
110 }
111
112 @Test
113 public void testCacheUpdatedWhenLastModifiedOfSnapDirNotUpdated() throws IOException {
114 SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, PERIOD, 10000000,
115 "test-snapshot-file-cache-refresh", new SnapshotFiles());
116
117
118 createAndTestSnapshotV1(cache, "snapshot1v1", false, false, true);
119 createAndTestSnapshotV1(cache, "snapshot1v2", false, false, true);
120
121
122 createAndTestSnapshotV2(cache, "snapshot2v1", true, false, true);
123
124
125 createAndTestSnapshotV2(cache, "snapshot2v2", true, false, true);
126 }
127
128 class SnapshotFiles implements SnapshotFileCache.SnapshotFileInspector {
129 public Collection<String> filesUnderSnapshot(final Path snapshotDir) throws IOException {
130 Collection<String> files = new HashSet<String>();
131 files.addAll(SnapshotReferenceUtil.getHFileNames(UTIL.getConfiguration(), fs, snapshotDir));
132 return files;
133 }
134 };
135
136 private SnapshotMock.SnapshotBuilder createAndTestSnapshotV1(final SnapshotFileCache cache,
137 final String name, final boolean tmp, final boolean removeOnExit, boolean setFolderTime)
138 throws IOException {
139 SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
140 SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV1(name, name);
141 createAndTestSnapshot(cache, builder, tmp, removeOnExit, setFolderTime);
142 return builder;
143 }
144
145 private void createAndTestSnapshotV2(final SnapshotFileCache cache, final String name,
146 final boolean tmp, final boolean removeOnExit, boolean setFolderTime) throws IOException {
147 SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
148 SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(name, name);
149 createAndTestSnapshot(cache, builder, tmp, removeOnExit, setFolderTime);
150 }
151
152 private void createAndTestSnapshot(final SnapshotFileCache cache,
153 final SnapshotMock.SnapshotBuilder builder,
154 final boolean tmp, final boolean removeOnExit, boolean setFolderTime) throws IOException {
155 List<Path> files = new ArrayList<Path>();
156 for (int i = 0; i < 3; ++i) {
157 for (Path filePath: builder.addRegion()) {
158 files.add(filePath);
159 }
160 }
161
162
163 builder.commit();
164
165 if (setFolderTime) {
166 fs.setTimes(snapshotDir, 0, -1);
167 }
168
169
170 for (Path path: files) {
171 assertFalse("Cache didn't find " + path, contains(getNonSnapshotFiles(cache, path), path));
172 }
173
174 FSUtils.logFileSystemState(fs, rootDir, LOG);
175 if (removeOnExit) {
176 LOG.debug("Deleting snapshot.");
177 fs.delete(builder.getSnapshotsDir(), true);
178 FSUtils.logFileSystemState(fs, rootDir, LOG);
179
180
181 for (Path filePath: files) {
182 assertFalse("Cache didn't find " + filePath,
183 contains(getNonSnapshotFiles(cache, filePath), filePath));
184 }
185
186
187 cache.triggerCacheRefreshForTesting();
188
189
190 for (Path filePath: files) {
191 assertFalse("Cache found '" + filePath + "', but it shouldn't have.",
192 contains(getNonSnapshotFiles(cache, filePath), filePath));
193 }
194 }
195 }
196
197 private static boolean contains(Iterable<FileStatus> files, Path filePath) {
198 for (FileStatus status: files) {
199 if (filePath.equals(status.getPath())) {
200 return true;
201 }
202 }
203 return false;
204 }
205
206 private static Iterable<FileStatus> getNonSnapshotFiles(SnapshotFileCache cache, Path storeFile)
207 throws IOException {
208 return cache.getUnreferencedFiles(
209 Arrays.asList(FSUtils.listStatus(fs, storeFile.getParent())), null
210 );
211 }
212 }