View Javadoc

1   package org.apache.helix.monitoring;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.management.ManagementFactory;
23  import java.util.concurrent.ConcurrentHashMap;
24  import java.util.concurrent.TimeUnit;
25  
26  import javax.management.MBeanServer;
27  import javax.management.MalformedObjectNameException;
28  import javax.management.ObjectName;
29  
30  import org.apache.helix.monitoring.mbeans.StateTransitionStatMonitor;
31  import org.apache.log4j.Logger;
32  
33  
34  public class ParticipantMonitor
35  {
36    private final ConcurrentHashMap<StateTransitionContext, StateTransitionStatMonitor> _monitorMap
37     = new ConcurrentHashMap<StateTransitionContext, StateTransitionStatMonitor>();
38    private static final Logger LOG = Logger.getLogger(ParticipantMonitor.class);
39  
40    private MBeanServer _beanServer;
41  
42    public ParticipantMonitor()
43    {
44      try
45      {
46        _beanServer = ManagementFactory.getPlatformMBeanServer();
47      }
48      catch(Exception e)
49      {
50        LOG.warn(e);
51        e.printStackTrace();
52        _beanServer = null;
53      }
54    }
55  
56    public void reportTransitionStat(StateTransitionContext cxt,
57        StateTransitionDataPoint data)
58    {
59      if(_beanServer == null)
60      {
61        LOG.warn("bean server is null, skip reporting");
62        return;
63      }
64      try
65      {
66        if(!_monitorMap.containsKey(cxt))
67        {
68          synchronized(this)
69          {
70            if(!_monitorMap.containsKey(cxt))
71            {
72              StateTransitionStatMonitor bean = new StateTransitionStatMonitor(cxt, TimeUnit.MILLISECONDS);
73              _monitorMap.put(cxt, bean);
74              String beanName = cxt.toString();
75              register(bean, getObjectName(beanName));
76            }
77          }
78        }
79        _monitorMap.get(cxt).addDataPoint(data);
80      }
81      catch(Exception e)
82      {
83        LOG.warn(e);
84        e.printStackTrace();
85      }
86    }
87  
88  
89    private ObjectName getObjectName(String name) throws MalformedObjectNameException
90    {
91      LOG.info("Registering bean: "+name);
92      return new ObjectName("CLMParticipantReport:"+name);
93    }
94  
95    private void register(Object bean, ObjectName name)
96    {
97      if(_beanServer == null)
98      {
99        LOG.warn("bean server is null, skip reporting");
100       return;
101     }
102     try
103     {
104       _beanServer.unregisterMBean(name);
105     }
106     catch (Exception e1)
107     {
108       // Swallow silently
109     }
110 
111     try
112     {
113       _beanServer.registerMBean(bean, name);
114     }
115     catch (Exception e)
116     {
117       LOG.warn("Could not register MBean", e);
118     }
119   }
120 
121   public void shutDown()
122   {
123     for(StateTransitionContext cxt : _monitorMap.keySet() )
124     {
125       try
126       {
127         ObjectName name = getObjectName(cxt.toString());
128         if (_beanServer.isRegistered(name))
129         {
130           _beanServer.unregisterMBean(name);
131         }
132       }
133       catch (Exception e)
134       {
135         LOG.warn("fail to unregister " + cxt.toString(), e);
136       }
137     }
138     _monitorMap.clear();
139 
140   }
141 }