1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.master.cleaner;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.fs.FileStatus;
34 import org.apache.hadoop.fs.FileSystem;
35 import org.apache.hadoop.fs.Path;
36 import org.apache.hadoop.hbase.HBaseTestingUtility;
37 import org.apache.hadoop.hbase.HConstants;
38 import org.apache.hadoop.hbase.MediumTests;
39 import org.apache.hadoop.hbase.client.HBaseAdmin;
40 import org.apache.hadoop.hbase.client.HTable;
41 import org.apache.hadoop.hbase.master.HMaster;
42 import org.apache.hadoop.hbase.master.snapshot.DisabledTableSnapshotHandler;
43 import org.apache.hadoop.hbase.master.snapshot.SnapshotHFileCleaner;
44 import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
45 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
46 import org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy;
47 import org.apache.hadoop.hbase.regionserver.HRegion;
48 import org.apache.hadoop.hbase.snapshot.HSnapshotDescription;
49 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
50 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
51 import org.apache.hadoop.hbase.snapshot.UnknownSnapshotException;
52 import org.apache.hadoop.hbase.util.Bytes;
53 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
54 import org.apache.hadoop.hbase.util.FSUtils;
55 import org.apache.hadoop.hbase.util.HFileArchiveUtil;
56 import org.junit.After;
57 import org.junit.AfterClass;
58 import org.junit.Before;
59 import org.junit.BeforeClass;
60 import org.junit.Test;
61 import org.junit.experimental.categories.Category;
62 import org.mockito.Mockito;
63
64 import com.google.common.collect.Lists;
65
66
67
68
69 @Category(MediumTests.class)
70 public class TestSnapshotFromMaster {
71
72 private static final Log LOG = LogFactory.getLog(TestSnapshotFromMaster.class);
73 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
74 private static final int NUM_RS = 2;
75 private static Path rootDir;
76 private static Path snapshots;
77 private static FileSystem fs;
78 private static HMaster master;
79
80
81 private static Path archiveDir;
82 private static final String STRING_TABLE_NAME = "test";
83 private static final byte[] TEST_FAM = Bytes.toBytes("fam");
84 private static final byte[] TABLE_NAME = Bytes.toBytes(STRING_TABLE_NAME);
85
86 private static final long cacheRefreshPeriod = 500;
87
88
89
90
91 @BeforeClass
92 public static void setupCluster() throws Exception {
93 setupConf(UTIL.getConfiguration());
94 UTIL.startMiniCluster(NUM_RS);
95 fs = UTIL.getDFSCluster().getFileSystem();
96 master = UTIL.getMiniHBaseCluster().getMaster();
97 rootDir = master.getMasterFileSystem().getRootDir();
98 snapshots = SnapshotDescriptionUtils.getSnapshotsDir(rootDir);
99 archiveDir = new Path(rootDir, HConstants.HFILE_ARCHIVE_DIRECTORY);
100 }
101
102 private static void setupConf(Configuration conf) {
103
104 conf.setInt("hbase.regionsever.info.port", -1);
105
106 conf.setInt("hbase.hregion.memstore.flush.size", 25000);
107
108
109 conf.setInt("hbase.hstore.compaction.min", 3);
110 conf.setInt("hbase.hstore.compactionThreshold", 5);
111
112 conf.setInt("hbase.hstore.blockingStoreFiles", 12);
113
114 conf.setInt("hbase.client.retries.number", 1);
115
116 conf.set(HFileCleaner.MASTER_HFILE_CLEANER_PLUGINS, "");
117 conf.set(HConstants.HBASE_MASTER_LOGCLEANER_PLUGINS, "");
118
119 conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
120 conf.setLong(SnapshotHFileCleaner.HFILE_CACHE_REFRESH_PERIOD_CONF_KEY, cacheRefreshPeriod);
121
122
123 conf.set(HConstants.HBASE_REGION_SPLIT_POLICY_KEY,
124 ConstantSizeRegionSplitPolicy.class.getName());
125 }
126
127 @Before
128 public void setup() throws Exception {
129 UTIL.createTable(TABLE_NAME, TEST_FAM);
130 master.getSnapshotManagerForTesting().setSnapshotHandlerForTesting(STRING_TABLE_NAME, null);
131 }
132
133 @After
134 public void tearDown() throws Exception {
135 UTIL.deleteTable(TABLE_NAME);
136 SnapshotTestingUtils.deleteAllSnapshots(UTIL.getHBaseAdmin());
137 SnapshotTestingUtils.deleteArchiveDirectory(UTIL);
138 }
139
140 @AfterClass
141 public static void cleanupTest() throws Exception {
142 try {
143 UTIL.shutdownMiniCluster();
144 } catch (Exception e) {
145
146 }
147 }
148
149
150
151
152
153
154
155
156
157
158 @Test(timeout = 60000)
159 public void testIsDoneContract() throws Exception {
160
161 String snapshotName = "asyncExpectedFailureTest";
162
163
164 SnapshotTestingUtils.expectSnapshotDoneException(master, new HSnapshotDescription(),
165 UnknownSnapshotException.class);
166
167
168 SnapshotDescription desc = SnapshotDescription.newBuilder()
169 .setName(snapshotName).setTable(STRING_TABLE_NAME).build();
170 SnapshotTestingUtils.expectSnapshotDoneException(master, new HSnapshotDescription(desc),
171 UnknownSnapshotException.class);
172
173
174 DisabledTableSnapshotHandler mockHandler = Mockito.mock(DisabledTableSnapshotHandler.class);
175 Mockito.when(mockHandler.getException()).thenReturn(null);
176 Mockito.when(mockHandler.getSnapshot()).thenReturn(desc);
177 Mockito.when(mockHandler.isFinished()).thenReturn(new Boolean(true));
178 Mockito.when(mockHandler.getCompletionTimestamp())
179 .thenReturn(EnvironmentEdgeManager.currentTimeMillis());
180
181 master.getSnapshotManagerForTesting()
182 .setSnapshotHandlerForTesting(STRING_TABLE_NAME, mockHandler);
183
184
185 SnapshotTestingUtils.expectSnapshotDoneException(master, new HSnapshotDescription(),
186 UnknownSnapshotException.class);
187
188
189 boolean isDone = master.isSnapshotDone(new HSnapshotDescription(desc));
190 assertTrue("Snapshot didn't complete when it should have.", isDone);
191
192
193 desc = SnapshotDescription.newBuilder().setName("Not A Snapshot").build();
194 SnapshotTestingUtils.expectSnapshotDoneException(master, new HSnapshotDescription(desc),
195 UnknownSnapshotException.class);
196
197
198 snapshotName = "completed";
199 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
200 desc = desc.toBuilder().setName(snapshotName).build();
201 SnapshotDescriptionUtils.writeSnapshotInfo(desc, snapshotDir, fs);
202
203 isDone = master.isSnapshotDone(new HSnapshotDescription(desc));
204 assertTrue("Completed, on-disk snapshot not found", isDone);
205 }
206
207 @Test
208 public void testGetCompletedSnapshots() throws Exception {
209
210 List<HSnapshotDescription> snapshots = master.getCompletedSnapshots();
211 assertEquals("Found unexpected number of snapshots", 0, snapshots.size());
212
213
214 String snapshotName = "completed";
215 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
216 SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName(snapshotName).build();
217 SnapshotDescriptionUtils.writeSnapshotInfo(snapshot, snapshotDir, fs);
218
219
220 snapshots = master.getCompletedSnapshots();
221 assertEquals("Found unexpected number of snapshots", 1, snapshots.size());
222 List<HSnapshotDescription> expected = Lists.newArrayList(new HSnapshotDescription(snapshot));
223 assertEquals("Returned snapshots don't match created snapshots", expected, snapshots);
224
225
226 snapshotName = "completed_two";
227 snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
228 snapshot = SnapshotDescription.newBuilder().setName(snapshotName).build();
229 SnapshotDescriptionUtils.writeSnapshotInfo(snapshot, snapshotDir, fs);
230 expected.add(new HSnapshotDescription(snapshot));
231
232
233 snapshots = master.getCompletedSnapshots();
234 assertEquals("Found unexpected number of snapshots", 2, snapshots.size());
235 assertEquals("Returned snapshots don't match created snapshots", expected, snapshots);
236 }
237
238 @Test
239 public void testDeleteSnapshot() throws Exception {
240
241 String snapshotName = "completed";
242 SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName(snapshotName).build();
243
244 try {
245 master.deleteSnapshot(new HSnapshotDescription(snapshot));
246 fail("Master didn't throw exception when attempting to delete snapshot that doesn't exist");
247 } catch (IOException e) {
248 LOG.debug("Correctly failed delete of non-existant snapshot:" + e.getMessage());
249 }
250
251
252 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
253 SnapshotDescriptionUtils.writeSnapshotInfo(snapshot, snapshotDir, fs);
254
255
256 master.deleteSnapshot(new HSnapshotDescription(snapshot));
257 }
258
259
260
261
262
263
264 @Test
265 public void testSnapshotHFileArchiving() throws Exception {
266 HBaseAdmin admin = UTIL.getHBaseAdmin();
267
268 SnapshotTestingUtils.assertNoSnapshots(admin);
269
270 UTIL.loadTable(new HTable(UTIL.getConfiguration(), TABLE_NAME), TEST_FAM);
271
272
273 admin.disableTable(TABLE_NAME);
274
275
276 String snapshotName = "snapshot";
277 byte[] snapshotNameBytes = Bytes.toBytes(snapshotName);
278 admin.snapshot(snapshotNameBytes, TABLE_NAME);
279
280 Configuration conf = master.getConfiguration();
281 LOG.info("After snapshot File-System state");
282 FSUtils.logFileSystemState(fs, rootDir, LOG);
283
284
285 SnapshotTestingUtils.assertOneSnapshotThatMatches(admin, snapshotNameBytes, TABLE_NAME);
286
287
288 admin.enableTable(TABLE_NAME);
289
290
291 List<HRegion> regions = UTIL.getHBaseCluster().getRegions(TABLE_NAME);
292 for (HRegion region : regions) {
293 region.waitForFlushesAndCompactions();
294 region.compactStores();
295 }
296 LOG.info("After compaction File-System state");
297 FSUtils.logFileSystemState(fs, rootDir, LOG);
298
299
300 LOG.debug("Running hfile cleaners");
301 ensureHFileCleanersRun();
302 LOG.info("After cleaners File-System state: " + rootDir);
303 FSUtils.logFileSystemState(fs, rootDir, LOG);
304
305
306 Path snapshotTable = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
307 Path[] snapshotHFiles = SnapshotTestingUtils.listHFiles(fs, snapshotTable);
308
309 LOG.debug("Have snapshot hfiles:");
310 for (Path file : snapshotHFiles) {
311 LOG.debug(file);
312 }
313
314 Collection<String> files = getArchivedHFiles(archiveDir, rootDir, fs, STRING_TABLE_NAME);
315
316
317 for (Path file : snapshotHFiles) {
318 assertTrue("Archived hfiles " + files + " is missing snapshot file:" + file,
319 files.contains(file.getName()));
320 }
321
322
323 admin.deleteSnapshot(snapshotNameBytes);
324 SnapshotTestingUtils.assertNoSnapshots(admin);
325
326
327
328 List<BaseHFileCleanerDelegate> delegates = UTIL.getMiniHBaseCluster().getMaster()
329 .getHFileCleaner().cleanersChain;
330 for (BaseHFileCleanerDelegate delegate: delegates) {
331 if (delegate instanceof SnapshotHFileCleaner) {
332 ((SnapshotHFileCleaner)delegate).getFileCacheForTesting().triggerCacheRefreshForTesting();
333 }
334 }
335
336 LOG.debug("Running hfile cleaners");
337 ensureHFileCleanersRun();
338 LOG.info("After delete snapshot cleaners run File-System state");
339 FSUtils.logFileSystemState(fs, rootDir, LOG);
340
341 files = getArchivedHFiles(archiveDir, rootDir, fs, STRING_TABLE_NAME);
342 assertEquals("Still have some hfiles in the archive, when their snapshot has been deleted.", 0,
343 files.size());
344 }
345
346
347
348
349
350 private final Collection<String> getArchivedHFiles(Path archiveDir, Path rootDir,
351 FileSystem fs, String tableName) throws IOException {
352 Path tableArchive = new Path(archiveDir, tableName);
353 Path[] archivedHFiles = SnapshotTestingUtils.listHFiles(fs, tableArchive);
354 List<String> files = new ArrayList<String>(archivedHFiles.length);
355 LOG.debug("Have archived hfiles: " + tableArchive);
356 for (Path file : archivedHFiles) {
357 LOG.debug(file);
358 files.add(file.getName());
359 }
360
361
362 Collections.sort(files);
363 return files;
364 }
365
366
367
368
369 private static void ensureHFileCleanersRun() {
370 UTIL.getHBaseCluster().getMaster().getHFileCleaner().chore();
371 }
372 }