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 javax.ws.rs.GET;
023import javax.ws.rs.Produces;
024import javax.ws.rs.core.CacheControl;
025import javax.ws.rs.core.Context;
026import javax.ws.rs.core.Response;
027import javax.ws.rs.core.Response.ResponseBuilder;
028import javax.ws.rs.core.UriInfo;
029import java.io.IOException;
030import java.util.Map;
031
032import org.apache.hadoop.hbase.MetaTableAccessor;
033import org.apache.hadoop.hbase.ServerName;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.TableNotFoundException;
036import org.apache.hadoop.hbase.client.Connection;
037import org.apache.hadoop.hbase.client.ConnectionFactory;
038import org.apache.hadoop.hbase.client.RegionInfo;
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      TableInfoModel model = new TableInfoModel(tableName.getNameAsString());
079
080      Connection connection = ConnectionFactory.createConnection(servlet.getConfiguration());
081      @SuppressWarnings("deprecation")
082      Map<RegionInfo, ServerName> regions = MetaTableAccessor
083          .allTableRegions(connection, tableName);
084      connection.close();
085      for (Map.Entry<RegionInfo,ServerName> e: regions.entrySet()) {
086        RegionInfo hri = e.getKey();
087        ServerName addr = e.getValue();
088        model.add(
089          new TableRegionModel(tableName.getNameAsString(), hri.getRegionId(),
090            hri.getStartKey(), hri.getEndKey(), addr.getHostAndPort()));
091      }
092      ResponseBuilder response = Response.ok(model);
093      response.cacheControl(cacheControl);
094      servlet.getMetrics().incrementSucessfulGetRequests(1);
095      return response.build();
096    } catch (TableNotFoundException e) {
097      servlet.getMetrics().incrementFailedGetRequests(1);
098      return Response.status(Response.Status.NOT_FOUND)
099        .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
100        .build();
101    } catch (IOException e) {
102      servlet.getMetrics().incrementFailedGetRequests(1);
103      return Response.status(Response.Status.SERVICE_UNAVAILABLE)
104        .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
105        .build();
106    }
107  }
108}