001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master.http.api_v1.cluster_metrics.resource;
019
020import java.io.IOException;
021import java.util.Collection;
022import java.util.EnumSet;
023import java.util.List;
024import javax.inject.Inject;
025import org.apache.hadoop.hbase.ClusterMetrics.Option;
026import org.apache.hadoop.hbase.ServerMetrics;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.client.Admin;
029import org.apache.hadoop.hbase.client.Connection;
030import org.apache.hadoop.hbase.master.MasterServices;
031import org.apache.hadoop.hbase.master.http.api_v1.cluster_metrics.model.ClusterMetrics;
032import org.apache.yetus.audience.InterfaceAudience;
033
034import org.apache.hbase.thirdparty.javax.ws.rs.GET;
035import org.apache.hbase.thirdparty.javax.ws.rs.Path;
036import org.apache.hbase.thirdparty.javax.ws.rs.Produces;
037import org.apache.hbase.thirdparty.javax.ws.rs.core.MediaType;
038
039/**
040 * The root object exposing a subset of {@link org.apache.hadoop.hbase.ClusterMetrics}.
041 */
042@InterfaceAudience.Private
043@Path("admin/cluster_metrics")
044@Produces({ MediaType.APPLICATION_JSON })
045public class ClusterMetricsResource {
046
047  // TODO: using the async client API lends itself well to using the JAX-RS 2.0 Spec's asynchronous
048  // server APIs. However, these are only available when Jersey is wired up using Servlet 3.x
049  // container and all of our existing InfoServer stuff is build on Servlet 2.x.
050  // See also https://blog.allegro.tech/2014/10/async-rest.html#mixing-with-completablefuture
051
052  final Connection connection;
053
054  @Inject
055  public ClusterMetricsResource(MasterServices master) throws IOException {
056    this.connection = master.getConnection();
057  }
058
059  private org.apache.hadoop.hbase.ClusterMetrics get(EnumSet<Option> fields) throws IOException {
060    try (Admin admin = connection.getAdmin()) {
061      return admin.getClusterMetrics(fields);
062    }
063  }
064
065  @GET
066  @Path("/")
067  public ClusterMetrics getBaseMetrics() throws IOException {
068    final EnumSet<Option> fields =
069      EnumSet.of(Option.HBASE_VERSION, Option.CLUSTER_ID, Option.MASTER, Option.BACKUP_MASTERS);
070    return ClusterMetrics.from(get(fields));
071  }
072
073  @GET
074  @Path("/live_servers")
075  public Collection<ServerMetrics> getLiveServers() throws IOException {
076    return get(EnumSet.of(Option.LIVE_SERVERS)).getLiveServerMetrics().values();
077  }
078
079  @GET
080  @Path("/dead_servers")
081  public List<ServerName> getDeadServers() throws IOException {
082    return get(EnumSet.of(Option.DEAD_SERVERS)).getDeadServerNames();
083  }
084}