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.util.Collection;
021import java.util.EnumSet;
022import java.util.List;
023import java.util.concurrent.ExecutionException;
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.AsyncAdmin;
029import org.apache.hadoop.hbase.master.MasterServices;
030import org.apache.hadoop.hbase.master.http.api_v1.cluster_metrics.model.ClusterMetrics;
031import org.apache.yetus.audience.InterfaceAudience;
032
033import org.apache.hbase.thirdparty.javax.ws.rs.GET;
034import org.apache.hbase.thirdparty.javax.ws.rs.Path;
035import org.apache.hbase.thirdparty.javax.ws.rs.Produces;
036import org.apache.hbase.thirdparty.javax.ws.rs.core.MediaType;
037
038/**
039 * The root object exposing a subset of {@link org.apache.hadoop.hbase.ClusterMetrics}.
040 */
041@InterfaceAudience.Private
042@Path("admin/cluster_metrics")
043@Produces({ MediaType.APPLICATION_JSON })
044public class ClusterMetricsResource {
045
046  // TODO: using the async client API lends itself well to using the JAX-RS 2.0 Spec's asynchronous
047  // server APIs. However, these are only available when Jersey is wired up using Servlet 3.x
048  // container and all of our existing InfoServer stuff is build on Servlet 2.x.
049  // See also https://blog.allegro.tech/2014/10/async-rest.html#mixing-with-completablefuture
050
051  private final AsyncAdmin admin;
052
053  @Inject
054  public ClusterMetricsResource(MasterServices master) {
055    this.admin = master.getAsyncConnection().getAdmin();
056  }
057
058  private org.apache.hadoop.hbase.ClusterMetrics get(EnumSet<Option> fields)
059    throws ExecutionException, InterruptedException {
060    return admin.getClusterMetrics(fields).get();
061  }
062
063  @GET
064  @Path("/")
065  public ClusterMetrics getBaseMetrics() throws ExecutionException, InterruptedException {
066    final EnumSet<Option> fields =
067      EnumSet.of(Option.HBASE_VERSION, Option.CLUSTER_ID, Option.MASTER, Option.BACKUP_MASTERS);
068    return ClusterMetrics.from(get(fields));
069  }
070
071  @GET
072  @Path("/live_servers")
073  public Collection<ServerMetrics> getLiveServers()
074    throws ExecutionException, InterruptedException {
075    return get(EnumSet.of(Option.LIVE_SERVERS)).getLiveServerMetrics().values();
076  }
077
078  @GET
079  @Path("/dead_servers")
080  public List<ServerName> getDeadServers() throws ExecutionException, InterruptedException {
081    return get(EnumSet.of(Option.DEAD_SERVERS)).getDeadServerNames();
082  }
083}