1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.client;
22
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.NavigableMap;
28 import java.util.TreeMap;
29 import java.util.TreeSet;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.hadoop.conf.Configuration;
34 import org.apache.hadoop.hbase.HConstants;
35 import org.apache.hadoop.hbase.HRegionInfo;
36 import org.apache.hadoop.hbase.ServerName;
37 import org.apache.hadoop.hbase.TableNotFoundException;
38 import org.apache.hadoop.hbase.errorhandling.TimeoutException;
39 import org.apache.hadoop.hbase.util.Bytes;
40 import org.apache.hadoop.hbase.util.Writables;
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class MetaScanner {
55 private static final Log LOG = LogFactory.getLog(MetaScanner.class);
56
57
58
59
60
61
62
63
64 public static void metaScan(Configuration configuration,
65 MetaScannerVisitor visitor)
66 throws IOException {
67 metaScan(configuration, null, visitor, null);
68 }
69
70
71
72
73
74
75
76
77
78
79 public static void metaScan(Configuration configuration, HConnection connection,
80 MetaScannerVisitor visitor, byte [] userTableName)
81 throws IOException {
82 metaScan(configuration, connection, visitor, userTableName, null, Integer.MAX_VALUE,
83 HConstants.META_TABLE_NAME);
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public static void metaScan(Configuration configuration,
102 MetaScannerVisitor visitor, byte [] userTableName, byte[] row,
103 int rowLimit)
104 throws IOException {
105 metaScan(configuration, null, visitor, userTableName, row, rowLimit,
106 HConstants.META_TABLE_NAME);
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public static void metaScan(Configuration configuration, HConnection connection,
127 MetaScannerVisitor visitor, byte [] tableName, byte[] row,
128 int rowLimit, final byte [] metaTableName)
129 throws IOException {
130 HTable metaTable = null;
131 try {
132 if (connection == null) {
133 metaTable = new HTable(configuration, HConstants.META_TABLE_NAME, null);
134 } else {
135 metaTable = new HTable(HConstants.META_TABLE_NAME, connection, null);
136 }
137 int rowUpperLimit = rowLimit > 0 ? rowLimit: Integer.MAX_VALUE;
138
139
140
141 byte[] startRow;
142 if (row != null) {
143
144 assert tableName != null;
145 byte[] searchRow =
146 HRegionInfo.createRegionName(tableName, row, HConstants.NINES,
147 false);
148 Result startRowResult = metaTable.getRowOrBefore(searchRow,
149 HConstants.CATALOG_FAMILY);
150 if (startRowResult == null) {
151 throw new TableNotFoundException("Cannot find row in .META. for table: "
152 + Bytes.toString(tableName) + ", row=" + Bytes.toStringBinary(searchRow));
153 }
154 byte[] value = startRowResult.getValue(HConstants.CATALOG_FAMILY,
155 HConstants.REGIONINFO_QUALIFIER);
156 if (value == null || value.length == 0) {
157 throw new IOException("HRegionInfo was null or empty in Meta for " +
158 Bytes.toString(tableName) + ", row=" + Bytes.toStringBinary(searchRow));
159 }
160 HRegionInfo regionInfo = Writables.getHRegionInfo(value);
161
162 byte[] rowBefore = regionInfo.getStartKey();
163 startRow = HRegionInfo.createRegionName(tableName, rowBefore,
164 HConstants.ZEROES, false);
165 } else if (tableName == null || tableName.length == 0) {
166
167 startRow = HConstants.EMPTY_START_ROW;
168 } else {
169
170 startRow = HRegionInfo.createRegionName(
171 tableName, HConstants.EMPTY_START_ROW, HConstants.ZEROES, false);
172 }
173
174
175 ScannerCallable callable;
176 int rows = Math.min(rowLimit, configuration.getInt(
177 HConstants.HBASE_META_SCANNER_CACHING,
178 HConstants.DEFAULT_HBASE_META_SCANNER_CACHING));
179 do {
180 final Scan scan = new Scan(startRow).addFamily(HConstants.CATALOG_FAMILY);
181 if (LOG.isDebugEnabled()) {
182 LOG.debug("Scanning " + Bytes.toString(metaTableName) +
183 " starting at row=" + Bytes.toStringBinary(startRow) + " for max=" +
184 rowUpperLimit + " rows using " + metaTable.getConnection().toString());
185 }
186 callable = new ScannerCallable(metaTable.getConnection(), metaTableName, scan, null);
187
188 callable.withRetries();
189
190 int processedRows = 0;
191 try {
192 callable.setCaching(rows);
193 done: do {
194 if (processedRows >= rowUpperLimit) {
195 break;
196 }
197
198 Result [] rrs = callable.withRetries();
199 if (rrs == null || rrs.length == 0 || rrs[0].size() == 0) {
200 break;
201 }
202 for (Result rr : rrs) {
203 if (processedRows >= rowUpperLimit) {
204 break done;
205 }
206 if (!visitor.processRow(rr))
207 break done;
208 processedRows++;
209 }
210
211 } while(true);
212
213 startRow = callable.getHRegionInfo().getEndKey();
214 } finally {
215
216 callable.setClose();
217 callable.withRetries();
218 }
219 } while (Bytes.compareTo(startRow, HConstants.LAST_ROW) != 0);
220 } finally {
221 visitor.close();
222 if (metaTable != null) {
223 metaTable.close();
224 }
225 }
226 }
227
228
229
230
231
232
233
234
235
236
237
238 public static List<HRegionInfo> listAllRegions(Configuration conf, final boolean offlined)
239 throws IOException {
240 final List<HRegionInfo> regions = new ArrayList<HRegionInfo>();
241 MetaScannerVisitor visitor = new BlockingMetaScannerVisitor(conf) {
242 @Override
243 public boolean processRowInternal(Result result) throws IOException {
244 if (result == null || result.isEmpty()) {
245 return true;
246 }
247 byte [] bytes = result.getValue(HConstants.CATALOG_FAMILY,
248 HConstants.REGIONINFO_QUALIFIER);
249 if (bytes == null) {
250 LOG.warn("Null REGIONINFO_QUALIFIER: " + result);
251 return true;
252 }
253 HRegionInfo regionInfo = Writables.getHRegionInfo(bytes);
254
255 if (regionInfo.isOffline() && !offlined) return true;
256 regions.add(regionInfo);
257 return true;
258 }
259 };
260 metaScan(conf, visitor);
261 return regions;
262 }
263
264
265
266
267
268
269
270
271
272
273 public static NavigableMap<HRegionInfo, ServerName> allTableRegions(Configuration conf,
274 HConnection connection, final byte[] tablename, final boolean offlined) throws IOException {
275 final NavigableMap<HRegionInfo, ServerName> regions =
276 new TreeMap<HRegionInfo, ServerName>();
277 MetaScannerVisitor visitor = new TableMetaScannerVisitor(conf, tablename) {
278 @Override
279 public boolean processRowInternal(Result rowResult) throws IOException {
280 HRegionInfo info = Writables.getHRegionInfo(
281 rowResult.getValue(HConstants.CATALOG_FAMILY,
282 HConstants.REGIONINFO_QUALIFIER));
283 byte [] value = rowResult.getValue(HConstants.CATALOG_FAMILY,
284 HConstants.SERVER_QUALIFIER);
285 String hostAndPort = null;
286 if (value != null && value.length > 0) {
287 hostAndPort = Bytes.toString(value);
288 }
289 value = rowResult.getValue(HConstants.CATALOG_FAMILY,
290 HConstants.STARTCODE_QUALIFIER);
291 long startcode = -1L;
292 if (value != null && value.length > 0) startcode = Bytes.toLong(value);
293 if (!(info.isOffline() || info.isSplit())) {
294 ServerName sn = null;
295 if (hostAndPort != null && hostAndPort.length() > 0) {
296 sn = new ServerName(hostAndPort, startcode);
297 }
298 regions.put(new UnmodifyableHRegionInfo(info), sn);
299 }
300 return true;
301 }
302 };
303 metaScan(conf, connection, visitor, tablename);
304 return regions;
305 }
306
307
308
309
310 public interface MetaScannerVisitor extends Closeable {
311
312
313
314
315
316
317
318
319
320 public boolean processRow(Result rowResult) throws IOException;
321 }
322
323 public static abstract class MetaScannerVisitorBase implements MetaScannerVisitor {
324 @Override
325 public void close() throws IOException {
326 }
327 }
328
329
330
331
332
333
334
335 public static abstract class BlockingMetaScannerVisitor
336 extends MetaScannerVisitorBase {
337
338 private static final int DEFAULT_BLOCKING_TIMEOUT = 10000;
339 private Configuration conf;
340 private TreeSet<byte[]> daughterRegions = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
341 private int blockingTimeout;
342 private HTable metaTable;
343
344 public BlockingMetaScannerVisitor(Configuration conf) {
345 this.conf = conf;
346 this.blockingTimeout = conf.getInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT,
347 DEFAULT_BLOCKING_TIMEOUT);
348 }
349
350 public abstract boolean processRowInternal(Result rowResult) throws IOException;
351
352 @Override
353 public void close() throws IOException {
354 super.close();
355 if (metaTable != null) {
356 metaTable.close();
357 metaTable = null;
358 }
359 }
360
361 public HTable getMetaTable() throws IOException {
362 if (metaTable == null) {
363 metaTable = new HTable(conf, HConstants.META_TABLE_NAME);
364 }
365 return metaTable;
366 }
367
368 @Override
369 public boolean processRow(Result rowResult) throws IOException {
370 HRegionInfo info = Writables.getHRegionInfoOrNull(
371 rowResult.getValue(HConstants.CATALOG_FAMILY,
372 HConstants.REGIONINFO_QUALIFIER));
373 if (info == null) {
374 return true;
375 }
376
377 if (daughterRegions.remove(info.getRegionName())) {
378 return true;
379 }
380
381 if (info.isSplitParent()) {
382
383
384
385
386 HRegionInfo splitA = Writables.getHRegionInfoOrNull(rowResult.getValue(HConstants.CATALOG_FAMILY,
387 HConstants.SPLITA_QUALIFIER));
388 HRegionInfo splitB = Writables.getHRegionInfoOrNull(rowResult.getValue(HConstants.CATALOG_FAMILY,
389 HConstants.SPLITB_QUALIFIER));
390
391 HTable metaTable = getMetaTable();
392 long start = System.currentTimeMillis();
393 if (splitA != null) {
394 try {
395 Result resultA = getRegionResultBlocking(metaTable, blockingTimeout,
396 info.getRegionName(), splitA.getRegionName());
397 if (resultA != null) {
398 processRow(resultA);
399 daughterRegions.add(splitA.getRegionName());
400 }
401
402 } catch (TimeoutException e) {
403 throw new RegionOfflineException("Split daughter region " +
404 splitA.getRegionNameAsString() + " cannot be found in META. Parent:" +
405 info.getRegionNameAsString());
406 }
407 }
408 long rem = blockingTimeout - (System.currentTimeMillis() - start);
409
410 if (splitB != null) {
411 try {
412 Result resultB = getRegionResultBlocking(metaTable, rem,
413 info.getRegionName(), splitB.getRegionName());
414 if (resultB != null) {
415 processRow(resultB);
416 daughterRegions.add(splitB.getRegionName());
417 }
418
419 } catch (TimeoutException e) {
420 throw new RegionOfflineException("Split daughter region " +
421 splitB.getRegionNameAsString() + " cannot be found in META. Parent:" +
422 info.getRegionNameAsString());
423 }
424 }
425 }
426
427 return processRowInternal(rowResult);
428 }
429
430
431
432
433
434
435
436
437 private Result getRegionResultBlocking(HTable metaTable, long timeout, byte[] parentRegionName, byte[] regionName)
438 throws IOException, TimeoutException {
439 boolean logged = false;
440 long start = System.currentTimeMillis();
441 while (System.currentTimeMillis() - start < timeout) {
442 Get get = new Get(regionName);
443 Result result = metaTable.get(get);
444 HRegionInfo info = Writables.getHRegionInfoOrNull(
445 result.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER));
446 if (info != null) {
447 return result;
448 }
449
450
451 Get parentGet = new Get(parentRegionName);
452 Result parentResult = metaTable.get(parentGet);
453 HRegionInfo parentInfo = Writables.getHRegionInfoOrNull(
454 parentResult.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER));
455 if (parentInfo == null) {
456
457 return null;
458 }
459
460 try {
461 if (!logged) {
462 if (LOG.isDebugEnabled()) {
463 LOG.debug("blocking until region is in META: " + Bytes.toStringBinary(regionName));
464 }
465 logged = true;
466 }
467 Thread.sleep(10);
468 } catch (InterruptedException ex) {
469 Thread.currentThread().interrupt();
470 break;
471 }
472 }
473 throw new TimeoutException("getRegionResultBlocking", start, System.currentTimeMillis(),
474 timeout);
475 }
476 }
477
478
479
480
481
482
483
484 public static abstract class TableMetaScannerVisitor extends BlockingMetaScannerVisitor {
485 private byte[] tableName;
486
487 public TableMetaScannerVisitor(Configuration conf, byte[] tableName) {
488 super(conf);
489 this.tableName = tableName;
490 }
491
492 @Override
493 public final boolean processRow(Result rowResult) throws IOException {
494 HRegionInfo info = Writables.getHRegionInfoOrNull(
495 rowResult.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER));
496 if (info == null) {
497 return true;
498 }
499 if (!(Bytes.equals(info.getTableName(), tableName))) {
500 return false;
501 }
502 return super.processRow(rowResult);
503 }
504
505 }
506 }