Optimize the logic of writing memory container(properties) to files i… (#9695)

* Optimize the logic of writing memory container(properties) to files in AbstractRegistry and AbstractMetadataReport

* FIX UT
This commit is contained in:
灼华 2022-03-15 14:24:25 +08:00 committed by GitHub
parent a5f0d9a632
commit bd5c620946
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 18 deletions

View File

@ -141,11 +141,11 @@ public class DefaultModuleDeployer extends AbstractDeployer<ModuleModel> impleme
// export services
exportServices();
// prepare application instance
// exclude internal module to avoid wait itself
if (moduleModel != moduleModel.getApplicationModel().getInternalModule()) {
applicationDeployer.prepareInternalModule();
}
// prepare application instance
// exclude internal module to avoid wait itself
if (moduleModel != moduleModel.getApplicationModel().getInternalModule()) {
applicationDeployer.prepareInternalModule();
}
// refer services
referServices();

View File

@ -143,9 +143,9 @@ public class SimpleReferenceCache implements ReferenceCache {
@Override
@SuppressWarnings("unchecked")
public <T> T get(String key) {
List<ReferenceConfigBase<?>> referenceConfigs = referenceKeyMap.get(key);
if (referenceConfigs != null && referenceConfigs.size() > 0) {
return (T) referenceConfigs.get(0).get();
List<ReferenceConfigBase<?>> referenceConfigBases = referenceKeyMap.get(key);
if (CollectionUtils.isNotEmpty(referenceConfigBases)) {
return (T) referenceConfigBases.get(0).get();
}
return null;
}
@ -168,7 +168,7 @@ public class SimpleReferenceCache implements ReferenceCache {
@SuppressWarnings("unchecked")
public <T> T get(Class<T> type) {
List<ReferenceConfigBase<?>> referenceConfigBases = referenceTypeMap.get(type);
if (referenceConfigBases != null && referenceConfigBases.size() > 0) {
if (CollectionUtils.isNotEmpty(referenceConfigBases)) {
return (T) referenceConfigBases.get(0).get();
}
return null;

View File

@ -175,8 +175,24 @@ public abstract class AbstractMetadataReport implements MetadataReport {
if (!file.exists()) {
file.createNewFile();
}
Properties tmpProperties;
if (!syncReport) {
// When syncReport = false, properties.setProperty and properties.store are called from the same
// thread(reportCacheExecutor), so deep copy is not required
tmpProperties = properties;
} else {
// Using store method and setProperty method of the this.properties will cause lock contention
// under multi-threading, so deep copy a new container
tmpProperties = new Properties();
Set<Map.Entry<Object, Object>> entries = properties.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
tmpProperties.setProperty((String) entry.getKey(), (String) entry.getValue());
}
}
try (FileOutputStream outputFile = new FileOutputStream(file)) {
properties.store(outputFile, "Dubbo metadataReport Cache");
tmpProperties.store(outputFile, "Dubbo metadataReport Cache");
}
} finally {
lock.release();

View File

@ -196,8 +196,24 @@ public abstract class AbstractRegistry implements Registry {
if (!file.exists()) {
file.createNewFile();
}
Properties tmpProperties;
if (syncSaveFile) {
// When syncReport = true, properties.setProperty and properties.store are called from the same
// thread(reportCacheExecutor), so deep copy is not required
tmpProperties = properties;
} else {
// Using properties.setProperty and properties.store method will cause lock contention
// under multi-threading, so deep copy a new container
tmpProperties = new Properties();
Set<Map.Entry<Object, Object>> entries = properties.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
tmpProperties.setProperty((String) entry.getKey(), (String) entry.getValue());
}
}
try (FileOutputStream outputFile = new FileOutputStream(file)) {
properties.store(outputFile, "Dubbo Registry Cache");
tmpProperties.store(outputFile, "Dubbo Registry Cache");
}
} finally {
lock.release();

View File

@ -55,13 +55,13 @@ public abstract class AbstractProxyFactory implements ProxyFactory {
public <T> T getProxy(Invoker<T> invoker, boolean generic) throws RpcException {
// when compiling with native image, ensure that the order of the interfaces remains unchanged
LinkedHashSet<Class<?>> interfaces = new LinkedHashSet<>();
ClassLoader classLoader = getClassLoader(invoker);
String config = invoker.getUrl().getParameter(INTERFACES);
if (StringUtils.isNotEmpty(config)) {
String[] types = COMMA_SPLIT_PATTERN.split(config);
for (String type : types) {
try {
ClassLoader classLoader = getClassLoader(invoker);
interfaces.add(ReflectUtils.forName(classLoader, type));
} catch (Throwable e) {
// ignore
@ -75,7 +75,6 @@ public abstract class AbstractProxyFactory implements ProxyFactory {
try {
// find the real interface from url
String realInterface = invoker.getUrl().getParameter(Constants.INTERFACE);
ClassLoader classLoader = getClassLoader(invoker);
realInterfaceClass = ReflectUtils.forName(classLoader, realInterface);
interfaces.add(realInterfaceClass);
} catch (Throwable e) {
@ -110,7 +109,7 @@ public abstract class AbstractProxyFactory implements ProxyFactory {
private <T> ClassLoader getClassLoader(Invoker<T> invoker) {
ServiceModel serviceModel = invoker.getUrl().getServiceModel();
ClassLoader classLoader = null;
if (serviceModel != null) {
if (serviceModel != null && serviceModel.getConfig() != null) {
classLoader = serviceModel.getConfig().getInterfaceClassLoader();
}
if (classLoader == null) {

View File

@ -75,14 +75,14 @@ public class JavassistProxyFactory extends AbstractProxyFactory {
// try fall back to JDK proxy factory
try {
Invoker<T> invoker = jdkProxyFactory.getInvoker(proxy, type, url);
logger.error("Failed to generate proxy by Javassist failed. Fallback to use JDK proxy success. " +
logger.error("Failed to generate invoker by Javassist failed. Fallback to use JDK proxy success. " +
"Interfaces: " + type, fromJavassist);
// log out error
return invoker;
} catch (Throwable fromJdk) {
logger.error("Failed to generate proxy by Javassist failed. Fallback to use JDK proxy is also failed. " +
logger.error("Failed to generate invoker by Javassist failed. Fallback to use JDK proxy is also failed. " +
"Interfaces: " + type + " Javassist Error.", fromJavassist);
logger.error("Failed to generate proxy by Javassist failed. Fallback to use JDK proxy is also failed. " +
logger.error("Failed to generate invoker by Javassist failed. Fallback to use JDK proxy is also failed. " +
"Interfaces: " + type + " JDK Error.", fromJdk);
throw fromJavassist;
}

View File

@ -343,7 +343,7 @@ public class DubboProtocol extends AbstractProtocol {
private void checkDestroyed() {
if (destroyed.get()) {
throw new IllegalStateException( getClass().getSimpleName() + " is destroyed");
throw new IllegalStateException(getClass().getSimpleName() + " is destroyed");
}
}