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