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