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 import static org.junit.Assert.assertTrue;
22
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.fs.FileSystem;
32 import org.apache.hadoop.fs.Path;
33 import org.apache.hadoop.hbase.HBaseTestingUtility;
34 import org.apache.hadoop.hbase.testclassification.MediumTests;
35 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
36 import org.apache.hadoop.hbase.regionserver.wal.HLogUtil;
37 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
38 import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
39 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils.SnapshotMock;
40 import org.apache.hadoop.hbase.util.FSUtils;
41 import org.junit.After;
42 import org.junit.AfterClass;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46
47
48
49
50 @Category(MediumTests.class)
51 public class TestSnapshotFileCache {
52
53 private static final Log LOG = LogFactory.getLog(TestSnapshotFileCache.class);
54 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
55 private static long sequenceId = 0;
56 private static FileSystem fs;
57 private static Path rootDir;
58
59 @BeforeClass
60 public static void startCluster() throws Exception {
61 UTIL.startMiniDFSCluster(1);
62 fs = UTIL.getDFSCluster().getFileSystem();
63 rootDir = UTIL.getDefaultRootDirPath();
64 }
65
66 @AfterClass
67 public static void stopCluster() throws Exception {
68 UTIL.shutdownMiniDFSCluster();
69 }
70
71 @After
72 public void cleanupFiles() throws Exception {
73
74 Path snapshotDir = SnapshotDescriptionUtils.getSnapshotsDir(rootDir);
75 fs.delete(snapshotDir, true);
76 }
77
78 @Test(timeout = 10000000)
79 public void testLoadAndDelete() throws IOException {
80
81 long period = Long.MAX_VALUE;
82 SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
83 "test-snapshot-file-cache-refresh", new SnapshotFiles());
84
85 createAndTestSnapshotV1(cache, "snapshot1a", false, true);
86 createAndTestSnapshotV1(cache, "snapshot1b", true, true);
87
88 createAndTestSnapshotV2(cache, "snapshot2a", false, true);
89 createAndTestSnapshotV2(cache, "snapshot2b", true, true);
90 }
91
92 @Test
93 public void testJustFindLogsDirectory() throws Exception {
94
95 long period = Long.MAX_VALUE;
96 Path snapshotDir = SnapshotDescriptionUtils.getSnapshotsDir(rootDir);
97 SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
98 "test-snapshot-file-cache-refresh", new SnapshotFileCache.SnapshotFileInspector() {
99 public Collection<String> filesUnderSnapshot(final Path snapshotDir)
100 throws IOException {
101 return SnapshotReferenceUtil.getHLogNames(fs, snapshotDir);
102 }
103 });
104
105
106 SnapshotDescription desc = SnapshotDescription.newBuilder().setName("snapshot").build();
107 Path snapshot = SnapshotDescriptionUtils.getCompletedSnapshotDir(desc, rootDir);
108 SnapshotDescriptionUtils.writeSnapshotInfo(desc, snapshot, fs);
109 Path file1 = new Path(new Path(new Path(snapshot, "7e91021"), "fam"), "file1");
110 fs.createNewFile(file1);
111
112
113 Path logs = getSnapshotHLogsDir(snapshot, "server");
114 Path log = new Path(logs, "me.hbase.com%2C58939%2C1350424310315.1350424315552");
115 fs.createNewFile(log);
116
117 FSUtils.logFileSystemState(fs, rootDir, LOG);
118
119
120 assertFalse("Cache found '" + file1 + "', but it shouldn't have.",
121 cache.contains(file1.getName()));
122 assertTrue("Cache didn't find:" + log, cache.contains(log.getName()));
123 }
124
125
126
127
128
129
130
131 public static Path getSnapshotHLogsDir(Path snapshotDir, String serverName) {
132 return new Path(snapshotDir, HLogUtil.getHLogDirectoryName(serverName));
133 }
134
135 @Test
136 public void testReloadModifiedDirectory() throws IOException {
137
138 long period = Long.MAX_VALUE;
139 SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
140 "test-snapshot-file-cache-refresh", new SnapshotFiles());
141
142 createAndTestSnapshotV1(cache, "snapshot1", false, true);
143
144 createAndTestSnapshotV1(cache, "snapshot1", false, false);
145
146 createAndTestSnapshotV2(cache, "snapshot2", false, true);
147
148 createAndTestSnapshotV2(cache, "snapshot2", false, false);
149 }
150
151 @Test
152 public void testSnapshotTempDirReload() throws IOException {
153 long period = Long.MAX_VALUE;
154
155 SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
156 "test-snapshot-file-cache-refresh", new SnapshotFiles());
157
158
159 createAndTestSnapshotV1(cache, "snapshot0v1", false, false);
160 createAndTestSnapshotV1(cache, "snapshot0v2", false, false);
161
162
163 createAndTestSnapshotV2(cache, "snapshot1", true, false);
164
165
166 createAndTestSnapshotV2(cache, "snapshot2", true, false);
167 }
168
169 class SnapshotFiles implements SnapshotFileCache.SnapshotFileInspector {
170 public Collection<String> filesUnderSnapshot(final Path snapshotDir) throws IOException {
171 Collection<String> files = new HashSet<String>();
172 files.addAll(SnapshotReferenceUtil.getHLogNames(fs, snapshotDir));
173 files.addAll(SnapshotReferenceUtil.getHFileNames(UTIL.getConfiguration(), fs, snapshotDir));
174 return files;
175 }
176 };
177
178 private void createAndTestSnapshotV1(final SnapshotFileCache cache, final String name,
179 final boolean tmp, final boolean removeOnExit) throws IOException {
180 SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
181 SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV1(name);
182 createAndTestSnapshot(cache, builder, tmp, removeOnExit);
183 }
184
185 private void createAndTestSnapshotV2(final SnapshotFileCache cache, final String name,
186 final boolean tmp, final boolean removeOnExit) throws IOException {
187 SnapshotMock snapshotMock = new SnapshotMock(UTIL.getConfiguration(), fs, rootDir);
188 SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(name);
189 createAndTestSnapshot(cache, builder, tmp, removeOnExit);
190 }
191
192 private void createAndTestSnapshot(final SnapshotFileCache cache,
193 final SnapshotMock.SnapshotBuilder builder,
194 final boolean tmp, final boolean removeOnExit) throws IOException {
195 List<String> files = new ArrayList<String>();
196 for (int i = 0; i < 3; ++i) {
197 for (Path filePath: builder.addRegion()) {
198 String fileName = filePath.getName();
199 if (tmp) {
200
201 FSUtils.logFileSystemState(fs, rootDir, LOG);
202 assertTrue("Cache didn't find " + fileName, cache.contains(fileName));
203 }
204 files.add(fileName);
205 }
206 }
207
208
209 if (!tmp) {
210 builder.commit();
211 }
212
213
214 for (String fileName: files) {
215 assertTrue("Cache didn't find " + fileName, cache.contains(fileName));
216 }
217
218 FSUtils.logFileSystemState(fs, rootDir, LOG);
219 if (removeOnExit) {
220 LOG.debug("Deleting snapshot.");
221 fs.delete(builder.getSnapshotsDir(), true);
222 FSUtils.logFileSystemState(fs, rootDir, LOG);
223
224
225 for (String fileName: files) {
226 assertTrue("Cache didn't find " + fileName, cache.contains(fileName));
227 }
228
229
230 cache.triggerCacheRefreshForTesting();
231
232 for (String fileName: files) {
233 assertFalse("Cache found '" + fileName + "', but it shouldn't have.",
234 cache.contains(fileName));
235 }
236 }
237 }
238 }