mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
fixed deadlock
This commit is contained in:
@@ -15,11 +15,14 @@
|
||||
*/
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.DruidDataSourceUtils;
|
||||
import com.alibaba.druid.util.JMXUtils;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import javax.management.JMException;
|
||||
import javax.management.MBeanServer;
|
||||
@@ -31,13 +34,12 @@ import javax.management.openmbean.SimpleType;
|
||||
import javax.management.openmbean.TabularData;
|
||||
import javax.management.openmbean.TabularDataSupport;
|
||||
import javax.management.openmbean.TabularType;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.DruidDataSourceUtils;
|
||||
import com.alibaba.druid.util.JMXUtils;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBean {
|
||||
@@ -52,7 +54,7 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
|
||||
private final AtomicLong resetCount = new AtomicLong();
|
||||
|
||||
// global instances
|
||||
private static volatile IdentityHashMap dataSources;
|
||||
private static volatile Map dataSources;
|
||||
|
||||
private final static String MBEAN_NAME = "com.alibaba.druid:type=DruidDataSourceStat";
|
||||
|
||||
@@ -60,32 +62,29 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
IdentityHashMap<Object, ObjectName> dataSources = getInstances();
|
||||
public synchronized static void clear() {
|
||||
Map<Object, ObjectName> dataSources = getInstances();
|
||||
|
||||
synchronized (dataSources) {
|
||||
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
for (Object item : dataSources.entrySet()) {
|
||||
Map.Entry entry = (Map.Entry) item;
|
||||
ObjectName objectName = (ObjectName) entry.getValue();
|
||||
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
for (Object item : dataSources.entrySet()) {
|
||||
Map.Entry entry = (Map.Entry) item;
|
||||
ObjectName objectName = (ObjectName) entry.getValue();
|
||||
|
||||
if (objectName == null) {
|
||||
continue;
|
||||
}
|
||||
if (objectName == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
mbeanServer.unregisterMBean(objectName);
|
||||
} catch (JMException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
try {
|
||||
mbeanServer.unregisterMBean(objectName);
|
||||
} catch (JMException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
dataSources.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static IdentityHashMap<Object, ObjectName> getInstances() {
|
||||
IdentityHashMap<Object, ObjectName> tmp = dataSources;
|
||||
public static Map<Object, ObjectName> getInstances() {
|
||||
Map<Object, ObjectName> tmp = dataSources;
|
||||
if (tmp == null) {
|
||||
synchronized (DruidDataSourceStatManager.class) {
|
||||
if (isRegisterToSystemProperty()) {
|
||||
@@ -93,7 +92,7 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
|
||||
} else {
|
||||
tmp = dataSources;
|
||||
if (null == tmp) {
|
||||
dataSources = tmp = new IdentityHashMap<Object, ObjectName>();
|
||||
dataSources = tmp = Collections.synchronizedMap(new IdentityHashMap<Object, ObjectName>());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,16 +107,16 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static IdentityHashMap<Object, ObjectName> getInstances0() {
|
||||
static Map<Object, ObjectName> getInstances0() {
|
||||
Properties properties = System.getProperties();
|
||||
IdentityHashMap<Object, ObjectName> instances = (IdentityHashMap<Object, ObjectName>) properties.get(SYS_PROP_INSTANCES);
|
||||
Map<Object, ObjectName> instances = (Map<Object, ObjectName>) properties.get(SYS_PROP_INSTANCES);
|
||||
|
||||
if (instances == null) {
|
||||
synchronized (properties) {
|
||||
instances = (IdentityHashMap<Object, ObjectName>) properties.get(SYS_PROP_INSTANCES);
|
||||
|
||||
if (instances == null) {
|
||||
instances = new IdentityHashMap<Object, ObjectName>();
|
||||
instances = Collections.synchronizedMap(new IdentityHashMap<Object, ObjectName>());
|
||||
properties.put(SYS_PROP_INSTANCES, instances);
|
||||
}
|
||||
}
|
||||
@@ -127,11 +126,10 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
|
||||
}
|
||||
|
||||
public synchronized static ObjectName addDataSource(Object dataSource, String name) {
|
||||
final IdentityHashMap<Object, ObjectName> instances = getInstances();
|
||||
final Map<Object, ObjectName> instances = getInstances();
|
||||
|
||||
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
synchronized (instances) {
|
||||
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
|
||||
if (instances.size() == 0) {
|
||||
try {
|
||||
ObjectName objectName = new ObjectName(MBEAN_NAME);
|
||||
@@ -144,66 +142,64 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
|
||||
|
||||
DruidStatService.registerMBean();
|
||||
}
|
||||
|
||||
ObjectName objectName = null;
|
||||
if (name != null) {
|
||||
try {
|
||||
objectName = new ObjectName("com.alibaba.druid:type=DruidDataSource,id=" + name);
|
||||
mbeanServer.registerMBean(dataSource, objectName);
|
||||
} catch (Throwable ex) {
|
||||
LOG.error("register mbean error", ex);
|
||||
objectName = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (objectName == null) {
|
||||
try {
|
||||
int id = System.identityHashCode(dataSource);
|
||||
objectName = new ObjectName("com.alibaba.druid:type=DruidDataSource,id=" + id);
|
||||
mbeanServer.registerMBean(dataSource, objectName);
|
||||
} catch (Throwable ex) {
|
||||
LOG.error("register mbean error", ex);
|
||||
objectName = null;
|
||||
}
|
||||
}
|
||||
|
||||
instances.put(dataSource, objectName);
|
||||
return objectName;
|
||||
}
|
||||
|
||||
ObjectName objectName = null;
|
||||
if (name != null) {
|
||||
try {
|
||||
objectName = new ObjectName("com.alibaba.druid:type=DruidDataSource,id=" + name);
|
||||
mbeanServer.registerMBean(dataSource, objectName);
|
||||
} catch (Throwable ex) {
|
||||
LOG.error("register mbean error", ex);
|
||||
objectName = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (objectName == null) {
|
||||
try {
|
||||
int id = System.identityHashCode(dataSource);
|
||||
objectName = new ObjectName("com.alibaba.druid:type=DruidDataSource,id=" + id);
|
||||
mbeanServer.registerMBean(dataSource, objectName);
|
||||
} catch (Throwable ex) {
|
||||
LOG.error("register mbean error", ex);
|
||||
objectName = null;
|
||||
}
|
||||
}
|
||||
|
||||
instances.put(dataSource, objectName);
|
||||
return objectName;
|
||||
}
|
||||
|
||||
public synchronized static void removeDataSource(Object dataSource) {
|
||||
IdentityHashMap<Object, ObjectName> instances = getInstances();
|
||||
Map<Object, ObjectName> instances = getInstances();
|
||||
|
||||
synchronized (instances) {
|
||||
ObjectName objectName = (ObjectName) instances.remove(dataSource);
|
||||
ObjectName objectName = (ObjectName) instances.remove(dataSource);
|
||||
|
||||
if (objectName == null) {
|
||||
objectName = DruidDataSourceUtils.getObjectName(dataSource);
|
||||
}
|
||||
if (objectName == null) {
|
||||
objectName = DruidDataSourceUtils.getObjectName(dataSource);
|
||||
}
|
||||
|
||||
if (objectName == null) {
|
||||
LOG.error("unregister mbean failed. url " + DruidDataSourceUtils.getUrl(dataSource));
|
||||
return;
|
||||
}
|
||||
if (objectName == null) {
|
||||
LOG.error("unregister mbean failed. url " + DruidDataSourceUtils.getUrl(dataSource));
|
||||
return;
|
||||
}
|
||||
|
||||
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
|
||||
try {
|
||||
mbeanServer.unregisterMBean(objectName);
|
||||
} catch (Throwable ex) {
|
||||
LOG.error("unregister mbean error", ex);
|
||||
}
|
||||
|
||||
if (instances.size() == 0) {
|
||||
try {
|
||||
mbeanServer.unregisterMBean(objectName);
|
||||
mbeanServer.unregisterMBean(new ObjectName(MBEAN_NAME));
|
||||
} catch (Throwable ex) {
|
||||
LOG.error("unregister mbean error", ex);
|
||||
}
|
||||
|
||||
if (instances.size() == 0) {
|
||||
try {
|
||||
mbeanServer.unregisterMBean(new ObjectName(MBEAN_NAME));
|
||||
} catch (Throwable ex) {
|
||||
LOG.error("unregister mbean error", ex);
|
||||
}
|
||||
|
||||
DruidStatService.unregisterMBean();
|
||||
}
|
||||
DruidStatService.unregisterMBean();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,37 +210,33 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
IdentityHashMap<Object, ObjectName> dataSources = getInstances();
|
||||
Map<Object, ObjectName> dataSources = getInstances();
|
||||
|
||||
synchronized (dataSources) {
|
||||
for (Object item : dataSources.keySet()) {
|
||||
try {
|
||||
Method method = item.getClass().getMethod("resetStat");
|
||||
method.invoke(item);
|
||||
} catch (Exception e) {
|
||||
LOG.error("resetStat error", e);
|
||||
}
|
||||
for (Object item : dataSources.keySet()) {
|
||||
try {
|
||||
Method method = item.getClass().getMethod("resetStat");
|
||||
method.invoke(item);
|
||||
} catch (Exception e) {
|
||||
LOG.error("resetStat error", e);
|
||||
}
|
||||
|
||||
resetCount.incrementAndGet();
|
||||
}
|
||||
|
||||
resetCount.incrementAndGet();
|
||||
}
|
||||
|
||||
public void logAndResetDataSource() {
|
||||
IdentityHashMap<Object, ObjectName> dataSources = getInstances();
|
||||
Map<Object, ObjectName> dataSources = getInstances();
|
||||
|
||||
synchronized (dataSources) {
|
||||
for (Object item : dataSources.keySet()) {
|
||||
try {
|
||||
Method method = item.getClass().getMethod("logStats");
|
||||
method.invoke(item);
|
||||
} catch (Exception e) {
|
||||
LOG.error("resetStat error", e);
|
||||
}
|
||||
for (Object item : dataSources.keySet()) {
|
||||
try {
|
||||
Method method = item.getClass().getMethod("logStats");
|
||||
method.invoke(item);
|
||||
} catch (Exception e) {
|
||||
LOG.error("resetStat error", e);
|
||||
}
|
||||
|
||||
resetCount.incrementAndGet();
|
||||
}
|
||||
|
||||
resetCount.incrementAndGet();
|
||||
}
|
||||
|
||||
public long getResetCount() {
|
||||
@@ -413,8 +405,11 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
|
||||
};
|
||||
|
||||
String[] indexDescriptions = indexNames;
|
||||
COMPOSITE_TYPE = new CompositeType("DataSourceStatistic", "DataSource Statistic", indexNames,
|
||||
indexDescriptions, indexTypes);
|
||||
COMPOSITE_TYPE = new CompositeType("DataSourceStatistic",
|
||||
"DataSource Statistic",
|
||||
indexNames,
|
||||
indexDescriptions,
|
||||
indexTypes);
|
||||
|
||||
return COMPOSITE_TYPE;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user