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.io.IOException; 023import java.util.ArrayList; 024import java.util.Collections; 025import java.util.List; 026import java.util.concurrent.ExecutorService; 027import java.util.concurrent.ScheduledThreadPoolExecutor; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.util.HBaseFsck; 031import org.apache.hadoop.hbase.util.HbckErrorReporter.ERROR_CODE; 032 033import org.apache.hbase.thirdparty.com.google.common.collect.Lists; 034 035public class HbckTestingUtil { 036 private static ExecutorService exec = new ScheduledThreadPoolExecutor(10); 037 038 public static HBaseFsck doFsck(Configuration conf, boolean fix) throws Exception { 039 return doFsck(conf, fix, null); 040 } 041 042 public static HBaseFsck doFsck(Configuration conf, boolean fix, TableName table) 043 throws Exception { 044 return doFsck(conf, fix, fix, fix, fix, fix, fix, fix, fix, fix, fix, fix, fix, fix, table); 045 } 046 047 public static HBaseFsck doFsck(Configuration conf, boolean fixAssignments, boolean fixMeta, 048 boolean fixHdfsHoles, boolean fixHdfsOverlaps, boolean fixHdfsOrphans, boolean fixTableOrphans, 049 boolean fixVersionFile, boolean fixReferenceFiles, boolean fixHFileLinks, 050 boolean fixEmptyMetaRegionInfo, boolean fixTableLocks, boolean fixReplication, 051 boolean cleanReplicationBarrier, TableName table) throws Exception { 052 HBaseFsck fsck = new HBaseFsck(conf, exec); 053 try { 054 HBaseFsck.setDisplayFullReport(); // i.e. -details 055 fsck.setTimeLag(0); 056 fsck.setFixAssignments(fixAssignments); 057 fsck.setFixMeta(fixMeta); 058 fsck.setFixHdfsHoles(fixHdfsHoles); 059 fsck.setFixHdfsOverlaps(fixHdfsOverlaps); 060 fsck.setFixHdfsOrphans(fixHdfsOrphans); 061 fsck.setFixTableOrphans(fixTableOrphans); 062 fsck.setFixVersionFile(fixVersionFile); 063 fsck.setFixReferenceFiles(fixReferenceFiles); 064 fsck.setFixHFileLinks(fixHFileLinks); 065 fsck.setFixEmptyMetaCells(fixEmptyMetaRegionInfo); 066 fsck.setFixReplication(fixReplication); 067 fsck.setCleanReplicationBarrier(cleanReplicationBarrier); 068 if (table != null) { 069 fsck.includeTable(table); 070 } 071 072 // Parse command line flags before connecting, to grab the lock. 073 fsck.connect(); 074 fsck.onlineHbck(); 075 } finally { 076 fsck.close(); 077 } 078 return fsck; 079 } 080 081 /** 082 * Runs hbck with the -sidelineCorruptHFiles option 083 * @param table table constraint 084 */ 085 public static HBaseFsck doHFileQuarantine(Configuration conf, TableName table) throws Exception { 086 String[] args = 087 { "-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 cleanReplicationBarrier(Configuration conf, TableName table) 094 throws IOException, ClassNotFoundException { 095 HBaseFsck hbck = new HBaseFsck(conf, null); 096 hbck.setCleanReplicationBarrierTable(table.getNameAsString()); 097 hbck.setCleanReplicationBarrier(true); 098 hbck.connect(); 099 hbck.cleanReplicationBarrier(); 100 return hbck.shouldRerun(); 101 } 102 103 public static boolean inconsistencyFound(HBaseFsck fsck) throws Exception { 104 List<ERROR_CODE> errs = fsck.getErrors().getErrorList(); 105 return (errs != null && !errs.isEmpty()); 106 } 107 108 public static void assertNoErrors(HBaseFsck fsck) throws Exception { 109 List<ERROR_CODE> errs = fsck.getErrors().getErrorList(); 110 assertEquals(new ArrayList<ERROR_CODE>(), errs); 111 } 112 113 public static void assertErrors(HBaseFsck fsck, ERROR_CODE[] expectedErrors) { 114 List<ERROR_CODE> errs = fsck.getErrors().getErrorList(); 115 Collections.sort(errs); 116 List<ERROR_CODE> expErrs = Lists.newArrayList(expectedErrors); 117 Collections.sort(expErrs); 118 assertEquals(expErrs, errs); 119 } 120}