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