View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest;
22  
23  import java.io.IOException;
24  import java.util.Map;
25  
26  import javax.ws.rs.GET;
27  import javax.ws.rs.Produces;
28  import javax.ws.rs.core.CacheControl;
29  import javax.ws.rs.core.Context;
30  import javax.ws.rs.core.Response;
31  import javax.ws.rs.core.UriInfo;
32  import javax.ws.rs.core.Response.ResponseBuilder;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  import org.apache.hadoop.hbase.HRegionInfo;
38  import org.apache.hadoop.hbase.ServerName;
39  import org.apache.hadoop.hbase.TableNotFoundException;
40  import org.apache.hadoop.hbase.client.MetaScanner;
41  import org.apache.hadoop.hbase.rest.model.TableInfoModel;
42  import org.apache.hadoop.hbase.rest.model.TableRegionModel;
43  import org.apache.hadoop.hbase.util.Bytes;
44  
45  public class RegionsResource extends ResourceBase {
46    private static final Log LOG = LogFactory.getLog(RegionsResource.class);
47  
48    static CacheControl cacheControl;
49    static {
50      cacheControl = new CacheControl();
51      cacheControl.setNoCache(true);
52      cacheControl.setNoTransform(false);
53    }
54  
55    TableResource tableResource;
56  
57    /**
58     * Constructor
59     * @param tableResource
60     * @throws IOException
61     */
62    public RegionsResource(TableResource tableResource) throws IOException {
63      super();
64      this.tableResource = tableResource;
65    }
66  
67    @GET
68    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF,
69      MIMETYPE_PROTOBUF_IETF})
70    public Response get(final @Context UriInfo uriInfo) {
71      if (LOG.isDebugEnabled()) {
72        LOG.debug("GET " + uriInfo.getAbsolutePath());
73      }
74      servlet.getMetrics().incrementRequests(1);
75      try {
76        String tableName = tableResource.getName();
77        TableInfoModel model = new TableInfoModel(tableName);
78        Map<HRegionInfo,ServerName> regions = MetaScanner.allTableRegions(
79          servlet.getConfiguration(), null, Bytes.toBytes(tableName), false);
80        for (Map.Entry<HRegionInfo,ServerName> e: regions.entrySet()) {
81          HRegionInfo hri = e.getKey();
82          ServerName addr = e.getValue();
83          model.add(
84            new TableRegionModel(tableName, hri.getRegionId(),
85              hri.getStartKey(), hri.getEndKey(), addr.getHostAndPort()));
86        }
87        ResponseBuilder response = Response.ok(model);
88        response.cacheControl(cacheControl);
89        servlet.getMetrics().incrementSucessfulGetRequests(1);
90        return response.build();
91      } catch (TableNotFoundException e) {
92        servlet.getMetrics().incrementFailedGetRequests(1);
93        return Response.status(Response.Status.NOT_FOUND)
94          .type(MIMETYPE_TEXT).entity("Not found" + CRLF)
95          .build();
96      } catch (IOException e) {
97        servlet.getMetrics().incrementFailedGetRequests(1);
98        return Response.status(Response.Status.SERVICE_UNAVAILABLE)
99          .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
100         .build();
101     }
102   }
103 }