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