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