1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
58
59
60 @InterfaceAudience.Private
61 public class HBaseFsckRepair {
62 private static final Log LOG = LogFactory.getLog(HBaseFsckRepair.class);
63
64
65
66
67
68
69
70
71
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
79 for(ServerName server : servers) {
80 closeRegionSilentlyAndWait(connection, server, actualRegion);
81 }
82
83
84 forceOfflineInZK(connection.getAdmin(), actualRegion);
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public static void fixUnassigned(Admin admin, HRegionInfo region)
100 throws IOException, KeeperException, InterruptedException {
101 HRegionInfo actualRegion = new HRegionInfo(region);
102
103
104 forceOfflineInZK(admin, actualRegion);
105 }
106
107
108
109
110
111
112
113
114
115
116
117
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
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
142 return;
143 }
144
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
159
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
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
184
185
186
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
197
198 public static HRegion createHDFSRegionDir(Configuration conf,
199 HRegionInfo hri, HTableDescriptor htd) throws IOException {
200
201 Path root = FSUtils.getRootDir(conf);
202 HRegion region = HRegion.createHRegion(hri, root, conf, htd, null);
203
204
205 HRegion.closeHRegion(region);
206 return region;
207 }
208
209
210
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 }