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.MetaScanner;
42  import org.apache.hadoop.hbase.rest.model.TableInfoModel;
43  import org.apache.hadoop.hbase.rest.model.TableRegionModel;
44  
45  @InterfaceAudience.Private
46  public class RegionsResource extends ResourceBase {
47    private static final Log LOG = LogFactory.getLog(RegionsResource.class);
48  
49    static CacheControl cacheControl;
50    static {
51      cacheControl = new CacheControl();
52      cacheControl.setNoCache(true);
53      cacheControl.setNoTransform(false);
54    }
55  
56    TableResource tableResource;
57  
58    /**
59     * Constructor
60     * @param tableResource
61     * @throws IOException
62     */
63    public RegionsResource(TableResource tableResource) throws IOException {
64      super();
65      this.tableResource = tableResource;
66    }
67  
68    @GET
69    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
70      MIMETYPE_PROTOBUF_IETF})
71    public Response get(final @Context UriInfo uriInfo) {
72      if (LOG.isDebugEnabled()) {
73        LOG.debug("GET " + uriInfo.getAbsolutePath());
74      }
75      servlet.getMetrics().incrementRequests(1);
76      try {
77        TableName tableName = TableName.valueOf(tableResource.getName());
78        TableInfoModel model = new TableInfoModel(tableName.getNameAsString());
79        Map<HRegionInfo,ServerName> regions = MetaScanner.allTableRegions(
80          servlet.getConfiguration(), null, tableName);
81        for (Map.Entry<HRegionInfo,ServerName> e: regions.entrySet()) {
82          HRegionInfo hri = e.getKey();
83          ServerName addr = e.getValue();
84          model.add(
85            new TableRegionModel(tableName.getNameAsString(), hri.getRegionId(),
86              hri.getStartKey(), hri.getEndKey(), addr.getHostAndPort()));
87        }
88        ResponseBuilder response = Response.ok(model);
89        response.cacheControl(cacheControl);
90        servlet.getMetrics().incrementSucessfulGetRequests(1);
91        return response.build();
92      } catch (TableNotFoundException e) {
93        servlet.getMetrics().incrementFailedGetRequests(1);
94        return Response.status(Response.Status.NOT_FOUND)
95          .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
96          .build();
97      } catch (IOException e) {
98        servlet.getMetrics().incrementFailedGetRequests(1);
99        return Response.status(Response.Status.SERVICE_UNAVAILABLE)
100         .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
101         .build();
102     }
103   }
104 }