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;
019
020import java.io.IOException;
021import java.util.List;
022import java.util.Map;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.ServerName;
025import org.apache.hadoop.hbase.client.RegionInfoBuilder;
026import org.apache.hadoop.hbase.client.TableDescriptor;
027import org.apache.hadoop.hbase.master.HMaster;
028import org.apache.hadoop.hbase.master.RegionState;
029import org.apache.hadoop.hbase.master.assignment.RegionStateNode;
030import org.apache.hadoop.hbase.util.FSUtils;
031import org.apache.yetus.audience.InterfaceAudience;
032
033/**
034 * Utility used by the web UI JSP pages.
035 */
036@InterfaceAudience.Private
037public final class MasterStatusUtil {
038
039  private MasterStatusUtil() {
040    // Do not instantiate.
041  }
042
043  public static String getUserTables(HMaster master, List<TableDescriptor> tables) {
044    if (master.isInitialized()) {
045      try {
046        Map<String, TableDescriptor> descriptorMap = master.getTableDescriptors().getAll();
047        if (descriptorMap != null) {
048          for (TableDescriptor desc : descriptorMap.values()) {
049            if (!desc.getTableName().isSystemTable()) {
050              tables.add(desc);
051            }
052          }
053        }
054      } catch (IOException e) {
055        return "Got user tables error, " + e.getMessage();
056      }
057    }
058    return null;
059  }
060
061  public static Map<String, Integer> getFragmentationInfo(HMaster master, Configuration conf)
062    throws IOException {
063    boolean showFragmentation = conf.getBoolean("hbase.master.ui.fragmentation.enabled", false);
064    if (showFragmentation) {
065      return FSUtils.getTableFragmentation(master);
066    } else {
067      return null;
068    }
069  }
070
071  public static ServerName getMetaLocationOrNull(HMaster master) {
072    RegionStateNode rsn = master.getAssignmentManager().getRegionStates()
073      .getRegionStateNode(RegionInfoBuilder.FIRST_META_REGIONINFO);
074    if (rsn != null) {
075      return rsn.isInState(RegionState.State.OPEN) ? rsn.getRegionLocation() : null;
076    }
077    return null;
078  }
079
080  public static String serverNameLink(HMaster master, ServerName serverName) {
081    int infoPort = master.getRegionServerInfoPort(serverName);
082    String url = "//" + serverName.getHostname() + ":" + infoPort + "/rs-status";
083    if (infoPort > 0) {
084      return "<a href=\"" + url + "\">" + serverName.getServerName() + "</a>";
085    } else {
086      return serverName.getServerName();
087    }
088  }
089}