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, 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 fixHFileLinks, boolean fixEmptyMetaRegionInfo,
50 boolean fixTableLocks, 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.setFixHFileLinks(fixHFileLinks);
64 fsck.setFixEmptyMetaCells(fixEmptyMetaRegionInfo);
65 fsck.setFixTableLocks(fixTableLocks);
66 fsck.setFixReplication(fixReplication);
67 fsck.setFixTableZNodes(fixTableZnodes);
68 fsck.connect();
69 if (table != null) {
70 fsck.includeTable(table);
71 }
72 fsck.onlineHbck();
73 fsck.close();
74 return fsck;
75 }
76
77
78
79
80
81
82
83
84 public static HBaseFsck doHFileQuarantine(Configuration conf, TableName table) throws Exception {
85 String[] args = {"-sidelineCorruptHFiles", "-ignorePreCheckPermission", table.getNameAsString()};
86 HBaseFsck hbck = new HBaseFsck(conf, exec);
87 hbck.exec(exec, args);
88 return hbck;
89 }
90
91 public static HBaseFsck checkRegionBoundaries(Configuration conf) throws Exception {
92 HBaseFsck hbck = new HBaseFsck(conf, exec);
93 hbck.connect();
94 hbck.checkRegionBoundaries();
95 hbck.close();
96 return hbck;
97 }
98
99 public static boolean inconsistencyFound(HBaseFsck fsck) throws Exception {
100 List<ERROR_CODE> errs = fsck.getErrors().getErrorList();
101 return (errs != null && !errs.isEmpty());
102 }
103
104 public static void assertNoErrors(HBaseFsck fsck) throws Exception {
105 List<ERROR_CODE> errs = fsck.getErrors().getErrorList();
106 assertEquals(new ArrayList<ERROR_CODE>(), errs);
107 }
108
109 public static void assertErrors(HBaseFsck fsck, ERROR_CODE[] expectedErrors) {
110 List<ERROR_CODE> errs = fsck.getErrors().getErrorList();
111 Collections.sort(errs);
112 List<ERROR_CODE> expErrs = Lists.newArrayList(expectedErrors);
113 Collections.sort(expErrs);
114 assertEquals(expErrs, errs);
115 }
116 }