diff --git a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/ResourceBase.java b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/ResourceBase.java index 422f4a3b9430..a37fe3a350f7 100644 --- a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/ResourceBase.java +++ b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/ResourceBase.java @@ -18,6 +18,7 @@ package org.apache.hadoop.hbase.rest; import java.io.IOException; +import org.apache.hadoop.hbase.TableNotEnabledException; import org.apache.hadoop.hbase.TableNotFoundException; import org.apache.hadoop.hbase.client.RetriesExhaustedException; import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException; @@ -74,6 +75,15 @@ protected Response processException(Throwable exp) { RetriesExhaustedException retryException = (RetriesExhaustedException) exp; processException(retryException.getCause()); } + if (exp instanceof TableNotEnabledException) { + throwServiceUnavailableException(exp); + } + + throwServiceUnavailableException(exp); + return null; + } + + private static void throwServiceUnavailableException(Throwable exp) { throw new WebApplicationException( Response.status(Response.Status.SERVICE_UNAVAILABLE).type(MIMETYPE_TEXT) .entity("Unavailable" + CRLF + StringUtils.stringifyException(exp) + CRLF).build()); diff --git a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/ServiceUnavailableException.java b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/ServiceUnavailableException.java new file mode 100644 index 000000000000..46b3e5ed14af --- /dev/null +++ b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/ServiceUnavailableException.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.rest; + +import java.io.IOException; +import org.apache.yetus.audience.InterfaceAudience; + +@InterfaceAudience.Private +public class ServiceUnavailableException extends IOException { + public ServiceUnavailableException(final String msg) { + super(msg); + } +} diff --git a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/TableResource.java b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/TableResource.java index 2c56706e06df..d77efe817789 100644 --- a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/TableResource.java +++ b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/TableResource.java @@ -43,7 +43,7 @@ @InterfaceAudience.Private public class TableResource extends ResourceBase { - String table; + private final String table; private static final Logger LOG = LoggerFactory.getLogger(TableResource.class); private static final Decoder base64Urldecoder = java.util.Base64.getUrlDecoder(); diff --git a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/TableScanResource.java b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/TableScanResource.java index 4bb30b0cc3c7..866ad86fac16 100644 --- a/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/TableScanResource.java +++ b/hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/TableScanResource.java @@ -63,36 +63,43 @@ public CellSetModelStream get(final @Context UriInfo uriInfo) { LOG.trace("GET " + uriInfo.getAbsolutePath()); } servlet.getMetrics().incrementRequests(1); - final int rowsToSend = userRequestedLimit; - servlet.getMetrics().incrementSucessfulScanRequests(1); - final Iterator itr = results.iterator(); - return new CellSetModelStream(new ArrayList() { - @Override - public Iterator iterator() { - return new Iterator() { - int count = rowsToSend; - - @Override - public boolean hasNext() { - return count > 0 && itr.hasNext(); - } + try { + final int rowsToSend = userRequestedLimit; + servlet.getMetrics().incrementSucessfulScanRequests(1); + final Iterator itr = results.iterator(); + return new CellSetModelStream(new ArrayList() { + @Override + public Iterator iterator() { + return new Iterator() { + int count = rowsToSend; - @Override - public RowModel next() { - Result rs = itr.next(); - if ((rs == null) || (count <= 0)) { - return null; + @Override + public boolean hasNext() { + return count > 0 && itr.hasNext(); } - RowModel rModel = RestUtil.createRowModelFromResult(rs); - count--; - if (count == 0) { - results.close(); + + @Override + public RowModel next() { + Result rs = itr.next(); + if ((rs == null) || (count <= 0)) { + return null; + } + RowModel rModel = RestUtil.createRowModelFromResult(rs); + count--; + if (count == 0) { + results.close(); + } + return rModel; } - return rModel; - } - }; - } - }); + }; + } + }); + } catch (Exception exp) { + servlet.getMetrics().incrementFailedScanRequests(1); + processException(exp); + LOG.warn(exp.toString(), exp); + return null; + } } @GET diff --git a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestTableResource.java b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestTableResource.java index c27d8ee2347a..decf8acb45c3 100644 --- a/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestTableResource.java +++ b/hbase-rest/src/test/java/org/apache/hadoop/hbase/rest/TestTableResource.java @@ -71,6 +71,8 @@ public class TestTableResource { private static final String COLUMN_FAMILY = "test"; private static final String COLUMN = COLUMN_FAMILY + ":qualifier"; private static final int NUM_REGIONS = 4; + private static final String DISABLED_TABLE_NAME = "disabled"; + private static final TableName DISABLED_TABLE = TableName.valueOf(DISABLED_TABLE_NAME); private static List regionMap; private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); @@ -117,6 +119,10 @@ public static void setUpBeforeClass() throws Exception { regionMap = m; LOG.error("regions: " + regionMap); regionLocator.close(); + + TEST_UTIL.createTable(DISABLED_TABLE, "cf"); + TEST_UTIL.getAdmin().disableTable(DISABLED_TABLE); + TEST_UTIL.waitTableDisabled(DISABLED_TABLE, 30); } @AfterClass @@ -262,4 +268,10 @@ public void testTableNotFound() throws IOException { assertEquals(404, response2.getCode()); } + @Test + public void testDisabledTableScan() throws IOException { + Response response = client.get("/" + DISABLED_TABLE_NAME + "/*", Constants.MIMETYPE_JSON); + assertEquals(503, response.getCode()); + assertTrue(Bytes.toString(response.getBody()).contains("Table disabled.")); + } }