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