1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.util;
21
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.HRegionInfo;
32 import org.apache.hadoop.hbase.HTableDescriptor;
33 import org.apache.hadoop.hbase.ServerName;
34 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
35 import org.apache.hadoop.hbase.client.HBaseAdmin;
36 import org.apache.hadoop.hbase.client.HConnection;
37 import org.apache.hadoop.hbase.client.HTable;
38 import org.apache.hadoop.hbase.client.Put;
39 import org.apache.hadoop.hbase.ipc.HRegionInterface;
40 import org.apache.hadoop.hbase.master.AssignmentManager.RegionState;
41 import org.apache.hadoop.hbase.regionserver.HRegion;
42 import org.apache.hadoop.hbase.regionserver.wal.HLog;
43 import org.apache.zookeeper.KeeperException;
44
45
46
47
48
49 public class HBaseFsckRepair {
50 public static final Log LOG = LogFactory.getLog(HBaseFsckRepair.class);
51
52
53
54
55
56
57
58
59
60
61 public static void fixMultiAssignment(HBaseAdmin admin, HRegionInfo region,
62 List<ServerName> servers)
63 throws IOException, KeeperException, InterruptedException {
64 HRegionInfo actualRegion = new HRegionInfo(region);
65
66
67 for(ServerName server : servers) {
68 closeRegionSilentlyAndWait(admin, server, actualRegion);
69 }
70
71
72 forceOfflineInZK(admin, actualRegion);
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public static void fixUnassigned(HBaseAdmin admin, HRegionInfo region)
88 throws IOException, KeeperException {
89 HRegionInfo actualRegion = new HRegionInfo(region);
90
91
92 forceOfflineInZK(admin, actualRegion);
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107 private static void forceOfflineInZK(HBaseAdmin admin, final HRegionInfo region)
108 throws ZooKeeperConnectionException, KeeperException, IOException {
109 admin.assign(region.getRegionName());
110 }
111
112
113
114
115 public static void waitUntilAssigned(HBaseAdmin admin,
116 HRegionInfo region) throws IOException, InterruptedException {
117 long timeout = admin.getConfiguration().getLong("hbase.hbck.assign.timeout", 120000);
118 long expiration = timeout + System.currentTimeMillis();
119 while (System.currentTimeMillis() < expiration) {
120 try {
121 Map<String, RegionState> rits=
122 admin.getClusterStatus().getRegionsInTransition();
123
124 if (rits.keySet() != null && !rits.keySet().contains(region.getEncodedName())) {
125
126 return;
127 }
128
129 LOG.info("Region still in transition, waiting for "
130 + "it to become assigned: " + region);
131 } catch (IOException e) {
132 LOG.warn("Exception when waiting for region to become assigned,"
133 + " retrying", e);
134 }
135 Thread.sleep(1000);
136 }
137 throw new IOException("Region " + region + " failed to move out of " +
138 "transition within timeout " + timeout + "ms");
139 }
140
141
142
143
144
145 public static void closeRegionSilentlyAndWait(HBaseAdmin admin,
146 ServerName server, HRegionInfo region) throws IOException, InterruptedException {
147 HConnection connection = admin.getConnection();
148 HRegionInterface rs = connection.getHRegionConnection(server.getHostname(),
149 server.getPort());
150 rs.closeRegion(region, false);
151 long timeout = admin.getConfiguration()
152 .getLong("hbase.hbck.close.timeout", 120000);
153 long expiration = timeout + System.currentTimeMillis();
154 while (System.currentTimeMillis() < expiration) {
155 try {
156 HRegionInfo rsRegion = rs.getRegionInfo(region.getRegionName());
157 if (rsRegion == null)
158 return;
159 } catch (IOException ioe) {
160 return;
161 }
162 Thread.sleep(1000);
163 }
164 throw new IOException("Region " + region + " failed to close within"
165 + " timeout " + timeout);
166 }
167
168
169
170
171 public static void fixMetaHoleOnline(Configuration conf,
172 HRegionInfo hri) throws IOException {
173 Put p = new Put(hri.getRegionName());
174 p.add(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
175 Writables.getBytes(hri));
176 HTable meta = new HTable(conf, HConstants.META_TABLE_NAME);
177 meta.put(p);
178 meta.close();
179 }
180
181
182
183
184 public static HRegion createHDFSRegionDir(Configuration conf,
185 HRegionInfo hri, HTableDescriptor htd) throws IOException {
186
187 Path root = FSUtils.getRootDir(conf);
188 HRegion region = HRegion.createHRegion(hri, root, conf, htd);
189 HLog hlog = region.getLog();
190
191
192 region.close();
193 hlog.closeAndDelete();
194 return region;
195 }
196 }