wchevreuil commented on a change in pull request #79: URL: https://github.com/apache/hbase-operator-tools/pull/79#discussion_r544425181
########## File path: hbase-hbck2/src/main/java/org/apache/hbase/MissingTableDescriptorGenerator.java ########## @@ -0,0 +1,202 @@ +/* + * 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.hbase; + +import java.io.IOException; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.TableNotFoundException; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +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.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.util.FSTableDescriptors; +import org.apache.hadoop.hbase.util.FSUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class can be used to generate missing table descriptor file based on the in-memory cache + * of the active master or based on the file system. + */ +public class MissingTableDescriptorGenerator { + + private static final Logger LOG = LoggerFactory.getLogger(MissingTableDescriptorGenerator.class); + + private final Configuration configuration; + private FileSystem fs; + private Path rootDir; + + public MissingTableDescriptorGenerator(Configuration configuration) throws IOException { + this.configuration = configuration; + this.rootDir = HBCKFsUtils.getRootDir(this.configuration); + this.fs = rootDir.getFileSystem(this.configuration); + } + + /** + * Trying to generate missing table descriptor. If anything goes wrong, then the method throws + * IllegalStateException without changing anything. The method follows these steps: + * + * - if the table folder is missing, then we return + * - if the .tableinfo file is not missing, then we return (we don't overwrite it) + * - if TableDescriptor is cached in master then recover the .tableinfo accordingly + * - if TableDescriptor is not cached in master, then we create a default .tableinfo file + * with the following items: + * - the table name + * - the column family list (determined based on the file system) + * - the default properties for both {@link TableDescriptor} and + * {@link ColumnFamilyDescriptor} + * + * This method does not change anything in HBase, only writes the new .tableinfo file + * to the file system. + * + * @param tableNameAsString the table name in standard 'table' or 'ns:table' format + */ + public void generateTableDescriptorFileIfMissing(String tableNameAsString) { + TableName tableName = TableName.valueOf(tableNameAsString); + assertTableFolderIsPresent(tableName); + if (checkIfTableInfoPresent(tableName)) { + LOG.info("Table descriptor already exists, exiting without changing anything."); + return; + } + + FSTableDescriptors fstd; + try { + fstd = new FSTableDescriptors(configuration); + } catch (IOException e) { + LOG.error("Unable to initialize FSTableDescriptors, exiting without changing anything.", e); + return; + } + + Optional<TableDescriptor> tableDescriptorFromMaster = getTableDescriptorFromMaster(tableName); + try { + if (tableDescriptorFromMaster.isPresent()) { + LOG.info("Table descriptor found in the cache of HBase Master, " + + "writing it to the file system."); + fstd.createTableDescriptor(tableDescriptorFromMaster.get(), false); + LOG.info("Table descriptor written successfully. Orphan table {} fixed.", tableName); + } else { + generateDefaultTableInfo(fstd, tableName); Review comment: > During my manual tests I saw the table to reappear (shown by the list command) quickly after the missing tableinfo file got generated. Interesting. Have you tried disable/enable the table after re-creating the table info? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
