View Javadoc

1   /*
2    *
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
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
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.
18   */
19  
20  package org.apache.hadoop.hbase.rest;
21  
22  import java.io.IOException;
23  import java.util.Map;
24  
25  import javax.ws.rs.GET;
26  import javax.ws.rs.Produces;
27  import javax.ws.rs.core.CacheControl;
28  import javax.ws.rs.core.Context;
29  import javax.ws.rs.core.Response;
30  import javax.ws.rs.core.UriInfo;
31  import javax.ws.rs.core.Response.ResponseBuilder;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import org.apache.hadoop.hbase.MetaTableAccessor;
37  import org.apache.hadoop.hbase.classification.InterfaceAudience;
38  import org.apache.hadoop.hbase.TableName;
39  import org.apache.hadoop.hbase.HRegionInfo;
40  import org.apache.hadoop.hbase.ServerName;
41  import org.apache.hadoop.hbase.TableNotFoundException;
42  import org.apache.hadoop.hbase.client.Connection;
43  import org.apache.hadoop.hbase.client.ConnectionFactory;
44  import org.apache.hadoop.hbase.rest.model.TableInfoModel;
45  import org.apache.hadoop.hbase.rest.model.TableRegionModel;
46  
47  @InterfaceAudience.Private
48  public class RegionsResource extends ResourceBase {
49    private static final Log LOG = LogFactory.getLog(RegionsResource.class);
50  
51    static CacheControl cacheControl;
52    static {
53      cacheControl = new CacheControl();
54      cacheControl.setNoCache(true);
55      cacheControl.setNoTransform(false);
56    }
57  
58    TableResource tableResource;
59  
60    /**
61     * Constructor
62     * @param tableResource
63     * @throws IOException
64     */
65    public RegionsResource(TableResource tableResource) throws IOException {
66      super();
67      this.tableResource = tableResource;
68    }
69  
70    @GET
71    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
72      MIMETYPE_PROTOBUF_IETF})
73    public Response get(final @Context UriInfo uriInfo) {
74      if (LOG.isDebugEnabled()) {
75        LOG.debug("GET " + uriInfo.getAbsolutePath());
76      }
77      servlet.getMetrics().incrementRequests(1);
78      try {
79        TableName tableName = TableName.valueOf(tableResource.getName());
80        TableInfoModel model = new TableInfoModel(tableName.getNameAsString());
81  
82        Connection connection = ConnectionFactory.createConnection(servlet.getConfiguration());
83        @SuppressWarnings("deprecation")
84        Map<HRegionInfo, ServerName> regions = MetaTableAccessor
85            .allTableRegions(connection, tableName);
86        connection.close();
87        for (Map.Entry<HRegionInfo,ServerName> e: regions.entrySet()) {
88          HRegionInfo hri = e.getKey();
89          ServerName addr = e.getValue();
90          model.add(
91            new TableRegionModel(tableName.getNameAsString(), hri.getRegionId(),
92              hri.getStartKey(), hri.getEndKey(), addr.getHostAndPort()));
93        }
94        ResponseBuilder response = Response.ok(model);
95        response.cacheControl(cacheControl);
96        servlet.getMetrics().incrementSucessfulGetRequests(1);
97        return response.build();
98      } catch (TableNotFoundException e) {
99        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 }