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.Closeable;
23 import java.io.IOException;
24 import java.io.InterruptedIOException;
25 import java.lang.reflect.Proxy;
26 import java.lang.reflect.UndeclaredThrowableException;
27 import java.net.SocketTimeoutException;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.concurrent.atomic.AtomicInteger;
33 import java.util.concurrent.atomic.AtomicReference;
34 import java.util.regex.Pattern;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.apache.hadoop.conf.Configuration;
39 import org.apache.hadoop.hbase.Abortable;
40 import org.apache.hadoop.hbase.ClusterStatus;
41 import org.apache.hadoop.hbase.HBaseConfiguration;
42 import org.apache.hadoop.hbase.HColumnDescriptor;
43 import org.apache.hadoop.hbase.HConstants;
44 import org.apache.hadoop.hbase.HRegionInfo;
45 import org.apache.hadoop.hbase.HRegionLocation;
46 import org.apache.hadoop.hbase.HTableDescriptor;
47 import org.apache.hadoop.hbase.MasterNotRunningException;
48 import org.apache.hadoop.hbase.NotServingRegionException;
49 import org.apache.hadoop.hbase.RegionException;
50 import org.apache.hadoop.hbase.RemoteExceptionHandler;
51 import org.apache.hadoop.hbase.ServerName;
52 import org.apache.hadoop.hbase.TableExistsException;
53 import org.apache.hadoop.hbase.TableNotEnabledException;
54 import org.apache.hadoop.hbase.TableNotFoundException;
55 import org.apache.hadoop.hbase.UnknownRegionException;
56 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
57 import org.apache.hadoop.hbase.catalog.CatalogTracker;
58 import org.apache.hadoop.hbase.catalog.MetaReader;
59 import org.apache.hadoop.hbase.client.MetaScanner.MetaScannerVisitor;
60 import org.apache.hadoop.hbase.client.MetaScanner.MetaScannerVisitorBase;
61 import org.apache.hadoop.hbase.ipc.CoprocessorProtocol;
62 import org.apache.hadoop.hbase.ipc.HMasterInterface;
63 import org.apache.hadoop.hbase.ipc.HRegionInterface;
64 import org.apache.hadoop.hbase.ipc.MasterExecRPCInvoker;
65 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
66 import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequest.CompactionState;
67 import org.apache.hadoop.hbase.regionserver.wal.FailedLogCloseException;
68 import org.apache.hadoop.hbase.snapshot.HBaseSnapshotException;
69 import org.apache.hadoop.hbase.snapshot.HSnapshotDescription;
70 import org.apache.hadoop.hbase.snapshot.RestoreSnapshotException;
71 import org.apache.hadoop.hbase.snapshot.SnapshotCreationException;
72 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
73 import org.apache.hadoop.hbase.snapshot.UnknownSnapshotException;
74 import org.apache.hadoop.hbase.util.Addressing;
75 import org.apache.hadoop.hbase.util.Bytes;
76 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
77 import org.apache.hadoop.hbase.util.Pair;
78 import org.apache.hadoop.ipc.RemoteException;
79 import org.apache.hadoop.util.StringUtils;
80
81
82
83
84
85
86
87
88
89
90 public class HBaseAdmin implements Abortable, Closeable {
91 private final Log LOG = LogFactory.getLog(this.getClass().getName());
92
93 private HConnection connection;
94 private volatile Configuration conf;
95 private final long pause;
96 private final int numRetries;
97
98
99
100 private final int retryLongerMultiplier;
101 private boolean aborted;
102
103 private static volatile boolean synchronousBalanceSwitchSupported = true;
104 private final boolean cleanupConnectionOnClose;
105
106
107
108
109
110
111
112
113 public HBaseAdmin(Configuration c)
114 throws MasterNotRunningException, ZooKeeperConnectionException {
115 this.conf = HBaseConfiguration.create(c);
116 this.connection = HConnectionManager.getConnection(this.conf);
117 this.pause = this.conf.getLong("hbase.client.pause", 1000);
118 this.numRetries = this.conf.getInt("hbase.client.retries.number", 10);
119 this.retryLongerMultiplier = this.conf.getInt(
120 "hbase.client.retries.longer.multiplier", 10);
121 this.cleanupConnectionOnClose = true;
122
123 int tries = 0;
124 while ( true ){
125 try {
126
127 this.connection.getMaster();
128 return;
129
130 } catch (MasterNotRunningException mnre) {
131 HConnectionManager.deleteStaleConnection(this.connection);
132 this.connection = HConnectionManager.getConnection(this.conf);
133 }
134
135 tries++;
136 if (tries >= numRetries) {
137
138 HConnectionManager.deleteStaleConnection(this.connection);
139 throw new MasterNotRunningException("Retried " + numRetries + " times");
140 }
141
142 try {
143 Thread.sleep(getPauseTime(tries));
144 } catch (InterruptedException e) {
145 Thread.currentThread().interrupt();
146
147 HConnectionManager.deleteStaleConnection(this.connection);
148 throw new MasterNotRunningException(
149 "Interrupted after "+tries+" tries");
150 }
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163
164 public HBaseAdmin(HConnection connection)
165 throws MasterNotRunningException, ZooKeeperConnectionException {
166 this.conf = connection.getConfiguration();
167 this.connection = connection;
168 this.cleanupConnectionOnClose = false;
169
170 this.pause = this.conf.getLong("hbase.client.pause", 1000);
171 this.numRetries = this.conf.getInt("hbase.client.retries.number", 10);
172 this.retryLongerMultiplier = this.conf.getInt(
173 "hbase.client.retries.longer.multiplier", 10);
174
175 this.connection.getMaster();
176 }
177
178
179
180
181
182
183
184
185 private synchronized CatalogTracker getCatalogTracker()
186 throws ZooKeeperConnectionException, IOException {
187 CatalogTracker ct = null;
188 try {
189 ct = new CatalogTracker(this.conf);
190 ct.start();
191 } catch (InterruptedException e) {
192
193 Thread.currentThread().interrupt();
194 throw new IOException("Interrupted", e);
195 }
196 return ct;
197 }
198
199 private void cleanupCatalogTracker(final CatalogTracker ct) {
200 ct.stop();
201 }
202
203 @Override
204 public void abort(String why, Throwable e) {
205
206 this.aborted = true;
207 throw new RuntimeException(why, e);
208 }
209
210 @Override
211 public boolean isAborted(){
212 return this.aborted;
213 }
214
215
216 public HConnection getConnection() {
217 return connection;
218 }
219
220
221
222
223
224
225
226
227
228 @Deprecated
229 public HMasterInterface getMaster()
230 throws MasterNotRunningException, ZooKeeperConnectionException {
231 return this.connection.getMaster();
232 }
233
234
235
236
237 public boolean isMasterRunning()
238 throws MasterNotRunningException, ZooKeeperConnectionException {
239 return this.connection.isMasterRunning();
240 }
241
242
243
244
245
246
247 public boolean tableExists(final String tableName)
248 throws IOException {
249 boolean b = false;
250 CatalogTracker ct = getCatalogTracker();
251 try {
252 b = MetaReader.tableExists(ct, tableName);
253 } finally {
254 cleanupCatalogTracker(ct);
255 }
256 return b;
257 }
258
259
260
261
262
263
264 public boolean tableExists(final byte [] tableName)
265 throws IOException {
266 return tableExists(Bytes.toString(tableName));
267 }
268
269
270
271
272
273
274
275
276
277
278
279 public HTableDescriptor[] listTables() throws IOException {
280 return this.connection.listTables();
281 }
282
283
284
285
286
287
288
289
290
291 public HTableDescriptor[] listTables(Pattern pattern) throws IOException {
292 List<HTableDescriptor> matched = new LinkedList<HTableDescriptor>();
293 HTableDescriptor[] tables = listTables();
294 for (HTableDescriptor table : tables) {
295 if (pattern.matcher(table.getNameAsString()).matches()) {
296 matched.add(table);
297 }
298 }
299 return matched.toArray(new HTableDescriptor[matched.size()]);
300 }
301
302
303
304
305
306
307
308
309
310 public HTableDescriptor[] listTables(String regex) throws IOException {
311 return listTables(Pattern.compile(regex));
312 }
313
314
315
316
317
318
319 public String[] getTableNames() throws IOException {
320 return this.connection.getTableNames();
321 }
322
323
324
325
326
327
328
329 public String[] getTableNames(Pattern pattern) throws IOException {
330 List<String> matched = new ArrayList<String>();
331 for (String name : this.connection.getTableNames()) {
332 if (pattern.matcher(name).matches()) {
333 matched.add(name);
334 }
335 }
336 return matched.toArray(new String[matched.size()]);
337 }
338
339
340
341
342
343
344
345 public String[] getTableNames(String regex) throws IOException {
346 return getTableNames(Pattern.compile(regex));
347 }
348
349
350
351
352
353
354
355
356 public HTableDescriptor getTableDescriptor(final byte [] tableName)
357 throws TableNotFoundException, IOException {
358 return this.connection.getHTableDescriptor(tableName);
359 }
360
361 private long getPauseTime(int tries) {
362 int triesCount = tries;
363 if (triesCount >= HConstants.RETRY_BACKOFF.length) {
364 triesCount = HConstants.RETRY_BACKOFF.length - 1;
365 }
366 return this.pause * HConstants.RETRY_BACKOFF[triesCount];
367 }
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382 public void createTable(HTableDescriptor desc)
383 throws IOException {
384 createTable(desc, null);
385 }
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411 public void createTable(HTableDescriptor desc, byte [] startKey,
412 byte [] endKey, int numRegions)
413 throws IOException {
414 HTableDescriptor.isLegalTableName(desc.getName());
415 if(numRegions < 3) {
416 throw new IllegalArgumentException("Must create at least three regions");
417 } else if(Bytes.compareTo(startKey, endKey) >= 0) {
418 throw new IllegalArgumentException("Start key must be smaller than end key");
419 }
420 if (numRegions == 3) {
421 createTable(desc, new byte[][] { startKey, endKey });
422 return;
423 }
424 byte [][] splitKeys = Bytes.split(startKey, endKey, numRegions - 3);
425 if(splitKeys == null || splitKeys.length != numRegions - 1) {
426 throw new IllegalArgumentException("Unable to split key range into enough regions");
427 }
428 createTable(desc, splitKeys);
429 }
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448 public void createTable(final HTableDescriptor desc, byte [][] splitKeys)
449 throws IOException {
450 HTableDescriptor.isLegalTableName(desc.getName());
451 try {
452 createTableAsync(desc, splitKeys);
453 } catch (SocketTimeoutException ste) {
454 LOG.warn("Creating " + desc.getNameAsString() + " took too long", ste);
455 }
456 int numRegs = splitKeys == null ? 1 : splitKeys.length + 1;
457 int prevRegCount = 0;
458 boolean doneWithMetaScan = false;
459 for (int tries = 0; tries < this.numRetries * this.retryLongerMultiplier;
460 ++tries) {
461 if (!doneWithMetaScan) {
462
463 final AtomicInteger actualRegCount = new AtomicInteger(0);
464 MetaScannerVisitor visitor = new MetaScannerVisitorBase() {
465 @Override
466 public boolean processRow(Result rowResult) throws IOException {
467 if (rowResult == null || rowResult.size() <= 0) {
468 return true;
469 }
470 HRegionInfo info = MetaReader.parseHRegionInfoFromCatalogResult(
471 rowResult, HConstants.REGIONINFO_QUALIFIER);
472 if (info == null) {
473 LOG.warn("No serialized HRegionInfo in " + rowResult);
474 return true;
475 }
476 if (!(Bytes.equals(info.getTableName(), desc.getName()))) {
477 return false;
478 }
479 String hostAndPort = null;
480 byte [] value = rowResult.getValue(HConstants.CATALOG_FAMILY,
481 HConstants.SERVER_QUALIFIER);
482
483 if (value != null && value.length > 0) {
484 hostAndPort = Bytes.toString(value);
485 }
486 if (!(info.isOffline() || info.isSplit()) && hostAndPort != null) {
487 actualRegCount.incrementAndGet();
488 }
489 return true;
490 }
491 };
492 MetaScanner.metaScan(conf, connection, visitor, desc.getName());
493 if (actualRegCount.get() != numRegs) {
494 if (tries == this.numRetries * this.retryLongerMultiplier - 1) {
495 throw new RegionOfflineException("Only " + actualRegCount.get() +
496 " of " + numRegs + " regions are online; retries exhausted.");
497 }
498 try {
499 Thread.sleep(getPauseTime(tries));
500 } catch (InterruptedException e) {
501 throw new InterruptedIOException("Interrupted when opening" +
502 " regions; " + actualRegCount.get() + " of " + numRegs +
503 " regions processed so far");
504 }
505 if (actualRegCount.get() > prevRegCount) {
506 prevRegCount = actualRegCount.get();
507 tries = -1;
508 }
509 } else {
510 doneWithMetaScan = true;
511 tries = -1;
512 }
513 } else if (isTableEnabled(desc.getName())) {
514 return;
515 } else {
516 try {
517 Thread.sleep(getPauseTime(tries));
518 } catch (InterruptedException e) {
519 throw new InterruptedIOException("Interrupted when waiting" +
520 " for table to be enabled; meta scan was done");
521 }
522 }
523 }
524 throw new TableNotEnabledException(
525 "Retries exhausted while still waiting for table: "
526 + desc.getNameAsString() + " to be enabled");
527 }
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545 public void createTableAsync(HTableDescriptor desc, byte [][] splitKeys)
546 throws IOException {
547 HTableDescriptor.isLegalTableName(desc.getName());
548 if(splitKeys != null && splitKeys.length > 0) {
549 Arrays.sort(splitKeys, Bytes.BYTES_COMPARATOR);
550
551 byte [] lastKey = null;
552 for(byte [] splitKey : splitKeys) {
553 if (Bytes.compareTo(splitKey, HConstants.EMPTY_BYTE_ARRAY) == 0) {
554 throw new IllegalArgumentException(
555 "Empty split key must not be passed in the split keys.");
556 }
557 if(lastKey != null && Bytes.equals(splitKey, lastKey)) {
558 throw new IllegalArgumentException("All split keys must be unique, " +
559 "found duplicate: " + Bytes.toStringBinary(splitKey) +
560 ", " + Bytes.toStringBinary(lastKey));
561 }
562 lastKey = splitKey;
563 }
564 }
565 try {
566 getMaster().createTable(desc, splitKeys);
567 } catch (RemoteException e) {
568 throw e.unwrapRemoteException();
569 }
570 }
571
572
573
574
575
576
577
578
579 public void deleteTable(final String tableName) throws IOException {
580 deleteTable(Bytes.toBytes(tableName));
581 }
582
583
584
585
586
587
588
589
590 public void deleteTable(final byte [] tableName) throws IOException {
591 isMasterRunning();
592 HTableDescriptor.isLegalTableName(tableName);
593 HRegionLocation firstMetaServer = getFirstMetaServerForTable(tableName);
594 boolean tableExists = true;
595 try {
596 getMaster().deleteTable(tableName);
597 } catch (RemoteException e) {
598 throw RemoteExceptionHandler.decodeRemoteException(e);
599 }
600
601 HRegionInterface server =
602 connection.getHRegionConnection(firstMetaServer.getHostname(), firstMetaServer.getPort());
603 List<String> tableNameAsList = new ArrayList<String>(1);
604 tableNameAsList.add(Bytes.toString(tableName));
605 for (int tries = 0; tries < (this.numRetries * this.retryLongerMultiplier); tries++) {
606 long scannerId = -1L;
607 try {
608
609 Scan scan = MetaReader.getScanForTableName(tableName);
610 scan.addColumn(HConstants.CATALOG_FAMILY,
611 HConstants.REGIONINFO_QUALIFIER);
612 scannerId = server.openScanner(
613 firstMetaServer.getRegionInfo().getRegionName(), scan);
614
615 Result values = server.next(scannerId);
616
617
618
619 if (values == null) {
620 tableExists = false;
621 HTableDescriptor[] htds = getMaster().getHTableDescriptors(tableNameAsList);
622 tableExists = (htds != null && htds.length > 0);
623 if (!tableExists) {
624 break;
625 }
626 }
627 } catch (IOException ex) {
628 if(tries == numRetries - 1) {
629 if (ex instanceof RemoteException) {
630 ex = RemoteExceptionHandler.decodeRemoteException((RemoteException) ex);
631 }
632 throw ex;
633 }
634 } finally {
635 if (scannerId != -1L) {
636 try {
637 server.close(scannerId);
638 } catch (Exception ex) {
639 LOG.warn(ex);
640 }
641 }
642 }
643 try {
644 Thread.sleep(getPauseTime(tries));
645 } catch (InterruptedException e) {
646
647 }
648 }
649
650 if (tableExists) {
651 throw new IOException("Retries exhausted, it took too long to wait"+
652 " for the table " + Bytes.toString(tableName) + " to be deleted.");
653 }
654
655 this.connection.clearRegionCache(tableName);
656 LOG.info("Deleted " + Bytes.toString(tableName));
657 }
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672 public HTableDescriptor[] deleteTables(String regex) throws IOException {
673 return deleteTables(Pattern.compile(regex));
674 }
675
676
677
678
679
680
681
682
683
684
685
686
687 public HTableDescriptor[] deleteTables(Pattern pattern) throws IOException {
688 List<HTableDescriptor> failed = new LinkedList<HTableDescriptor>();
689 for (HTableDescriptor table : listTables(pattern)) {
690 try {
691 deleteTable(table.getName());
692 } catch (IOException ex) {
693 LOG.info("Failed to delete table " + table.getNameAsString(), ex);
694 failed.add(table);
695 }
696 }
697 return failed.toArray(new HTableDescriptor[failed.size()]);
698 }
699
700
701 public void enableTable(final String tableName)
702 throws IOException {
703 enableTable(Bytes.toBytes(tableName));
704 }
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719 public void enableTable(final byte [] tableName)
720 throws IOException {
721 enableTableAsync(tableName);
722
723
724 waitUntilTableIsEnabled(tableName);
725
726 LOG.info("Enabled table " + Bytes.toString(tableName));
727 }
728
729
730
731
732
733
734
735
736 private void waitUntilTableIsEnabled(final byte[] tableName) throws IOException {
737 boolean enabled = false;
738 long start = EnvironmentEdgeManager.currentTimeMillis();
739 for (int tries = 0; tries < (this.numRetries * this.retryLongerMultiplier); tries++) {
740 enabled = isTableEnabled(tableName) && isTableAvailable(tableName);
741 if (enabled) {
742 break;
743 }
744 long sleep = getPauseTime(tries);
745 if (LOG.isDebugEnabled()) {
746 LOG.debug("Sleeping= " + sleep + "ms, waiting for all regions to be " +
747 "enabled in " + Bytes.toString(tableName));
748 }
749 try {
750 Thread.sleep(sleep);
751 } catch (InterruptedException e) {
752 Thread.currentThread().interrupt();
753
754
755 throw new IOException("Interrupted", e);
756 }
757 }
758 if (!enabled) {
759 long msec = EnvironmentEdgeManager.currentTimeMillis() - start;
760 throw new IOException("Table '" + Bytes.toString(tableName) +
761 "' not yet enabled, after " + msec + "ms.");
762 }
763 }
764
765 public void enableTableAsync(final String tableName)
766 throws IOException {
767 enableTableAsync(Bytes.toBytes(tableName));
768 }
769
770
771
772
773
774
775
776
777
778
779
780 public void enableTableAsync(final byte [] tableName)
781 throws IOException {
782 HTableDescriptor.isLegalTableName(tableName);
783 isMasterRunning();
784 try {
785 getMaster().enableTable(tableName);
786 } catch (RemoteException e) {
787 throw e.unwrapRemoteException();
788 }
789 LOG.info("Started enable of " + Bytes.toString(tableName));
790 }
791
792
793
794
795
796
797
798
799
800
801
802
803
804 public HTableDescriptor[] enableTables(String regex) throws IOException {
805 return enableTables(Pattern.compile(regex));
806 }
807
808
809
810
811
812
813
814
815
816
817
818 public HTableDescriptor[] enableTables(Pattern pattern) throws IOException {
819 List<HTableDescriptor> failed = new LinkedList<HTableDescriptor>();
820 for (HTableDescriptor table : listTables(pattern)) {
821 if (isTableDisabled(table.getName())) {
822 try {
823 enableTable(table.getName());
824 } catch (IOException ex) {
825 LOG.info("Failed to enable table " + table.getNameAsString(), ex);
826 failed.add(table);
827 }
828 }
829 }
830 return failed.toArray(new HTableDescriptor[failed.size()]);
831 }
832
833 public void disableTableAsync(final String tableName) throws IOException {
834 disableTableAsync(Bytes.toBytes(tableName));
835 }
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850 public void disableTableAsync(final byte [] tableName) throws IOException {
851 HTableDescriptor.isLegalTableName(tableName);
852 isMasterRunning();
853 try {
854 getMaster().disableTable(tableName);
855 } catch (RemoteException e) {
856 throw e.unwrapRemoteException();
857 }
858 LOG.info("Started disable of " + Bytes.toString(tableName));
859 }
860
861 public void disableTable(final String tableName)
862 throws IOException {
863 disableTable(Bytes.toBytes(tableName));
864 }
865
866
867
868
869
870
871
872
873
874
875
876
877 public void disableTable(final byte [] tableName)
878 throws IOException {
879 disableTableAsync(tableName);
880
881 boolean disabled = false;
882 for (int tries = 0; tries < (this.numRetries * this.retryLongerMultiplier); tries++) {
883 disabled = isTableDisabled(tableName);
884 if (disabled) {
885 break;
886 }
887 long sleep = getPauseTime(tries);
888 if (LOG.isDebugEnabled()) {
889 LOG.debug("Sleeping= " + sleep + "ms, waiting for all regions to be " +
890 "disabled in " + Bytes.toString(tableName));
891 }
892 try {
893 Thread.sleep(sleep);
894 } catch (InterruptedException e) {
895
896
897 Thread.currentThread().interrupt();
898 throw new IOException("Interrupted", e);
899 }
900 }
901 if (!disabled) {
902 throw new RegionException("Retries exhausted, it took too long to wait"+
903 " for the table " + Bytes.toString(tableName) + " to be disabled.");
904 }
905 LOG.info("Disabled " + Bytes.toString(tableName));
906 }
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921 public HTableDescriptor[] disableTables(String regex) throws IOException {
922 return disableTables(Pattern.compile(regex));
923 }
924
925
926
927
928
929
930
931
932
933
934
935
936 public HTableDescriptor[] disableTables(Pattern pattern) throws IOException {
937 List<HTableDescriptor> failed = new LinkedList<HTableDescriptor>();
938 for (HTableDescriptor table : listTables(pattern)) {
939 if (isTableEnabled(table.getName())) {
940 try {
941 disableTable(table.getName());
942 } catch (IOException ex) {
943 LOG.info("Failed to disable table " + table.getNameAsString(), ex);
944 failed.add(table);
945 }
946 }
947 }
948 return failed.toArray(new HTableDescriptor[failed.size()]);
949 }
950
951
952
953
954
955 private void checkTableExistence(byte[] tableName) throws IOException {
956 if (!tableExists(tableName)) {
957 throw new TableNotFoundException(Bytes.toString(tableName));
958 }
959 }
960
961
962
963
964
965
966 public boolean isTableEnabled(String tableName) throws IOException {
967 return isTableEnabled(Bytes.toBytes(tableName));
968 }
969
970
971
972
973
974 public boolean isTableEnabled(byte[] tableName) throws IOException {
975 if (!HTableDescriptor.isMetaTable(tableName)) {
976 HTableDescriptor.isLegalTableName(tableName);
977 }
978 checkTableExistence(tableName);
979 return connection.isTableEnabled(tableName);
980 }
981
982
983
984
985
986
987 public boolean isTableDisabled(final String tableName) throws IOException {
988 return isTableDisabled(Bytes.toBytes(tableName));
989 }
990
991
992
993
994
995
996 public boolean isTableDisabled(byte[] tableName) throws IOException {
997 if (!HTableDescriptor.isMetaTable(tableName)) {
998 HTableDescriptor.isLegalTableName(tableName);
999 }
1000 checkTableExistence(tableName);
1001 return connection.isTableDisabled(tableName);
1002 }
1003
1004
1005
1006
1007
1008
1009 public boolean isTableAvailable(byte[] tableName) throws IOException {
1010 return connection.isTableAvailable(tableName);
1011 }
1012
1013
1014
1015
1016
1017
1018 public boolean isTableAvailable(String tableName) throws IOException {
1019 return connection.isTableAvailable(Bytes.toBytes(tableName));
1020 }
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034 public Pair<Integer, Integer> getAlterStatus(final byte[] tableName)
1035 throws IOException {
1036 HTableDescriptor.isLegalTableName(tableName);
1037 try {
1038 return getMaster().getAlterStatus(tableName);
1039 } catch (RemoteException e) {
1040 throw RemoteExceptionHandler.decodeRemoteException(e);
1041 }
1042 }
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052 public void addColumn(final String tableName, HColumnDescriptor column)
1053 throws IOException {
1054 addColumn(Bytes.toBytes(tableName), column);
1055 }
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065 public void addColumn(final byte [] tableName, HColumnDescriptor column)
1066 throws IOException {
1067 HTableDescriptor.isLegalTableName(tableName);
1068 try {
1069 getMaster().addColumn(tableName, column);
1070 } catch (RemoteException e) {
1071 throw RemoteExceptionHandler.decodeRemoteException(e);
1072 }
1073 }
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083 public void deleteColumn(final String tableName, final String columnName)
1084 throws IOException {
1085 deleteColumn(Bytes.toBytes(tableName), Bytes.toBytes(columnName));
1086 }
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096 public void deleteColumn(final byte [] tableName, final byte [] columnName)
1097 throws IOException {
1098 try {
1099 getMaster().deleteColumn(tableName, columnName);
1100 } catch (RemoteException e) {
1101 throw RemoteExceptionHandler.decodeRemoteException(e);
1102 }
1103 }
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113 public void modifyColumn(final String tableName, HColumnDescriptor descriptor)
1114 throws IOException {
1115 modifyColumn(Bytes.toBytes(tableName), descriptor);
1116 }
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126 public void modifyColumn(final byte [] tableName, HColumnDescriptor descriptor)
1127 throws IOException {
1128 try {
1129 getMaster().modifyColumn(tableName, descriptor);
1130 } catch (RemoteException re) {
1131
1132
1133
1134 throw RemoteExceptionHandler.decodeRemoteException(re);
1135 }
1136 }
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146 public void closeRegion(final String regionname, final String serverName)
1147 throws IOException {
1148 closeRegion(Bytes.toBytes(regionname), serverName);
1149 }
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161 public void closeRegion(final byte [] regionname, final String serverName)
1162 throws IOException {
1163 CatalogTracker ct = getCatalogTracker();
1164 try {
1165 if (serverName != null) {
1166 Pair<HRegionInfo, ServerName> pair = MetaReader.getRegion(ct, regionname);
1167 if (pair == null || pair.getFirst() == null) {
1168 throw new UnknownRegionException(Bytes.toStringBinary(regionname));
1169 } else {
1170 closeRegion(new ServerName(serverName), pair.getFirst());
1171 }
1172 } else {
1173 Pair<HRegionInfo, ServerName> pair = MetaReader.getRegion(ct, regionname);
1174 if (pair == null) {
1175 throw new UnknownRegionException(Bytes.toStringBinary(regionname));
1176 } else if (pair.getSecond() == null) {
1177 throw new NoServerForRegionException(Bytes.toStringBinary(regionname));
1178 } else {
1179 closeRegion(pair.getSecond(), pair.getFirst());
1180 }
1181 }
1182 } finally {
1183 cleanupCatalogTracker(ct);
1184 }
1185 }
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208 public boolean closeRegionWithEncodedRegionName(final String encodedRegionName,
1209 final String serverName) throws IOException {
1210 byte[] encodedRegionNameInBytes = Bytes.toBytes(encodedRegionName);
1211 if (null == serverName || ("").equals(serverName.trim())) {
1212 throw new IllegalArgumentException(
1213 "The servername cannot be null or empty.");
1214 }
1215 ServerName sn = new ServerName(serverName);
1216 HRegionInterface rs = this.connection.getHRegionConnection(
1217 sn.getHostname(), sn.getPort());
1218
1219 boolean isRegionClosed = rs.closeRegion(encodedRegionNameInBytes, false);
1220 if (false == isRegionClosed) {
1221 LOG.error("Not able to close the region " + encodedRegionName + ".");
1222 }
1223 return isRegionClosed;
1224 }
1225
1226
1227
1228
1229
1230
1231
1232
1233 public void closeRegion(final ServerName sn, final HRegionInfo hri)
1234 throws IOException {
1235 HRegionInterface rs =
1236 this.connection.getHRegionConnection(sn.getHostname(), sn.getPort());
1237
1238 rs.closeRegion(hri, false);
1239 }
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249 public void flush(final String tableNameOrRegionName)
1250 throws IOException, InterruptedException {
1251 flush(Bytes.toBytes(tableNameOrRegionName));
1252 }
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262 public void flush(final byte [] tableNameOrRegionName)
1263 throws IOException, InterruptedException {
1264 CatalogTracker ct = getCatalogTracker();
1265 try {
1266 Pair<HRegionInfo, ServerName> regionServerPair
1267 = getRegion(tableNameOrRegionName, ct);
1268 if (regionServerPair != null) {
1269 if (regionServerPair.getSecond() == null) {
1270 throw new NoServerForRegionException(Bytes.toStringBinary(tableNameOrRegionName));
1271 } else {
1272 flush(regionServerPair.getSecond(), regionServerPair.getFirst());
1273 }
1274 } else {
1275 final String tableName = tableNameString(tableNameOrRegionName, ct);
1276 List<Pair<HRegionInfo, ServerName>> pairs =
1277 MetaReader.getTableRegionsAndLocations(ct,
1278 tableName);
1279 for (Pair<HRegionInfo, ServerName> pair: pairs) {
1280 if (pair.getFirst().isOffline()) continue;
1281 if (pair.getSecond() == null) continue;
1282 try {
1283 flush(pair.getSecond(), pair.getFirst());
1284 } catch (NotServingRegionException e) {
1285 if (LOG.isDebugEnabled()) {
1286 LOG.debug("Trying to flush " + pair.getFirst() + ": " +
1287 StringUtils.stringifyException(e));
1288 }
1289 }
1290 }
1291 }
1292 } finally {
1293 cleanupCatalogTracker(ct);
1294 }
1295 }
1296
1297 private void flush(final ServerName sn, final HRegionInfo hri)
1298 throws IOException {
1299 HRegionInterface rs =
1300 this.connection.getHRegionConnection(sn.getHostname(), sn.getPort());
1301 rs.flushRegion(hri);
1302 }
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312 public void compact(final String tableNameOrRegionName)
1313 throws IOException, InterruptedException {
1314 compact(Bytes.toBytes(tableNameOrRegionName));
1315 }
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325 public void compact(final byte [] tableNameOrRegionName)
1326 throws IOException, InterruptedException {
1327 compact(tableNameOrRegionName, null, false);
1328 }
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339 public void compact(String tableOrRegionName, String columnFamily)
1340 throws IOException, InterruptedException {
1341 compact(Bytes.toBytes(tableOrRegionName), Bytes.toBytes(columnFamily));
1342 }
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353 public void compact(final byte [] tableNameOrRegionName, final byte[] columnFamily)
1354 throws IOException, InterruptedException {
1355 compact(tableNameOrRegionName, columnFamily, false);
1356 }
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366 public void majorCompact(final String tableNameOrRegionName)
1367 throws IOException, InterruptedException {
1368 majorCompact(Bytes.toBytes(tableNameOrRegionName));
1369 }
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379 public void majorCompact(final byte [] tableNameOrRegionName)
1380 throws IOException, InterruptedException {
1381 compact(tableNameOrRegionName, null, true);
1382 }
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393 public void majorCompact(final String tableNameOrRegionName,
1394 final String columnFamily) throws IOException, InterruptedException {
1395 majorCompact(Bytes.toBytes(tableNameOrRegionName),
1396 Bytes.toBytes(columnFamily));
1397 }
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408 public void majorCompact(final byte [] tableNameOrRegionName,
1409 final byte[] columnFamily) throws IOException, InterruptedException {
1410 compact(tableNameOrRegionName, columnFamily, true);
1411 }
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423 private void compact(final byte [] tableNameOrRegionName,
1424 final byte[] columnFamily, final boolean major)
1425 throws IOException, InterruptedException {
1426 CatalogTracker ct = getCatalogTracker();
1427 try {
1428 Pair<HRegionInfo, ServerName> regionServerPair
1429 = getRegion(tableNameOrRegionName, ct);
1430 if (regionServerPair != null) {
1431 if (regionServerPair.getSecond() == null) {
1432 throw new NoServerForRegionException(Bytes.toStringBinary(tableNameOrRegionName));
1433 } else {
1434 compact(regionServerPair.getSecond(), regionServerPair.getFirst(), major, columnFamily);
1435 }
1436 } else {
1437 final String tableName = tableNameString(tableNameOrRegionName, ct);
1438 List<Pair<HRegionInfo, ServerName>> pairs =
1439 MetaReader.getTableRegionsAndLocations(ct,
1440 tableName);
1441 for (Pair<HRegionInfo, ServerName> pair: pairs) {
1442 if (pair.getFirst().isOffline()) continue;
1443 if (pair.getSecond() == null) continue;
1444 try {
1445 compact(pair.getSecond(), pair.getFirst(), major, columnFamily);
1446 } catch (NotServingRegionException e) {
1447 if (LOG.isDebugEnabled()) {
1448 LOG.debug("Trying to" + (major ? " major" : "") + " compact " +
1449 pair.getFirst() + ": " +
1450 StringUtils.stringifyException(e));
1451 }
1452 }
1453 }
1454 }
1455 } finally {
1456 cleanupCatalogTracker(ct);
1457 }
1458 }
1459
1460 private void compact(final ServerName sn, final HRegionInfo hri,
1461 final boolean major, final byte [] family)
1462 throws IOException {
1463 HRegionInterface rs =
1464 this.connection.getHRegionConnection(sn.getHostname(), sn.getPort());
1465 if (family != null) {
1466 try {
1467 rs.compactRegion(hri, major, family);
1468 } catch (IOException ioe) {
1469 String notFoundMsg = "java.lang.NoSuchMethodException: org.apache.hadoop.hbase.ipc.HRegionInterface."
1470 + "compactRegion(org.apache.hadoop.hbase.HRegionInfo, boolean, [B)";
1471 if (ioe.getMessage().contains(notFoundMsg)) {
1472 throw new IOException("per-column family compaction not supported on this version "
1473 + "of the HBase server. You may still compact at the table or region level by "
1474 + "omitting the column family name. Alternatively, you can upgrade the HBase server");
1475 }
1476 throw ioe;
1477 }
1478 } else {
1479 rs.compactRegion(hri, major);
1480 }
1481 }
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498 public void move(final byte [] encodedRegionName, final byte [] destServerName)
1499 throws UnknownRegionException, MasterNotRunningException, ZooKeeperConnectionException {
1500 getMaster().move(encodedRegionName, destServerName);
1501 }
1502
1503
1504
1505
1506
1507
1508
1509
1510 public void assign(final byte[] regionName) throws MasterNotRunningException,
1511 ZooKeeperConnectionException, IOException {
1512 getMaster().assign(regionName);
1513 }
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529 public void unassign(final byte [] regionName, final boolean force)
1530 throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
1531 getMaster().unassign(regionName, force);
1532 }
1533
1534
1535
1536
1537
1538
1539
1540 @Deprecated
1541 public boolean balanceSwitch(final boolean b)
1542 throws MasterNotRunningException, ZooKeeperConnectionException {
1543 return getMaster().balanceSwitch(b);
1544 }
1545
1546
1547
1548
1549
1550
1551
1552 public boolean setBalancerRunning(final boolean on, final boolean synchronous)
1553 throws MasterNotRunningException, ZooKeeperConnectionException {
1554 if (synchronous && synchronousBalanceSwitchSupported) {
1555 try {
1556 return getMaster().synchronousBalanceSwitch(on);
1557 } catch (UndeclaredThrowableException ute) {
1558 String error = ute.getCause().getMessage();
1559 if (error != null && error.matches(
1560 "(?s).+NoSuchMethodException:.+synchronousBalanceSwitch.+")) {
1561 LOG.info("HMaster doesn't support synchronousBalanceSwitch");
1562 synchronousBalanceSwitchSupported = false;
1563 } else {
1564 throw ute;
1565 }
1566 }
1567 }
1568 return balanceSwitch(on);
1569 }
1570
1571
1572
1573
1574
1575
1576
1577 public boolean balancer()
1578 throws MasterNotRunningException, ZooKeeperConnectionException {
1579 return getMaster().balance();
1580 }
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590 public void split(final String tableNameOrRegionName)
1591 throws IOException, InterruptedException {
1592 split(Bytes.toBytes(tableNameOrRegionName));
1593 }
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603 public void split(final byte [] tableNameOrRegionName)
1604 throws IOException, InterruptedException {
1605 split(tableNameOrRegionName, null);
1606 }
1607
1608 public void split(final String tableNameOrRegionName,
1609 final String splitPoint) throws IOException, InterruptedException {
1610 split(Bytes.toBytes(tableNameOrRegionName), Bytes.toBytes(splitPoint));
1611 }
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622 public void split(final byte [] tableNameOrRegionName,
1623 final byte [] splitPoint) throws IOException, InterruptedException {
1624 CatalogTracker ct = getCatalogTracker();
1625 try {
1626 Pair<HRegionInfo, ServerName> regionServerPair
1627 = getRegion(tableNameOrRegionName, ct);
1628 if (regionServerPair != null) {
1629 if (regionServerPair.getSecond() == null) {
1630 throw new NoServerForRegionException(Bytes.toStringBinary(tableNameOrRegionName));
1631 } else {
1632 split(regionServerPair.getSecond(), regionServerPair.getFirst(), splitPoint);
1633 }
1634 } else {
1635 final String tableName = tableNameString(tableNameOrRegionName, ct);
1636 List<Pair<HRegionInfo, ServerName>> pairs =
1637 MetaReader.getTableRegionsAndLocations(ct,
1638 tableName);
1639 for (Pair<HRegionInfo, ServerName> pair: pairs) {
1640
1641 if (pair.getSecond() == null) continue;
1642 HRegionInfo r = pair.getFirst();
1643
1644 if (r.isSplitParent()) continue;
1645
1646 if (splitPoint != null && !r.containsRow(splitPoint)) continue;
1647
1648 split(pair.getSecond(), pair.getFirst(), splitPoint);
1649 }
1650 }
1651 } finally {
1652 cleanupCatalogTracker(ct);
1653 }
1654 }
1655
1656 private void split(final ServerName sn, final HRegionInfo hri,
1657 byte[] splitPoint) throws IOException {
1658 HRegionInterface rs =
1659 this.connection.getHRegionConnection(sn.getHostname(), sn.getPort());
1660 rs.splitRegion(hri, splitPoint);
1661 }
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672 public void modifyTable(final byte [] tableName, HTableDescriptor htd)
1673 throws IOException {
1674 try {
1675 getMaster().modifyTable(tableName, htd);
1676 } catch (RemoteException re) {
1677
1678
1679
1680 throw RemoteExceptionHandler.decodeRemoteException(re);
1681 }
1682 }
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693 Pair<HRegionInfo, ServerName> getRegion(final byte[] tableNameOrRegionName,
1694 final CatalogTracker ct) throws IOException {
1695 if (tableNameOrRegionName == null) {
1696 throw new IllegalArgumentException("Pass a table name or region name");
1697 }
1698 Pair<HRegionInfo, ServerName> pair = MetaReader.getRegion(ct, tableNameOrRegionName);
1699 if (pair == null) {
1700 final AtomicReference<Pair<HRegionInfo, ServerName>> result =
1701 new AtomicReference<Pair<HRegionInfo, ServerName>>(null);
1702 final String encodedName = Bytes.toString(tableNameOrRegionName);
1703 MetaScannerVisitor visitor = new MetaScannerVisitorBase() {
1704 @Override
1705 public boolean processRow(Result data) throws IOException {
1706 if (data == null || data.size() <= 0) {
1707 return true;
1708 }
1709 HRegionInfo info = MetaReader.parseHRegionInfoFromCatalogResult(
1710 data, HConstants.REGIONINFO_QUALIFIER);
1711 if (info == null) {
1712 LOG.warn("No serialized HRegionInfo in " + data);
1713 return true;
1714 }
1715 if (!encodedName.equals(info.getEncodedName())) return true;
1716 ServerName sn = MetaReader.getServerNameFromCatalogResult(data);
1717 result.set(new Pair<HRegionInfo, ServerName>(info, sn));
1718 return false;
1719 }
1720 };
1721
1722 MetaScanner.metaScan(conf, connection, visitor, null);
1723 pair = result.get();
1724 }
1725 return pair;
1726 }
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737 private String tableNameString(final byte[] tableNameBytes, CatalogTracker ct)
1738 throws IOException {
1739 String tableNameString = Bytes.toString(tableNameBytes);
1740 if (!MetaReader.tableExists(ct, tableNameString)) {
1741 throw new TableNotFoundException(tableNameString);
1742 }
1743 return tableNameString;
1744 }
1745
1746
1747
1748
1749
1750 public synchronized void shutdown() throws IOException {
1751 isMasterRunning();
1752 try {
1753 getMaster().shutdown();
1754 } catch (RemoteException e) {
1755 throw RemoteExceptionHandler.decodeRemoteException(e);
1756 }
1757 }
1758
1759
1760
1761
1762
1763
1764
1765 public synchronized void stopMaster() throws IOException {
1766 isMasterRunning();
1767 try {
1768 getMaster().stopMaster();
1769 } catch (RemoteException e) {
1770 throw RemoteExceptionHandler.decodeRemoteException(e);
1771 }
1772 }
1773
1774
1775
1776
1777
1778
1779
1780 public synchronized void stopRegionServer(final String hostnamePort)
1781 throws IOException {
1782 String hostname = Addressing.parseHostname(hostnamePort);
1783 int port = Addressing.parsePort(hostnamePort);
1784 HRegionInterface rs =
1785 this.connection.getHRegionConnection(hostname, port);
1786 rs.stop("Called by admin client " + this.connection.toString());
1787 }
1788
1789
1790
1791
1792
1793 public ClusterStatus getClusterStatus() throws IOException {
1794 return getMaster().getClusterStatus();
1795 }
1796
1797 private HRegionLocation getFirstMetaServerForTable(final byte [] tableName)
1798 throws IOException {
1799 return connection.locateRegion(HConstants.META_TABLE_NAME,
1800 HRegionInfo.createRegionName(tableName, null, HConstants.NINES, false));
1801 }
1802
1803
1804
1805
1806 public Configuration getConfiguration() {
1807 return this.conf;
1808 }
1809
1810
1811
1812
1813
1814
1815
1816
1817 public static void checkHBaseAvailable(Configuration conf)
1818 throws MasterNotRunningException, ZooKeeperConnectionException {
1819 Configuration copyOfConf = HBaseConfiguration.create(conf);
1820 copyOfConf.setInt("hbase.client.retries.number", 1);
1821 HBaseAdmin admin = new HBaseAdmin(copyOfConf);
1822 try {
1823 admin.close();
1824 } catch (IOException ioe) {
1825 admin.LOG.info("Failed to close connection", ioe);
1826 }
1827 }
1828
1829
1830
1831
1832
1833
1834
1835
1836 public List<HRegionInfo> getTableRegions(final byte[] tableName)
1837 throws IOException {
1838 CatalogTracker ct = getCatalogTracker();
1839 List<HRegionInfo> Regions = null;
1840 try {
1841 Regions = MetaReader.getTableRegions(ct, tableName, true);
1842 } finally {
1843 cleanupCatalogTracker(ct);
1844 }
1845 return Regions;
1846 }
1847
1848 public void close() throws IOException {
1849 if (cleanupConnectionOnClose && this.connection != null) {
1850 this.connection.close();
1851 }
1852 }
1853
1854
1855
1856
1857
1858
1859
1860 public HTableDescriptor[] getTableDescriptors(List<String> tableNames)
1861 throws IOException {
1862 return this.connection.getHTableDescriptors(tableNames);
1863 }
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878 public synchronized byte[][] rollHLogWriter(String serverName)
1879 throws IOException, FailedLogCloseException {
1880 ServerName sn = new ServerName(serverName);
1881 HRegionInterface rs = this.connection.getHRegionConnection(
1882 sn.getHostname(), sn.getPort());
1883 return rs.rollHLogWriter();
1884 }
1885
1886 public String[] getMasterCoprocessors() {
1887 try {
1888 return getClusterStatus().getMasterCoprocessors();
1889 } catch (IOException e) {
1890 LOG.error("Could not getClusterStatus()",e);
1891 return null;
1892 }
1893 }
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904 public CompactionState getCompactionState(final String tableNameOrRegionName)
1905 throws IOException, InterruptedException {
1906 return getCompactionState(Bytes.toBytes(tableNameOrRegionName));
1907 }
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918 public CompactionState getCompactionState(final byte [] tableNameOrRegionName)
1919 throws IOException, InterruptedException {
1920 CompactionState state = CompactionState.NONE;
1921 CatalogTracker ct = getCatalogTracker();
1922 try {
1923 Pair<HRegionInfo, ServerName> regionServerPair
1924 = getRegion(tableNameOrRegionName, ct);
1925 if (regionServerPair != null) {
1926 if (regionServerPair.getSecond() == null) {
1927 throw new NoServerForRegionException(Bytes.toStringBinary(tableNameOrRegionName));
1928 } else {
1929 ServerName sn = regionServerPair.getSecond();
1930 HRegionInterface rs =
1931 this.connection.getHRegionConnection(sn.getHostname(), sn.getPort());
1932 return CompactionState.valueOf(
1933 rs.getCompactionState(regionServerPair.getFirst().getRegionName()));
1934 }
1935 } else {
1936 final String tableName = tableNameString(tableNameOrRegionName, ct);
1937 List<Pair<HRegionInfo, ServerName>> pairs =
1938 MetaReader.getTableRegionsAndLocations(ct, tableName);
1939 for (Pair<HRegionInfo, ServerName> pair: pairs) {
1940 if (pair.getFirst().isOffline()) continue;
1941 if (pair.getSecond() == null) continue;
1942 try {
1943 ServerName sn = pair.getSecond();
1944 HRegionInterface rs =
1945 this.connection.getHRegionConnection(sn.getHostname(), sn.getPort());
1946 switch (CompactionState.valueOf(
1947 rs.getCompactionState(pair.getFirst().getRegionName()))) {
1948 case MAJOR_AND_MINOR:
1949 return CompactionState.MAJOR_AND_MINOR;
1950 case MAJOR:
1951 if (state == CompactionState.MINOR) {
1952 return CompactionState.MAJOR_AND_MINOR;
1953 }
1954 state = CompactionState.MAJOR;
1955 break;
1956 case MINOR:
1957 if (state == CompactionState.MAJOR) {
1958 return CompactionState.MAJOR_AND_MINOR;
1959 }
1960 state = CompactionState.MINOR;
1961 break;
1962 case NONE:
1963 default:
1964 }
1965 } catch (NotServingRegionException e) {
1966 if (LOG.isDebugEnabled()) {
1967 LOG.debug("Trying to get compaction state of " +
1968 pair.getFirst() + ": " +
1969 StringUtils.stringifyException(e));
1970 }
1971 }
1972 }
1973 }
1974 } finally {
1975 cleanupCatalogTracker(ct);
1976 }
1977 return state;
1978 }
1979
1980
1981
1982
1983
1984
1985
1986
1987 public <T extends CoprocessorProtocol> T coprocessorProxy(
1988 Class<T> protocol) {
1989 return (T) Proxy.newProxyInstance(this.getClass().getClassLoader(),
1990 new Class[]{protocol},
1991 new MasterExecRPCInvoker(conf,
1992 connection,
1993 protocol));
1994 }
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012 public void snapshot(final String snapshotName, final String tableName) throws IOException,
2013 SnapshotCreationException, IllegalArgumentException {
2014 snapshot(snapshotName, tableName, SnapshotDescription.Type.FLUSH);
2015 }
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033 public void snapshot(final byte[] snapshotName, final byte[] tableName) throws IOException,
2034 SnapshotCreationException, IllegalArgumentException {
2035 snapshot(Bytes.toString(snapshotName), Bytes.toString(tableName));
2036 }
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056 public void snapshot(final String snapshotName, final String tableName,
2057 SnapshotDescription.Type type) throws IOException, SnapshotCreationException,
2058 IllegalArgumentException {
2059 SnapshotDescription.Builder builder = SnapshotDescription.newBuilder();
2060 builder.setTable(tableName);
2061 builder.setName(snapshotName);
2062 builder.setType(type);
2063 snapshot(builder.build());
2064 }
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087 public void snapshot(SnapshotDescription snapshot) throws IOException, SnapshotCreationException,
2088 IllegalArgumentException {
2089 HSnapshotDescription snapshotWritable = new HSnapshotDescription(snapshot);
2090
2091 try {
2092
2093 long max = takeSnapshotAsync(snapshot);
2094 long start = EnvironmentEdgeManager.currentTimeMillis();
2095 long maxPauseTime = max / this.numRetries;
2096 boolean done = false;
2097 int tries = 0;
2098 LOG.debug("Waiting a max of " + max + " ms for snapshot '" +
2099 SnapshotDescriptionUtils.toString(snapshot) + "' to complete. (max " +
2100 maxPauseTime + " ms per retry)");
2101 while (tries == 0 || (EnvironmentEdgeManager.currentTimeMillis() - start) < max && !done) {
2102 try {
2103
2104 long sleep = getPauseTime(tries++);
2105 sleep = sleep > maxPauseTime ? maxPauseTime : sleep;
2106 LOG.debug("(#" + tries + ") Sleeping: " + sleep +
2107 "ms while waiting for snapshot completion.");
2108 Thread.sleep(sleep);
2109
2110 } catch (InterruptedException e) {
2111 LOG.debug("Interrupted while waiting for snapshot " + snapshot + " to complete");
2112 Thread.currentThread().interrupt();
2113 }
2114 LOG.debug("Getting current status of snapshot from master...");
2115 done = getMaster().isSnapshotDone(snapshotWritable);
2116 }
2117
2118 if (!done) {
2119 throw new SnapshotCreationException("Snapshot '" + snapshot.getName()
2120 + "' wasn't completed in expectedTime:" + max + " ms", snapshot);
2121 }
2122 } catch (RemoteException e) {
2123 throw RemoteExceptionHandler.decodeRemoteException(e);
2124 }
2125 }
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137 public long takeSnapshotAsync(SnapshotDescription snapshot) throws IOException,
2138 SnapshotCreationException {
2139 SnapshotDescriptionUtils.assertSnapshotRequestIsValid(snapshot);
2140 HSnapshotDescription snapshotWritable = new HSnapshotDescription(snapshot);
2141 return getMaster().snapshot(snapshotWritable);
2142 }
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164 public boolean isSnapshotFinished(final SnapshotDescription snapshot)
2165 throws IOException, HBaseSnapshotException, UnknownSnapshotException {
2166 try {
2167 return getMaster().isSnapshotDone(new HSnapshotDescription(snapshot));
2168 } catch (RemoteException e) {
2169 throw RemoteExceptionHandler.decodeRemoteException(e);
2170 }
2171 }
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183 public void restoreSnapshot(final byte[] snapshotName)
2184 throws IOException, RestoreSnapshotException {
2185 restoreSnapshot(Bytes.toString(snapshotName));
2186 }
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198 public void restoreSnapshot(final String snapshotName)
2199 throws IOException, RestoreSnapshotException {
2200 String rollbackSnapshot = snapshotName + "-" + EnvironmentEdgeManager.currentTimeMillis();
2201
2202 String tableName = null;
2203 for (SnapshotDescription snapshotInfo: listSnapshots()) {
2204 if (snapshotInfo.getName().equals(snapshotName)) {
2205 tableName = snapshotInfo.getTable();
2206 break;
2207 }
2208 }
2209
2210 if (tableName == null) {
2211 throw new RestoreSnapshotException(
2212 "Unable to find the table name for snapshot=" + snapshotName);
2213 }
2214
2215
2216 snapshot(rollbackSnapshot, tableName);
2217
2218
2219 try {
2220 internalRestoreSnapshot(snapshotName, tableName);
2221 } catch (IOException e) {
2222
2223 try {
2224 String msg = "Restore snapshot=" + snapshotName +
2225 " failed. Rollback to snapshot=" + rollbackSnapshot + " succeeded.";
2226 LOG.error(msg, e);
2227 internalRestoreSnapshot(rollbackSnapshot, tableName);
2228 throw new RestoreSnapshotException(msg, e);
2229 } catch (IOException ex) {
2230 String msg = "Failed to restore and rollback to snapshot=" + rollbackSnapshot;
2231 LOG.error(msg, ex);
2232 throw new RestoreSnapshotException(msg, ex);
2233 }
2234 }
2235 }
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247 public void cloneSnapshot(final byte[] snapshotName, final byte[] tableName)
2248 throws IOException, TableExistsException, RestoreSnapshotException, InterruptedException {
2249 cloneSnapshot(Bytes.toString(snapshotName), Bytes.toString(tableName));
2250 }
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262 public void cloneSnapshot(final String snapshotName, final String tableName)
2263 throws IOException, TableExistsException, RestoreSnapshotException, InterruptedException {
2264 if (tableExists(tableName)) {
2265 throw new TableExistsException("Table '" + tableName + " already exists");
2266 }
2267 internalRestoreSnapshot(snapshotName, tableName);
2268 waitUntilTableIsEnabled(Bytes.toBytes(tableName));
2269 }
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281 private void internalRestoreSnapshot(final String snapshotName, final String tableName)
2282 throws IOException, RestoreSnapshotException {
2283 HSnapshotDescription snapshot = new HSnapshotDescription(
2284 SnapshotDescription.newBuilder().setName(snapshotName).setTable(tableName).build());
2285
2286 try {
2287
2288 getMaster().restoreSnapshot(snapshot);
2289
2290 final long maxPauseTime = 5000;
2291 boolean done = false;
2292 int tries = 0;
2293 while (!done) {
2294 try {
2295
2296 long sleep = getPauseTime(tries++);
2297 sleep = sleep > maxPauseTime ? maxPauseTime : sleep;
2298 LOG.debug(tries + ") Sleeping: " + sleep + " ms while we wait for snapshot restore to complete.");
2299 Thread.sleep(sleep);
2300 } catch (InterruptedException e) {
2301 LOG.debug("Interrupted while waiting for snapshot " + snapshot + " restore to complete");
2302 Thread.currentThread().interrupt();
2303 }
2304 LOG.debug("Getting current status of snapshot restore from master...");
2305 done = getMaster().isRestoreSnapshotDone(snapshot);
2306 }
2307 if (!done) {
2308 throw new RestoreSnapshotException("Snapshot '" + snapshot.getName() + "' wasn't restored.");
2309 }
2310 } catch (RemoteException e) {
2311 throw RemoteExceptionHandler.decodeRemoteException(e);
2312 }
2313 }
2314
2315
2316
2317
2318
2319
2320 public List<SnapshotDescription> listSnapshots() throws IOException {
2321 List<SnapshotDescription> snapshots = new LinkedList<SnapshotDescription>();
2322 try {
2323 for (HSnapshotDescription snapshot: getMaster().getCompletedSnapshots()) {
2324 snapshots.add(snapshot.getProto());
2325 }
2326 } catch (RemoteException e) {
2327 throw RemoteExceptionHandler.decodeRemoteException(e);
2328 }
2329 return snapshots;
2330 }
2331
2332
2333
2334
2335
2336
2337 public void deleteSnapshot(final byte[] snapshotName) throws IOException {
2338
2339 HTableDescriptor.isLegalTableName(snapshotName);
2340
2341 SnapshotDescription snapshot = SnapshotDescription.newBuilder()
2342 .setName(Bytes.toString(snapshotName)).build();
2343 try {
2344 getMaster().deleteSnapshot(new HSnapshotDescription(snapshot));
2345 } catch (RemoteException e) {
2346 throw RemoteExceptionHandler.decodeRemoteException(e);
2347 }
2348 }
2349
2350
2351
2352
2353
2354
2355 public void deleteSnapshot(final String snapshotName) throws IOException {
2356 deleteSnapshot(Bytes.toBytes(snapshotName));
2357 }
2358 }