1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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.Arrays;
24  import java.util.List;
25  import java.util.concurrent.ExecutorService;
26  import java.util.concurrent.ScheduledThreadPoolExecutor;
27  
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.util.HBaseFsck;
30  import org.apache.hadoop.hbase.util.HBaseFsck.ErrorReporter.ERROR_CODE;
31  
32  public class HbckTestingUtil {
33    private static ExecutorService exec = new ScheduledThreadPoolExecutor(10);
34    public static HBaseFsck doFsck(
35        Configuration conf, boolean fix) throws Exception {
36      return doFsck(conf, fix, null);
37    }
38  
39    public static HBaseFsck doFsck(
40        Configuration conf, boolean fix, String table) throws Exception {
41      return doFsck(conf, fix, fix, fix, fix,fix, fix, fix, fix, table);
42    }
43  
44    public static HBaseFsck doFsck(Configuration conf, boolean fixAssignments,
45        boolean fixMeta, boolean fixHdfsHoles, boolean fixHdfsOverlaps,
46        boolean fixHdfsOrphans, boolean fixTableOrphans, boolean fixVersionFile,
47        boolean fixReferenceFiles, String table) throws Exception {
48      HBaseFsck fsck = new HBaseFsck(conf, exec);
49      fsck.connect();
50      fsck.setDisplayFullReport(); // i.e. -details
51      fsck.setTimeLag(0);
52      fsck.setFixAssignments(fixAssignments);
53      fsck.setFixMeta(fixMeta);
54      fsck.setFixHdfsHoles(fixHdfsHoles);
55      fsck.setFixHdfsOverlaps(fixHdfsOverlaps);
56      fsck.setFixHdfsOrphans(fixHdfsOrphans);
57      fsck.setFixTableOrphans(fixTableOrphans);
58      fsck.setFixVersionFile(fixVersionFile);
59      fsck.setFixReferenceFiles(fixReferenceFiles);
60      if (table != null) {
61        fsck.includeTable(table);
62      }
63      fsck.onlineHbck();
64      return fsck;
65    }
66  
67    /**
68     * Runs hbck with the -sidelineCorruptHFiles option
69     * @param conf
70     * @param table table constraint
71     * @return <returncode, hbckInstance>
72     * @throws Exception
73     */
74    public static HBaseFsck doHFileQuarantine(Configuration conf, String table) throws Exception {
75      String[] args = {"-sidelineCorruptHFiles", "-ignorePreCheckPermission", table};
76      HBaseFsck hbck = new HBaseFsck(conf, exec);
77      hbck.exec(exec, args);
78      return hbck;
79    }
80  
81    public static void assertNoErrors(HBaseFsck fsck) throws Exception {
82      List<ERROR_CODE> errs = fsck.getErrors().getErrorList();
83      assertEquals(new ArrayList<ERROR_CODE>(), errs);
84    }
85  
86    public static void assertErrors(HBaseFsck fsck, ERROR_CODE[] expectedErrors) {
87      List<ERROR_CODE> errs = fsck.getErrors().getErrorList();
88      assertEquals(Arrays.asList(expectedErrors), errs);
89    }
90  }