1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase;
19
20 import com.google.common.annotations.VisibleForTesting;
21 import com.google.protobuf.ServiceException;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.classification.InterfaceAudience;
27 import org.apache.hadoop.hbase.client.ClusterConnection;
28 import org.apache.hadoop.hbase.client.Connection;
29 import org.apache.hadoop.hbase.client.ConnectionFactory;
30 import org.apache.hadoop.hbase.client.Delete;
31 import org.apache.hadoop.hbase.client.Get;
32 import org.apache.hadoop.hbase.client.HTable;
33 import org.apache.hadoop.hbase.client.Mutation;
34 import org.apache.hadoop.hbase.client.Put;
35 import org.apache.hadoop.hbase.client.RegionLocator;
36 import org.apache.hadoop.hbase.client.RegionReplicaUtil;
37 import org.apache.hadoop.hbase.client.Result;
38 import org.apache.hadoop.hbase.client.ResultScanner;
39 import org.apache.hadoop.hbase.client.Scan;
40 import org.apache.hadoop.hbase.client.Table;
41 import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel;
42 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
43 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
44 import org.apache.hadoop.hbase.protobuf.generated.MultiRowMutationProtos;
45 import org.apache.hadoop.hbase.util.Bytes;
46 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
47 import org.apache.hadoop.hbase.util.Pair;
48 import org.apache.hadoop.hbase.util.PairOfSameType;
49 import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
50 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
51
52 import java.io.IOException;
53 import java.io.InterruptedIOException;
54 import java.util.ArrayList;
55 import java.util.List;
56 import java.util.Map;
57 import java.util.NavigableMap;
58 import java.util.Set;
59 import java.util.SortedMap;
60 import java.util.TreeMap;
61 import java.util.regex.Matcher;
62 import java.util.regex.Pattern;
63
64
65
66
67
68
69
70
71
72
73 @InterfaceAudience.Private
74 public class MetaTableAccessor {
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 private static final Log LOG = LogFactory.getLog(MetaTableAccessor.class);
113
114 static final byte [] META_REGION_PREFIX;
115 static {
116
117
118 int len = HRegionInfo.FIRST_META_REGIONINFO.getRegionName().length - 2;
119 META_REGION_PREFIX = new byte [len];
120 System.arraycopy(HRegionInfo.FIRST_META_REGIONINFO.getRegionName(), 0,
121 META_REGION_PREFIX, 0, len);
122 }
123
124
125 protected static final char META_REPLICA_ID_DELIMITER = '_';
126
127
128 private static final Pattern SERVER_COLUMN_PATTERN
129 = Pattern.compile("^server(_[0-9a-fA-F]{4})?$");
130
131
132
133
134
135
136
137
138
139
140 public static List<Result> fullScanOfMeta(Connection connection)
141 throws IOException {
142 CollectAllVisitor v = new CollectAllVisitor();
143 fullScan(connection, v, null);
144 return v.getResults();
145 }
146
147
148
149
150
151
152
153 public static void fullScan(Connection connection,
154 final Visitor visitor)
155 throws IOException {
156 fullScan(connection, visitor, null);
157 }
158
159
160
161
162
163
164
165 public static List<Result> fullScan(Connection connection)
166 throws IOException {
167 CollectAllVisitor v = new CollectAllVisitor();
168 fullScan(connection, v, null);
169 return v.getResults();
170 }
171
172
173
174
175
176
177
178 static Table getMetaHTable(final Connection connection)
179 throws IOException {
180
181 if (connection == null) {
182 throw new NullPointerException("No connection");
183 } else if (connection.isClosed()) {
184 throw new IOException("connection is closed");
185 }
186
187
188
189
190
191
192
193
194 if (connection instanceof ClusterConnection) {
195 if (((ClusterConnection) connection).isManaged()) {
196 return new HTable(TableName.META_TABLE_NAME, (ClusterConnection) connection);
197 }
198 }
199 return connection.getTable(TableName.META_TABLE_NAME);
200 }
201
202
203
204
205
206
207 private static Result get(final Table t, final Get g) throws IOException {
208 try {
209 return t.get(g);
210 } finally {
211 t.close();
212 }
213 }
214
215
216
217
218
219
220
221
222
223 @Deprecated
224 public static Pair<HRegionInfo, ServerName> getRegion(Connection connection, byte [] regionName)
225 throws IOException {
226 HRegionLocation location = getRegionLocation(connection, regionName);
227 return location == null
228 ? null
229 : new Pair<HRegionInfo, ServerName>(location.getRegionInfo(), location.getServerName());
230 }
231
232
233
234
235
236
237
238
239 public static HRegionLocation getRegionLocation(Connection connection,
240 byte[] regionName) throws IOException {
241 byte[] row = regionName;
242 HRegionInfo parsedInfo = null;
243 try {
244 parsedInfo = parseRegionInfoFromRegionName(regionName);
245 row = getMetaKeyForRegion(parsedInfo);
246 } catch (Exception parseEx) {
247
248 }
249 Get get = new Get(row);
250 get.addFamily(HConstants.CATALOG_FAMILY);
251 Result r = get(getMetaHTable(connection), get);
252 RegionLocations locations = getRegionLocations(r);
253 return locations == null
254 ? null
255 : locations.getRegionLocation(parsedInfo == null ? 0 : parsedInfo.getReplicaId());
256 }
257
258
259
260
261
262
263
264
265 public static HRegionLocation getRegionLocation(Connection connection,
266 HRegionInfo regionInfo) throws IOException {
267 byte[] row = getMetaKeyForRegion(regionInfo);
268 Get get = new Get(row);
269 get.addFamily(HConstants.CATALOG_FAMILY);
270 Result r = get(getMetaHTable(connection), get);
271 return getRegionLocation(r, regionInfo, regionInfo.getReplicaId());
272 }
273
274
275 public static byte[] getMetaKeyForRegion(HRegionInfo regionInfo) {
276 return RegionReplicaUtil.getRegionInfoForDefaultReplica(regionInfo).getRegionName();
277 }
278
279
280
281
282
283 protected static HRegionInfo parseRegionInfoFromRegionName(byte[] regionName)
284 throws IOException {
285 byte[][] fields = HRegionInfo.parseRegionName(regionName);
286 long regionId = Long.parseLong(Bytes.toString(fields[2]));
287 int replicaId = fields.length > 3 ? Integer.parseInt(Bytes.toString(fields[3]), 16) : 0;
288 return new HRegionInfo(
289 TableName.valueOf(fields[0]), fields[1], fields[1], false, regionId, replicaId);
290 }
291
292
293
294
295
296
297
298
299 public static Result getRegionResult(Connection connection,
300 byte[] regionName) throws IOException {
301 Get get = new Get(regionName);
302 get.addFamily(HConstants.CATALOG_FAMILY);
303 return get(getMetaHTable(connection), get);
304 }
305
306
307
308
309
310
311 public static Pair<HRegionInfo, HRegionInfo> getRegionsFromMergeQualifier(
312 Connection connection, byte[] regionName) throws IOException {
313 Result result = getRegionResult(connection, regionName);
314 HRegionInfo mergeA = getHRegionInfo(result, HConstants.MERGEA_QUALIFIER);
315 HRegionInfo mergeB = getHRegionInfo(result, HConstants.MERGEB_QUALIFIER);
316 if (mergeA == null && mergeB == null) {
317 return null;
318 }
319 return new Pair<HRegionInfo, HRegionInfo>(mergeA, mergeB);
320 }
321
322
323
324
325
326
327
328
329
330 public static boolean tableExists(Connection connection,
331 final TableName tableName)
332 throws IOException {
333 if (tableName.equals(TableName.META_TABLE_NAME)) {
334
335 return true;
336 }
337
338 CollectingVisitor<HRegionInfo> visitor = new CollectingVisitor<HRegionInfo>() {
339 private HRegionInfo current = null;
340
341 @Override
342 public boolean visit(Result r) throws IOException {
343 RegionLocations locations = getRegionLocations(r);
344 if (locations == null || locations.getRegionLocation().getRegionInfo() == null) {
345 LOG.warn("No serialized HRegionInfo in " + r);
346 return true;
347 }
348 this.current = locations.getRegionLocation().getRegionInfo();
349 if (this.current == null) {
350 LOG.warn("No serialized HRegionInfo in " + r);
351 return true;
352 }
353 if (!isInsideTable(this.current, tableName)) return false;
354
355 super.visit(r);
356
357 return false;
358 }
359
360 @Override
361 void add(Result r) {
362
363 this.results.add(this.current);
364 }
365 };
366 fullScan(connection, visitor, getTableStartRowForMeta(tableName));
367
368 return visitor.getResults().size() >= 1;
369 }
370
371
372
373
374
375
376
377
378
379 public static List<HRegionInfo> getTableRegions(ZooKeeperWatcher zkw,
380 Connection connection, TableName tableName)
381 throws IOException {
382 return getTableRegions(zkw, connection, tableName, false);
383 }
384
385
386
387
388
389
390
391
392
393
394
395 public static List<HRegionInfo> getTableRegions(ZooKeeperWatcher zkw,
396 Connection connection, TableName tableName, final boolean excludeOfflinedSplitParents)
397 throws IOException {
398 List<Pair<HRegionInfo, ServerName>> result = null;
399 result = getTableRegionsAndLocations(zkw, connection, tableName,
400 excludeOfflinedSplitParents);
401 return getListOfHRegionInfos(result);
402 }
403
404 static List<HRegionInfo> getListOfHRegionInfos(final List<Pair<HRegionInfo, ServerName>> pairs) {
405 if (pairs == null || pairs.isEmpty()) return null;
406 List<HRegionInfo> result = new ArrayList<HRegionInfo>(pairs.size());
407 for (Pair<HRegionInfo, ServerName> pair: pairs) {
408 result.add(pair.getFirst());
409 }
410 return result;
411 }
412
413
414
415
416
417
418
419 static boolean isInsideTable(final HRegionInfo current, final TableName tableName) {
420 return tableName.equals(current.getTable());
421 }
422
423
424
425
426
427
428 static byte [] getTableStartRowForMeta(TableName tableName) {
429 byte [] startRow = new byte[tableName.getName().length + 2];
430 System.arraycopy(tableName.getName(), 0, startRow, 0, tableName.getName().length);
431 startRow[startRow.length - 2] = HConstants.DELIMITER;
432 startRow[startRow.length - 1] = HConstants.DELIMITER;
433 return startRow;
434 }
435
436
437
438
439
440
441
442
443
444
445 public static Scan getScanForTableName(TableName tableName) {
446 String strName = tableName.getNameAsString();
447
448 byte[] startKey = Bytes.toBytes(strName + ",,");
449
450 byte[] stopKey = Bytes.toBytes(strName + " ,,");
451
452 Scan scan = new Scan(startKey);
453 scan.setStopRow(stopKey);
454 return scan;
455 }
456
457
458
459
460
461
462
463
464 public static List<Pair<HRegionInfo, ServerName>>
465 getTableRegionsAndLocations(ZooKeeperWatcher zkw,
466 Connection connection, TableName tableName)
467 throws IOException {
468 return getTableRegionsAndLocations(zkw, connection, tableName, true);
469 }
470
471
472
473
474
475
476
477
478 public static List<Pair<HRegionInfo, ServerName>> getTableRegionsAndLocations(
479 ZooKeeperWatcher zkw, Connection connection, final TableName tableName,
480 final boolean excludeOfflinedSplitParents) throws IOException {
481
482 if (tableName.equals(TableName.META_TABLE_NAME)) {
483
484 ServerName serverName = new MetaTableLocator().getMetaRegionLocation(zkw);
485 List<Pair<HRegionInfo, ServerName>> list =
486 new ArrayList<Pair<HRegionInfo, ServerName>>();
487 list.add(new Pair<HRegionInfo, ServerName>(HRegionInfo.FIRST_META_REGIONINFO,
488 serverName));
489 return list;
490 }
491
492 CollectingVisitor<Pair<HRegionInfo, ServerName>> visitor =
493 new CollectingVisitor<Pair<HRegionInfo, ServerName>>() {
494 private RegionLocations current = null;
495
496 @Override
497 public boolean visit(Result r) throws IOException {
498 current = getRegionLocations(r);
499 if (current == null || current.getRegionLocation().getRegionInfo() == null) {
500 LOG.warn("No serialized HRegionInfo in " + r);
501 return true;
502 }
503 HRegionInfo hri = current.getRegionLocation().getRegionInfo();
504 if (!isInsideTable(hri, tableName)) return false;
505 if (excludeOfflinedSplitParents && hri.isSplitParent()) return true;
506
507 return super.visit(r);
508 }
509
510 @Override
511 void add(Result r) {
512 if (current == null) {
513 return;
514 }
515 for (HRegionLocation loc : current.getRegionLocations()) {
516 if (loc != null) {
517 this.results.add(new Pair<HRegionInfo, ServerName>(
518 loc.getRegionInfo(), loc.getServerName()));
519 }
520 }
521 }
522 };
523 fullScan(connection, visitor, getTableStartRowForMeta(tableName));
524 return visitor.getResults();
525 }
526
527
528
529
530
531
532
533
534 public static NavigableMap<HRegionInfo, Result>
535 getServerUserRegions(Connection connection, final ServerName serverName)
536 throws IOException {
537 final NavigableMap<HRegionInfo, Result> hris = new TreeMap<HRegionInfo, Result>();
538
539
540 CollectingVisitor<Result> v = new CollectingVisitor<Result>() {
541 @Override
542 void add(Result r) {
543 if (r == null || r.isEmpty()) return;
544 RegionLocations locations = getRegionLocations(r);
545 if (locations == null) return;
546 for (HRegionLocation loc : locations.getRegionLocations()) {
547 if (loc != null) {
548 if (loc.getServerName() != null && loc.getServerName().equals(serverName)) {
549 hris.put(loc.getRegionInfo(), r);
550 }
551 }
552 }
553 }
554 };
555 fullScan(connection, v);
556 return hris;
557 }
558
559 public static void fullScanMetaAndPrint(Connection connection)
560 throws IOException {
561 Visitor v = new Visitor() {
562 @Override
563 public boolean visit(Result r) throws IOException {
564 if (r == null || r.isEmpty()) return true;
565 LOG.info("fullScanMetaAndPrint.Current Meta Row: " + r);
566 RegionLocations locations = getRegionLocations(r);
567 if (locations == null) return true;
568 for (HRegionLocation loc : locations.getRegionLocations()) {
569 if (loc != null) {
570 LOG.info("fullScanMetaAndPrint.HRI Print= " + loc.getRegionInfo());
571 }
572 }
573 return true;
574 }
575 };
576 fullScan(connection, v);
577 }
578
579
580
581
582
583
584
585
586
587
588 public static void fullScan(Connection connection,
589 final Visitor visitor, final byte [] startrow)
590 throws IOException {
591 Scan scan = new Scan();
592 if (startrow != null) scan.setStartRow(startrow);
593 if (startrow == null) {
594 int caching = connection.getConfiguration()
595 .getInt(HConstants.HBASE_META_SCANNER_CACHING, 100);
596 scan.setCaching(caching);
597 }
598 scan.addFamily(HConstants.CATALOG_FAMILY);
599 Table metaTable = getMetaHTable(connection);
600 ResultScanner scanner = null;
601 try {
602 scanner = metaTable.getScanner(scan);
603 Result data;
604 while((data = scanner.next()) != null) {
605 if (data.isEmpty()) continue;
606
607 if (!visitor.visit(data)) break;
608 }
609 } finally {
610 if (scanner != null) scanner.close();
611 metaTable.close();
612 }
613 }
614
615
616
617
618
619 protected static byte[] getFamily() {
620 return HConstants.CATALOG_FAMILY;
621 }
622
623
624
625
626
627 protected static byte[] getRegionInfoColumn() {
628 return HConstants.REGIONINFO_QUALIFIER;
629 }
630
631
632
633
634
635
636 @VisibleForTesting
637 public static byte[] getServerColumn(int replicaId) {
638 return replicaId == 0
639 ? HConstants.SERVER_QUALIFIER
640 : Bytes.toBytes(HConstants.SERVER_QUALIFIER_STR + META_REPLICA_ID_DELIMITER
641 + String.format(HRegionInfo.REPLICA_ID_FORMAT, replicaId));
642 }
643
644
645
646
647
648
649 @VisibleForTesting
650 public static byte[] getStartCodeColumn(int replicaId) {
651 return replicaId == 0
652 ? HConstants.STARTCODE_QUALIFIER
653 : Bytes.toBytes(HConstants.STARTCODE_QUALIFIER_STR + META_REPLICA_ID_DELIMITER
654 + String.format(HRegionInfo.REPLICA_ID_FORMAT, replicaId));
655 }
656
657
658
659
660
661
662 @VisibleForTesting
663 public static byte[] getSeqNumColumn(int replicaId) {
664 return replicaId == 0
665 ? HConstants.SEQNUM_QUALIFIER
666 : Bytes.toBytes(HConstants.SEQNUM_QUALIFIER_STR + META_REPLICA_ID_DELIMITER
667 + String.format(HRegionInfo.REPLICA_ID_FORMAT, replicaId));
668 }
669
670
671
672
673
674
675
676 @VisibleForTesting
677 static int parseReplicaIdFromServerColumn(byte[] serverColumn) {
678 String serverStr = Bytes.toString(serverColumn);
679
680 Matcher matcher = SERVER_COLUMN_PATTERN.matcher(serverStr);
681 if (matcher.matches() && matcher.groupCount() > 0) {
682 String group = matcher.group(1);
683 if (group != null && group.length() > 0) {
684 return Integer.parseInt(group.substring(1), 16);
685 } else {
686 return 0;
687 }
688 }
689 return -1;
690 }
691
692
693
694
695
696
697 private static ServerName getServerName(final Result r, final int replicaId) {
698 byte[] serverColumn = getServerColumn(replicaId);
699 Cell cell = r.getColumnLatestCell(getFamily(), serverColumn);
700 if (cell == null || cell.getValueLength() == 0) return null;
701 String hostAndPort = Bytes.toString(
702 cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
703 byte[] startcodeColumn = getStartCodeColumn(replicaId);
704 cell = r.getColumnLatestCell(getFamily(), startcodeColumn);
705 if (cell == null || cell.getValueLength() == 0) return null;
706 return ServerName.valueOf(hostAndPort,
707 Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
708 }
709
710
711
712
713
714
715
716 private static long getSeqNumDuringOpen(final Result r, final int replicaId) {
717 Cell cell = r.getColumnLatestCell(getFamily(), getSeqNumColumn(replicaId));
718 if (cell == null || cell.getValueLength() == 0) return HConstants.NO_SEQNUM;
719 return Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
720 }
721
722
723
724
725
726
727 public static RegionLocations getRegionLocations(final Result r) {
728 if (r == null) return null;
729 HRegionInfo regionInfo = getHRegionInfo(r, getRegionInfoColumn());
730 if (regionInfo == null) return null;
731
732 List<HRegionLocation> locations = new ArrayList<HRegionLocation>(1);
733 NavigableMap<byte[],NavigableMap<byte[],byte[]>> familyMap = r.getNoVersionMap();
734
735 locations.add(getRegionLocation(r, regionInfo, 0));
736
737 NavigableMap<byte[], byte[]> infoMap = familyMap.get(getFamily());
738 if (infoMap == null) return new RegionLocations(locations);
739
740
741 int replicaId = 0;
742 byte[] serverColumn = getServerColumn(replicaId);
743 SortedMap<byte[], byte[]> serverMap = infoMap.tailMap(serverColumn, false);
744 if (serverMap.isEmpty()) return new RegionLocations(locations);
745
746 for (Map.Entry<byte[], byte[]> entry : serverMap.entrySet()) {
747 replicaId = parseReplicaIdFromServerColumn(entry.getKey());
748 if (replicaId < 0) {
749 break;
750 }
751 HRegionLocation location = getRegionLocation(r, regionInfo, replicaId);
752
753
754 if (location == null || location.getServerName() == null) {
755 locations.add(null);
756 } else {
757 locations.add(location);
758 }
759 }
760
761 return new RegionLocations(locations);
762 }
763
764
765
766
767
768
769
770
771
772
773 private static HRegionLocation getRegionLocation(final Result r, final HRegionInfo regionInfo,
774 final int replicaId) {
775 ServerName serverName = getServerName(r, replicaId);
776 long seqNum = getSeqNumDuringOpen(r, replicaId);
777 HRegionInfo replicaInfo = RegionReplicaUtil.getRegionInfoForReplica(regionInfo, replicaId);
778 return new HRegionLocation(replicaInfo, serverName, seqNum);
779 }
780
781
782
783
784
785
786
787
788 public static HRegionInfo getHRegionInfo(Result data) {
789 return getHRegionInfo(data, HConstants.REGIONINFO_QUALIFIER);
790 }
791
792
793
794
795
796
797
798
799 private static HRegionInfo getHRegionInfo(final Result r, byte [] qualifier) {
800 Cell cell = r.getColumnLatestCell(getFamily(), qualifier);
801 if (cell == null) return null;
802 return HRegionInfo.parseFromOrNull(cell.getValueArray(),
803 cell.getValueOffset(), cell.getValueLength());
804 }
805
806
807
808
809
810
811
812
813 public static PairOfSameType<HRegionInfo> getDaughterRegions(Result data) {
814 HRegionInfo splitA = getHRegionInfo(data, HConstants.SPLITA_QUALIFIER);
815 HRegionInfo splitB = getHRegionInfo(data, HConstants.SPLITB_QUALIFIER);
816
817 return new PairOfSameType<HRegionInfo>(splitA, splitB);
818 }
819
820
821
822
823
824
825
826
827 public static PairOfSameType<HRegionInfo> getMergeRegions(Result data) {
828 HRegionInfo mergeA = getHRegionInfo(data, HConstants.MERGEA_QUALIFIER);
829 HRegionInfo mergeB = getHRegionInfo(data, HConstants.MERGEB_QUALIFIER);
830
831 return new PairOfSameType<HRegionInfo>(mergeA, mergeB);
832 }
833
834
835
836
837 public interface Visitor {
838
839
840
841
842
843
844 boolean visit(final Result r) throws IOException;
845 }
846
847
848
849
850 public static abstract class DefaultVisitorBase implements Visitor {
851
852 public DefaultVisitorBase() {
853 super();
854 }
855
856 public abstract boolean visitInternal(Result rowResult) throws IOException;
857
858 @Override
859 public boolean visit(Result rowResult) throws IOException {
860 HRegionInfo info = getHRegionInfo(rowResult);
861 if (info == null) {
862 return true;
863 }
864
865
866 if (!(info.isOffline() || info.isSplit())) {
867 return visitInternal(rowResult);
868 }
869 return true;
870 }
871 }
872
873
874
875
876 static abstract class CollectingVisitor<T> implements Visitor {
877 final List<T> results = new ArrayList<T>();
878 @Override
879 public boolean visit(Result r) throws IOException {
880 if (r == null || r.isEmpty()) return true;
881 add(r);
882 return true;
883 }
884
885 abstract void add(Result r);
886
887
888
889
890
891 List<T> getResults() {
892 return this.results;
893 }
894 }
895
896
897
898
899 static class CollectAllVisitor extends CollectingVisitor<Result> {
900 @Override
901 void add(Result r) {
902 this.results.add(r);
903 }
904 }
905
906
907
908
909
910
911
912
913 @Deprecated
914 public static int getRegionCount(final Configuration c, final String tableName)
915 throws IOException {
916 return getRegionCount(c, TableName.valueOf(tableName));
917 }
918
919
920
921
922
923
924
925
926 public static int getRegionCount(final Configuration c, final TableName tableName)
927 throws IOException {
928 try (Connection connection = ConnectionFactory.createConnection(c)) {
929 return getRegionCount(connection, tableName);
930 }
931 }
932
933
934
935
936
937
938
939
940 public static int getRegionCount(final Connection connection, final TableName tableName)
941 throws IOException {
942 try (RegionLocator locator = connection.getRegionLocator(tableName)) {
943 List<HRegionLocation> locations = locator.getAllRegionLocations();
944 return locations == null? 0: locations.size();
945 }
946 }
947
948
949
950
951
952
953
954
955 public static Put makePutFromRegionInfo(HRegionInfo regionInfo)
956 throws IOException {
957 return makePutFromRegionInfo(regionInfo, HConstants.LATEST_TIMESTAMP);
958 }
959
960
961
962 public static Put makePutFromRegionInfo(HRegionInfo regionInfo, long ts)
963 throws IOException {
964 Put put = new Put(regionInfo.getRegionName(), ts);
965 addRegionInfo(put, regionInfo);
966 return put;
967 }
968
969
970
971
972
973 public static Delete makeDeleteFromRegionInfo(HRegionInfo regionInfo) {
974 return makeDeleteFromRegionInfo(regionInfo, HConstants.LATEST_TIMESTAMP);
975 }
976
977
978
979
980
981 public static Delete makeDeleteFromRegionInfo(HRegionInfo regionInfo, long ts) {
982 if (regionInfo == null) {
983 throw new IllegalArgumentException("Can't make a delete for null region");
984 }
985 Delete delete = new Delete(regionInfo.getRegionName(), ts);
986 return delete;
987 }
988
989
990
991
992 public static Put addDaughtersToPut(Put put, HRegionInfo splitA, HRegionInfo splitB) {
993 if (splitA != null) {
994 put.addImmutable(
995 HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER, splitA.toByteArray());
996 }
997 if (splitB != null) {
998 put.addImmutable(
999 HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER, splitB.toByteArray());
1000 }
1001 return put;
1002 }
1003
1004
1005
1006
1007
1008
1009
1010 static void putToMetaTable(final Connection connection, final Put p)
1011 throws IOException {
1012 put(getMetaHTable(connection), p);
1013 }
1014
1015
1016
1017
1018
1019
1020 private static void put(final Table t, final Put p) throws IOException {
1021 try {
1022 t.put(p);
1023 } finally {
1024 t.close();
1025 }
1026 }
1027
1028
1029
1030
1031
1032
1033
1034 public static void putsToMetaTable(final Connection connection, final List<Put> ps)
1035 throws IOException {
1036 Table t = getMetaHTable(connection);
1037 try {
1038 t.put(ps);
1039 } finally {
1040 t.close();
1041 }
1042 }
1043
1044
1045
1046
1047
1048
1049
1050 static void deleteFromMetaTable(final Connection connection, final Delete d)
1051 throws IOException {
1052 List<Delete> dels = new ArrayList<Delete>(1);
1053 dels.add(d);
1054 deleteFromMetaTable(connection, dels);
1055 }
1056
1057
1058
1059
1060
1061
1062
1063 public static void deleteFromMetaTable(final Connection connection, final List<Delete> deletes)
1064 throws IOException {
1065 Table t = getMetaHTable(connection);
1066 try {
1067 t.delete(deletes);
1068 } finally {
1069 t.close();
1070 }
1071 }
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081 public static void removeRegionReplicasFromMeta(Set<byte[]> metaRows,
1082 int replicaIndexToDeleteFrom, int numReplicasToRemove, Connection connection)
1083 throws IOException {
1084 int absoluteIndex = replicaIndexToDeleteFrom + numReplicasToRemove;
1085 for (byte[] row : metaRows) {
1086 Delete deleteReplicaLocations = new Delete(row);
1087 for (int i = replicaIndexToDeleteFrom; i < absoluteIndex; i++) {
1088 deleteReplicaLocations.deleteColumns(HConstants.CATALOG_FAMILY,
1089 getServerColumn(i));
1090 deleteReplicaLocations.deleteColumns(HConstants.CATALOG_FAMILY,
1091 getSeqNumColumn(i));
1092 deleteReplicaLocations.deleteColumns(HConstants.CATALOG_FAMILY,
1093 getStartCodeColumn(i));
1094 }
1095 deleteFromMetaTable(connection, deleteReplicaLocations);
1096 }
1097 }
1098
1099
1100
1101
1102
1103
1104
1105 public static void mutateMetaTable(final Connection connection,
1106 final List<Mutation> mutations)
1107 throws IOException {
1108 Table t = getMetaHTable(connection);
1109 try {
1110 t.batch(mutations);
1111 } catch (InterruptedException e) {
1112 InterruptedIOException ie = new InterruptedIOException(e.getMessage());
1113 ie.initCause(e);
1114 throw ie;
1115 } finally {
1116 t.close();
1117 }
1118 }
1119
1120
1121
1122
1123
1124
1125
1126 public static void addRegionToMeta(Connection connection,
1127 HRegionInfo regionInfo)
1128 throws IOException {
1129 putToMetaTable(connection, makePutFromRegionInfo(regionInfo));
1130 LOG.info("Added " + regionInfo.getRegionNameAsString());
1131 }
1132
1133
1134
1135
1136
1137
1138
1139
1140 public static void addRegionToMeta(Table meta, HRegionInfo regionInfo) throws IOException {
1141 addRegionToMeta(meta, regionInfo, null, null);
1142 }
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156 public static void addRegionToMeta(Table meta, HRegionInfo regionInfo,
1157 HRegionInfo splitA, HRegionInfo splitB) throws IOException {
1158 Put put = makePutFromRegionInfo(regionInfo);
1159 addDaughtersToPut(put, splitA, splitB);
1160 meta.put(put);
1161 if (LOG.isDebugEnabled()) {
1162 LOG.debug("Added " + regionInfo.getRegionNameAsString());
1163 }
1164 }
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178 public static void addRegionToMeta(Connection connection, HRegionInfo regionInfo,
1179 HRegionInfo splitA, HRegionInfo splitB) throws IOException {
1180 Table meta = getMetaHTable(connection);
1181 try {
1182 addRegionToMeta(meta, regionInfo, splitA, splitB);
1183 } finally {
1184 meta.close();
1185 }
1186 }
1187
1188
1189
1190
1191
1192
1193
1194 public static void addRegionsToMeta(Connection connection,
1195 List<HRegionInfo> regionInfos, int regionReplication)
1196 throws IOException {
1197 addRegionsToMeta(connection, regionInfos, regionReplication, HConstants.LATEST_TIMESTAMP);
1198 }
1199
1200
1201
1202
1203
1204
1205
1206
1207 public static void addRegionsToMeta(Connection connection,
1208 List<HRegionInfo> regionInfos, int regionReplication, long ts)
1209 throws IOException {
1210 List<Put> puts = new ArrayList<Put>();
1211 for (HRegionInfo regionInfo : regionInfos) {
1212 if (RegionReplicaUtil.isDefaultReplica(regionInfo)) {
1213 Put put = makePutFromRegionInfo(regionInfo, ts);
1214
1215
1216 for (int i = 1; i < regionReplication; i++) {
1217 addEmptyLocation(put, i);
1218 }
1219 puts.add(put);
1220 }
1221 }
1222 putsToMetaTable(connection, puts);
1223 LOG.info("Added " + puts.size());
1224 }
1225
1226
1227
1228
1229
1230
1231
1232 public static void addDaughter(final Connection connection,
1233 final HRegionInfo regionInfo, final ServerName sn, final long openSeqNum)
1234 throws NotAllMetaRegionsOnlineException, IOException {
1235 Put put = new Put(regionInfo.getRegionName());
1236 addRegionInfo(put, regionInfo);
1237 if (sn != null) {
1238 addLocation(put, sn, openSeqNum, -1, regionInfo.getReplicaId());
1239 }
1240 putToMetaTable(connection, put);
1241 LOG.info("Added daughter " + regionInfo.getEncodedName() +
1242 (sn == null? ", serverName=null": ", serverName=" + sn.toString()));
1243 }
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257 public static void mergeRegions(final Connection connection, HRegionInfo mergedRegion,
1258 HRegionInfo regionA, HRegionInfo regionB, ServerName sn, int regionReplication,
1259 long masterSystemTime)
1260 throws IOException {
1261 Table meta = getMetaHTable(connection);
1262 try {
1263 HRegionInfo copyOfMerged = new HRegionInfo(mergedRegion);
1264
1265
1266 long time = Math.max(EnvironmentEdgeManager.currentTime(), masterSystemTime);
1267
1268
1269 Put putOfMerged = makePutFromRegionInfo(copyOfMerged, time);
1270 putOfMerged.addImmutable(HConstants.CATALOG_FAMILY, HConstants.MERGEA_QUALIFIER,
1271 regionA.toByteArray());
1272 putOfMerged.addImmutable(HConstants.CATALOG_FAMILY, HConstants.MERGEB_QUALIFIER,
1273 regionB.toByteArray());
1274
1275
1276 Delete deleteA = makeDeleteFromRegionInfo(regionA, time);
1277 Delete deleteB = makeDeleteFromRegionInfo(regionB, time);
1278
1279
1280 addLocation(putOfMerged, sn, 1, -1, mergedRegion.getReplicaId());
1281
1282
1283
1284 for (int i = 1; i < regionReplication; i++) {
1285 addEmptyLocation(putOfMerged, i);
1286 }
1287
1288 byte[] tableRow = Bytes.toBytes(mergedRegion.getRegionNameAsString()
1289 + HConstants.DELIMITER);
1290 multiMutate(meta, tableRow, putOfMerged, deleteA, deleteB);
1291 } finally {
1292 meta.close();
1293 }
1294 }
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307 public static void splitRegion(final Connection connection,
1308 HRegionInfo parent, HRegionInfo splitA, HRegionInfo splitB,
1309 ServerName sn, int regionReplication) throws IOException {
1310 Table meta = getMetaHTable(connection);
1311 try {
1312 HRegionInfo copyOfParent = new HRegionInfo(parent);
1313 copyOfParent.setOffline(true);
1314 copyOfParent.setSplit(true);
1315
1316
1317 Put putParent = makePutFromRegionInfo(copyOfParent);
1318 addDaughtersToPut(putParent, splitA, splitB);
1319
1320
1321 Put putA = makePutFromRegionInfo(splitA);
1322 Put putB = makePutFromRegionInfo(splitB);
1323
1324 addLocation(putA, sn, 1, -1, splitA.getReplicaId());
1325 addLocation(putB, sn, 1, -1, splitB.getReplicaId());
1326
1327
1328
1329 for (int i = 1; i < regionReplication; i++) {
1330 addEmptyLocation(putA, i);
1331 addEmptyLocation(putB, i);
1332 }
1333
1334 byte[] tableRow = Bytes.toBytes(parent.getRegionNameAsString() + HConstants.DELIMITER);
1335 multiMutate(meta, tableRow, putParent, putA, putB);
1336 } finally {
1337 meta.close();
1338 }
1339 }
1340
1341
1342
1343
1344 private static void multiMutate(Table table, byte[] row, Mutation... mutations)
1345 throws IOException {
1346 CoprocessorRpcChannel channel = table.coprocessorService(row);
1347 MultiRowMutationProtos.MutateRowsRequest.Builder mmrBuilder
1348 = MultiRowMutationProtos.MutateRowsRequest.newBuilder();
1349 for (Mutation mutation : mutations) {
1350 if (mutation instanceof Put) {
1351 mmrBuilder.addMutationRequest(ProtobufUtil.toMutation(
1352 ClientProtos.MutationProto.MutationType.PUT, mutation));
1353 } else if (mutation instanceof Delete) {
1354 mmrBuilder.addMutationRequest(ProtobufUtil.toMutation(
1355 ClientProtos.MutationProto.MutationType.DELETE, mutation));
1356 } else {
1357 throw new DoNotRetryIOException("multi in MetaEditor doesn't support "
1358 + mutation.getClass().getName());
1359 }
1360 }
1361
1362 MultiRowMutationProtos.MultiRowMutationService.BlockingInterface service =
1363 MultiRowMutationProtos.MultiRowMutationService.newBlockingStub(channel);
1364 try {
1365 service.mutateRows(null, mmrBuilder.build());
1366 } catch (ServiceException ex) {
1367 ProtobufUtil.toIOException(ex);
1368 }
1369 }
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385 public static void updateRegionLocation(Connection connection,
1386 HRegionInfo regionInfo, ServerName sn, long openSeqNum,
1387 long masterSystemTime)
1388 throws IOException {
1389 updateLocation(connection, regionInfo, sn, openSeqNum, masterSystemTime);
1390 }
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406 private static void updateLocation(final Connection connection,
1407 HRegionInfo regionInfo, ServerName sn, long openSeqNum,
1408 long masterSystemTime)
1409 throws IOException {
1410
1411
1412 long time = Math.max(EnvironmentEdgeManager.currentTime(), masterSystemTime);
1413
1414
1415 Put put = new Put(getMetaKeyForRegion(regionInfo), time);
1416 addLocation(put, sn, openSeqNum, time, regionInfo.getReplicaId());
1417 putToMetaTable(connection, put);
1418 LOG.info("Updated row " + regionInfo.getRegionNameAsString() +
1419 " with server=" + sn);
1420 }
1421
1422
1423
1424
1425
1426
1427
1428 public static void deleteRegion(Connection connection,
1429 HRegionInfo regionInfo)
1430 throws IOException {
1431 Delete delete = new Delete(regionInfo.getRegionName());
1432 deleteFromMetaTable(connection, delete);
1433 LOG.info("Deleted " + regionInfo.getRegionNameAsString());
1434 }
1435
1436
1437
1438
1439
1440
1441
1442
1443 public static void deleteRegions(Connection connection,
1444 List<HRegionInfo> regionsInfo, long ts) throws IOException {
1445 List<Delete> deletes = new ArrayList<Delete>(regionsInfo.size());
1446 for (HRegionInfo hri: regionsInfo) {
1447 deletes.add(new Delete(hri.getRegionName(), ts));
1448 }
1449 deleteFromMetaTable(connection, deletes);
1450 LOG.info("Deleted " + regionsInfo);
1451 }
1452
1453
1454
1455
1456
1457
1458 public static void deleteRegions(Connection connection,
1459 List<HRegionInfo> regionsInfo) throws IOException {
1460 deleteRegions(connection, regionsInfo, HConstants.LATEST_TIMESTAMP);
1461 }
1462
1463
1464
1465
1466
1467
1468
1469
1470 public static void mutateRegions(Connection connection,
1471 final List<HRegionInfo> regionsToRemove,
1472 final List<HRegionInfo> regionsToAdd)
1473 throws IOException {
1474 List<Mutation> mutation = new ArrayList<Mutation>();
1475 if (regionsToRemove != null) {
1476 for (HRegionInfo hri: regionsToRemove) {
1477 mutation.add(new Delete(hri.getRegionName()));
1478 }
1479 }
1480 if (regionsToAdd != null) {
1481 for (HRegionInfo hri: regionsToAdd) {
1482 mutation.add(makePutFromRegionInfo(hri));
1483 }
1484 }
1485 mutateMetaTable(connection, mutation);
1486 if (regionsToRemove != null && regionsToRemove.size() > 0) {
1487 LOG.debug("Deleted " + regionsToRemove);
1488 }
1489 if (regionsToAdd != null && regionsToAdd.size() > 0) {
1490 LOG.debug("Added " + regionsToAdd);
1491 }
1492 }
1493
1494
1495
1496
1497
1498
1499
1500 public static void overwriteRegions(Connection connection,
1501 List<HRegionInfo> regionInfos, int regionReplication) throws IOException {
1502
1503 long now = EnvironmentEdgeManager.currentTime();
1504 deleteRegions(connection, regionInfos, now);
1505
1506
1507
1508
1509
1510
1511 addRegionsToMeta(connection, regionInfos, regionReplication, now+1);
1512 LOG.info("Overwritten " + regionInfos);
1513 }
1514
1515
1516
1517
1518
1519
1520
1521 public static void deleteMergeQualifiers(Connection connection,
1522 final HRegionInfo mergedRegion) throws IOException {
1523 Delete delete = new Delete(mergedRegion.getRegionName());
1524 delete.deleteColumns(HConstants.CATALOG_FAMILY, HConstants.MERGEA_QUALIFIER);
1525 delete.deleteColumns(HConstants.CATALOG_FAMILY, HConstants.MERGEB_QUALIFIER);
1526 deleteFromMetaTable(connection, delete);
1527 LOG.info("Deleted references in merged region "
1528 + mergedRegion.getRegionNameAsString() + ", qualifier="
1529 + Bytes.toStringBinary(HConstants.MERGEA_QUALIFIER) + " and qualifier="
1530 + Bytes.toStringBinary(HConstants.MERGEB_QUALIFIER));
1531 }
1532
1533 private static Put addRegionInfo(final Put p, final HRegionInfo hri)
1534 throws IOException {
1535 p.addImmutable(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
1536 hri.toByteArray());
1537 return p;
1538 }
1539
1540 public static Put addLocation(final Put p, final ServerName sn, long openSeqNum,
1541 long time, int replicaId){
1542 if (time <= 0) {
1543 time = EnvironmentEdgeManager.currentTime();
1544 }
1545 p.addImmutable(HConstants.CATALOG_FAMILY, getServerColumn(replicaId), time,
1546 Bytes.toBytes(sn.getHostAndPort()));
1547 p.addImmutable(HConstants.CATALOG_FAMILY, getStartCodeColumn(replicaId), time,
1548 Bytes.toBytes(sn.getStartcode()));
1549 p.addImmutable(HConstants.CATALOG_FAMILY, getSeqNumColumn(replicaId), time,
1550 Bytes.toBytes(openSeqNum));
1551 return p;
1552 }
1553
1554 public static Put addEmptyLocation(final Put p, int replicaId) {
1555 long now = EnvironmentEdgeManager.currentTime();
1556 p.addImmutable(HConstants.CATALOG_FAMILY, getServerColumn(replicaId), now, null);
1557 p.addImmutable(HConstants.CATALOG_FAMILY, getStartCodeColumn(replicaId), now, null);
1558 p.addImmutable(HConstants.CATALOG_FAMILY, getSeqNumColumn(replicaId), now, null);
1559 return p;
1560 }
1561 }