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