View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master;
19  
20  import java.util.AbstractMap.SimpleImmutableEntry;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.Map.Entry;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.apache.hadoop.hbase.ServerName;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.quotas.QuotaObserverChore;
31  import org.apache.hadoop.hbase.quotas.SpaceQuotaSnapshot;
32  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
33  
34  /**
35   * Impl for exposing HMaster Information through JMX
36   */
37  @InterfaceAudience.Private
38  public class MetricsMasterWrapperImpl implements MetricsMasterWrapper {
39  
40    private final HMaster master;
41  
42    public MetricsMasterWrapperImpl(final HMaster master) {
43      this.master = master;
44    }
45  
46    @Override
47    public double getAverageLoad() {
48      return master.getAverageLoad();
49    }
50  
51    @Override
52    public String getClusterId() {
53      return master.getClusterId();
54    }
55  
56    @Override
57    public String getZookeeperQuorum() {
58      ZooKeeperWatcher zk = master.getZooKeeper();
59      if (zk == null) {
60        return "";
61      }
62      return zk.getQuorum();
63    }
64  
65    @Override
66    public String[] getCoprocessors() {
67      return master.getMasterCoprocessors();
68    }
69  
70    @Override
71    public long getStartTime() {
72      return master.getMasterStartTime();
73    }
74  
75    @Override
76    public long getActiveTime() {
77      return master.getMasterActiveTime();
78    }
79  
80    @Override
81    public String getRegionServers() {
82      ServerManager serverManager = this.master.getServerManager();
83      if (serverManager == null) {
84        return "";
85      }
86      return StringUtils.join(serverManager.getOnlineServers().keySet(), ";");
87    }
88    
89    @Override
90    public int getNumRegionServers() {
91      ServerManager serverManager = this.master.getServerManager();
92      if (serverManager == null) {
93        return 0;
94      }
95      return serverManager.getOnlineServers().size();
96    }
97  
98    @Override
99    public String getDeadRegionServers() {
100     ServerManager serverManager = this.master.getServerManager();
101     if (serverManager == null) {
102       return "";
103     }
104     return StringUtils.join(serverManager.getDeadServers().copyServerNames(), ";");
105   }
106 
107   
108   @Override
109   public int getNumDeadRegionServers() {
110     ServerManager serverManager = this.master.getServerManager();
111     if (serverManager == null) {
112       return 0;
113     }
114     return serverManager.getDeadServers().size();
115   }
116 
117   @Override
118   public String getServerName() {
119     ServerName serverName = master.getServerName();
120     if (serverName == null) {
121       return "";
122     }
123     return serverName.getServerName();
124   }
125 
126   @Override
127   public boolean getIsActiveMaster() {
128     return master.isActiveMaster();
129   }
130 
131   @Override
132   public long getNumWALFiles() {
133     return master.getNumWALFiles();
134   }
135 
136   @Override
137   public Map<String,Entry<Long,Long>> getTableSpaceUtilization() {
138     QuotaObserverChore quotaChore = master.getQuotaObserverChore();
139     if (quotaChore == null) {
140       return Collections.emptyMap();
141     }
142     Map<TableName,SpaceQuotaSnapshot> tableSnapshots = quotaChore.getTableQuotaSnapshots();
143     Map<String,Entry<Long,Long>> convertedData = new HashMap<>();
144     for (Entry<TableName,SpaceQuotaSnapshot> entry : tableSnapshots.entrySet()) {
145       convertedData.put(entry.getKey().toString(), convertSnapshot(entry.getValue()));
146     }
147     return convertedData;
148   }
149 
150   @Override
151   public Map<String,Entry<Long,Long>> getNamespaceSpaceUtilization() {
152     QuotaObserverChore quotaChore = master.getQuotaObserverChore();
153     if (quotaChore == null) {
154       return Collections.emptyMap();
155     }
156     Map<String,SpaceQuotaSnapshot> namespaceSnapshots = quotaChore.getNamespaceQuotaSnapshots();
157     Map<String,Entry<Long,Long>> convertedData = new HashMap<>();
158     for (Entry<String,SpaceQuotaSnapshot> entry : namespaceSnapshots.entrySet()) {
159       convertedData.put(entry.getKey(), convertSnapshot(entry.getValue()));
160     }
161     return convertedData;
162   }
163 
164   Entry<Long,Long> convertSnapshot(SpaceQuotaSnapshot snapshot) {
165     return new SimpleImmutableEntry<Long,Long>(snapshot.getUsage(), snapshot.getLimit());
166   }
167 }