HBASE-8426 Opening a region failed on Metrics source RegionServer,sub=Regions already exists

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1477247 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
eclark 2013-04-29 18:17:37 +00:00
parent 6b35308cf0
commit b33878f6fc
3 changed files with 65 additions and 46 deletions

View File

@ -31,8 +31,12 @@ import java.util.ServiceLoader;
* created. * created.
*/ */
public class CompatibilitySingletonFactory extends CompatibilityFactory { public class CompatibilitySingletonFactory extends CompatibilityFactory {
public static enum SingletonStorage {
INSTANCE;
Object lock = new Object();
private static final Map<Class, Object> instances = new HashMap<Class, Object>();
}
private static final Log LOG = LogFactory.getLog(CompatibilitySingletonFactory.class); private static final Log LOG = LogFactory.getLog(CompatibilitySingletonFactory.class);
private static final Map<Class, Object> instances = new HashMap<Class, Object>();
/** /**
* This is a static only class don't let anyone create an instance. * This is a static only class don't let anyone create an instance.
@ -45,37 +49,40 @@ public class CompatibilitySingletonFactory extends CompatibilityFactory {
* @return the singleton * @return the singleton
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static synchronized <T> T getInstance(Class<T> klass) { public static <T> T getInstance(Class<T> klass) {
T instance = (T) instances.get(klass); synchronized (SingletonStorage.INSTANCE.lock) {
if (instance == null) { T instance = (T) SingletonStorage.INSTANCE.instances.get(klass);
try {
ServiceLoader<T> loader = ServiceLoader.load(klass);
Iterator<T> it = loader.iterator();
instance = it.next();
if (it.hasNext()) {
StringBuilder msg = new StringBuilder();
msg.append("ServiceLoader provided more than one implementation for class: ")
.append(klass)
.append(", using implementation: ").append(instance.getClass())
.append(", other implementations: {");
while (it.hasNext()) {
msg.append(it.next()).append(" ");
}
msg.append("}");
LOG.warn(msg);
}
} catch (Exception e) {
throw new RuntimeException(createExceptionString(klass), e);
} catch (Error e) {
throw new RuntimeException(createExceptionString(klass), e);
}
// If there was nothing returned and no exception then throw an exception.
if (instance == null) { if (instance == null) {
throw new RuntimeException(createExceptionString(klass)); try {
ServiceLoader<T> loader = ServiceLoader.load(klass);
Iterator<T> it = loader.iterator();
instance = it.next();
if (it.hasNext()) {
StringBuilder msg = new StringBuilder();
msg.append("ServiceLoader provided more than one implementation for class: ")
.append(klass)
.append(", using implementation: ").append(instance.getClass())
.append(", other implementations: {");
while (it.hasNext()) {
msg.append(it.next()).append(" ");
}
msg.append("}");
LOG.warn(msg);
}
} catch (Exception e) {
throw new RuntimeException(createExceptionString(klass), e);
} catch (Error e) {
throw new RuntimeException(createExceptionString(klass), e);
}
// If there was nothing returned and no exception then throw an exception.
if (instance == null) {
throw new RuntimeException(createExceptionString(klass));
}
SingletonStorage.INSTANCE.instances.put(klass, instance);
} }
instances.put(klass, instance); return instance;
} }
return instance;
} }
} }

View File

@ -22,27 +22,33 @@ package org.apache.hadoop.hbase.regionserver;
* Factory to create MetricsRegionServerSource when given a MetricsRegionServerWrapper * Factory to create MetricsRegionServerSource when given a MetricsRegionServerWrapper
*/ */
public class MetricsRegionServerSourceFactoryImpl implements MetricsRegionServerSourceFactory { public class MetricsRegionServerSourceFactoryImpl implements MetricsRegionServerSourceFactory {
private static enum FactoryStorage { public static enum FactoryStorage {
INSTANCE; INSTANCE;
private Object aggLock = new Object();
private Object serverLock = new Object();
private MetricsRegionServerSource serverSource; private MetricsRegionServerSource serverSource;
private MetricsRegionAggregateSourceImpl aggImpl; private MetricsRegionAggregateSourceImpl aggImpl;
} }
private synchronized MetricsRegionAggregateSourceImpl getAggregate() { private synchronized MetricsRegionAggregateSourceImpl getAggregate() {
if (FactoryStorage.INSTANCE.aggImpl == null) { synchronized (FactoryStorage.INSTANCE.aggLock) {
FactoryStorage.INSTANCE.aggImpl = new MetricsRegionAggregateSourceImpl(); if (FactoryStorage.INSTANCE.aggImpl == null) {
FactoryStorage.INSTANCE.aggImpl = new MetricsRegionAggregateSourceImpl();
}
return FactoryStorage.INSTANCE.aggImpl;
} }
return FactoryStorage.INSTANCE.aggImpl;
} }
@Override @Override
public synchronized MetricsRegionServerSource createServer(MetricsRegionServerWrapper regionServerWrapper) { public synchronized MetricsRegionServerSource createServer(MetricsRegionServerWrapper regionServerWrapper) {
if (FactoryStorage.INSTANCE.serverSource == null) { synchronized (FactoryStorage.INSTANCE.serverLock) {
FactoryStorage.INSTANCE.serverSource = new MetricsRegionServerSourceImpl( if (FactoryStorage.INSTANCE.serverSource == null) {
regionServerWrapper); FactoryStorage.INSTANCE.serverSource = new MetricsRegionServerSourceImpl(
regionServerWrapper);
}
return FactoryStorage.INSTANCE.serverSource;
} }
return FactoryStorage.INSTANCE.serverSource;
} }
@Override @Override

View File

@ -22,27 +22,33 @@ package org.apache.hadoop.hbase.regionserver;
* Factory to create MetricsRegionServerSource when given a MetricsRegionServerWrapper * Factory to create MetricsRegionServerSource when given a MetricsRegionServerWrapper
*/ */
public class MetricsRegionServerSourceFactoryImpl implements MetricsRegionServerSourceFactory { public class MetricsRegionServerSourceFactoryImpl implements MetricsRegionServerSourceFactory {
private static enum FactoryStorage { public static enum FactoryStorage {
INSTANCE; INSTANCE;
private Object aggLock = new Object();
private Object serverLock = new Object();
private MetricsRegionServerSource serverSource; private MetricsRegionServerSource serverSource;
private MetricsRegionAggregateSourceImpl aggImpl; private MetricsRegionAggregateSourceImpl aggImpl;
} }
private synchronized MetricsRegionAggregateSourceImpl getAggregate() { private synchronized MetricsRegionAggregateSourceImpl getAggregate() {
if (FactoryStorage.INSTANCE.aggImpl == null) { synchronized (FactoryStorage.INSTANCE.aggLock) {
FactoryStorage.INSTANCE.aggImpl = new MetricsRegionAggregateSourceImpl(); if (FactoryStorage.INSTANCE.aggImpl == null) {
FactoryStorage.INSTANCE.aggImpl = new MetricsRegionAggregateSourceImpl();
}
return FactoryStorage.INSTANCE.aggImpl;
} }
return FactoryStorage.INSTANCE.aggImpl;
} }
@Override @Override
public synchronized MetricsRegionServerSource createServer(MetricsRegionServerWrapper regionServerWrapper) { public synchronized MetricsRegionServerSource createServer(MetricsRegionServerWrapper regionServerWrapper) {
if (FactoryStorage.INSTANCE.serverSource == null) { synchronized (FactoryStorage.INSTANCE.serverLock) {
FactoryStorage.INSTANCE.serverSource = new MetricsRegionServerSourceImpl( if (FactoryStorage.INSTANCE.serverSource == null) {
regionServerWrapper); FactoryStorage.INSTANCE.serverSource = new MetricsRegionServerSourceImpl(
regionServerWrapper);
}
return FactoryStorage.INSTANCE.serverSource;
} }
return FactoryStorage.INSTANCE.serverSource;
} }
@Override @Override