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:
parent
6b35308cf0
commit
b33878f6fc
|
@ -31,8 +31,12 @@ import java.util.ServiceLoader;
|
|||
* created.
|
||||
*/
|
||||
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 Map<Class, Object> instances = new HashMap<Class, Object>();
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static synchronized <T> T getInstance(Class<T> klass) {
|
||||
T instance = (T) instances.get(klass);
|
||||
if (instance == null) {
|
||||
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.
|
||||
public static <T> T getInstance(Class<T> klass) {
|
||||
synchronized (SingletonStorage.INSTANCE.lock) {
|
||||
T instance = (T) SingletonStorage.INSTANCE.instances.get(klass);
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,27 +22,33 @@ package org.apache.hadoop.hbase.regionserver;
|
|||
* Factory to create MetricsRegionServerSource when given a MetricsRegionServerWrapper
|
||||
*/
|
||||
public class MetricsRegionServerSourceFactoryImpl implements MetricsRegionServerSourceFactory {
|
||||
private static enum FactoryStorage {
|
||||
public static enum FactoryStorage {
|
||||
INSTANCE;
|
||||
private Object aggLock = new Object();
|
||||
private Object serverLock = new Object();
|
||||
private MetricsRegionServerSource serverSource;
|
||||
private MetricsRegionAggregateSourceImpl aggImpl;
|
||||
}
|
||||
|
||||
private synchronized MetricsRegionAggregateSourceImpl getAggregate() {
|
||||
if (FactoryStorage.INSTANCE.aggImpl == null) {
|
||||
FactoryStorage.INSTANCE.aggImpl = new MetricsRegionAggregateSourceImpl();
|
||||
synchronized (FactoryStorage.INSTANCE.aggLock) {
|
||||
if (FactoryStorage.INSTANCE.aggImpl == null) {
|
||||
FactoryStorage.INSTANCE.aggImpl = new MetricsRegionAggregateSourceImpl();
|
||||
}
|
||||
return FactoryStorage.INSTANCE.aggImpl;
|
||||
}
|
||||
return FactoryStorage.INSTANCE.aggImpl;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public synchronized MetricsRegionServerSource createServer(MetricsRegionServerWrapper regionServerWrapper) {
|
||||
if (FactoryStorage.INSTANCE.serverSource == null) {
|
||||
FactoryStorage.INSTANCE.serverSource = new MetricsRegionServerSourceImpl(
|
||||
regionServerWrapper);
|
||||
synchronized (FactoryStorage.INSTANCE.serverLock) {
|
||||
if (FactoryStorage.INSTANCE.serverSource == null) {
|
||||
FactoryStorage.INSTANCE.serverSource = new MetricsRegionServerSourceImpl(
|
||||
regionServerWrapper);
|
||||
}
|
||||
return FactoryStorage.INSTANCE.serverSource;
|
||||
}
|
||||
return FactoryStorage.INSTANCE.serverSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -22,27 +22,33 @@ package org.apache.hadoop.hbase.regionserver;
|
|||
* Factory to create MetricsRegionServerSource when given a MetricsRegionServerWrapper
|
||||
*/
|
||||
public class MetricsRegionServerSourceFactoryImpl implements MetricsRegionServerSourceFactory {
|
||||
private static enum FactoryStorage {
|
||||
public static enum FactoryStorage {
|
||||
INSTANCE;
|
||||
private Object aggLock = new Object();
|
||||
private Object serverLock = new Object();
|
||||
private MetricsRegionServerSource serverSource;
|
||||
private MetricsRegionAggregateSourceImpl aggImpl;
|
||||
}
|
||||
|
||||
private synchronized MetricsRegionAggregateSourceImpl getAggregate() {
|
||||
if (FactoryStorage.INSTANCE.aggImpl == null) {
|
||||
FactoryStorage.INSTANCE.aggImpl = new MetricsRegionAggregateSourceImpl();
|
||||
synchronized (FactoryStorage.INSTANCE.aggLock) {
|
||||
if (FactoryStorage.INSTANCE.aggImpl == null) {
|
||||
FactoryStorage.INSTANCE.aggImpl = new MetricsRegionAggregateSourceImpl();
|
||||
}
|
||||
return FactoryStorage.INSTANCE.aggImpl;
|
||||
}
|
||||
return FactoryStorage.INSTANCE.aggImpl;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public synchronized MetricsRegionServerSource createServer(MetricsRegionServerWrapper regionServerWrapper) {
|
||||
if (FactoryStorage.INSTANCE.serverSource == null) {
|
||||
FactoryStorage.INSTANCE.serverSource = new MetricsRegionServerSourceImpl(
|
||||
regionServerWrapper);
|
||||
synchronized (FactoryStorage.INSTANCE.serverLock) {
|
||||
if (FactoryStorage.INSTANCE.serverSource == null) {
|
||||
FactoryStorage.INSTANCE.serverSource = new MetricsRegionServerSourceImpl(
|
||||
regionServerWrapper);
|
||||
}
|
||||
return FactoryStorage.INSTANCE.serverSource;
|
||||
}
|
||||
return FactoryStorage.INSTANCE.serverSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue