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