HDFS-12286. Ozone: Extend MBeans utility to add any key value pairs to the registered MXBeans. Contributed by Elek, Marton.
This commit is contained in:
parent
482c094622
commit
4fac755bc7
|
@ -18,13 +18,17 @@
|
||||||
package org.apache.hadoop.metrics2.util;
|
package org.apache.hadoop.metrics2.util;
|
||||||
|
|
||||||
import java.lang.management.ManagementFactory;
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.management.InstanceAlreadyExistsException;
|
import javax.management.InstanceAlreadyExistsException;
|
||||||
import javax.management.MBeanServer;
|
import javax.management.MBeanServer;
|
||||||
import javax.management.ObjectName;
|
import javax.management.ObjectName;
|
||||||
|
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import org.apache.hadoop.classification.InterfaceAudience;
|
import org.apache.hadoop.classification.InterfaceAudience;
|
||||||
import org.apache.hadoop.classification.InterfaceStability;
|
import org.apache.hadoop.classification.InterfaceStability;
|
||||||
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
|
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
|
||||||
|
@ -60,8 +64,25 @@ public class MBeans {
|
||||||
*/
|
*/
|
||||||
static public ObjectName register(String serviceName, String nameName,
|
static public ObjectName register(String serviceName, String nameName,
|
||||||
Object theMbean) {
|
Object theMbean) {
|
||||||
|
return register(serviceName, nameName, new HashMap<String, String>(), theMbean);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register the MBean using our standard MBeanName format
|
||||||
|
* "hadoop:service=<serviceName>,name=<nameName>"
|
||||||
|
* Where the <serviceName> and <nameName> are the supplied parameters
|
||||||
|
*
|
||||||
|
* @param serviceName
|
||||||
|
* @param nameName
|
||||||
|
* @param properties - Key value pairs to define additional JMX ObjectName properties.
|
||||||
|
* @param theMbean - the MBean to register
|
||||||
|
* @return the named used to register the MBean
|
||||||
|
*/
|
||||||
|
static public ObjectName register(String serviceName, String nameName,
|
||||||
|
Map<String, String> properties,
|
||||||
|
Object theMbean) {
|
||||||
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
|
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
|
||||||
ObjectName name = getMBeanName(serviceName, nameName);
|
ObjectName name = getMBeanName(serviceName, nameName, properties);
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
try {
|
try {
|
||||||
mbs.registerMBean(theMbean, name);
|
mbs.registerMBean(theMbean, name);
|
||||||
|
@ -116,9 +137,17 @@ public class MBeans {
|
||||||
DefaultMetricsSystem.removeMBeanName(mbeanName);
|
DefaultMetricsSystem.removeMBeanName(mbeanName);
|
||||||
}
|
}
|
||||||
|
|
||||||
static private ObjectName getMBeanName(String serviceName, String nameName) {
|
@VisibleForTesting
|
||||||
|
static ObjectName getMBeanName(String serviceName, String nameName,
|
||||||
|
Map<String, String> additionalParameters) {
|
||||||
|
|
||||||
|
String additionalKeys = additionalParameters.entrySet()
|
||||||
|
.stream()
|
||||||
|
.map(entry -> entry.getKey() + "=" + entry.getValue())
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
|
||||||
String nameStr = DOMAIN_PREFIX + SERVICE_PREFIX + serviceName + "," +
|
String nameStr = DOMAIN_PREFIX + SERVICE_PREFIX + serviceName + "," +
|
||||||
NAME_PREFIX + nameName;
|
NAME_PREFIX + nameName + (additionalKeys.isEmpty() ? "" : "," + additionalKeys);
|
||||||
try {
|
try {
|
||||||
return DefaultMetricsSystem.newMBeanName(nameStr);
|
return DefaultMetricsSystem.newMBeanName(nameStr);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
/**
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with this
|
||||||
|
* work for additional information regarding copyright ownership. The ASF
|
||||||
|
* licenses this file to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations under
|
||||||
|
* the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.hadoop.metrics2.util;
|
||||||
|
|
||||||
|
public interface DummyMXBean {
|
||||||
|
|
||||||
|
int getCounter();
|
||||||
|
}
|
|
@ -0,0 +1,101 @@
|
||||||
|
/**
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with this
|
||||||
|
* work for additional information regarding copyright ownership. The ASF
|
||||||
|
* licenses this file to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations under
|
||||||
|
* the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.hadoop.metrics2.util;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import javax.management.MBeanServer;
|
||||||
|
import javax.management.ObjectName;
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class TestMBeans implements DummyMXBean {
|
||||||
|
|
||||||
|
public int counter = 1;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegister() throws Exception {
|
||||||
|
ObjectName objectName = null;
|
||||||
|
try {
|
||||||
|
counter = 23;
|
||||||
|
objectName = MBeans.register("UnitTest",
|
||||||
|
"RegisterTest", this);
|
||||||
|
|
||||||
|
MBeanServer platformMBeanServer =
|
||||||
|
ManagementFactory.getPlatformMBeanServer();
|
||||||
|
|
||||||
|
int jmxCounter = (int) platformMBeanServer
|
||||||
|
.getAttribute(objectName, "Counter");
|
||||||
|
Assert.assertEquals(counter, jmxCounter);
|
||||||
|
} finally {
|
||||||
|
if (objectName != null) {
|
||||||
|
MBeans.unregister(objectName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegisterWithAdditionalProperties() throws Exception {
|
||||||
|
ObjectName objectName = null;
|
||||||
|
try {
|
||||||
|
counter = 42;
|
||||||
|
|
||||||
|
Map<String, String> properties = new HashMap<String, String>();
|
||||||
|
properties.put("flavour", "server");
|
||||||
|
objectName = MBeans.register("UnitTest", "RegisterTest", properties, this);
|
||||||
|
|
||||||
|
MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||||
|
int jmxCounter = (int) platformMBeanServer.getAttribute(objectName, "Counter");
|
||||||
|
Assert.assertEquals(counter, jmxCounter);
|
||||||
|
} finally {
|
||||||
|
if (objectName != null) {
|
||||||
|
MBeans.unregister(objectName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetMbeanNameName() {
|
||||||
|
HashMap<String, String> properties = new HashMap<>();
|
||||||
|
|
||||||
|
ObjectName mBeanName = MBeans.getMBeanName("Service",
|
||||||
|
"Name", properties);
|
||||||
|
|
||||||
|
Assert.assertEquals("Service",
|
||||||
|
MBeans.getMbeanNameService(mBeanName));
|
||||||
|
|
||||||
|
properties.put("key", "value");
|
||||||
|
mBeanName = MBeans.getMBeanName(
|
||||||
|
"Service",
|
||||||
|
"Name",
|
||||||
|
properties);
|
||||||
|
|
||||||
|
Assert.assertEquals("Service",
|
||||||
|
MBeans.getMbeanNameService(mBeanName));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCounter() {
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue