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.classification.InterfaceAudience;
25  import org.apache.hadoop.classification.InterfaceStability;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.Path;
28  import org.apache.hadoop.hbase.HBaseConfiguration;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.util.FSUtils;
31  import org.apache.hadoop.hbase.util.HBaseFsck;
32  import org.apache.hadoop.io.MultipleIOException;
33  
34  /**
35   * This code is used to rebuild meta off line from file system data. If there
36   * are any problem detected, it will fail suggesting actions for the user to do
37   * to "fix" problems. If it succeeds, it will backup the previous hbase:meta and
38   * -ROOT- dirs and write new tables in place.
39   * 
40   * This is an advanced feature, so is only exposed for use if explicitly
41   * mentioned.
42   * 
43   * hbase org.apache.hadoop.hbase.util.hbck.OfflineMetaRepair ...
44   */
45  @InterfaceAudience.Public
46  @InterfaceStability.Evolving
47  public class OfflineMetaRepair {
48    private static final Log LOG = LogFactory.getLog(OfflineMetaRepair.class.getName());
49  
50    protected static void printUsageAndExit() {
51      StringBuilder sb = new StringBuilder();
52      sb.append("Usage: OfflineMetaRepair [opts]\n").
53         append(" where [opts] are:\n").
54         append("   -details               Display full report of all regions.\n").
55         append("   -base <hdfs://>        Base Hbase Data directory.\n").
56         append("   -sidelineDir <hdfs://> HDFS path to backup existing meta and root.\n").
57         append("   -fix                   Auto fix as many problems as possible.\n").
58         append("   -fixHoles              Auto fix as region holes.");
59      System.err.println(sb.toString());
60      Runtime.getRuntime().exit(-2);
61    }
62  
63    /**
64     * Main program
65     * 
66     * @param args
67     * @throws Exception
68     */
69    public static void main(String[] args) throws Exception {
70  
71      // create a fsck object
72      Configuration conf = HBaseConfiguration.create();
73      // Cover both bases, the old way of setting default fs and the new.
74      // We're supposed to run on 0.20 and 0.21 anyways.
75      FSUtils.setFsDefault(conf, FSUtils.getRootDir(conf));
76      HBaseFsck fsck = new HBaseFsck(conf);
77      boolean fixHoles = false;
78  
79      // Process command-line args.
80      for (int i = 0; i < args.length; i++) {
81        String cmd = args[i];
82        if (cmd.equals("-details")) {
83          fsck.setDisplayFullReport();
84        } else if (cmd.equals("-base")) {
85          if (i == args.length - 1) {
86            System.err.println("OfflineMetaRepair: -base needs an HDFS path.");
87            printUsageAndExit();
88          }
89          // update hbase root dir to user-specified base
90          i++;
91          FSUtils.setRootDir(conf, new Path(args[i]));
92          FSUtils.setFsDefault(conf, FSUtils.getRootDir(conf));
93        } else if (cmd.equals("-sidelineDir")) {
94          if (i == args.length - 1) {
95            System.err.println("OfflineMetaRepair: -sidelineDir needs an HDFS path.");
96            printUsageAndExit();
97          }
98          // set the hbck sideline dir to user-specified one
99          i++;
100         fsck.setSidelineDir(args[i]);
101       } else if (cmd.equals("-fixHoles")) {
102         fixHoles = true;
103       } else if (cmd.equals("-fix")) {
104         // make all fix options true
105         fixHoles = true;
106       } else {
107         String str = "Unknown command line option : " + cmd;
108         LOG.info(str);
109         System.out.println(str);
110         printUsageAndExit();
111       }
112     }
113 
114     // Fsck doesn't shutdown and and doesn't provide a way to shutdown its
115     // threads cleanly, so we do a System.exit.
116     boolean success = false;
117     try {
118       success = fsck.rebuildMeta(fixHoles);
119     } catch (MultipleIOException mioes) {
120       for (IOException ioe : mioes.getExceptions()) {
121         LOG.error("Bailed out due to:", ioe);
122       }
123     } catch (Exception e) {
124       LOG.error("Bailed out due to: ", e);
125     } finally {
126       System.exit(success ? 0 : 1);
127     }
128   }
129 }