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; 019 020import java.util.AbstractMap.SimpleImmutableEntry; 021import java.util.Collections; 022import java.util.HashMap; 023import java.util.Map; 024import java.util.Map.Entry; 025 026import org.apache.commons.lang3.StringUtils; 027import org.apache.hadoop.hbase.ServerName; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.yetus.audience.InterfaceAudience; 030import org.apache.hadoop.hbase.quotas.QuotaObserverChore; 031import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot; 032import org.apache.hadoop.hbase.zookeeper.ZKWatcher; 033 034/** 035 * Impl for exposing HMaster Information through JMX 036 */ 037@InterfaceAudience.Private 038public class MetricsMasterWrapperImpl implements MetricsMasterWrapper { 039 040 private final HMaster master; 041 042 public MetricsMasterWrapperImpl(final HMaster master) { 043 this.master = master; 044 } 045 046 @Override 047 public double getAverageLoad() { 048 return master.getAverageLoad(); 049 } 050 051 @Override 052 public long getSplitPlanCount() { 053 return master.getSplitPlanCount(); 054 } 055 056 @Override 057 public long getMergePlanCount() { 058 return master.getMergePlanCount(); 059 } 060 061 @Override 062 public long getMasterInitializationTime() { 063 return master.getMasterFinishedInitializationTime(); 064 } 065 066 @Override 067 public String getClusterId() { 068 return master.getClusterId(); 069 } 070 071 @Override 072 public String getZookeeperQuorum() { 073 ZKWatcher zk = master.getZooKeeper(); 074 if (zk == null) { 075 return ""; 076 } 077 return zk.getQuorum(); 078 } 079 080 @Override 081 public String[] getCoprocessors() { 082 return master.getMasterCoprocessors(); 083 } 084 085 @Override 086 public long getStartTime() { 087 return master.getMasterStartTime(); 088 } 089 090 @Override 091 public long getActiveTime() { 092 return master.getMasterActiveTime(); 093 } 094 095 @Override 096 public String getRegionServers() { 097 ServerManager serverManager = this.master.getServerManager(); 098 if (serverManager == null) { 099 return ""; 100 } 101 return StringUtils.join(serverManager.getOnlineServers().keySet(), ";"); 102 } 103 104 @Override 105 public int getNumRegionServers() { 106 ServerManager serverManager = this.master.getServerManager(); 107 if (serverManager == null) { 108 return 0; 109 } 110 return serverManager.getOnlineServers().size(); 111 } 112 113 @Override 114 public String getDeadRegionServers() { 115 ServerManager serverManager = this.master.getServerManager(); 116 if (serverManager == null) { 117 return ""; 118 } 119 return StringUtils.join(serverManager.getDeadServers().copyServerNames(), ";"); 120 } 121 122 123 @Override 124 public int getNumDeadRegionServers() { 125 ServerManager serverManager = this.master.getServerManager(); 126 if (serverManager == null) { 127 return 0; 128 } 129 return serverManager.getDeadServers().size(); 130 } 131 132 @Override 133 public String getServerName() { 134 ServerName serverName = master.getServerName(); 135 if (serverName == null) { 136 return ""; 137 } 138 return serverName.getServerName(); 139 } 140 141 @Override 142 public boolean getIsActiveMaster() { 143 return master.isActiveMaster(); 144 } 145 146 @Override 147 public long getNumWALFiles() { 148 return master.getNumWALFiles(); 149 } 150 151 @Override 152 public Map<String,Entry<Long,Long>> getTableSpaceUtilization() { 153 QuotaObserverChore quotaChore = master.getQuotaObserverChore(); 154 if (quotaChore == null) { 155 return Collections.emptyMap(); 156 } 157 Map<TableName,SpaceQuotaSnapshot> tableSnapshots = quotaChore.getTableQuotaSnapshots(); 158 Map<String,Entry<Long,Long>> convertedData = new HashMap<>(); 159 for (Entry<TableName,SpaceQuotaSnapshot> entry : tableSnapshots.entrySet()) { 160 convertedData.put(entry.getKey().toString(), convertSnapshot(entry.getValue())); 161 } 162 return convertedData; 163 } 164 165 @Override 166 public Map<String,Entry<Long,Long>> getNamespaceSpaceUtilization() { 167 QuotaObserverChore quotaChore = master.getQuotaObserverChore(); 168 if (quotaChore == null) { 169 return Collections.emptyMap(); 170 } 171 Map<String,SpaceQuotaSnapshot> namespaceSnapshots = quotaChore.getNamespaceQuotaSnapshots(); 172 Map<String,Entry<Long,Long>> convertedData = new HashMap<>(); 173 for (Entry<String,SpaceQuotaSnapshot> entry : namespaceSnapshots.entrySet()) { 174 convertedData.put(entry.getKey(), convertSnapshot(entry.getValue())); 175 } 176 return convertedData; 177 } 178 179 Entry<Long,Long> convertSnapshot(SpaceQuotaSnapshot snapshot) { 180 return new SimpleImmutableEntry<Long,Long>(snapshot.getUsage(), snapshot.getLimit()); 181 } 182}