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.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
60
61
62 @InterfaceAudience.Private
63 public class HBaseFsckRepair {
64 public static final Log LOG = LogFactory.getLog(HBaseFsckRepair.class);
65
66
67
68
69
70
71
72
73
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
81 for(ServerName server : servers) {
82 closeRegionSilentlyAndWait(connection, server, actualRegion);
83 }
84
85
86 forceOfflineInZK(connection.getAdmin(), actualRegion);
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public static void fixUnassigned(Admin admin, HRegionInfo region)
102 throws IOException, KeeperException, InterruptedException {
103 HRegionInfo actualRegion = new HRegionInfo(region);
104
105
106 forceOfflineInZK(admin, actualRegion);
107 }
108
109
110
111
112
113
114
115
116
117
118
119
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
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
140 return;
141 }
142
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
157
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
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
182
183
184
185 MetaTableAccessor.addLocation(put, sn, sn.getStartcode(), i);
186 }
187 }
188 meta.put(put);
189 meta.close();
190 conn.close();
191 }
192
193
194
195
196 public static HRegion createHDFSRegionDir(Configuration conf,
197 HRegionInfo hri, HTableDescriptor htd) throws IOException {
198
199 Path root = FSUtils.getRootDir(conf);
200 HRegion region = HRegion.createHRegion(hri, root, conf, htd, null);
201
202
203 HRegion.closeHRegion(region);
204 return region;
205 }
206 }