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