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.conf.Configuration;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.HTableDescriptor;
32  import org.apache.hadoop.hbase.ServerName;
33  import org.apache.hadoop.hbase.TableName;
34  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
35  import org.apache.hadoop.hbase.MetaTableAccessor;
36  import org.apache.hadoop.hbase.client.HBaseAdmin;
37  import org.apache.hadoop.hbase.client.HConnection;
38  import org.apache.hadoop.hbase.client.HTable;
39  import org.apache.hadoop.hbase.master.RegionState;
40  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
41  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.AdminService;
42  import org.apache.hadoop.hbase.regionserver.HRegion;
43  import org.apache.zookeeper.KeeperException;
44  
45  /**
46   * This class contains helper methods that repair parts of hbase's filesystem
47   * contents.
48   */
49  @InterfaceAudience.Private
50  public class HBaseFsckRepair {
51    public static final Log LOG = LogFactory.getLog(HBaseFsckRepair.class);
52  
53    /**
54     * Fix multiple assignment by doing silent closes on each RS hosting the region
55     * and then force ZK unassigned node to OFFLINE to trigger assignment by
56     * master.
57     *
58     * @param admin HBase admin used to undeploy
59     * @param region Region to undeploy
60     * @param servers list of Servers to undeploy from
61     */
62    public static void fixMultiAssignment(HBaseAdmin admin, HRegionInfo region,
63        List<ServerName> servers)
64    throws IOException, KeeperException, InterruptedException {
65      HRegionInfo actualRegion = new HRegionInfo(region);
66  
67      // Close region on the servers silently
68      for(ServerName server : servers) {
69        closeRegionSilentlyAndWait(admin, server, actualRegion);
70      }
71  
72      // Force ZK node to OFFLINE so master assigns
73      forceOfflineInZK(admin, actualRegion);
74    }
75  
76    /**
77     * Fix unassigned by creating/transition the unassigned ZK node for this
78     * region to OFFLINE state with a special flag to tell the master that this is
79     * a forced operation by HBCK.
80     *
81     * This assumes that info is in META.
82     *
83     * @param admin
84     * @param region
85     * @throws IOException
86     * @throws KeeperException
87     */
88    public static void fixUnassigned(HBaseAdmin admin, HRegionInfo region)
89        throws IOException, KeeperException {
90      HRegionInfo actualRegion = new HRegionInfo(region);
91  
92      // Force ZK node to OFFLINE so master assigns
93      forceOfflineInZK(admin, actualRegion);
94    }
95  
96    /**
97     * In 0.90, this forces an HRI offline by setting the RegionTransitionData
98     * in ZK to have HBCK_CODE_NAME as the server.  This is a special case in
99     * the AssignmentManager that attempts an assign call by the master.
100    *
101    * @see org.apache.hadoop.hbase.master.AssignementManager#handleHBCK
102    *
103    * This doesn't seem to work properly in the updated version of 0.92+'s hbck
104    * so we use assign to force the region into transition.  This has the
105    * side-effect of requiring a HRegionInfo that considers regionId (timestamp)
106    * in comparators that is addressed by HBASE-5563.
107    */
108   private static void forceOfflineInZK(HBaseAdmin admin, final HRegionInfo region)
109   throws ZooKeeperConnectionException, KeeperException, IOException {
110     admin.assign(region.getRegionName());
111   }
112 
113   /*
114    * Should we check all assignments or just not in RIT?
115    */
116   public static void waitUntilAssigned(HBaseAdmin admin,
117       HRegionInfo region) throws IOException, InterruptedException {
118     long timeout = admin.getConfiguration().getLong("hbase.hbck.assign.timeout", 120000);
119     long expiration = timeout + System.currentTimeMillis();
120     while (System.currentTimeMillis() < expiration) {
121       try {
122         Map<String, RegionState> rits=
123             admin.getClusterStatus().getRegionsInTransition();
124 
125         if (rits.keySet() != null && !rits.keySet().contains(region.getEncodedName())) {
126           // yay! no longer RIT
127           return;
128         }
129         // still in rit
130         LOG.info("Region still in transition, waiting for "
131             + "it to become assigned: " + region);
132       } catch (IOException e) {
133         LOG.warn("Exception when waiting for region to become assigned,"
134             + " retrying", e);
135       }
136       Thread.sleep(1000);
137     }
138     throw new IOException("Region " + region + " failed to move out of " +
139         "transition within timeout " + timeout + "ms");
140   }
141 
142   /**
143    * Contacts a region server and waits up to hbase.hbck.close.timeout ms
144    * (default 120s) to close the region.  This bypasses the active hmaster.
145    */
146   public static void closeRegionSilentlyAndWait(HBaseAdmin admin,
147       ServerName server, HRegionInfo region) throws IOException, InterruptedException {
148     HConnection connection = admin.getConnection();
149     AdminService.BlockingInterface rs = connection.getAdmin(server);
150     try {
151       ProtobufUtil.closeRegion(rs, server, region.getRegionName(), false);
152     } catch (IOException e) {
153       LOG.warn("Exception when closing region: " + region.getRegionNameAsString(), e);
154     }
155     long timeout = admin.getConfiguration()
156       .getLong("hbase.hbck.close.timeout", 120000);
157     long expiration = timeout + System.currentTimeMillis();
158     while (System.currentTimeMillis() < expiration) {
159       try {
160         HRegionInfo rsRegion =
161           ProtobufUtil.getRegionInfo(rs, region.getRegionName());
162         if (rsRegion == null) return;
163       } catch (IOException ioe) {
164         return;
165       }
166       Thread.sleep(1000);
167     }
168     throw new IOException("Region " + region + " failed to close within"
169         + " timeout " + timeout);
170   }
171 
172   /**
173    * Puts the specified HRegionInfo into META.
174    */
175   public static void fixMetaHoleOnline(Configuration conf,
176       HRegionInfo hri) throws IOException {
177     HTable meta = new HTable(conf, TableName.META_TABLE_NAME);
178     MetaTableAccessor.addRegionToMeta(meta, hri);
179     meta.close();
180   }
181 
182   /**
183    * Creates, flushes, and closes a new region.
184    */
185   public static HRegion createHDFSRegionDir(Configuration conf,
186       HRegionInfo hri, HTableDescriptor htd) throws IOException {
187     // Create HRegion
188     Path root = FSUtils.getRootDir(conf);
189     HRegion region = HRegion.createHRegion(hri, root, conf, htd, null);
190 
191     // Close the new region to flush to disk. Close log file too.
192     region.close();
193     return region;
194   }
195 }