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