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 */ 018 019package org.apache.hadoop.hbase.rsgroup; 020 021import java.util.Arrays; 022 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.HBaseConfiguration; 025import org.apache.hadoop.hbase.HBaseInterfaceAudience; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.client.Connection; 028import org.apache.hadoop.hbase.client.ConnectionFactory; 029import org.apache.hadoop.hbase.util.compaction.MajorCompactorTTL; 030import org.apache.hadoop.util.ToolRunner; 031import org.apache.yetus.audience.InterfaceAudience; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; 035import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine; 036import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLineParser; 037import org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser; 038import org.apache.hbase.thirdparty.org.apache.commons.cli.Option; 039import org.apache.hbase.thirdparty.org.apache.commons.cli.Options; 040import org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException; 041 042/** 043 * This script takes an rsgroup as argument and compacts part/all of regions of that table 044 * based on the table's TTL. 045 */ 046@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS) 047public class RSGroupMajorCompactionTTL extends MajorCompactorTTL { 048 049 private static final Logger LOG = LoggerFactory.getLogger(RSGroupMajorCompactionTTL.class); 050 051 @VisibleForTesting 052 RSGroupMajorCompactionTTL() { 053 super(); 054 } 055 056 public int compactTTLRegionsOnGroup(Configuration conf, String rsgroup, int concurrency, 057 long sleep, int numServers, int numRegions, boolean dryRun, boolean skipWait) 058 throws Exception { 059 060 Connection conn = ConnectionFactory.createConnection(conf); 061 RSGroupAdmin rsGroupAdmin = new RSGroupAdminClient(conn); 062 063 RSGroupInfo rsGroupInfo = rsGroupAdmin.getRSGroupInfo(rsgroup); 064 if (rsGroupInfo == null) { 065 LOG.error("Invalid rsgroup specified: " + rsgroup); 066 throw new IllegalArgumentException("Invalid rsgroup specified: " + rsgroup); 067 } 068 069 for (TableName tableName : rsGroupInfo.getTables()) { 070 int status = compactRegionsTTLOnTable(conf, tableName.getNameAsString(), concurrency, sleep, 071 numServers, numRegions, dryRun, skipWait); 072 if (status != 0) { 073 LOG.error("Failed to compact table: " + tableName); 074 return status; 075 } 076 } 077 return 0; 078 } 079 080 protected Options getOptions() { 081 Options options = getCommonOptions(); 082 083 options.addOption( 084 Option.builder("rsgroup") 085 .required() 086 .desc("Tables of rsgroup to be compacted") 087 .hasArg() 088 .build() 089 ); 090 091 return options; 092 } 093 094 @Override 095 public int run(String[] args) throws Exception { 096 Options options = getOptions(); 097 098 final CommandLineParser cmdLineParser = new DefaultParser(); 099 CommandLine commandLine; 100 try { 101 commandLine = cmdLineParser.parse(options, args); 102 } catch (ParseException parseException) { 103 System.out.println( 104 "ERROR: Unable to parse command-line arguments " + Arrays.toString(args) + " due to: " 105 + parseException); 106 printUsage(options); 107 return -1; 108 } 109 if (commandLine == null) { 110 System.out.println("ERROR: Failed parse, empty commandLine; " + Arrays.toString(args)); 111 printUsage(options); 112 return -1; 113 } 114 115 String rsgroup = commandLine.getOptionValue("rsgroup"); 116 int numServers = Integer.parseInt(commandLine.getOptionValue("numservers", "-1")); 117 int numRegions = Integer.parseInt(commandLine.getOptionValue("numregions", "-1")); 118 int concurrency = Integer.parseInt(commandLine.getOptionValue("servers", "1")); 119 long sleep = Long.parseLong(commandLine.getOptionValue("sleep", Long.toString(30000))); 120 boolean dryRun = commandLine.hasOption("dryRun"); 121 boolean skipWait = commandLine.hasOption("skipWait"); 122 Configuration conf = getConf(); 123 124 return compactTTLRegionsOnGroup(conf, rsgroup, concurrency, sleep, numServers, numRegions, 125 dryRun, skipWait); 126 } 127 128 public static void main(String[] args) throws Exception { 129 ToolRunner.run(HBaseConfiguration.create(), new RSGroupMajorCompactionTTL(), args); 130 } 131}