1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.snapshot;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.Set;
30 import java.util.HashSet;
31 import java.util.TreeSet;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.hadoop.fs.FileStatus;
36 import org.apache.hadoop.fs.FileSystem;
37 import org.apache.hadoop.fs.Path;
38 import org.apache.hadoop.fs.PathFilter;
39 import org.apache.hadoop.hbase.HBaseTestingUtility;
40 import org.apache.hadoop.hbase.HColumnDescriptor;
41 import org.apache.hadoop.hbase.HConstants;
42 import org.apache.hadoop.hbase.HRegionInfo;
43 import org.apache.hadoop.hbase.HTableDescriptor;
44 import org.apache.hadoop.hbase.TableNotEnabledException;
45 import org.apache.hadoop.hbase.client.Durability;
46 import org.apache.hadoop.hbase.client.HBaseAdmin;
47 import org.apache.hadoop.hbase.client.HTable;
48 import org.apache.hadoop.hbase.client.Put;
49 import org.apache.hadoop.hbase.io.HFileLink;
50 import org.apache.hadoop.hbase.master.HMaster;
51 import org.apache.hadoop.hbase.master.MasterFileSystem;
52 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
53 import org.apache.hadoop.hbase.regionserver.HRegion;
54 import org.apache.hadoop.hbase.regionserver.HRegionServer;
55 import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
56 import org.apache.hadoop.hbase.util.Bytes;
57 import org.apache.hadoop.hbase.util.FSTableDescriptors;
58 import org.apache.hadoop.hbase.util.FSUtils;
59 import org.apache.hadoop.hbase.util.FSVisitor;
60 import org.apache.hadoop.hbase.util.MD5Hash;
61 import org.junit.Assert;
62
63
64
65
66 public class SnapshotTestingUtils {
67
68 private static final Log LOG = LogFactory.getLog(SnapshotTestingUtils.class);
69 private static byte[] KEYS = Bytes.toBytes("0123456789");
70
71
72
73
74
75 public static void assertNoSnapshots(HBaseAdmin admin) throws IOException {
76 assertEquals("Have some previous snapshots", 0, admin.listSnapshots().size());
77 }
78
79
80
81
82
83 public static List<SnapshotDescription> assertExistsMatchingSnapshot(
84 HBaseAdmin admin, String snapshotName, String tableName)
85 throws IOException {
86
87 List<SnapshotDescription> snapshots = admin.listSnapshots();
88
89 List<SnapshotDescription> returnedSnapshots = new ArrayList<SnapshotDescription>();
90 for (SnapshotDescription sd : snapshots) {
91 if (snapshotName.equals(sd.getName()) &&
92 tableName.equals(sd.getTable())) {
93 returnedSnapshots.add(sd);
94 }
95 }
96
97 Assert.assertTrue("No matching snapshots found.", returnedSnapshots.size()>0);
98 return returnedSnapshots;
99 }
100
101
102
103
104 public static void assertOneSnapshotThatMatches(HBaseAdmin admin,
105 HSnapshotDescription snapshot) throws IOException {
106 assertOneSnapshotThatMatches(admin, snapshot.getName(),
107 snapshot.getTable());
108 }
109
110
111
112
113
114 public static void assertOneSnapshotThatMatches(HBaseAdmin admin, SnapshotDescription snapshot)
115 throws IOException {
116 assertOneSnapshotThatMatches(admin, snapshot.getName(), snapshot.getTable());
117 }
118
119
120
121
122
123 public static List<SnapshotDescription> assertOneSnapshotThatMatches(
124 HBaseAdmin admin, String snapshotName, String tableName)
125 throws IOException {
126
127 List<SnapshotDescription> snapshots = admin.listSnapshots();
128
129 assertEquals("Should only have 1 snapshot", 1, snapshots.size());
130 assertEquals(snapshotName, snapshots.get(0).getName());
131 assertEquals(tableName, snapshots.get(0).getTable());
132
133 return snapshots;
134 }
135
136
137
138
139
140 public static List<SnapshotDescription> assertOneSnapshotThatMatches(
141 HBaseAdmin admin, byte[] snapshot, byte[] tableName) throws IOException {
142 return assertOneSnapshotThatMatches(admin, Bytes.toString(snapshot),
143 Bytes.toString(tableName));
144 }
145
146
147
148
149
150 public static void confirmSnapshotValid(
151 SnapshotDescription snapshotDescriptor, byte[] tableName,
152 byte[] testFamily, Path rootDir, HBaseAdmin admin, FileSystem fs,
153 boolean requireLogs, Path logsDir, Set<String> snapshotServers)
154 throws IOException {
155 ArrayList nonEmptyTestFamilies = new ArrayList(1);
156 nonEmptyTestFamilies.add(testFamily);
157 confirmSnapshotValid(snapshotDescriptor, Bytes.toString(tableName),
158 nonEmptyTestFamilies, null, rootDir, admin, fs, requireLogs,
159 logsDir, snapshotServers);
160 }
161
162
163
164
165 public static void confirmEmptySnapshotValid(
166 SnapshotDescription snapshotDescriptor, byte[] tableName,
167 byte[] testFamily, Path rootDir, HBaseAdmin admin, FileSystem fs,
168 boolean requireLogs, Path logsDir, Set<String> snapshotServers)
169 throws IOException {
170 ArrayList emptyTestFamilies = new ArrayList(1);
171 emptyTestFamilies.add(testFamily);
172 confirmSnapshotValid(snapshotDescriptor, Bytes.toString(tableName),
173 null, emptyTestFamilies, rootDir, admin, fs, requireLogs,
174 logsDir, snapshotServers);
175 }
176
177
178
179
180
181
182
183 public static void confirmSnapshotValid(
184 SnapshotDescription snapshotDescriptor, String tableName,
185 List<byte[]> nonEmptyTestFamilies, List<byte[]> emptyTestFamilies,
186 Path rootDir, HBaseAdmin admin, FileSystem fs, boolean requireLogs,
187 Path logsDir, Set<String> snapshotServers) throws IOException {
188
189 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(
190 snapshotDescriptor, rootDir);
191 assertTrue(fs.exists(snapshotDir));
192
193
194 Path snapshotinfo = new Path(snapshotDir, SnapshotDescriptionUtils.SNAPSHOTINFO_FILE);
195 assertTrue(fs.exists(snapshotinfo));
196
197
198 if (requireLogs) {
199 TakeSnapshotUtils.verifyAllLogsGotReferenced(fs, logsDir,
200 snapshotServers, snapshotDescriptor, new Path(snapshotDir,
201 HConstants.HREGION_LOGDIR_NAME));
202 }
203
204
205 HTableDescriptor desc = FSTableDescriptors.getTableDescriptorFromFs(fs, rootDir, tableName);
206 HTableDescriptor snapshotDesc = FSTableDescriptors.getTableDescriptorFromFs(fs, snapshotDir);
207 assertEquals(desc, snapshotDesc);
208
209
210 final Set<String> snapshotRegions = new HashSet<String>();
211 final Set<byte[]> snapshotFamilies = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
212 FSVisitor.visitTableStoreFiles(fs, snapshotDir, new FSVisitor.StoreFileVisitor() {
213 public void storeFile(final String region, final String family, final String hfileName)
214 throws IOException {
215 snapshotRegions.add(region);
216 snapshotFamilies.add(Bytes.toBytes(family));
217 }
218 });
219
220
221 if (nonEmptyTestFamilies != null) {
222 for (final byte[] familyName: nonEmptyTestFamilies) {
223 assertTrue(snapshotFamilies.contains(familyName));
224 }
225 }
226
227
228 if (emptyTestFamilies != null) {
229 for (final byte[] familyName: emptyTestFamilies) {
230 assertFalse(snapshotFamilies.contains(familyName));
231 }
232 }
233
234
235 if ((nonEmptyTestFamilies == null || nonEmptyTestFamilies.size() == 0) &&
236 (emptyTestFamilies != null && emptyTestFamilies.size() > 0)) {
237 assertEquals(0, snapshotRegions.size());
238 return;
239 }
240
241
242 List<HRegionInfo> regions = admin.getTableRegions(Bytes.toBytes(tableName));
243 assertEquals(regions.size(), snapshotRegions.size());
244
245
246 for (HRegionInfo info : regions) {
247 String regionName = info.getEncodedName();
248 assertTrue(snapshotRegions.contains(regionName));
249
250 Path regionDir = new Path(snapshotDir, regionName);
251 HRegionInfo snapshotRegionInfo = HRegion.loadDotRegionInfoFileContent(fs, regionDir);
252 assertEquals(info, snapshotRegionInfo);
253 }
254 }
255
256
257
258
259
260
261
262
263
264 public static void waitForSnapshotToComplete(HMaster master, HSnapshotDescription snapshot,
265 long sleep) throws IOException {
266 boolean done = false;
267 while (!done) {
268 done = master.isSnapshotDone(snapshot);
269 try {
270 Thread.sleep(sleep);
271 } catch (InterruptedException e) {
272 throw new IOException(e);
273 }
274 }
275 }
276
277 public static void cleanupSnapshot(HBaseAdmin admin, byte[] tableName)
278 throws IOException {
279 SnapshotTestingUtils.cleanupSnapshot(admin, Bytes.toString(tableName));
280 }
281
282 public static void cleanupSnapshot(HBaseAdmin admin, String snapshotName)
283 throws IOException {
284
285 admin.deleteSnapshot(snapshotName);
286 assertNoSnapshots(admin);
287 }
288
289
290
291
292
293
294
295 public static void expectSnapshotDoneException(HMaster master, HSnapshotDescription snapshot,
296 Class<? extends HBaseSnapshotException> clazz) {
297 try {
298 boolean res = master.isSnapshotDone(snapshot);
299 Assert.fail("didn't fail to lookup a snapshot: res=" + res);
300 } catch (HBaseSnapshotException e) {
301 assertEquals("Threw wrong snapshot exception!", clazz, e.getClass());
302 } catch (Throwable t) {
303 Assert.fail("Threw an unexpected exception:" + t);
304 }
305 }
306
307
308
309
310
311
312
313
314
315 public static Path[] listHFiles(final FileSystem fs, final Path tableDir)
316 throws IOException {
317 final ArrayList<Path> hfiles = new ArrayList<Path>();
318 FSVisitor.visitTableStoreFiles(fs, tableDir, new FSVisitor.StoreFileVisitor() {
319 public void storeFile(final String region, final String family, final String hfileName)
320 throws IOException {
321 hfiles.add(new Path(tableDir, new Path(region, new Path(family, hfileName))));
322 }
323 });
324 return hfiles.toArray(new Path[hfiles.size()]);
325 }
326
327
328
329
330
331
332 public static void createSnapshotAndValidate(HBaseAdmin admin,
333 String tableName, String familyName, String snapshotNameString,
334 Path rootDir, FileSystem fs, boolean onlineSnapshot)
335 throws Exception {
336 ArrayList<byte[]> nonEmptyFamilyNames = new ArrayList<byte[]>(1);
337 nonEmptyFamilyNames.add(Bytes.toBytes(familyName));
338 createSnapshotAndValidate(admin, tableName, nonEmptyFamilyNames,
339 snapshotNameString, rootDir, fs, onlineSnapshot);
340 }
341
342
343
344
345
346 public static void createSnapshotAndValidate(HBaseAdmin admin,
347 String tableName, List<byte[]> nonEmptyFamilyNames, List<byte[]> emptyFamilyNames,
348 String snapshotNameString, Path rootDir, FileSystem fs, boolean onlineSnapshot)
349 throws Exception {
350 if (!onlineSnapshot) {
351 try {
352 admin.disableTable(tableName);
353 } catch (TableNotEnabledException tne) {
354 LOG.info("In attempting to disable " + tableName + " it turns out that the this table is " +
355 "already disabled.");
356 }
357 }
358 admin.snapshot(snapshotNameString, tableName);
359
360 List<SnapshotDescription> snapshots = SnapshotTestingUtils.assertExistsMatchingSnapshot(admin,
361 snapshotNameString, tableName);
362 if (snapshots == null || snapshots.size() != 1) {
363 Assert.fail("Incorrect number of snapshots for table " + tableName);
364 }
365
366 SnapshotTestingUtils.confirmSnapshotValid(snapshots.get(0), tableName, nonEmptyFamilyNames,
367 emptyFamilyNames, rootDir, admin, fs, false,
368 new Path(rootDir, HConstants.HREGION_LOGDIR_NAME), null);
369 }
370
371
372
373
374
375
376
377
378
379 public static ArrayList corruptSnapshot(final HBaseTestingUtility util, final String snapshotName)
380 throws IOException {
381 final MasterFileSystem mfs = util.getHBaseCluster().getMaster().getMasterFileSystem();
382 final FileSystem fs = mfs.getFileSystem();
383
384 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName,
385 mfs.getRootDir());
386 SnapshotDescription snapshotDesc = SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotDir);
387 final String table = snapshotDesc.getTable();
388
389 final ArrayList corruptedFiles = new ArrayList();
390 SnapshotReferenceUtil.visitTableStoreFiles(fs, snapshotDir, new FSVisitor.StoreFileVisitor() {
391 public void storeFile (final String region, final String family, final String hfile)
392 throws IOException {
393 HFileLink link = HFileLink.create(util.getConfiguration(), table, region, family, hfile);
394 if (corruptedFiles.size() % 2 == 0) {
395 fs.delete(link.getAvailablePath(fs));
396 corruptedFiles.add(hfile);
397 }
398 }
399 });
400
401 assertTrue(corruptedFiles.size() > 0);
402 return corruptedFiles;
403 }
404
405
406
407
408 public static void waitForTableToBeOnline(final HBaseTestingUtility util, final byte[] tableName)
409 throws IOException, InterruptedException {
410 HRegionServer rs = util.getRSForFirstRegionInTable(tableName);
411 List<HRegion> onlineRegions = rs.getOnlineRegions(tableName);
412 for (HRegion region : onlineRegions) {
413 region.waitForFlushesAndCompactions();
414 }
415 util.getHBaseAdmin().isTableAvailable(tableName);
416 }
417
418 public static void createTable(final HBaseTestingUtility util, final byte[] tableName,
419 final byte[]... families) throws IOException, InterruptedException {
420 HTableDescriptor htd = new HTableDescriptor(tableName);
421 for (byte[] family: families) {
422 HColumnDescriptor hcd = new HColumnDescriptor(family);
423 htd.addFamily(hcd);
424 }
425 byte[][] splitKeys = new byte[KEYS.length-2][];
426 for (int i = 0; i < splitKeys.length; ++i) {
427 splitKeys[i] = new byte[] { KEYS[i+1] };
428 }
429 util.getHBaseAdmin().createTable(htd, splitKeys);
430 waitForTableToBeOnline(util, tableName);
431 assertEquals(KEYS.length-1, util.getHBaseAdmin().getTableRegions(tableName).size());
432 }
433
434 public static void loadData(final HBaseTestingUtility util, final byte[] tableName, int rows,
435 byte[]... families) throws IOException, InterruptedException {
436 loadData(util, new HTable(util.getConfiguration(), tableName), rows, families);
437 }
438
439 public static void loadData(final HBaseTestingUtility util, final HTable table, int rows,
440 byte[]... families) throws IOException, InterruptedException {
441 table.setAutoFlush(false);
442
443
444 assertTrue(rows >= KEYS.length);
445 for (byte k0: KEYS) {
446 byte[] k = new byte[] { k0 };
447 byte[] value = Bytes.add(Bytes.toBytes(System.currentTimeMillis()), k);
448 byte[] key = Bytes.add(k, Bytes.toBytes(MD5Hash.getMD5AsHex(value)));
449 putData(table, families, key, value);
450 rows--;
451 }
452
453
454 while (rows-- > 0) {
455 byte[] value = Bytes.add(Bytes.toBytes(System.currentTimeMillis()), Bytes.toBytes(rows));
456 byte[] key = Bytes.toBytes(MD5Hash.getMD5AsHex(value));
457 putData(table, families, key, value);
458 }
459 table.flushCommits();
460
461 waitForTableToBeOnline(util, table.getTableName());
462 }
463
464 private static void putData(final HTable table, final byte[][] families,
465 final byte[] key, final byte[] value) throws IOException {
466 byte[] q = Bytes.toBytes("q");
467 Put put = new Put(key);
468 put.setDurability(Durability.SKIP_WAL);
469 for (byte[] family: families) {
470 put.add(family, q, value);
471 }
472 table.put(put);
473 }
474
475 public static void deleteAllSnapshots(final HBaseAdmin admin)
476 throws IOException {
477
478 for (SnapshotDescription snapshot: admin.listSnapshots()) {
479 admin.deleteSnapshot(snapshot.getName());
480 }
481 SnapshotTestingUtils.assertNoSnapshots(admin);
482 }
483
484 public static void deleteArchiveDirectory(final HBaseTestingUtility util)
485 throws IOException {
486
487 MasterFileSystem mfs = util.getMiniHBaseCluster().getMaster().getMasterFileSystem();
488 Path archiveDir = new Path(mfs.getRootDir(), HConstants.HFILE_ARCHIVE_DIRECTORY);
489 mfs.getFileSystem().delete(archiveDir, true);
490 }
491
492 public static void verifyRowCount(final HBaseTestingUtility util, final byte[] tableName,
493 long expectedRows) throws IOException {
494 HTable table = new HTable(util.getConfiguration(), tableName);
495 try {
496 assertEquals(expectedRows, util.countRows(table));
497 } finally {
498 table.close();
499 }
500 }
501 }