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.tool;
019
020import java.util.Arrays;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.HBaseConfiguration;
023import org.apache.hadoop.hbase.HBaseInterfaceAudience;
024import org.apache.hadoop.hbase.tool.coprocessor.CoprocessorValidator;
025import org.apache.hadoop.hbase.util.AbstractHBaseTool;
026import org.apache.hadoop.util.Tool;
027import org.apache.hadoop.util.ToolRunner;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Tool for validating that cluster can be upgraded from HBase 1.x to 2.0
034 * <p>
035 * Available validations:
036 * <ul>
037 * <li>validate-cp: Validates Co-processors compatibility</li>
038 * <li>validate-dbe: Check Data Block Encoding for column families</li>
039 * <li>validate-hfile: Check for corrupted HFiles</li>
040 * </ul>
041 * </p>
042 */
043@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
044public class PreUpgradeValidator implements Tool {
045  private static final Logger LOG = LoggerFactory.getLogger(PreUpgradeValidator.class);
046
047  public static final String TOOL_NAME = "pre-upgrade";
048  public static final String VALIDATE_CP_NAME = "validate-cp";
049  public static final String VALIDATE_DBE_NAME = "validate-dbe";
050  public static final String VALIDATE_HFILE = "validate-hfile";
051
052  private Configuration configuration;
053
054  @Override
055  public Configuration getConf() {
056    return configuration;
057  }
058
059  @Override
060  public void setConf(Configuration conf) {
061    this.configuration = conf;
062  }
063
064  private void printUsage() {
065    System.out.println("usage: hbase " + TOOL_NAME + " command ...");
066    System.out.println("Available commands:");
067    System.out.printf(" %-15s Validate co-processors are compatible with HBase%n",
068      VALIDATE_CP_NAME);
069    System.out.printf(" %-15s Validate DataBlockEncodings are compatible with HBase%n",
070      VALIDATE_DBE_NAME);
071    System.out.printf(" %-15s Validate HFile contents are readable%n", VALIDATE_HFILE);
072    System.out.println("For further information, please use command -h");
073  }
074
075  @Override
076  public int run(String[] args) throws Exception {
077    if (args.length == 0) {
078      printUsage();
079      return AbstractHBaseTool.EXIT_FAILURE;
080    }
081
082    Tool tool;
083
084    switch (args[0]) {
085      case VALIDATE_CP_NAME:
086        tool = new CoprocessorValidator();
087        break;
088      case VALIDATE_DBE_NAME:
089        tool = new DataBlockEncodingValidator();
090        break;
091      case VALIDATE_HFILE:
092        tool = new HFileContentValidator();
093        break;
094      case "-h":
095        printUsage();
096        return AbstractHBaseTool.EXIT_FAILURE;
097      default:
098        System.err.println("Unknown command: " + args[0]);
099        printUsage();
100        return AbstractHBaseTool.EXIT_FAILURE;
101    }
102
103    tool.setConf(getConf());
104    return tool.run(Arrays.copyOfRange(args, 1, args.length));
105  }
106
107  public static void main(String[] args) {
108    int ret;
109
110    Configuration conf = HBaseConfiguration.create();
111
112    try {
113      ret = ToolRunner.run(conf, new PreUpgradeValidator(), args);
114    } catch (Exception e) {
115      LOG.error("Error running command-line tool", e);
116      ret = AbstractHBaseTool.EXIT_FAILURE;
117    }
118
119    System.exit(ret);
120  }
121}