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