1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.client;
21
22 import java.io.IOException;
23 import java.util.Arrays;
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.conf.Configuration;
29 import org.apache.hadoop.hbase.Abortable;
30 import org.apache.hadoop.hbase.ClusterStatus;
31 import org.apache.hadoop.hbase.HBaseConfiguration;
32 import org.apache.hadoop.hbase.HColumnDescriptor;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.HRegionInfo;
35 import org.apache.hadoop.hbase.HRegionLocation;
36 import org.apache.hadoop.hbase.HServerAddress;
37 import org.apache.hadoop.hbase.HTableDescriptor;
38 import org.apache.hadoop.hbase.MasterNotRunningException;
39 import org.apache.hadoop.hbase.NotServingRegionException;
40 import org.apache.hadoop.hbase.RegionException;
41 import org.apache.hadoop.hbase.RemoteExceptionHandler;
42 import org.apache.hadoop.hbase.TableExistsException;
43 import org.apache.hadoop.hbase.UnknownRegionException;
44 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
45 import org.apache.hadoop.hbase.catalog.CatalogTracker;
46 import org.apache.hadoop.hbase.catalog.MetaReader;
47 import org.apache.hadoop.hbase.ipc.HMasterInterface;
48 import org.apache.hadoop.hbase.ipc.HRegionInterface;
49 import org.apache.hadoop.hbase.util.Bytes;
50 import org.apache.hadoop.hbase.util.Pair;
51 import org.apache.hadoop.ipc.RemoteException;
52 import org.apache.hadoop.util.StringUtils;
53
54
55
56
57
58
59
60
61
62
63 public class HBaseAdmin implements Abortable {
64 private final Log LOG = LogFactory.getLog(this.getClass().getName());
65
66 final HConnection connection;
67 private volatile Configuration conf;
68 private final long pause;
69 private final int numRetries;
70
71
72
73 private final int retryLongerMultiplier;
74
75
76
77
78
79
80
81
82 public HBaseAdmin(Configuration conf)
83 throws MasterNotRunningException, ZooKeeperConnectionException {
84 this.connection = HConnectionManager.getConnection(conf);
85 this.conf = conf;
86 this.pause = conf.getLong("hbase.client.pause", 1000);
87 this.numRetries = conf.getInt("hbase.client.retries.number", 10);
88 this.retryLongerMultiplier = conf.getInt("hbase.client.retries.longer.multiplier", 10);
89 this.connection.getMaster();
90 }
91
92
93
94
95
96
97
98
99 private synchronized CatalogTracker getCatalogTracker()
100 throws ZooKeeperConnectionException, IOException {
101 CatalogTracker ct = null;
102 try {
103 HConnection connection =
104 HConnectionManager.getConnection(new Configuration(this.conf));
105 ct = new CatalogTracker(connection);
106 ct.start();
107 } catch (InterruptedException e) {
108
109 Thread.currentThread().interrupt();
110 throw new IOException("Interrupted", e);
111 }
112 return ct;
113 }
114
115 private void cleanupCatalogTracker(final CatalogTracker ct) {
116 ct.stop();
117 HConnectionManager.deleteConnection(ct.getConnection().getConfiguration(), true);
118 }
119
120 @Override
121 public void abort(String why, Throwable e) {
122
123 throw new RuntimeException(why, e);
124 }
125
126
127 public HConnection getConnection() {
128 return connection;
129 }
130
131
132
133
134
135
136
137 public HMasterInterface getMaster()
138 throws MasterNotRunningException, ZooKeeperConnectionException {
139 return this.connection.getMaster();
140 }
141
142
143
144
145 public boolean isMasterRunning()
146 throws MasterNotRunningException, ZooKeeperConnectionException {
147 return this.connection.isMasterRunning();
148 }
149
150
151
152
153
154
155 public boolean tableExists(final String tableName)
156 throws IOException {
157 boolean b = false;
158 CatalogTracker ct = getCatalogTracker();
159 try {
160 b = MetaReader.tableExists(ct, tableName);
161 } finally {
162 cleanupCatalogTracker(ct);
163 }
164 return b;
165 }
166
167
168
169
170
171
172 public boolean tableExists(final byte [] tableName)
173 throws IOException {
174 return tableExists(Bytes.toString(tableName));
175 }
176
177
178
179
180
181
182
183
184
185
186
187 public HTableDescriptor[] listTables() throws IOException {
188 return this.connection.listTables();
189 }
190
191
192
193
194
195
196
197
198 public HTableDescriptor getTableDescriptor(final byte [] tableName)
199 throws IOException {
200 return this.connection.getHTableDescriptor(tableName);
201 }
202
203 private long getPauseTime(int tries) {
204 int triesCount = tries;
205 if (triesCount >= HConstants.RETRY_BACKOFF.length) {
206 triesCount = HConstants.RETRY_BACKOFF.length - 1;
207 }
208 return this.pause * HConstants.RETRY_BACKOFF[triesCount];
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 public void createTable(HTableDescriptor desc)
225 throws IOException {
226 createTable(desc, null);
227 }
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253 public void createTable(HTableDescriptor desc, byte [] startKey,
254 byte [] endKey, int numRegions)
255 throws IOException {
256 HTableDescriptor.isLegalTableName(desc.getName());
257 if(numRegions < 3) {
258 throw new IllegalArgumentException("Must create at least three regions");
259 } else if(Bytes.compareTo(startKey, endKey) >= 0) {
260 throw new IllegalArgumentException("Start key must be smaller than end key");
261 }
262 byte [][] splitKeys = Bytes.split(startKey, endKey, numRegions - 3);
263 if(splitKeys == null || splitKeys.length != numRegions - 1) {
264 throw new IllegalArgumentException("Unable to split key range into enough regions");
265 }
266 createTable(desc, splitKeys);
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286 public void createTable(HTableDescriptor desc, byte [][] splitKeys)
287 throws IOException {
288 HTableDescriptor.isLegalTableName(desc.getName());
289 if(splitKeys != null && splitKeys.length > 1) {
290 Arrays.sort(splitKeys, Bytes.BYTES_COMPARATOR);
291
292 byte [] lastKey = null;
293 for(byte [] splitKey : splitKeys) {
294 if(lastKey != null && Bytes.equals(splitKey, lastKey)) {
295 throw new IllegalArgumentException("All split keys must be unique, " +
296 "found duplicate: " + Bytes.toStringBinary(splitKey) +
297 ", " + Bytes.toStringBinary(lastKey));
298 }
299 lastKey = splitKey;
300 }
301 }
302 createTableAsync(desc, splitKeys);
303 for (int tries = 0; tries < numRetries; tries++) {
304 try {
305
306 connection.locateRegion(desc.getName(), HConstants.EMPTY_START_ROW);
307 break;
308
309 } catch (RegionException e) {
310 if (tries == numRetries - 1) {
311
312 throw e;
313 }
314 }
315 try {
316 Thread.sleep(getPauseTime(tries));
317 } catch (InterruptedException e) {
318
319 }
320 }
321 }
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336 public void createTableAsync(HTableDescriptor desc, byte [][] splitKeys)
337 throws IOException {
338 HTableDescriptor.isLegalTableName(desc.getName());
339 try {
340 getMaster().createTable(desc, splitKeys);
341 } catch (RemoteException e) {
342 throw e.unwrapRemoteException();
343 }
344 }
345
346
347
348
349
350
351
352
353 public void deleteTable(final String tableName) throws IOException {
354 deleteTable(Bytes.toBytes(tableName));
355 }
356
357
358
359
360
361
362
363
364 public void deleteTable(final byte [] tableName) throws IOException {
365 isMasterRunning();
366 HTableDescriptor.isLegalTableName(tableName);
367 HRegionLocation firstMetaServer = getFirstMetaServerForTable(tableName);
368 try {
369 getMaster().deleteTable(tableName);
370 } catch (RemoteException e) {
371 throw RemoteExceptionHandler.decodeRemoteException(e);
372 }
373
374 HRegionInterface server =
375 connection.getHRegionConnection(firstMetaServer.getServerAddress());
376 for (int tries = 0; tries < (this.numRetries * this.retryLongerMultiplier); tries++) {
377 long scannerId = -1L;
378 try {
379
380 Scan scan = MetaReader.getScanForTableName(tableName);
381 scan.addColumn(HConstants.CATALOG_FAMILY,
382 HConstants.REGIONINFO_QUALIFIER);
383 scannerId = server.openScanner(
384 firstMetaServer.getRegionInfo().getRegionName(), scan);
385
386 Result values = server.next(scannerId);
387 if (values == null) {
388 break;
389 }
390 } catch (IOException ex) {
391 if(tries == numRetries - 1) {
392 if (ex instanceof RemoteException) {
393 ex = RemoteExceptionHandler.decodeRemoteException((RemoteException) ex);
394 }
395 throw ex;
396 }
397 } finally {
398 if (scannerId != -1L) {
399 try {
400 server.close(scannerId);
401 } catch (Exception ex) {
402 LOG.warn(ex);
403 }
404 }
405 }
406 try {
407 Thread.sleep(getPauseTime(tries));
408 } catch (InterruptedException e) {
409
410 }
411 }
412
413 this.connection.clearRegionCache(tableName);
414 LOG.info("Deleted " + Bytes.toString(tableName));
415 }
416
417 public void enableTable(final String tableName)
418 throws IOException {
419 enableTable(Bytes.toBytes(tableName));
420 }
421
422
423
424
425
426
427
428
429
430
431 public void enableTable(final byte [] tableName)
432 throws IOException {
433 enableTableAsync(tableName);
434
435
436 boolean enabled = false;
437 for (int tries = 0; tries < (this.numRetries * this.retryLongerMultiplier); tries++) {
438 enabled = isTableEnabled(tableName);
439 if (enabled) {
440 break;
441 }
442 long sleep = getPauseTime(tries);
443 if (LOG.isDebugEnabled()) {
444 LOG.debug("Sleeping= " + sleep + "ms, waiting for all regions to be " +
445 "enabled in " + Bytes.toString(tableName));
446 }
447 try {
448 Thread.sleep(sleep);
449 } catch (InterruptedException e) {
450 Thread.currentThread().interrupt();
451
452
453 throw new IOException("Interrupted", e);
454 }
455 }
456 if (!enabled) {
457 throw new IOException("Unable to enable table " +
458 Bytes.toString(tableName));
459 }
460 LOG.info("Enabled table " + Bytes.toString(tableName));
461 }
462
463 public void enableTableAsync(final String tableName)
464 throws IOException {
465 enableTableAsync(Bytes.toBytes(tableName));
466 }
467
468
469
470
471
472
473
474
475
476
477
478 public void enableTableAsync(final byte [] tableName)
479 throws IOException {
480 isMasterRunning();
481 try {
482 getMaster().enableTable(tableName);
483 } catch (RemoteException e) {
484 throw e.unwrapRemoteException();
485 }
486 LOG.info("Started enable of " + Bytes.toString(tableName));
487 }
488
489 public void disableTableAsync(final String tableName) throws IOException {
490 disableTableAsync(Bytes.toBytes(tableName));
491 }
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506 public void disableTableAsync(final byte [] tableName) throws IOException {
507 isMasterRunning();
508 try {
509 getMaster().disableTable(tableName);
510 } catch (RemoteException e) {
511 throw e.unwrapRemoteException();
512 }
513 LOG.info("Started disable of " + Bytes.toString(tableName));
514 }
515
516 public void disableTable(final String tableName)
517 throws IOException {
518 disableTable(Bytes.toBytes(tableName));
519 }
520
521
522
523
524
525
526
527
528 public void disableTable(final byte [] tableName)
529 throws IOException {
530 disableTableAsync(tableName);
531
532 boolean disabled = false;
533 for (int tries = 0; tries < (this.numRetries * this.retryLongerMultiplier); tries++) {
534 disabled = isTableDisabled(tableName);
535 if (disabled) {
536 break;
537 }
538 long sleep = getPauseTime(tries);
539 if (LOG.isDebugEnabled()) {
540 LOG.debug("Sleeping= " + sleep + "ms, waiting for all regions to be " +
541 "disabled in " + Bytes.toString(tableName));
542 }
543 try {
544 Thread.sleep(sleep);
545 } catch (InterruptedException e) {
546
547
548 Thread.currentThread().interrupt();
549 throw new IOException("Interrupted", e);
550 }
551 }
552 if (!disabled) {
553 throw new RegionException("Retries exhausted, it took too long to wait"+
554 " for the table " + Bytes.toString(tableName) + " to be disabled.");
555 }
556 LOG.info("Disabled " + Bytes.toString(tableName));
557 }
558
559
560
561
562
563
564 public boolean isTableEnabled(String tableName) throws IOException {
565 return isTableEnabled(Bytes.toBytes(tableName));
566 }
567
568
569
570
571
572 public boolean isTableEnabled(byte[] tableName) throws IOException {
573 return connection.isTableEnabled(tableName);
574 }
575
576
577
578
579
580
581 public boolean isTableDisabled(final String tableName) throws IOException {
582 return isTableDisabled(Bytes.toBytes(tableName));
583 }
584
585
586
587
588
589
590 public boolean isTableDisabled(byte[] tableName) throws IOException {
591 return connection.isTableDisabled(tableName);
592 }
593
594
595
596
597
598
599 public boolean isTableAvailable(byte[] tableName) throws IOException {
600 return connection.isTableAvailable(tableName);
601 }
602
603
604
605
606
607
608 public boolean isTableAvailable(String tableName) throws IOException {
609 return connection.isTableAvailable(Bytes.toBytes(tableName));
610 }
611
612
613
614
615
616
617
618
619
620 public void addColumn(final String tableName, HColumnDescriptor column)
621 throws IOException {
622 addColumn(Bytes.toBytes(tableName), column);
623 }
624
625
626
627
628
629
630
631
632
633 public void addColumn(final byte [] tableName, HColumnDescriptor column)
634 throws IOException {
635 HTableDescriptor.isLegalTableName(tableName);
636 try {
637 getMaster().addColumn(tableName, column);
638 } catch (RemoteException e) {
639 throw RemoteExceptionHandler.decodeRemoteException(e);
640 }
641 }
642
643
644
645
646
647
648
649
650
651 public void deleteColumn(final String tableName, final String columnName)
652 throws IOException {
653 deleteColumn(Bytes.toBytes(tableName), Bytes.toBytes(columnName));
654 }
655
656
657
658
659
660
661
662
663
664 public void deleteColumn(final byte [] tableName, final byte [] columnName)
665 throws IOException {
666 try {
667 getMaster().deleteColumn(tableName, columnName);
668 } catch (RemoteException e) {
669 throw RemoteExceptionHandler.decodeRemoteException(e);
670 }
671 }
672
673
674
675
676
677
678
679
680
681
682
683 public void modifyColumn(final String tableName, final String columnName,
684 HColumnDescriptor descriptor)
685 throws IOException {
686 modifyColumn(tableName, descriptor);
687 }
688
689
690
691
692
693
694
695
696
697 public void modifyColumn(final String tableName, HColumnDescriptor descriptor)
698 throws IOException {
699 modifyColumn(Bytes.toBytes(tableName), descriptor);
700 }
701
702
703
704
705
706
707
708
709
710
711
712 public void modifyColumn(final byte [] tableName, final byte [] columnName,
713 HColumnDescriptor descriptor)
714 throws IOException {
715 modifyColumn(tableName, descriptor);
716 }
717
718
719
720
721
722
723
724
725
726 public void modifyColumn(final byte [] tableName, HColumnDescriptor descriptor)
727 throws IOException {
728 try {
729 getMaster().modifyColumn(tableName, descriptor);
730 } catch (RemoteException re) {
731
732
733
734 throw RemoteExceptionHandler.decodeRemoteException(re);
735 }
736 }
737
738
739
740
741
742
743
744
745
746 public void closeRegion(final String regionname, final String hostAndPort)
747 throws IOException {
748 closeRegion(Bytes.toBytes(regionname), hostAndPort);
749 }
750
751
752
753
754
755
756
757
758
759 public void closeRegion(final byte [] regionname, final String hostAndPort)
760 throws IOException {
761 CatalogTracker ct = getCatalogTracker();
762 try {
763 if (hostAndPort != null) {
764 HServerAddress hsa = new HServerAddress(hostAndPort);
765 Pair<HRegionInfo, HServerAddress> pair =
766 MetaReader.getRegion(ct, regionname);
767 if (pair == null || pair.getSecond() == null) {
768 LOG.info("No server in .META. for " +
769 Bytes.toString(regionname) + "; pair=" + pair);
770 } else {
771 closeRegion(hsa, pair.getFirst());
772 }
773 } else {
774 Pair<HRegionInfo, HServerAddress> pair =
775 MetaReader.getRegion(ct, regionname);
776 if (pair == null || pair.getSecond() == null) {
777 LOG.info("No server in .META. for " +
778 Bytes.toString(regionname) + "; pair=" + pair);
779 } else {
780 closeRegion(pair.getSecond(), pair.getFirst());
781 }
782 }
783 } finally {
784 cleanupCatalogTracker(ct);
785 }
786 }
787
788 private void closeRegion(final HServerAddress hsa, final HRegionInfo hri)
789 throws IOException {
790 HRegionInterface rs = this.connection.getHRegionConnection(hsa);
791
792 rs.closeRegion(hri, false);
793 }
794
795
796
797
798
799
800
801
802
803 public void flush(final String tableNameOrRegionName)
804 throws IOException, InterruptedException {
805 flush(Bytes.toBytes(tableNameOrRegionName));
806 }
807
808
809
810
811
812
813
814
815
816 public void flush(final byte [] tableNameOrRegionName)
817 throws IOException, InterruptedException {
818 boolean isRegionName = isRegionName(tableNameOrRegionName);
819 CatalogTracker ct = getCatalogTracker();
820 try {
821 if (isRegionName) {
822 Pair<HRegionInfo, HServerAddress> pair =
823 MetaReader.getRegion(getCatalogTracker(), tableNameOrRegionName);
824 if (pair == null || pair.getSecond() == null) {
825 LOG.info("No server in .META. for " +
826 Bytes.toString(tableNameOrRegionName) + "; pair=" + pair);
827 } else {
828 flush(pair.getSecond(), pair.getFirst());
829 }
830 } else {
831 List<Pair<HRegionInfo, HServerAddress>> pairs =
832 MetaReader.getTableRegionsAndLocations(getCatalogTracker(),
833 Bytes.toString(tableNameOrRegionName));
834 for (Pair<HRegionInfo, HServerAddress> pair: pairs) {
835 if (pair.getFirst().isOffline()) continue;
836 if (pair.getSecond() == null) continue;
837 try {
838 flush(pair.getSecond(), pair.getFirst());
839 } catch (NotServingRegionException e) {
840 if (LOG.isDebugEnabled()) {
841 LOG.debug("Trying to flush " + pair.getFirst() + ": " +
842 StringUtils.stringifyException(e));
843 }
844 }
845 }
846 }
847 } finally {
848 cleanupCatalogTracker(ct);
849 }
850 }
851
852 private void flush(final HServerAddress hsa, final HRegionInfo hri)
853 throws IOException {
854 HRegionInterface rs = this.connection.getHRegionConnection(hsa);
855 rs.flushRegion(hri);
856 }
857
858
859
860
861
862
863
864
865
866 public void compact(final String tableNameOrRegionName)
867 throws IOException, InterruptedException {
868 compact(Bytes.toBytes(tableNameOrRegionName));
869 }
870
871
872
873
874
875
876
877
878
879 public void compact(final byte [] tableNameOrRegionName)
880 throws IOException, InterruptedException {
881 compact(tableNameOrRegionName, false);
882 }
883
884
885
886
887
888
889
890
891
892 public void majorCompact(final String tableNameOrRegionName)
893 throws IOException, InterruptedException {
894 majorCompact(Bytes.toBytes(tableNameOrRegionName));
895 }
896
897
898
899
900
901
902
903
904
905 public void majorCompact(final byte [] tableNameOrRegionName)
906 throws IOException, InterruptedException {
907 compact(tableNameOrRegionName, true);
908 }
909
910
911
912
913
914
915
916
917
918
919 private void compact(final byte [] tableNameOrRegionName, final boolean major)
920 throws IOException, InterruptedException {
921 CatalogTracker ct = getCatalogTracker();
922 try {
923 if (isRegionName(tableNameOrRegionName)) {
924 Pair<HRegionInfo, HServerAddress> pair =
925 MetaReader.getRegion(ct, tableNameOrRegionName);
926 if (pair == null || pair.getSecond() == null) {
927 LOG.info("No server in .META. for " +
928 Bytes.toString(tableNameOrRegionName) + "; pair=" + pair);
929 } else {
930 compact(pair.getSecond(), pair.getFirst(), major);
931 }
932 } else {
933 List<Pair<HRegionInfo, HServerAddress>> pairs =
934 MetaReader.getTableRegionsAndLocations(ct,
935 Bytes.toString(tableNameOrRegionName));
936 for (Pair<HRegionInfo, HServerAddress> pair: pairs) {
937 if (pair.getFirst().isOffline()) continue;
938 if (pair.getSecond() == null) continue;
939 try {
940 compact(pair.getSecond(), pair.getFirst(), major);
941 } catch (NotServingRegionException e) {
942 if (LOG.isDebugEnabled()) {
943 LOG.debug("Trying to" + (major ? " major" : "") + " compact " +
944 pair.getFirst() + ": " +
945 StringUtils.stringifyException(e));
946 }
947 }
948 }
949 }
950 } finally {
951 cleanupCatalogTracker(ct);
952 }
953 }
954
955 private void compact(final HServerAddress hsa, final HRegionInfo hri,
956 final boolean major)
957 throws IOException {
958 HRegionInterface rs = this.connection.getHRegionConnection(hsa);
959 rs.compactRegion(hri, major);
960 }
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977 public void move(final byte [] encodedRegionName, final byte [] destServerName)
978 throws UnknownRegionException, MasterNotRunningException, ZooKeeperConnectionException {
979 getMaster().move(encodedRegionName, destServerName);
980 }
981
982
983
984
985
986
987
988
989 public void assign(final byte [] regionName, final boolean force)
990 throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
991 getMaster().assign(regionName, force);
992 }
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007 public void unassign(final byte [] regionName, final boolean force)
1008 throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
1009 getMaster().unassign(regionName, force);
1010 }
1011
1012
1013
1014
1015
1016
1017 public boolean balanceSwitch(final boolean b)
1018 throws MasterNotRunningException, ZooKeeperConnectionException {
1019 return getMaster().balanceSwitch(b);
1020 }
1021
1022
1023
1024
1025
1026
1027
1028 public boolean balancer()
1029 throws MasterNotRunningException, ZooKeeperConnectionException {
1030 return getMaster().balance();
1031 }
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041 public void split(final String tableNameOrRegionName)
1042 throws IOException, InterruptedException {
1043 split(Bytes.toBytes(tableNameOrRegionName));
1044 }
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054 public void split(final byte [] tableNameOrRegionName)
1055 throws IOException, InterruptedException {
1056 split(tableNameOrRegionName, null);
1057 }
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068 public void split(final byte [] tableNameOrRegionName,
1069 final byte [] splitPoint) throws IOException, InterruptedException {
1070 CatalogTracker ct = getCatalogTracker();
1071 try {
1072 if (isRegionName(tableNameOrRegionName)) {
1073
1074 Pair<HRegionInfo, HServerAddress> pair =
1075 MetaReader.getRegion(getCatalogTracker(), tableNameOrRegionName);
1076 if (pair == null || pair.getSecond() == null) {
1077 LOG.info("No server in .META. for " +
1078 Bytes.toString(tableNameOrRegionName) + "; pair=" + pair);
1079 } else {
1080 split(pair.getSecond(), pair.getFirst(), splitPoint);
1081 }
1082 } else {
1083 List<Pair<HRegionInfo, HServerAddress>> pairs =
1084 MetaReader.getTableRegionsAndLocations(getCatalogTracker(),
1085 Bytes.toString(tableNameOrRegionName));
1086 for (Pair<HRegionInfo, HServerAddress> pair: pairs) {
1087
1088 if (pair.getSecond() == null) continue;
1089 HRegionInfo r = pair.getFirst();
1090
1091 if (r.isSplitParent()) continue;
1092 if (splitPoint != null) {
1093
1094 if (!r.containsRow(splitPoint)) continue;
1095 }
1096 split(pair.getSecond(), pair.getFirst(), splitPoint);
1097 }
1098 }
1099 } finally {
1100 cleanupCatalogTracker(ct);
1101 }
1102 }
1103
1104 private void split(final HServerAddress hsa, final HRegionInfo hri,
1105 byte[] splitPoint) throws IOException {
1106 HRegionInterface rs = this.connection.getHRegionConnection(hsa);
1107 rs.splitRegion(hri, splitPoint);
1108 }
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119 public void modifyTable(final byte [] tableName, HTableDescriptor htd)
1120 throws IOException {
1121 try {
1122 getMaster().modifyTable(tableName, htd);
1123 } catch (RemoteException re) {
1124
1125
1126
1127 throw RemoteExceptionHandler.decodeRemoteException(re);
1128 }
1129 }
1130
1131
1132
1133
1134
1135
1136
1137
1138 private boolean isRegionName(final byte [] tableNameOrRegionName)
1139 throws IOException {
1140 if (tableNameOrRegionName == null) {
1141 throw new IllegalArgumentException("Pass a table name or region name");
1142 }
1143 return !tableExists(tableNameOrRegionName);
1144 }
1145
1146
1147
1148
1149
1150 public synchronized void shutdown() throws IOException {
1151 isMasterRunning();
1152 try {
1153 getMaster().shutdown();
1154 } catch (RemoteException e) {
1155 throw RemoteExceptionHandler.decodeRemoteException(e);
1156 }
1157 }
1158
1159
1160
1161
1162
1163
1164
1165 public synchronized void stopMaster() throws IOException {
1166 isMasterRunning();
1167 try {
1168 getMaster().stopMaster();
1169 } catch (RemoteException e) {
1170 throw RemoteExceptionHandler.decodeRemoteException(e);
1171 }
1172 }
1173
1174
1175
1176
1177
1178 public synchronized void stopRegionServer(final HServerAddress hsa)
1179 throws IOException {
1180 HRegionInterface rs = this.connection.getHRegionConnection(hsa);
1181 rs.stop("Called by admin client " + this.connection.toString());
1182 }
1183
1184
1185
1186
1187
1188 public ClusterStatus getClusterStatus() throws IOException {
1189 return getMaster().getClusterStatus();
1190 }
1191
1192 private HRegionLocation getFirstMetaServerForTable(final byte [] tableName)
1193 throws IOException {
1194 return connection.locateRegion(HConstants.META_TABLE_NAME,
1195 HRegionInfo.createRegionName(tableName, null, HConstants.NINES, false));
1196 }
1197
1198
1199
1200
1201 public Configuration getConfiguration() {
1202 return this.conf;
1203 }
1204
1205
1206
1207
1208
1209
1210
1211
1212 public static void checkHBaseAvailable(Configuration conf)
1213 throws MasterNotRunningException, ZooKeeperConnectionException {
1214 Configuration copyOfConf = HBaseConfiguration.create(conf);
1215 copyOfConf.setInt("hbase.client.retries.number", 1);
1216 new HBaseAdmin(copyOfConf);
1217 }
1218 }