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.io.IOException;
021import java.util.List;
022import org.apache.hadoop.hbase.HBaseInterfaceAudience;
023import org.apache.hadoop.hbase.client.Admin;
024import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
025import org.apache.hadoop.hbase.client.Connection;
026import org.apache.hadoop.hbase.client.ConnectionFactory;
027import org.apache.hadoop.hbase.client.TableDescriptor;
028import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
029import org.apache.hadoop.hbase.util.AbstractHBaseTool;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.apache.yetus.audience.InterfaceAudience;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine;
036
037@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
038public class DataBlockEncodingValidator extends AbstractHBaseTool {
039
040  private static final Logger LOG = LoggerFactory.getLogger(DataBlockEncodingValidator.class);
041  private static final byte[] DATA_BLOCK_ENCODING = Bytes.toBytes("DATA_BLOCK_ENCODING");
042
043  /**
044   * Check DataBlockEncodings of column families are compatible.
045   * @return number of column families with incompatible DataBlockEncoding
046   * @throws IOException if a remote or network exception occurs
047   */
048  private int validateDBE() throws IOException {
049    int incompatibilities = 0;
050
051    LOG.info("Validating Data Block Encodings");
052
053    try (Connection connection = ConnectionFactory.createConnection(getConf());
054      Admin admin = connection.getAdmin()) {
055      List<TableDescriptor> tableDescriptors = admin.listTableDescriptors();
056      String encoding = "";
057
058      for (TableDescriptor td : tableDescriptors) {
059        ColumnFamilyDescriptor[] columnFamilies = td.getColumnFamilies();
060        for (ColumnFamilyDescriptor cfd : columnFamilies) {
061          try {
062            encoding = Bytes.toString(cfd.getValue(DATA_BLOCK_ENCODING));
063            // IllegalArgumentException will be thrown if encoding is incompatible with 2.0
064            DataBlockEncoding.valueOf(encoding);
065          } catch (IllegalArgumentException e) {
066            incompatibilities++;
067            LOG.warn("Incompatible DataBlockEncoding for table: {}, cf: {}, encoding: {}",
068              td.getTableName().getNameAsString(), cfd.getNameAsString(), encoding);
069          }
070        }
071      }
072    }
073
074    if (incompatibilities > 0) {
075      LOG.warn("There are {} column families with incompatible Data Block Encodings. Do not "
076        + "upgrade until these encodings are converted to a supported one. "
077        + "Check https://s.apache.org/prefixtree for instructions.", incompatibilities);
078    } else {
079      LOG.info("The used Data Block Encodings are compatible with HBase 2.0.");
080    }
081
082    return incompatibilities;
083  }
084
085  @Override
086  protected void printUsage() {
087    String header =
088      "hbase " + PreUpgradeValidator.TOOL_NAME + " " + PreUpgradeValidator.VALIDATE_DBE_NAME;
089    printUsage(header, null, "");
090  }
091
092  @Override
093  protected void addOptions() {
094  }
095
096  @Override
097  protected void processOptions(CommandLine cmd) {
098  }
099
100  @Override
101  protected int doWork() throws Exception {
102    return (validateDBE() == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
103  }
104}