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