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.net.InetSocketAddress;
25  import java.util.Map;
26  
27  import javax.ws.rs.GET;
28  import javax.ws.rs.Produces;
29  import javax.ws.rs.WebApplicationException;
30  import javax.ws.rs.core.CacheControl;
31  import javax.ws.rs.core.Context;
32  import javax.ws.rs.core.Response;
33  import javax.ws.rs.core.UriInfo;
34  import javax.ws.rs.core.Response.ResponseBuilder;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  import org.apache.hadoop.hbase.HRegionInfo;
40  import org.apache.hadoop.hbase.HServerAddress;
41  import org.apache.hadoop.hbase.TableNotFoundException;
42  import org.apache.hadoop.hbase.client.HTable;
43  import org.apache.hadoop.hbase.client.HTableInterface;
44  import org.apache.hadoop.hbase.client.HTablePool;
45  import org.apache.hadoop.hbase.rest.model.TableInfoModel;
46  import org.apache.hadoop.hbase.rest.model.TableRegionModel;
47  
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    private Map<HRegionInfo,HServerAddress> getTableRegions()
71        throws IOException {
72      HTablePool pool = servlet.getTablePool();
73      HTableInterface table = pool.getTable(tableResource.getName());
74      try {
75        return ((HTable)table).getRegionsInfo();
76      } finally {
77        pool.putTable(table);
78      }
79    }
80  
81    @GET
82    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF})
83    public Response get(final @Context UriInfo uriInfo) {
84      if (LOG.isDebugEnabled()) {
85        LOG.debug("GET " + uriInfo.getAbsolutePath());
86      }
87      servlet.getMetrics().incrementRequests(1);
88      try {
89        String tableName = tableResource.getName();
90        TableInfoModel model = new TableInfoModel(tableName);
91        Map<HRegionInfo,HServerAddress> regions = getTableRegions();
92        for (Map.Entry<HRegionInfo,HServerAddress> e: regions.entrySet()) {
93          HRegionInfo hri = e.getKey();
94          HServerAddress addr = e.getValue();
95          InetSocketAddress sa = addr.getInetSocketAddress();
96          model.add(
97            new TableRegionModel(tableName, hri.getRegionId(),
98              hri.getStartKey(), hri.getEndKey(),
99              sa.getHostName() + ":" + Integer.valueOf(sa.getPort())));
100       }
101       ResponseBuilder response = Response.ok(model);
102       response.cacheControl(cacheControl);
103       return response.build();
104     } catch (TableNotFoundException e) {
105       throw new WebApplicationException(Response.Status.NOT_FOUND);
106     } catch (IOException e) {
107       throw new WebApplicationException(e,
108                   Response.Status.SERVICE_UNAVAILABLE);
109     }
110   }
111 }