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.classification.InterfaceAudience;
37  import org.apache.hadoop.hbase.TableName;
38  import org.apache.hadoop.hbase.HRegionInfo;
39  import org.apache.hadoop.hbase.ServerName;
40  import org.apache.hadoop.hbase.TableNotFoundException;
41  import org.apache.hadoop.hbase.client.Connection;
42  import org.apache.hadoop.hbase.client.ConnectionFactory;
43  import org.apache.hadoop.hbase.client.MetaScanner;
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.isTraceEnabled()) {
75        LOG.trace("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        Map<HRegionInfo, ServerName> regions = MetaScanner.allTableRegions(connection, tableName);
84        connection.close();
85        for (Map.Entry<HRegionInfo,ServerName> e: regions.entrySet()) {
86          HRegionInfo hri = e.getKey();
87          ServerName addr = e.getValue();
88          model.add(
89            new TableRegionModel(tableName.getNameAsString(), hri.getRegionId(),
90              hri.getStartKey(), hri.getEndKey(), addr.getHostAndPort()));
91        }
92        ResponseBuilder response = Response.ok(model);
93        response.cacheControl(cacheControl);
94        servlet.getMetrics().incrementSucessfulGetRequests(1);
95        return response.build();
96      } catch (TableNotFoundException e) {
97        servlet.getMetrics().incrementFailedGetRequests(1);
98        return Response.status(Response.Status.NOT_FOUND)
99          .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 }