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