View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.util;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.classification.InterfaceAudience;
28  import org.apache.hadoop.classification.InterfaceStability;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.HTableDescriptor;
33  import org.apache.hadoop.hbase.ServerName;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
36  import org.apache.hadoop.hbase.catalog.MetaEditor;
37  import org.apache.hadoop.hbase.client.HBaseAdmin;
38  import org.apache.hadoop.hbase.client.HConnection;
39  import org.apache.hadoop.hbase.client.HTable;
40  import org.apache.hadoop.hbase.master.RegionState;
41  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
42  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.AdminService;
43  import org.apache.hadoop.hbase.regionserver.HRegion;
44  import org.apache.hadoop.hbase.regionserver.wal.HLog;
45  import org.apache.zookeeper.KeeperException;
46  
47  /**
48   * This class contains helper methods that repair parts of hbase's filesystem
49   * contents.
50   */
51  @InterfaceAudience.Public
52  @InterfaceStability.Evolving
53  public class HBaseFsckRepair {
54    public static final Log LOG = LogFactory.getLog(HBaseFsckRepair.class);
55  
56    /**
57     * Fix multiple assignment by doing silent closes on each RS hosting the region
58     * and then force ZK unassigned node to OFFLINE to trigger assignment by
59     * master.
60     *
61     * @param admin HBase admin used to undeploy
62     * @param region Region to undeploy
63     * @param servers list of Servers to undeploy from
64     */
65    public static void fixMultiAssignment(HBaseAdmin admin, HRegionInfo region,
66        List<ServerName> servers)
67    throws IOException, KeeperException, InterruptedException {
68      HRegionInfo actualRegion = new HRegionInfo(region);
69  
70      // Close region on the servers silently
71      for(ServerName server : servers) {
72        closeRegionSilentlyAndWait(admin, server, actualRegion);
73      }
74  
75      // Force ZK node to OFFLINE so master assigns
76      forceOfflineInZK(admin, actualRegion);
77    }
78  
79    /**
80     * Fix unassigned by creating/transition the unassigned ZK node for this
81     * region to OFFLINE state with a special flag to tell the master that this is
82     * a forced operation by HBCK.
83     *
84     * This assumes that info is in META.
85     *
86     * @param admin
87     * @param region
88     * @throws IOException
89     * @throws KeeperException
90     */
91    public static void fixUnassigned(HBaseAdmin admin, HRegionInfo region)
92        throws IOException, KeeperException {
93      HRegionInfo actualRegion = new HRegionInfo(region);
94  
95      // Force ZK node to OFFLINE so master assigns
96      forceOfflineInZK(admin, actualRegion);
97    }
98  
99    /**
100    * In 0.90, this forces an HRI offline by setting the RegionTransitionData
101    * in ZK to have HBCK_CODE_NAME as the server.  This is a special case in
102    * the AssignmentManager that attempts an assign call by the master.
103    *
104    * @see org.apache.hadoop.hbase.master.AssignementManager#handleHBCK
105    *
106    * This doesn't seem to work properly in the updated version of 0.92+'s hbck
107    * so we use assign to force the region into transition.  This has the
108    * side-effect of requiring a HRegionInfo that considers regionId (timestamp)
109    * in comparators that is addressed by HBASE-5563.
110    */
111   private static void forceOfflineInZK(HBaseAdmin admin, final HRegionInfo region)
112   throws ZooKeeperConnectionException, KeeperException, IOException {
113     admin.assign(region.getRegionName());
114   }
115 
116   /*
117    * Should we check all assignments or just not in RIT?
118    */
119   public static void waitUntilAssigned(HBaseAdmin admin,
120       HRegionInfo region) throws IOException, InterruptedException {
121     long timeout = admin.getConfiguration().getLong("hbase.hbck.assign.timeout", 120000);
122     long expiration = timeout + System.currentTimeMillis();
123     while (System.currentTimeMillis() < expiration) {
124       try {
125         Map<String, RegionState> rits=
126             admin.getClusterStatus().getRegionsInTransition();
127 
128         if (rits.keySet() != null && !rits.keySet().contains(region.getEncodedName())) {
129           // yay! no longer RIT
130           return;
131         }
132         // still in rit
133         LOG.info("Region still in transition, waiting for "
134             + "it to become assigned: " + region);
135       } catch (IOException e) {
136         LOG.warn("Exception when waiting for region to become assigned,"
137             + " retrying", e);
138       }
139       Thread.sleep(1000);
140     }
141     throw new IOException("Region " + region + " failed to move out of " +
142         "transition within timeout " + timeout + "ms");
143   }
144 
145   /**
146    * Contacts a region server and waits up to hbase.hbck.close.timeout ms
147    * (default 120s) to close the region.  This bypasses the active hmaster.
148    */
149   public static void closeRegionSilentlyAndWait(HBaseAdmin admin,
150       ServerName server, HRegionInfo region) throws IOException, InterruptedException {
151     HConnection connection = admin.getConnection();
152     AdminService.BlockingInterface rs = connection.getAdmin(server);
153     try {
154       ProtobufUtil.closeRegion(rs, region.getRegionName(), false);
155     } catch (IOException e) {
156       LOG.warn("Exception when closing region: " + region.getRegionNameAsString(), e);
157     }
158     long timeout = admin.getConfiguration()
159       .getLong("hbase.hbck.close.timeout", 120000);
160     long expiration = timeout + System.currentTimeMillis();
161     while (System.currentTimeMillis() < expiration) {
162       try {
163         HRegionInfo rsRegion =
164           ProtobufUtil.getRegionInfo(rs, region.getRegionName());
165         if (rsRegion == null) return;
166       } catch (IOException ioe) {
167         return;
168       }
169       Thread.sleep(1000);
170     }
171     throw new IOException("Region " + region + " failed to close within"
172         + " timeout " + timeout);
173   }
174 
175   /**
176    * Puts the specified HRegionInfo into META.
177    */
178   public static void fixMetaHoleOnline(Configuration conf,
179       HRegionInfo hri) throws IOException {
180     HTable meta = new HTable(conf, TableName.META_TABLE_NAME);
181     MetaEditor.addRegionToMeta(meta, hri);
182     meta.close();
183   }
184 
185   /**
186    * Creates, flushes, and closes a new region.
187    */
188   public static HRegion createHDFSRegionDir(Configuration conf,
189       HRegionInfo hri, HTableDescriptor htd) throws IOException {
190     // Create HRegion
191     Path root = FSUtils.getRootDir(conf);
192     HRegion region = HRegion.createHRegion(hri, root, conf, htd);
193     HLog hlog = region.getLog();
194 
195     // Close the new region to flush to disk. Close log file too.
196     region.close();
197     hlog.closeAndDelete();
198     return region;
199   }
200 }