View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.rsgroup;
20  
21  import java.util.Arrays;
22  import org.apache.commons.cli.BasicParser;
23  import org.apache.commons.cli.CommandLine;
24  import org.apache.commons.cli.CommandLineParser;
25  import org.apache.commons.cli.Option;
26  import org.apache.commons.cli.Options;
27  import org.apache.commons.cli.ParseException;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.HBaseConfiguration;
30  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.classification.InterfaceAudience;
33  import org.apache.hadoop.hbase.client.Connection;
34  import org.apache.hadoop.hbase.client.ConnectionFactory;
35  import org.apache.hadoop.hbase.util.compaction.MajorCompactorTTL;
36  import org.apache.hadoop.util.ToolRunner;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * This script takes an rsgroup as argument and compacts part/all of regions of that table
42   * based on the table's TTL.
43   */
44  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
45  public class RSGroupMajorCompactionTTL extends MajorCompactorTTL {
46  
47    private static final Logger LOG = LoggerFactory.getLogger(RSGroupMajorCompactionTTL.class);
48  
49    @InterfaceAudience.Private
50    RSGroupMajorCompactionTTL() {
51      super();
52    }
53  
54    public int compactTTLRegionsOnGroup(Configuration conf, String rsgroup, int concurrency,
55        long sleep, int numServers, int numRegions, boolean dryRun, boolean skipWait)
56        throws Exception {
57  
58      Connection conn = ConnectionFactory.createConnection(conf);
59      RSGroupAdmin rsGroupAdmin = new RSGroupAdminClient(conn);
60  
61      RSGroupInfo rsGroupInfo = rsGroupAdmin.getRSGroupInfo(rsgroup);
62      if (rsGroupInfo == null) {
63        LOG.error("Invalid rsgroup specified: " + rsgroup);
64        throw new IllegalArgumentException("Invalid rsgroup specified: " + rsgroup);
65      }
66  
67      for (TableName tableName : rsGroupInfo.getTables()) {
68        int status = compactRegionsTTLOnTable(conf, tableName.getNameAsString(), concurrency, sleep,
69            numServers, numRegions, dryRun, skipWait);
70        if (status != 0) {
71          LOG.error("Failed to compact table: " + tableName);
72          return status;
73        }
74      }
75      return 0;
76    }
77  
78    protected Options getOptions() {
79      Options options = getCommonOptions();
80  
81      Option rsGroupOption = new Option("rsgroup", true, "Tables of rsgroup to be compacted");
82      rsGroupOption.setRequired(true);
83      options.addOption(rsGroupOption);
84  
85      return options;
86    }
87  
88    @Override
89    public int run(String[] args) throws Exception {
90      Options options = getOptions();
91  
92      final CommandLineParser cmdLineParser =  new BasicParser();
93      CommandLine commandLine;
94      try {
95        commandLine = cmdLineParser.parse(options, args);
96      } catch (ParseException parseException) {
97        System.err.println("ERROR: Unable to parse command-line arguments " + Arrays.toString(args)
98            + " due to: " + parseException);
99        printUsage(options);
100       throw parseException;
101     }
102 
103     String rsgroup = commandLine.getOptionValue("rsgroup");
104     int numServers = Integer.parseInt(commandLine.getOptionValue("numservers", "-1"));
105     int numRegions = Integer.parseInt(commandLine.getOptionValue("numregions", "-1"));
106     int concurrency = Integer.parseInt(commandLine.getOptionValue("servers", "1"));
107     long sleep = Long.parseLong(commandLine.getOptionValue("sleep", "30000"));
108     boolean dryRun = commandLine.hasOption("dryRun");
109     boolean skipWait = commandLine.hasOption("skipWait");
110     Configuration conf = HBaseConfiguration.create();
111 
112     return compactTTLRegionsOnGroup(conf, rsgroup, concurrency, sleep, numServers, numRegions,
113         dryRun, skipWait);
114   }
115 
116   public static void main(String[] args) throws Exception {
117     ToolRunner.run(HBaseConfiguration.create(), new RSGroupMajorCompactionTTL(), args);
118   }
119 }