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.ClusterConnection;
34  import org.apache.hadoop.hbase.client.Connection;
35  import org.apache.hadoop.hbase.client.ConnectionFactory;
36  import org.apache.hadoop.hbase.client.HConnection;
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.master.ServerManager;
41  import org.apache.hadoop.hbase.regionserver.HRegion;
42  import org.apache.zookeeper.KeeperException;
43  
44  import java.io.IOException;
45  import java.util.Collection;
46  import java.util.List;
47  import java.util.Map;
48  import java.util.Random;
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    private 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 connection HBase connection to the cluster
64     * @param region Region to undeploy
65     * @param servers list of Servers to undeploy from
66     */
67    public static void fixMultiAssignment(HConnection connection, 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(connection, server, actualRegion);
75      }
76  
77      // Force ZK node to OFFLINE so master assigns
78      forceOfflineInZK(connection.getAdmin(), 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, InterruptedException {
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, InterruptedException {
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 + EnvironmentEdgeManager.currentTime();
125     while (EnvironmentEdgeManager.currentTime() < 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(HConnection connection,
153       ServerName server, HRegionInfo region) throws IOException, InterruptedException {
154     long timeout = connection.getConfiguration()
155       .getLong("hbase.hbck.close.timeout", 120000);
156     ServerManager.closeRegionSilentlyAndWait((ClusterConnection)connection, server,
157          region, timeout);
158   }
159 
160   /**
161    * Puts the specified HRegionInfo into META with replica related columns
162    */
163   public static void fixMetaHoleOnlineAndAddReplicas(Configuration conf,
164       HRegionInfo hri, Collection<ServerName> servers, int numReplicas) throws IOException {
165     Connection conn = ConnectionFactory.createConnection(conf);
166     Table meta = conn.getTable(TableName.META_TABLE_NAME);
167     Put put = MetaTableAccessor.makePutFromRegionInfo(hri);
168     if (numReplicas > 1) {
169       Random r = new Random();
170       ServerName[] serversArr = servers.toArray(new ServerName[servers.size()]);
171       for (int i = 1; i < numReplicas; i++) {
172         ServerName sn = serversArr[r.nextInt(serversArr.length)];
173         // the column added here is just to make sure the master is able to
174         // see the additional replicas when it is asked to assign. The
175         // final value of these columns will be different and will be updated
176         // by the actual regionservers that start hosting the respective replicas
177         MetaTableAccessor.addLocation(put, sn, sn.getStartcode(), -1, i);
178       }
179     }
180     meta.put(put);
181     meta.close();
182     conn.close();
183   }
184 
185   /**
186    * Creates, flushes, and closes a new region.
187    */
188   public static HRegion createHDFSRegionDir(Configuration conf,
189       HRegionInfo hri, HTableDescriptor htd) throws IOException {
190     // Create HRegion
191     Path root = FSUtils.getRootDir(conf);
192     HRegion region = HRegion.createHRegion(hri, root, conf, htd, null);
193 
194     // Close the new region to flush to disk. Close log file too.
195     region.close();
196     return region;
197   }
198 }