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