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