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