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;
025import org.apache.commons.lang3.StringUtils;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.quotas.QuotaObserverChore;
029import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot;
030import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
031import org.apache.yetus.audience.InterfaceAudience;
032
033/**
034 * Impl for exposing HMaster Information through JMX
035 */
036@InterfaceAudience.Private
037public class MetricsMasterWrapperImpl implements MetricsMasterWrapper {
038
039  private final HMaster master;
040
041  public MetricsMasterWrapperImpl(final HMaster master) {
042    this.master = master;
043  }
044
045  @Override
046  public double getAverageLoad() {
047    return master.getAverageLoad();
048  }
049
050  @Override
051  public long getSplitPlanCount() {
052    if (master.getRegionNormalizerManager() == null) {
053      return 0;
054    }
055    return master.getRegionNormalizerManager().getSplitPlanCount();
056  }
057
058  @Override
059  public long getMergePlanCount() {
060    if (master.getRegionNormalizerManager() == null) {
061      return 0;
062    }
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  @Override
128  public int getNumDeadRegionServers() {
129    ServerManager serverManager = this.master.getServerManager();
130    if (serverManager == null) {
131      return 0;
132    }
133    return serverManager.getDeadServers().size();
134  }
135
136  @Override
137  public boolean isRunning() {
138    return !(master.isStopped() || master.isStopping());
139  }
140
141  @Override
142  public String getDrainingRegionServers() {
143    ServerManager serverManager = this.master.getServerManager();
144    if (serverManager == null) {
145      return "";
146    }
147    return StringUtils.join(serverManager.getDrainingServersList(), ";");
148  }
149
150  @Override
151  public int getNumDrainingRegionServers() {
152    ServerManager serverManager = this.master.getServerManager();
153    if (serverManager == null) {
154      return 0;
155    }
156    return serverManager.getDrainingServersList().size();
157  }
158
159  @Override
160  public String getServerName() {
161    ServerName serverName = master.getServerName();
162    if (serverName == null) {
163      return "";
164    }
165    return serverName.getServerName();
166  }
167
168  @Override
169  public boolean getIsActiveMaster() {
170    return master.isActiveMaster();
171  }
172
173  @Override
174  public long getNumWALFiles() {
175    return master.getNumWALFiles();
176  }
177
178  @Override
179  public Map<String, Entry<Long, Long>> getTableSpaceUtilization() {
180    if (master == null) {
181      return Collections.emptyMap();
182    }
183    QuotaObserverChore quotaChore = master.getQuotaObserverChore();
184    if (quotaChore == null) {
185      return Collections.emptyMap();
186    }
187    Map<TableName, SpaceQuotaSnapshot> tableSnapshots = quotaChore.getTableQuotaSnapshots();
188    Map<String, Entry<Long, Long>> convertedData = new HashMap<>();
189    for (Entry<TableName, SpaceQuotaSnapshot> entry : tableSnapshots.entrySet()) {
190      convertedData.put(entry.getKey().toString(), convertSnapshot(entry.getValue()));
191    }
192    return convertedData;
193  }
194
195  @Override
196  public Map<String, Entry<Long, Long>> getNamespaceSpaceUtilization() {
197    QuotaObserverChore quotaChore = master.getQuotaObserverChore();
198    if (quotaChore == null) {
199      return Collections.emptyMap();
200    }
201    Map<String, SpaceQuotaSnapshot> namespaceSnapshots = quotaChore.getNamespaceQuotaSnapshots();
202    Map<String, Entry<Long, Long>> convertedData = new HashMap<>();
203    for (Entry<String, SpaceQuotaSnapshot> entry : namespaceSnapshots.entrySet()) {
204      convertedData.put(entry.getKey(), convertSnapshot(entry.getValue()));
205    }
206    return convertedData;
207  }
208
209  Entry<Long, Long> convertSnapshot(SpaceQuotaSnapshot snapshot) {
210    return new SimpleImmutableEntry<Long, Long>(snapshot.getUsage(), snapshot.getLimit());
211  }
212
213  @Override
214  public long getOldWALsDirSize() {
215    if (master == null || !master.isInitialized()) {
216      return 0;
217    }
218    return master.getMasterWalManager().getOldWALsDirSize();
219  }
220}