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.io.IOException;
021import java.util.AbstractMap.SimpleImmutableEntry;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.Map.Entry;
027
028import org.apache.commons.lang3.StringUtils;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.RegionInfo;
032import org.apache.hadoop.hbase.client.TableDescriptor;
033import org.apache.yetus.audience.InterfaceAudience;
034import org.apache.hadoop.hbase.quotas.QuotaObserverChore;
035import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot;
036import org.apache.hadoop.hbase.util.PairOfSameType;
037import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
038
039/**
040 * Impl for exposing HMaster Information through JMX
041 */
042@InterfaceAudience.Private
043public class MetricsMasterWrapperImpl implements MetricsMasterWrapper {
044
045  private final HMaster master;
046
047  public MetricsMasterWrapperImpl(final HMaster master) {
048    this.master = master;
049  }
050
051  @Override
052  public double getAverageLoad() {
053    return master.getAverageLoad();
054  }
055
056  @Override
057  public long getSplitPlanCount() {
058    return master.getRegionNormalizerManager().getSplitPlanCount();
059  }
060
061  @Override
062  public long getMergePlanCount() {
063    return master.getRegionNormalizerManager().getMergePlanCount();
064  }
065
066  @Override
067  public long getMasterInitializationTime() {
068    return master.getMasterFinishedInitializationTime();
069  }
070
071  @Override
072  public String getClusterId() {
073    return master.getClusterId();
074  }
075
076  @Override
077  public String getZookeeperQuorum() {
078    ZKWatcher zk = master.getZooKeeper();
079    if (zk == null) {
080      return "";
081    }
082    return zk.getQuorum();
083  }
084
085  @Override
086  public String[] getCoprocessors() {
087    return master.getMasterCoprocessors();
088  }
089
090  @Override
091  public long getStartTime() {
092    return master.getMasterStartTime();
093  }
094
095  @Override
096  public long getActiveTime() {
097    return master.getMasterActiveTime();
098  }
099
100  @Override
101  public String getRegionServers() {
102    ServerManager serverManager = this.master.getServerManager();
103    if (serverManager == null) {
104      return "";
105    }
106    return StringUtils.join(serverManager.getOnlineServers().keySet(), ";");
107  }
108
109  @Override
110  public int getNumRegionServers() {
111    ServerManager serverManager = this.master.getServerManager();
112    if (serverManager == null) {
113      return 0;
114    }
115    return serverManager.getOnlineServers().size();
116  }
117
118  @Override
119  public String getDeadRegionServers() {
120    ServerManager serverManager = this.master.getServerManager();
121    if (serverManager == null) {
122      return "";
123    }
124    return StringUtils.join(serverManager.getDeadServers().copyServerNames(), ";");
125  }
126
127
128  @Override
129  public int getNumDeadRegionServers() {
130    ServerManager serverManager = this.master.getServerManager();
131    if (serverManager == null) {
132      return 0;
133    }
134    return serverManager.getDeadServers().size();
135  }
136
137  @Override public boolean isRunning() {
138    return !(master.isStopped() || master.isStopping());
139  }
140
141  @Override
142  public String getServerName() {
143    ServerName serverName = master.getServerName();
144    if (serverName == null) {
145      return "";
146    }
147    return serverName.getServerName();
148  }
149
150  @Override
151  public boolean getIsActiveMaster() {
152    return master.isActiveMaster();
153  }
154
155  @Override
156  public long getNumWALFiles() {
157    return master.getNumWALFiles();
158  }
159
160  @Override
161  public Map<String,Entry<Long,Long>> getTableSpaceUtilization() {
162    if (master == null) {
163      return Collections.emptyMap();
164    }
165    QuotaObserverChore quotaChore = master.getQuotaObserverChore();
166    if (quotaChore == null) {
167      return Collections.emptyMap();
168    }
169    Map<TableName,SpaceQuotaSnapshot> tableSnapshots = quotaChore.getTableQuotaSnapshots();
170    Map<String,Entry<Long,Long>> convertedData = new HashMap<>();
171    for (Entry<TableName,SpaceQuotaSnapshot> entry : tableSnapshots.entrySet()) {
172      convertedData.put(entry.getKey().toString(), convertSnapshot(entry.getValue()));
173    }
174    return convertedData;
175  }
176
177  @Override
178  public Map<String,Entry<Long,Long>> getNamespaceSpaceUtilization() {
179    QuotaObserverChore quotaChore = master.getQuotaObserverChore();
180    if (quotaChore == null) {
181      return Collections.emptyMap();
182    }
183    Map<String,SpaceQuotaSnapshot> namespaceSnapshots = quotaChore.getNamespaceQuotaSnapshots();
184    Map<String,Entry<Long,Long>> convertedData = new HashMap<>();
185    for (Entry<String,SpaceQuotaSnapshot> entry : namespaceSnapshots.entrySet()) {
186      convertedData.put(entry.getKey(), convertSnapshot(entry.getValue()));
187    }
188    return convertedData;
189  }
190
191  Entry<Long,Long> convertSnapshot(SpaceQuotaSnapshot snapshot) {
192    return new SimpleImmutableEntry<Long,Long>(snapshot.getUsage(), snapshot.getLimit());
193  }
194
195  @Override
196  public PairOfSameType<Integer> getRegionCounts() {
197    try {
198      if (!master.isInitialized()) {
199        return new PairOfSameType<>(0, 0);
200      }
201      Integer onlineRegionCount = 0;
202      Integer offlineRegionCount = 0;
203
204      List<TableDescriptor> descriptors = master.listTableDescriptors(null, null,
205          null, false);
206
207      for (TableDescriptor htDesc : descriptors) {
208        TableName tableName = htDesc.getTableName();
209        Map<RegionState.State, List<RegionInfo>> tableRegions =
210            master.getAssignmentManager().getRegionStates()
211                .getRegionByStateOfTable(tableName);
212        onlineRegionCount += tableRegions.get(RegionState.State.OPEN).size();
213        offlineRegionCount += tableRegions.get(RegionState.State.OFFLINE).size();
214      }
215      return new PairOfSameType<>(onlineRegionCount, offlineRegionCount);
216    } catch (IOException e) {
217      return new PairOfSameType<>(0, 0);
218    }
219  }
220}