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