1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.catalog;
19
20 import java.io.IOException;
21 import java.io.InterruptedIOException;
22 import java.net.ConnectException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.HRegionInfo;
30 import org.apache.hadoop.hbase.NotAllMetaRegionsOnlineException;
31 import org.apache.hadoop.hbase.ServerName;
32 import org.apache.hadoop.hbase.client.Delete;
33 import org.apache.hadoop.hbase.client.HTable;
34 import org.apache.hadoop.hbase.client.Mutation;
35 import org.apache.hadoop.hbase.client.Put;
36 import org.apache.hadoop.hbase.client.Result;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.apache.hadoop.hbase.util.PairOfSameType;
39 import org.apache.hadoop.hbase.util.Writables;
40
41
42
43
44
45
46 public class MetaEditor {
47
48
49
50 private static final Log LOG = LogFactory.getLog(MetaEditor.class);
51
52 private static Put makePutFromRegionInfo(HRegionInfo regionInfo)
53 throws IOException {
54 Put put = new Put(regionInfo.getRegionName());
55 put.add(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
56 Writables.getBytes(regionInfo));
57 return put;
58 }
59
60
61
62
63
64
65
66 static void putToMetaTable(final CatalogTracker ct, final Put p)
67 throws IOException {
68 put(MetaReader.getMetaHTable(ct), p);
69 }
70
71
72
73
74
75
76
77 static void putToRootTable(final CatalogTracker ct, final Put p)
78 throws IOException {
79 put(MetaReader.getRootHTable(ct), p);
80 }
81
82
83
84
85
86
87
88 static void putToCatalogTable(final CatalogTracker ct, final Put p)
89 throws IOException {
90 HTable t = MetaReader.getCatalogHTable(ct, p.getRow());
91 put(t, p);
92 }
93
94
95
96
97
98
99 private static void put(final HTable t, final Put p) throws IOException {
100 try {
101 t.put(p);
102 } finally {
103 t.close();
104 }
105 }
106
107
108
109
110
111
112
113 static void putsToMetaTable(final CatalogTracker ct, final List<Put> ps)
114 throws IOException {
115 HTable t = MetaReader.getMetaHTable(ct);
116 try {
117 t.put(ps);
118 } finally {
119 t.close();
120 }
121 }
122
123
124
125
126
127
128
129 static void deleteFromMetaTable(final CatalogTracker ct, final Delete d)
130 throws IOException {
131 List<Delete> dels = new ArrayList<Delete>(1);
132 dels.add(d);
133 deleteFromMetaTable(ct, dels);
134 }
135
136
137
138
139
140
141
142 public static void deleteFromMetaTable(final CatalogTracker ct, final List<Delete> deletes)
143 throws IOException {
144 HTable t = MetaReader.getMetaHTable(ct);
145 try {
146 t.delete(deletes);
147 } finally {
148 t.close();
149 }
150 }
151
152
153
154
155
156
157
158 static void mutateMetaTable(final CatalogTracker ct, final List<Mutation> mutations)
159 throws IOException {
160 HTable t = MetaReader.getMetaHTable(ct);
161 try {
162 t.batch(mutations);
163 } catch (InterruptedException e) {
164 InterruptedIOException ie = new InterruptedIOException(e.getMessage());
165 ie.initCause(e);
166 throw ie;
167 } finally {
168 t.close();
169 }
170 }
171
172
173
174
175
176
177 public static void addRegionToMeta(CatalogTracker catalogTracker,
178 HRegionInfo regionInfo)
179 throws IOException {
180 putToMetaTable(catalogTracker, makePutFromRegionInfo(regionInfo));
181 LOG.info("Added region " + regionInfo.getRegionNameAsString() + " to META");
182 }
183
184
185
186
187
188
189
190 public static void addRegionsToMeta(CatalogTracker catalogTracker,
191 List<HRegionInfo> regionInfos)
192 throws IOException {
193 List<Put> puts = new ArrayList<Put>();
194 for (HRegionInfo regionInfo : regionInfos) {
195 puts.add(makePutFromRegionInfo(regionInfo));
196 }
197 putsToMetaTable(catalogTracker, puts);
198 LOG.info("Added " + puts.size() + " regions in META");
199 }
200
201
202
203
204
205
206
207
208
209
210
211 public static void offlineParentInMeta(CatalogTracker catalogTracker,
212 HRegionInfo parent, final HRegionInfo a, final HRegionInfo b)
213 throws NotAllMetaRegionsOnlineException, IOException {
214 HRegionInfo copyOfParent = new HRegionInfo(parent);
215 copyOfParent.setOffline(true);
216 copyOfParent.setSplit(true);
217 Put put = new Put(copyOfParent.getRegionName());
218 addRegionInfo(put, copyOfParent);
219 put.add(HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER,
220 Writables.getBytes(a));
221 put.add(HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER,
222 Writables.getBytes(b));
223 putToMetaTable(catalogTracker, put);
224 LOG.info("Offlined parent region " + parent.getRegionNameAsString() +
225 " in META");
226 }
227
228 public static void addDaughter(final CatalogTracker catalogTracker,
229 final HRegionInfo regionInfo, final ServerName sn)
230 throws NotAllMetaRegionsOnlineException, IOException {
231 Put put = new Put(regionInfo.getRegionName());
232 addRegionInfo(put, regionInfo);
233 if (sn != null) addLocation(put, sn);
234 putToMetaTable(catalogTracker, put);
235 LOG.info("Added daughter " + regionInfo.getRegionNameAsString() +
236 (sn == null? ", serverName=null": ", serverName=" + sn.toString()));
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public static void updateMetaLocation(CatalogTracker catalogTracker,
255 HRegionInfo regionInfo, ServerName sn)
256 throws IOException, ConnectException {
257 updateLocation(catalogTracker, regionInfo, sn);
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271
272 public static void updateRegionLocation(CatalogTracker catalogTracker,
273 HRegionInfo regionInfo, ServerName sn)
274 throws IOException {
275 updateLocation(catalogTracker, regionInfo, sn);
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289
290 private static void updateLocation(final CatalogTracker catalogTracker,
291 HRegionInfo regionInfo, ServerName sn)
292 throws IOException {
293 Put put = new Put(regionInfo.getRegionName());
294 addLocation(put, sn);
295 putToCatalogTable(catalogTracker, put);
296 LOG.info("Updated row " + regionInfo.getRegionNameAsString() +
297 " with server=" + sn);
298 }
299
300
301
302
303
304
305
306 public static void deleteRegion(CatalogTracker catalogTracker,
307 HRegionInfo regionInfo)
308 throws IOException {
309 Delete delete = new Delete(regionInfo.getRegionName());
310 deleteFromMetaTable(catalogTracker, delete);
311 LOG.info("Deleted region " + regionInfo.getRegionNameAsString() + " from META");
312 }
313
314
315
316
317
318
319
320 public static void deleteRegions(CatalogTracker catalogTracker,
321 List<HRegionInfo> regionsInfo) throws IOException {
322 List<Delete> deletes = new ArrayList<Delete>(regionsInfo.size());
323 for (HRegionInfo hri: regionsInfo) {
324 deletes.add(new Delete(hri.getRegionName()));
325 }
326 deleteFromMetaTable(catalogTracker, deletes);
327 LOG.info("Deleted from META, regions: " + regionsInfo);
328 }
329
330
331
332
333
334
335
336
337 public static void mutateRegions(CatalogTracker catalogTracker,
338 final List<HRegionInfo> regionsToRemove, final List<HRegionInfo> regionsToAdd)
339 throws IOException {
340 List<Mutation> mutation = new ArrayList<Mutation>();
341 if (regionsToRemove != null) {
342 for (HRegionInfo hri: regionsToRemove) {
343 mutation.add(new Delete(hri.getRegionName()));
344 }
345 }
346 if (regionsToAdd != null) {
347 for (HRegionInfo hri: regionsToAdd) {
348 mutation.add(makePutFromRegionInfo(hri));
349 }
350 }
351 mutateMetaTable(catalogTracker, mutation);
352 if (regionsToRemove != null && regionsToRemove.size() > 0) {
353 LOG.debug("Deleted from META, regions: " + regionsToRemove);
354 }
355 if (regionsToAdd != null && regionsToAdd.size() > 0) {
356 LOG.debug("Add to META, regions: " + regionsToAdd);
357 }
358 }
359
360 public static HRegionInfo getHRegionInfo(
361 Result data) throws IOException {
362 byte [] bytes =
363 data.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
364 if (bytes == null) return null;
365 HRegionInfo info = Writables.getHRegionInfo(bytes);
366 LOG.info("Current INFO from scan results = " + info);
367 return info;
368 }
369
370
371
372
373
374 public static PairOfSameType<HRegionInfo> getDaughterRegions(Result data) throws IOException {
375 HRegionInfo splitA = Writables.getHRegionInfoOrNull(
376 data.getValue(HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER));
377 HRegionInfo splitB = Writables.getHRegionInfoOrNull(
378 data.getValue(HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER));
379 return new PairOfSameType<HRegionInfo>(splitA, splitB);
380 }
381
382 private static Put addRegionInfo(final Put p, final HRegionInfo hri)
383 throws IOException {
384 p.add(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
385 Writables.getBytes(hri));
386 return p;
387 }
388
389 private static Put addLocation(final Put p, final ServerName sn) {
390 p.add(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
391 Bytes.toBytes(sn.getHostAndPort()));
392 p.add(HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER,
393 Bytes.toBytes(sn.getStartcode()));
394 return p;
395 }
396 }