z-york commented on a change in pull request #46: HBASE-23562 [operator tools] Add a RegionsMerge tool that allows for … URL: https://github.com/apache/hbase-operator-tools/pull/46#discussion_r356764475
########## File path: hbase-hbck2/src/main/java/org/apache/hbase/RegionsMerger.java ########## @@ -0,0 +1,160 @@ +/* + * 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 org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.conf.Configured; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.client.ConnectionFactory; +import org.apache.hadoop.hbase.client.RegionInfo; +import org.apache.hadoop.hbase.util.Pair; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Future; + +public class RegionsMerger extends Configured implements org.apache.hadoop.util.Tool { + + private static final Logger LOG = LoggerFactory.getLogger(RegionsMerger.class.getName()); + + private Configuration conf; + private FileSystem fs; + + public RegionsMerger(Configuration conf) throws IOException { + this.conf = conf; + fs = FileSystem.get(this.conf); + } + + private Path getTablePath(TableName table){ + Path basePath = new Path(conf.get(HConstants.HBASE_DIR)); + basePath = new Path(basePath, "data"); + Path tablePath = new Path(basePath, table.getNamespaceAsString()); + return new Path(tablePath, table.getNameAsString()); + } + + private long sumSizeInFS(Path parentPath) throws IOException { + long size = 0; + FileStatus[] files = this.fs.listStatus(parentPath); + for(FileStatus f : files) { + if(f.isFile()) { + size += f.getLen(); + } else if(f.isDirectory()) { + size += sumSizeInFS(f.getPath()); + } + } + return size; + } + + private boolean canMerge(Path path, RegionInfo region1, RegionInfo region2) throws IOException { + long size1 = sumSizeInFS(new Path(path, region1.getEncodedName())); + long size2 = sumSizeInFS(new Path(path, region2.getEncodedName())); + long threshold = this.conf.getLong(HConstants.HREGION_MAX_FILESIZE, + HConstants.DEFAULT_MAX_FILE_SIZE); + boolean mergeable = ((threshold*0.9) > (size1 + size2)); + if(!mergeable) { + LOG.info("Not merging regions {} and {} because resulting region size would get close to " + + "the {} limit. {} total size: {}; {} total size:{}", region1.getEncodedName(), + region2.getEncodedName(), threshold, + region1.getEncodedName(), size1, + region2.getEncodedName(), size2); + } + return mergeable; + } + + public void mergeRegions(String tblName, int numRegions) throws Exception { + TableName table = TableName.valueOf(tblName); + Path tableDir = getTablePath(table); + try(Connection conn = ConnectionFactory.createConnection(conf)) { + Admin admin = conn.getAdmin(); + int counter = 0; + List<RegionInfo> regions = admin.getRegions(table); + while (regions.size() > numRegions) { + LOG.info("Iteration: {}", counter); + RegionInfo previous = null; + Map<Future, Pair<RegionInfo, RegionInfo>> futures = new HashMap<>(); + LOG.info("About to merge {} regions...", regions.size()); Review comment: This log line doesn't seem accurate since not all regions will qualify. You could change 'About' to 'Attempting'. Also it might be nice to include the table name since it doesn't appear you log that anywhere else. ---------------------------------------------------------------- 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] With regards, Apache Git Services
