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