1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util.hbck;
19
20 import static org.junit.Assert.assertEquals;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.ScheduledThreadPoolExecutor;
27
28 import com.google.common.collect.Lists;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.TableName;
31 import org.apache.hadoop.hbase.util.HBaseFsck;
32 import org.apache.hadoop.hbase.util.HBaseFsck.ErrorReporter.ERROR_CODE;
33
34 public class HbckTestingUtil {
35 private static ExecutorService exec = new ScheduledThreadPoolExecutor(10);
36 public static HBaseFsck doFsck(
37 Configuration conf, boolean fix) throws Exception {
38 return doFsck(conf, fix, null);
39 }
40
41 public static HBaseFsck doFsck(
42 Configuration conf, boolean fix, TableName table) throws Exception {
43 return doFsck(conf, fix, fix, fix, fix, fix, fix, fix, fix, fix, fix, fix, fix, table);
44 }
45
46 public static HBaseFsck doFsck(Configuration conf, boolean fixAssignments,
47 boolean fixMeta, boolean fixHdfsHoles, boolean fixHdfsOverlaps,
48 boolean fixHdfsOrphans, boolean fixTableOrphans, boolean fixVersionFile,
49 boolean fixReferenceFiles, boolean fixEmptyMetaRegionInfo, boolean fixTableLocks,
50 boolean fixTableZnodes, Boolean fixReplication,
51 TableName table) throws Exception {
52 HBaseFsck fsck = new HBaseFsck(conf, exec);
53 fsck.setDisplayFullReport();
54 fsck.setTimeLag(0);
55 fsck.setFixAssignments(fixAssignments);
56 fsck.setFixMeta(fixMeta);
57 fsck.setFixHdfsHoles(fixHdfsHoles);
58 fsck.setFixHdfsOverlaps(fixHdfsOverlaps);
59 fsck.setFixHdfsOrphans(fixHdfsOrphans);
60 fsck.setFixTableOrphans(fixTableOrphans);
61 fsck.setFixVersionFile(fixVersionFile);
62 fsck.setFixReferenceFiles(fixReferenceFiles);
63 fsck.setFixEmptyMetaCells(fixEmptyMetaRegionInfo);
64 fsck.setFixTableLocks(fixTableLocks);
65 fsck.setFixReplication(fixReplication);
66 fsck.setFixTableZNodes(fixTableZnodes);
67 fsck.connect();
68 if (table != null) {
69 fsck.includeTable(table);
70 }
71 fsck.onlineHbck();
72 fsck.close();
73 return fsck;
74 }
75
76
77
78
79
80
81
82
83 public static HBaseFsck doHFileQuarantine(Configuration conf, TableName table) throws Exception {
84 String[] args = {"-sidelineCorruptHFiles", "-ignorePreCheckPermission", table.getNameAsString()};
85 HBaseFsck hbck = new HBaseFsck(conf, exec);
86 hbck.exec(exec, args);
87 return hbck;
88 }
89
90 public static HBaseFsck checkRegionBoundaries(Configuration conf) throws Exception {
91 HBaseFsck hbck = new HBaseFsck(conf, exec);
92 hbck.connect();
93 hbck.checkRegionBoundaries();
94 hbck.close();
95 return hbck;
96 }
97
98 public static boolean inconsistencyFound(HBaseFsck fsck) throws Exception {
99 List<ERROR_CODE> errs = fsck.getErrors().getErrorList();
100 return (errs != null && !errs.isEmpty());
101 }
102
103 public static void assertNoErrors(HBaseFsck fsck) throws Exception {
104 List<ERROR_CODE> errs = fsck.getErrors().getErrorList();
105 assertEquals(new ArrayList<ERROR_CODE>(), errs);
106 }
107
108 public static void assertErrors(HBaseFsck fsck, ERROR_CODE[] expectedErrors) {
109 List<ERROR_CODE> errs = fsck.getErrors().getErrorList();
110 Collections.sort(errs);
111 List<ERROR_CODE> expErrs = Lists.newArrayList(expectedErrors);
112 Collections.sort(expErrs);
113 assertEquals(expErrs, errs);
114 }
115 }