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  
25  import javax.ws.rs.GET;
26  import javax.ws.rs.Produces;
27  import javax.ws.rs.WebApplicationException;
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.Response.ResponseBuilder;
32  import javax.ws.rs.core.UriInfo;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  import org.apache.hadoop.hbase.ClusterStatus;
38  import org.apache.hadoop.hbase.HServerInfo;
39  import org.apache.hadoop.hbase.HServerLoad;
40  import org.apache.hadoop.hbase.client.HBaseAdmin;
41  import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
42  
43  public class StorageClusterStatusResource extends ResourceBase {
44    private static final Log LOG =
45      LogFactory.getLog(StorageClusterStatusResource.class);
46  
47    static CacheControl cacheControl;
48    static {
49      cacheControl = new CacheControl();
50      cacheControl.setNoCache(true);
51      cacheControl.setNoTransform(false);
52    }
53  
54    /**
55     * Constructor
56     * @throws IOException
57     */
58    public StorageClusterStatusResource() throws IOException {
59      super();
60    }
61  
62    @GET
63    @Produces({MIMETYPE_TEXT, MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF})
64    public Response get(final @Context UriInfo uriInfo) {
65      if (LOG.isDebugEnabled()) {
66        LOG.debug("GET " + uriInfo.getAbsolutePath());
67      }
68      servlet.getMetrics().incrementRequests(1);
69      try {
70        HBaseAdmin admin = new HBaseAdmin(servlet.getConfiguration());
71        ClusterStatus status = admin.getClusterStatus();
72        StorageClusterStatusModel model = new StorageClusterStatusModel();
73        model.setRegions(status.getRegionsCount());
74        model.setRequests(status.getRequestsCount());
75        model.setAverageLoad(status.getAverageLoad());
76        for (HServerInfo info: status.getServerInfo()) {
77          HServerLoad load = info.getLoad();
78          StorageClusterStatusModel.Node node = 
79            model.addLiveNode(
80              info.getServerAddress().getHostname() + ":" + 
81              Integer.toString(info.getServerAddress().getPort()),
82              info.getStartCode(), load.getUsedHeapMB(),
83              load.getMaxHeapMB());
84          node.setRequests(load.getNumberOfRequests());
85          for (HServerLoad.RegionLoad region: load.getRegionsLoad()) {
86            node.addRegion(region.getName(), region.getStores(),
87              region.getStorefiles(), region.getStorefileSizeMB(),
88              region.getMemStoreSizeMB(), region.getStorefileIndexSizeMB());
89          }
90        }
91        for (String name: status.getDeadServerNames()) {
92          model.addDeadNode(name);
93        }
94        ResponseBuilder response = Response.ok(model);
95        response.cacheControl(cacheControl);
96        return response.build();
97      } catch (IOException e) {
98        throw new WebApplicationException(e, 
99                    Response.Status.SERVICE_UNAVAILABLE);
100     }
101   }
102 }