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