From 1bb871f963c7123623a5081b6aa1bdc9b380b5d0 Mon Sep 17 00:00:00 2001 From: Peter Somogyi Date: Fri, 25 May 2018 15:03:17 +0200 Subject: [PATCH] HBASE-20592 Create a tool to verify tables do not have prefix tree encoding --- .../tool/DataBlockEncodingValidator.java | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 hbase-server/src/main/java/org/apache/hadoop/hbase/tool/DataBlockEncodingValidator.java diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/tool/DataBlockEncodingValidator.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/tool/DataBlockEncodingValidator.java new file mode 100644 index 0000000000..edb2341060 --- /dev/null +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/tool/DataBlockEncodingValidator.java @@ -0,0 +1,103 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.tool; + +import org.apache.hadoop.conf.Configured; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.HBaseInterfaceAudience; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.client.ConnectionFactory; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.util.Pair; +import org.apache.hadoop.util.Tool; +import org.apache.hadoop.util.ToolRunner; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; + +/** + * Tool to validate that there are no column families with PREFIX_TREE Data Block Encoding set + * for any table. Before upgrading to HBase 2.0+ the cluster cannot have this encoding and + * it must be converted to a supported one. + * + * Usage: hbase org.apache.hadoop.hbase.tool.DataBlockEncodingValidator + */ +@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS) +public class DataBlockEncodingValidator extends Configured implements Tool { + + private static final Logger LOG = LoggerFactory.getLogger(DataBlockEncodingValidator.class); + private static final String NAME = DataBlockEncodingValidator.class.getName(); + + @Override + public int run(String[] args) throws Exception { + if (args.length >= 1) { + printUsage(); + return 1; + } + + List> incompatibleCols = new ArrayList<>(); + try (Connection connection = ConnectionFactory.createConnection(getConf()); + Admin admin = connection.getAdmin()) { + List tableDescriptors = admin.listTableDescriptors(); + for (TableDescriptor td : tableDescriptors) { + ColumnFamilyDescriptor[] columnFamilies = td.getColumnFamilies(); + for (ColumnFamilyDescriptor cf : columnFamilies) { + try { + cf.getDataBlockEncoding(); + } catch (IllegalArgumentException e) { + // Can't instantiate DataBlockEncoding.PREFIX_TREE + Pair p = new Pair<>(td.getTableName(), cf); + incompatibleCols.add(p); + } + } + } + } + if (!incompatibleCols.isEmpty()) { + LOG.warn("There are {} column families with Data Block Encodings that are not compatible " + + "with HBase 2.0+. Do not upgrade until these encodings are converted to a " + + "supported one.", incompatibleCols.size()); + printIncompatibleCols(incompatibleCols); + return 1; + } + return 0; + } + + private void printIncompatibleCols(List> pairs) { + for (Pair p : pairs) { + LOG.warn("Incompatible DataBlockEncoding: Table: {}, cf: {}", + p.getFirst().getNameAsString(), p.getSecond().getNameAsString()); + } + } + + private void printUsage() { + System.err.println("Usage: hbase " + NAME); + } + + public static void main(String[] args) throws Exception { + int errCode = ToolRunner.run(HBaseConfiguration.create(), + new DataBlockEncodingValidator(), args); + System.exit(errCode); + } + +} -- 2.17.0