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  package org.apache.hadoop.hbase.util.hbck;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.util.HBaseFsck;
28  import org.apache.hadoop.io.MultipleIOException;
29  
30  /**
31   * This code is used to rebuild meta off line from file system data. If there
32   * are any problem detected, it will fail suggesting actions for the user to do
33   * to "fix" problems. If it succeeds, it will backup the previous .META. and
34   * -ROOT- dirs and write new tables in place.
35   * 
36   * This is an advanced feature, so is only exposed for use if explicitly
37   * mentioned.
38   * 
39   * hbase org.apache.hadoop.hbase.util.hbck.OfflineMetaRepair ...
40   */
41  public class OfflineMetaRepair {
42    private static final Log LOG = LogFactory.getLog(HBaseFsck.class.getName());
43    HBaseFsck fsck;
44  
45    protected static void printUsageAndExit() {
46      System.err.println("Usage: OfflineMetaRepair [opts] ");
47      System.err.println(" where [opts] are:");
48      System.err
49          .println("   -details          Display full report of all regions.");
50      System.err.println("   -base <hdfs://>   Base Hbase Data directory");
51      Runtime.getRuntime().exit(-2);
52    }
53  
54    /**
55     * Main program
56     * 
57     * @param args
58     * @throws Exception
59     */
60    public static void main(String[] args) throws Exception {
61  
62      // create a fsck object
63      Configuration conf = HBaseConfiguration.create();
64      conf.set("fs.defaultFS", conf.get(HConstants.HBASE_DIR));
65      HBaseFsck fsck = new HBaseFsck(conf);
66  
67      // Process command-line args.
68      for (int i = 0; i < args.length; i++) {
69        String cmd = args[i];
70        if (cmd.equals("-details")) {
71          fsck.displayFullReport();
72        } else if (cmd.equals("-base")) {
73          // update hbase root dir to user-specified base
74          i++;
75          String path = args[i];
76          conf.set(HConstants.HBASE_DIR, path);
77          conf.set("fs.defaultFS", conf.get(HConstants.HBASE_DIR));
78        } else {
79          String str = "Unknown command line option : " + cmd;
80          LOG.info(str);
81          System.out.println(str);
82          printUsageAndExit();
83        }
84      }
85  
86      // Fsck doesn't shutdown and and doesn't provide a way to shutdown its
87      // threads cleanly, so we do a System.exit.
88      boolean success = false;
89      try {
90        success = fsck.rebuildMeta();
91      } catch (MultipleIOException mioes) {
92        for (IOException ioe : mioes.getExceptions()) {
93          LOG.error("Bailed out due to:", ioe);
94        }
95      } catch (Exception e) {
96        LOG.error("Bailed out due to: ", e);
97      } finally {
98        System.exit(success ? 0 : 1);
99      }
100   }
101 }