HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-rest / src / main / java / org / apache / hadoop / hbase / rest / RegionsResource.java
blob784894e275713ba729a72585ade847f5168f696e
1 /*
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 package org.apache.hadoop.hbase.rest;
22 import java.io.IOException;
23 import java.util.List;
24 import org.apache.hadoop.hbase.HRegionLocation;
25 import org.apache.hadoop.hbase.ServerName;
26 import org.apache.hadoop.hbase.TableName;
27 import org.apache.hadoop.hbase.TableNotFoundException;
28 import org.apache.hadoop.hbase.client.Connection;
29 import org.apache.hadoop.hbase.client.ConnectionFactory;
30 import org.apache.hadoop.hbase.client.RegionInfo;
31 import org.apache.hadoop.hbase.client.RegionLocator;
32 import org.apache.hadoop.hbase.rest.model.TableInfoModel;
33 import org.apache.hadoop.hbase.rest.model.TableRegionModel;
34 import org.apache.yetus.audience.InterfaceAudience;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import org.apache.hbase.thirdparty.javax.ws.rs.GET;
39 import org.apache.hbase.thirdparty.javax.ws.rs.Produces;
40 import org.apache.hbase.thirdparty.javax.ws.rs.core.CacheControl;
41 import org.apache.hbase.thirdparty.javax.ws.rs.core.Context;
42 import org.apache.hbase.thirdparty.javax.ws.rs.core.Response;
43 import org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder;
44 import org.apache.hbase.thirdparty.javax.ws.rs.core.UriInfo;
46 @InterfaceAudience.Private
47 public class RegionsResource extends ResourceBase {
48 private static final Logger LOG = LoggerFactory.getLogger(RegionsResource.class);
50 static CacheControl cacheControl;
51 static {
52 cacheControl = new CacheControl();
53 cacheControl.setNoCache(true);
54 cacheControl.setNoTransform(false);
57 TableResource tableResource;
59 /**
60 * Constructor
61 * @param tableResource
62 * @throws IOException
64 public RegionsResource(TableResource tableResource) throws IOException {
65 super();
66 this.tableResource = tableResource;
69 @GET
70 @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
71 MIMETYPE_PROTOBUF_IETF})
72 public Response get(final @Context UriInfo uriInfo) {
73 if (LOG.isTraceEnabled()) {
74 LOG.trace("GET " + uriInfo.getAbsolutePath());
76 servlet.getMetrics().incrementRequests(1);
77 try {
78 TableName tableName = TableName.valueOf(tableResource.getName());
79 if (!tableResource.exists()) {
80 throw new TableNotFoundException(tableName);
82 TableInfoModel model = new TableInfoModel(tableName.getNameAsString());
84 List<HRegionLocation> locs;
85 try (Connection connection = ConnectionFactory.createConnection(servlet.getConfiguration());
86 RegionLocator locator = connection.getRegionLocator(tableName)) {
87 locs = locator.getAllRegionLocations();
89 for (HRegionLocation loc : locs) {
90 RegionInfo hri = loc.getRegion();
91 ServerName addr = loc.getServerName();
92 model.add(new TableRegionModel(tableName.getNameAsString(), hri.getRegionId(),
93 hri.getStartKey(), hri.getEndKey(), addr.getAddress().toString()));
95 ResponseBuilder response = Response.ok(model);
96 response.cacheControl(cacheControl);
97 servlet.getMetrics().incrementSucessfulGetRequests(1);
98 return response.build();
99 } catch (TableNotFoundException e) {
100 servlet.getMetrics().incrementFailedGetRequests(1);
101 return Response.status(Response.Status.NOT_FOUND)
102 .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
103 .build();
104 } catch (IOException e) {
105 servlet.getMetrics().incrementFailedGetRequests(1);
106 return Response.status(Response.Status.SERVICE_UNAVAILABLE)
107 .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
108 .build();