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