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.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Map.Entry;
25  
26  import org.apache.hadoop.hbase.HServerLoad;
27  import org.apache.hadoop.hbase.ServerName;
28  import org.apache.hadoop.hbase.master.AssignmentManager.RegionState;
29  
30  /**
31   * Impl for exposing HMaster Information through JMX
32   */
33  public class MXBeanImpl implements MXBean {
34  
35    private final HMaster master;
36  
37    private static MXBeanImpl instance = null;
38    public synchronized static MXBeanImpl init(final HMaster master) {
39      if (instance == null) {
40        instance = new MXBeanImpl(master);
41      }
42      return instance;
43    }
44  
45    protected MXBeanImpl(final HMaster master) {
46      this.master = master;
47    }
48  
49    @Override
50    public double getAverageLoad() {
51      return master.getAverageLoad();
52    }
53  
54    @Override
55    public String getClusterId() {
56      return master.getClusterId();
57    }
58  
59    @Override
60    public String getZookeeperQuorum() {
61      return master.getZooKeeperWatcher().getQuorum();
62    }
63  
64    @Override
65    public String[] getCoprocessors() {
66      return master.getCoprocessors();
67    }
68  
69    @Override
70    public long getMasterStartTime() {
71      return master.getMasterStartTime();
72    }
73  
74    @Override
75    public long getMasterActiveTime() {
76      return master.getMasterActiveTime();
77    }
78  
79    @Override
80    public Map<String, HServerLoad> getRegionServers() {
81      Map<String, HServerLoad> data = new HashMap<String, HServerLoad>();
82      for (final Entry<ServerName, HServerLoad> entry :
83        master.getServerManager().getOnlineServers().entrySet()) {
84        data.put(entry.getKey().getServerName(),
85            entry.getValue());
86      }
87      return data;
88    }
89  
90    @Override
91    public String[] getDeadRegionServers() {
92      List<String> deadServers = new ArrayList<String>();
93      for (ServerName name : master.getServerManager().getDeadServers()) {
94        deadServers.add(name.getHostAndPort());
95      }
96      return deadServers.toArray(new String[0]);
97    }
98  
99    @Override
100   public RegionsInTransitionInfo[] getRegionsInTransition() {
101     List<RegionsInTransitionInfo> info =
102         new ArrayList<RegionsInTransitionInfo>();
103     for (final Entry<String, RegionState> entry :
104       master.getAssignmentManager().getRegionsInTransition().entrySet()) {
105       RegionsInTransitionInfo innerinfo = new RegionsInTransitionInfo() {
106 
107         @Override
108         public String getRegionState() {
109           return entry.getValue().getState().toString();
110         }
111 
112         @Override
113         public String getRegionName() {
114           return entry.getKey();
115         }
116 
117         @Override
118         public long getLastUpdateTime() {
119           return entry.getValue().getStamp();
120         }
121 
122         @Override
123         public String getRegionServerName() {
124           ServerName serverName = entry.getValue().getServerName();
125           if (serverName != null) {
126             return serverName.getServerName();
127           }
128           else {
129             return "";
130           }
131         }
132       };
133       info.add(innerinfo);
134     }
135     RegionsInTransitionInfo[] data =
136         new RegionsInTransitionInfo[info.size()];
137     info.toArray(data);
138     return data;
139   }
140 
141   @Override
142   public String getServerName() {
143     return master.getServerName().getServerName();
144   }
145 
146   @Override
147   public boolean getIsActiveMaster() {
148     return master.isActiveMaster();
149   }
150 }