001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util.hbck;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025import java.util.concurrent.ExecutorService;
026import java.util.concurrent.ScheduledThreadPoolExecutor;
027
028import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.util.HBaseFsck;
032import org.apache.hadoop.hbase.util.HbckErrorReporter.ERROR_CODE;
033
034public class HbckTestingUtil {
035  private static ExecutorService exec = new ScheduledThreadPoolExecutor(10);
036  public static HBaseFsck doFsck(
037      Configuration conf, boolean fix) throws Exception {
038    return doFsck(conf, fix, null);
039  }
040
041  public static HBaseFsck doFsck(
042      Configuration conf, boolean fix, TableName table) throws Exception {
043    return doFsck(conf, fix, fix, fix, fix, fix, fix, fix, fix, fix, fix, fix, fix, table);
044  }
045
046  public static HBaseFsck doFsck(Configuration conf, boolean fixAssignments, boolean fixMeta,
047      boolean fixHdfsHoles, boolean fixHdfsOverlaps, boolean fixHdfsOrphans,
048      boolean fixTableOrphans, boolean fixVersionFile, boolean fixReferenceFiles, boolean fixHFileLinks,
049      boolean fixEmptyMetaRegionInfo, boolean fixTableLocks, Boolean fixReplication,
050      TableName table) throws Exception {
051    HBaseFsck fsck = new HBaseFsck(conf, exec);
052    try {
053      HBaseFsck.setDisplayFullReport(); // i.e. -details
054      fsck.setTimeLag(0);
055      fsck.setFixAssignments(fixAssignments);
056      fsck.setFixMeta(fixMeta);
057      fsck.setFixHdfsHoles(fixHdfsHoles);
058      fsck.setFixHdfsOverlaps(fixHdfsOverlaps);
059      fsck.setFixHdfsOrphans(fixHdfsOrphans);
060      fsck.setFixTableOrphans(fixTableOrphans);
061      fsck.setFixVersionFile(fixVersionFile);
062      fsck.setFixReferenceFiles(fixReferenceFiles);
063      fsck.setFixHFileLinks(fixHFileLinks);
064      fsck.setFixEmptyMetaCells(fixEmptyMetaRegionInfo);
065      fsck.setFixReplication(fixReplication);
066      if (table != null) {
067        fsck.includeTable(table);
068      }
069
070      // Parse command line flags before connecting, to grab the lock.
071      fsck.connect();
072      fsck.onlineHbck();
073    } finally {
074      fsck.close();
075    }
076    return fsck;
077  }
078
079  /**
080   * Runs hbck with the -sidelineCorruptHFiles option
081   * @param conf
082   * @param table table constraint
083   * @return <returncode, hbckInstance>
084   * @throws Exception
085   */
086  public static HBaseFsck doHFileQuarantine(Configuration conf, TableName table) throws Exception {
087    String[] args = {"-sidelineCorruptHFiles", "-ignorePreCheckPermission", table.getNameAsString()};
088    HBaseFsck hbck = new HBaseFsck(conf, exec);
089    hbck.exec(exec, args);
090    return hbck;
091  }
092
093  public static boolean inconsistencyFound(HBaseFsck fsck) throws Exception {
094    List<ERROR_CODE> errs = fsck.getErrors().getErrorList();
095    return (errs != null && !errs.isEmpty());
096  }
097
098  public static void assertNoErrors(HBaseFsck fsck) throws Exception {
099    List<ERROR_CODE> errs = fsck.getErrors().getErrorList();
100    assertEquals(new ArrayList<ERROR_CODE>(), errs);
101  }
102
103  public static void assertErrors(HBaseFsck fsck, ERROR_CODE[] expectedErrors) {
104    List<ERROR_CODE> errs = fsck.getErrors().getErrorList();
105    Collections.sort(errs);
106    List<ERROR_CODE> expErrs = Lists.newArrayList(expectedErrors);
107    Collections.sort(expErrs);
108    assertEquals(expErrs, errs);
109  }
110}