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