001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.tool; 020 021import java.util.Arrays; 022 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.HBaseConfiguration; 025import org.apache.hadoop.hbase.HBaseInterfaceAudience; 026import org.apache.hadoop.hbase.tool.coprocessor.CoprocessorValidator; 027import org.apache.hadoop.hbase.util.AbstractHBaseTool; 028import org.apache.hadoop.util.Tool; 029import org.apache.hadoop.util.ToolRunner; 030import org.apache.yetus.audience.InterfaceAudience; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034/** 035 * Tool for validating that cluster can be upgraded from HBase 1.x to 2.0 036 * <p> 037 * Available validations: 038 * <ul> 039 * <li>validate-cp: Validates Co-processors compatibility</li> 040 * <li>validate-dbe: Check Data Block Encoding for column families</li> 041 * <li>validate-hfile: Check for corrupted HFiles</li> 042 * </ul> 043 * </p> 044 */ 045@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS) 046public class PreUpgradeValidator implements Tool { 047 private static final Logger LOG = LoggerFactory 048 .getLogger(PreUpgradeValidator.class); 049 050 public static final String TOOL_NAME = "pre-upgrade"; 051 public static final String VALIDATE_CP_NAME = "validate-cp"; 052 public static final String VALIDATE_DBE_NAME = "validate-dbe"; 053 public static final String VALIDATE_HFILE = "validate-hfile"; 054 055 private Configuration configuration; 056 057 @Override 058 public Configuration getConf() { 059 return configuration; 060 } 061 062 @Override 063 public void setConf(Configuration conf) { 064 this.configuration = conf; 065 } 066 067 private void printUsage() { 068 System.out.println("usage: hbase " + TOOL_NAME + " command ..."); 069 System.out.println("Available commands:"); 070 System.out.printf(" %-15s Validate co-processors are compatible with HBase%n", 071 VALIDATE_CP_NAME); 072 System.out.printf(" %-15s Validate DataBlockEncodings are compatible with HBase%n", 073 VALIDATE_DBE_NAME); 074 System.out.printf(" %-15s Validate HFile contents are readable%n", 075 VALIDATE_HFILE); 076 System.out.println("For further information, please use command -h"); 077 } 078 079 @Override 080 public int run(String[] args) throws Exception { 081 if (args.length == 0) { 082 printUsage(); 083 return AbstractHBaseTool.EXIT_FAILURE; 084 } 085 086 Tool tool; 087 088 switch (args[0]) { 089 case VALIDATE_CP_NAME: 090 tool = new CoprocessorValidator(); 091 break; 092 case VALIDATE_DBE_NAME: 093 tool = new DataBlockEncodingValidator(); 094 break; 095 case VALIDATE_HFILE: 096 tool = new HFileContentValidator(); 097 break; 098 case "-h": 099 printUsage(); 100 return AbstractHBaseTool.EXIT_FAILURE; 101 default: 102 System.err.println("Unknown command: " + args[0]); 103 printUsage(); 104 return AbstractHBaseTool.EXIT_FAILURE; 105 } 106 107 tool.setConf(getConf()); 108 return tool.run(Arrays.copyOfRange(args, 1, args.length)); 109 } 110 111 public static void main(String[] args) { 112 int ret; 113 114 Configuration conf = HBaseConfiguration.create(); 115 116 try { 117 ret = ToolRunner.run(conf, new PreUpgradeValidator(), args); 118 } catch (Exception e) { 119 LOG.error("Error running command-line tool", e); 120 ret = AbstractHBaseTool.EXIT_FAILURE; 121 } 122 123 System.exit(ret); 124 } 125}