001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest;
019
020import java.io.IOException;
021import java.util.List;
022import org.apache.hadoop.hbase.HRegionLocation;
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.TableNotFoundException;
026import org.apache.hadoop.hbase.client.Connection;
027import org.apache.hadoop.hbase.client.ConnectionFactory;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.hadoop.hbase.client.RegionLocator;
030import org.apache.hadoop.hbase.rest.model.TableInfoModel;
031import org.apache.hadoop.hbase.rest.model.TableRegionModel;
032import org.apache.yetus.audience.InterfaceAudience;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036import org.apache.hbase.thirdparty.javax.ws.rs.GET;
037import org.apache.hbase.thirdparty.javax.ws.rs.Produces;
038import org.apache.hbase.thirdparty.javax.ws.rs.core.CacheControl;
039import org.apache.hbase.thirdparty.javax.ws.rs.core.Context;
040import org.apache.hbase.thirdparty.javax.ws.rs.core.Response;
041import org.apache.hbase.thirdparty.javax.ws.rs.core.Response.ResponseBuilder;
042import org.apache.hbase.thirdparty.javax.ws.rs.core.UriInfo;
043
044@InterfaceAudience.Private
045public class RegionsResource extends ResourceBase {
046  private static final Logger LOG = LoggerFactory.getLogger(RegionsResource.class);
047
048  static CacheControl cacheControl;
049  static {
050    cacheControl = new CacheControl();
051    cacheControl.setNoCache(true);
052    cacheControl.setNoTransform(false);
053  }
054
055  TableResource tableResource;
056
057  /**
058   * Constructor
059   */
060  public RegionsResource(TableResource tableResource) throws IOException {
061    super();
062    this.tableResource = tableResource;
063  }
064
065  @GET
066  @Produces({ MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
067    MIMETYPE_PROTOBUF_IETF })
068  public Response get(final @Context UriInfo uriInfo) {
069    if (LOG.isTraceEnabled()) {
070      LOG.trace("GET " + uriInfo.getAbsolutePath());
071    }
072    servlet.getMetrics().incrementRequests(1);
073    try {
074      TableName tableName = TableName.valueOf(tableResource.getName());
075      if (!tableResource.exists()) {
076        throw new TableNotFoundException(tableName);
077      }
078      TableInfoModel model = new TableInfoModel(tableName.getNameAsString());
079
080      List<HRegionLocation> locs;
081      try (Connection connection = ConnectionFactory.createConnection(servlet.getConfiguration());
082        RegionLocator locator = connection.getRegionLocator(tableName)) {
083        locs = locator.getAllRegionLocations();
084      }
085      for (HRegionLocation loc : locs) {
086        RegionInfo hri = loc.getRegion();
087        ServerName addr = loc.getServerName();
088        model.add(new TableRegionModel(tableName.getNameAsString(), hri.getRegionId(),
089          hri.getStartKey(), hri.getEndKey(), addr.getAddress().toString()));
090      }
091      ResponseBuilder response = Response.ok(model);
092      response.cacheControl(cacheControl);
093      servlet.getMetrics().incrementSucessfulGetRequests(1);
094      return response.build();
095    } catch (TableNotFoundException e) {
096      servlet.getMetrics().incrementFailedGetRequests(1);
097      return Response.status(Response.Status.NOT_FOUND).type(MIMETYPE_TEXT)
098        .entity("Not found" + CRLF).build();
099    } catch (IOException e) {
100      servlet.getMetrics().incrementFailedGetRequests(1);
101      return Response.status(Response.Status.SERVICE_UNAVAILABLE).type(MIMETYPE_TEXT)
102        .entity("Unavailable" + CRLF).build();
103    }
104  }
105}