Review Board 1.7.22


QuorumPeer should unregister only its previsously registered MBeans instead of use MBeanRegistry.unregisterAll() method.

Review Request #2208 - Created Oct. 5, 2011 and updated

César Álvarez Núñez
zookeeper-trunk
ZOOKEEPER-1214
Reviewers
zookeeper
zookeeper
See https://issues.apache.org/jira/browse/ZOOKEEPER-1214
-
/src/java/main/org/apache/zookeeper/jmx/MBeanRegistry.java
Revision 1226094 New Change
[20] 16 lines
[+20]
17
 */
17
 */
18

    
   
18

   
19
package org.apache.zookeeper.jmx;
19
package org.apache.zookeeper.jmx;
20

    
   
20

   
21
import java.lang.management.ManagementFactory;
21
import java.lang.management.ManagementFactory;

    
   
22
import java.util.Collection;
22
import java.util.Map;
23
import java.util.Map;

    
   
24
import java.util.Set;
23
import java.util.concurrent.ConcurrentHashMap;
25
import java.util.concurrent.ConcurrentHashMap;
24

    
   
26

   
25
import javax.management.JMException;
27
import javax.management.JMException;
26
import javax.management.MBeanServer;
28
import javax.management.MBeanServer;
27
import javax.management.MBeanServerFactory;
29
import javax.management.MBeanServerFactory;
[+20] [20] 15 lines
[+20] [+] public class MBeanRegistry {
43
    private static MBeanRegistry instance = new MBeanRegistry(); 
45
    private static MBeanRegistry instance = new MBeanRegistry(); 
44
    
46
    
45
    private Map<ZKMBeanInfo, String> mapBean2Path =
47
    private Map<ZKMBeanInfo, String> mapBean2Path =
46
        new ConcurrentHashMap<ZKMBeanInfo, String>();
48
        new ConcurrentHashMap<ZKMBeanInfo, String>();
47
    
49
    
48
    private Map<String, ZKMBeanInfo> mapName2Bean =

   
49
        new ConcurrentHashMap<String, ZKMBeanInfo>();

   
50

    
   

   
51
    private MBeanServer mBeanServer;
50
    private MBeanServer mBeanServer;
52

    
   
51

   
53
    public static MBeanRegistry getInstance() {
52
    public static MBeanRegistry getInstance() {
54
        return instance;
53
        return instance;
55
    }
54
    }
[+20] [20] 32 lines
[+20] [+] public void register(ZKMBeanInfo bean, ZKMBeanInfo parent)
88
            path = mapBean2Path.get(parent);
87
            path = mapBean2Path.get(parent);
89
            assert path != null;
88
            assert path != null;
90
        }
89
        }
91
        path = makeFullPath(path, parent);
90
        path = makeFullPath(path, parent);
92
        mapBean2Path.put(bean, path);
91
        mapBean2Path.put(bean, path);
93
        mapName2Bean.put(bean.getName(), bean);

   
94
        if(bean.isHidden())
92
        if(bean.isHidden())
95
            return;
93
            return;
96
        ObjectName oname = makeObjectName(path, bean);
94
        ObjectName oname = makeObjectName(path, bean);
97
        try {
95
        try {
98
            mBeanServer.registerMBean(bean, oname);
96
            mBeanServer.registerMBean(bean, oname);
[+20] [20] 6 lines
[+20] public void register(ZKMBeanInfo bean, ZKMBeanInfo parent)
105
    /**
103
    /**
106
     * Unregister the MBean identified by the path.
104
     * Unregister the MBean identified by the path.
107
     * @param path
105
     * @param path
108
     * @param bean
106
     * @param bean
109
     */
107
     */
110
    private void unregister(String path,ZKMBeanInfo bean) throws JMException {
108
    private void unregister(String path,ZKMBeanInfo bean) throws JMException  {
111
        if(path==null)
109
        if(path==null)
112
            return;
110
            return;
113
        if (!bean.isHidden()) {
111
        if (!bean.isHidden()) {
114
            try {
112
            final ObjectName objName = makeObjectName(path, bean);
115
                mBeanServer.unregisterMBean(makeObjectName(path, bean));
113
            if (LOG.isInfoEnabled()) {
116
            } catch (JMException e) {
114
                LOG.info("Unregister MBean [{}]", objName);
117
                LOG.warn("Failed to unregister MBean " + bean.getName());

   
118
                throw e;

   
119
            }
115
            }

    
   
116
            mBeanServer.unregisterMBean(objName);
120
        }        
117
        }        
121
    }
118
    }
122
    
119
    
123
    /**
120
    /**

    
   
121
     * @return a {@link Collection} with the {@link ZKMBeanInfo} instances not

    
   
122
     *         unregistered. Mainly for testing purposes.

    
   
123
     */

    
   
124
    public Set<ZKMBeanInfo> getRegisteredBeans() {

    
   
125
        return mapBean2Path.keySet();

    
   
126
    }

    
   
127

   

    
   
128
    /**
124
     * Unregister MBean.
129
     * Unregister MBean.
125
     * @param bean
130
     * @param bean
126
     */
131
     */
127
    public void unregister(ZKMBeanInfo bean) {
132
    public void unregister(ZKMBeanInfo bean) {
128
        if(bean==null)
133
        if(bean==null)
129
            return;
134
            return;
130
        String path=mapBean2Path.get(bean);
135
        String path = mapBean2Path.remove(bean);
131
        try {
136
        try {
132
            unregister(path,bean);
137
            unregister(path,bean);
133
        } catch (JMException e) {
138
        } catch (JMException e) {
134
            LOG.warn("Error during unregister", e);
139
            LOG.warn("Error during unregister of [{}]", bean.getName(), e);

    
   
140
        } catch (Throwable t) {

    
   
141
            LOG.error("Unexpected exception during unregister of [{}]. It should be reviewed and fixed.", bean.getName(), t);
135
        }
142
        }
136
        mapBean2Path.remove(bean);

   
137
        mapName2Bean.remove(bean.getName());

   
138
    }

   
139
    /**

   
140
     * Unregister all currently registered MBeans

   
141
     */

   
142
    public void unregisterAll() {

   
143
        for(Map.Entry<ZKMBeanInfo,String> e: mapBean2Path.entrySet()) {

   
144
            try {

   
145
                unregister(e.getValue(), e.getKey());

   
146
            } catch (JMException e1) {

   
147
                LOG.warn("Error during unregister", e1);

   
148
            }

   
149
        }

   
150
        mapBean2Path.clear();

   
151
        mapName2Bean.clear();

   
152
    }
143
    }

    
   
144

   
153
    /**
145
    /**
154
     * Generate a filesystem-like path.
146
     * Generate a filesystem-like path.
155
     * @param prefix path prefix
147
     * @param prefix path prefix
156
     * @param name path elements
148
     * @param name path elements
157
     * @return absolute path
149
     * @return absolute path
[+20] [20] 58 lines
/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java
Revision 1226094 New Change
 
/src/java/test/org/apache/zookeeper/test/QuorumUtil.java
Revision 1226094 New Change
 
/src/java/test/org/apache/zookeeper/test/QuorumUtilTest.java
New File
 
  1. /src/java/main/org/apache/zookeeper/jmx/MBeanRegistry.java: Loading...
  2. /src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java: Loading...
  3. /src/java/test/org/apache/zookeeper/test/QuorumUtil.java: Loading...
  4. /src/java/test/org/apache/zookeeper/test/QuorumUtilTest.java: Loading...